using System;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Data;

using Cosmos.BaseFrame;
using Cosmos.UserFrame;
using Cosmos.ServiceProvider;
using Cosmos.Common;
using Cosmos.CommonManager;

/*-----------------------------------------------------------------------------------------------*/
// 설      명 : 캐셔 로그온 처리
// 작  성  자 : 
// 변경  이력 : 
/*-----------------------------------------------------------------------------------------------*/
namespace Cosmos.Service
{
    public class CashierLogOn : IServiceUs
    {
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조
        private TranStatus m_cTrnStatus = new TranStatus();                         // 거래정보 참조

        private IDatabaseSQL m_cSqlDbService = null;                                // 데이터베이스 관리
        private IDataProcessUs m_cDataService = null;                               // 거래데이터 합계금액 계산 및 관리
        private ISaleCompleteUs m_cSaleComplete = null;                             // 거래완료처리

        private IServiceUs m_cSaveCfgSrv = null;                                    // 설정파일 저장
        private IServiceUs m_cReadCfgSrv = null;                                    // 설정파일 읽기
        private IDataCommonUs m_cDataCommon = null;                                 // POS 공통함수 인터페이스

        public CashierLogOn()
        {
            
            m_cPosStatus = (PosStatus)StateObject.POS;
            m_cTrnStatus = (TranStatus)StateObject.TRAN;

            m_cSqlDbService = (IDatabaseSQL)sManager.InitServiceInstance(ServiceLists.AGENT_DATABASE.DLL, ServiceLists.AGENT_DATABASE.DATABASE_MSSQL);
            m_cDataService = (IDataProcessUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_SERVICE);
            m_cSaleComplete = (ISaleCompleteUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.SALE_COMPLETE);
            
            m_cSaveCfgSrv = (IServiceUs)sManager.InitServiceInstance(ServiceLists.BSV_OPEN_CLOSE.DLL, ServiceLists.BSV_OPEN_CLOSE.SAVE_CONFIGINFO);
            m_cReadCfgSrv = (IServiceUs)sManager.InitServiceInstance(ServiceLists.BSV_OPEN_CLOSE.DLL, ServiceLists.BSV_OPEN_CLOSE.READ_CONFIGINFO);
            m_cDataCommon = (IDataCommonUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_COMMON);
        }

        /// <summary>
        /// 캐셔 로그인 처리[캐셔번호(0), 비밀번호(1)]
        /// </summary>
        public string Execute(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;

            try
            {
                string sCashierNo = aParam[0];          // 담당자
                string sCashierPwd = aParam[1];         // 비밀번호
                //Add 2017.09.18
                string sRegtype = aParam[2];


                //#20171107 통제관리 start, phj 
                string sMenuKey = string.Empty;
                if (aParam.Length == 4)
                {
                    if (aParam[3] != null && aParam[3] != "")
                    {
                        sMenuKey = aParam[3]; 
                    }
                }
                //#20171107 통제관리 end, phj

                string sMstCashierId = "";              // 담당자
                string sMstCashierTy = "";              // 담당자 구분
                string sMstCashierNm = "";              // 담당자 명
                string sMstCashierPw = "";              // 비밀번호
                string sMstAuthGroup = "";              // 권한그룹
                string sMstCashierRfndPw = "";          // 반품비밀번호
                string sMstCashierWebPosPw = "";        // 웹업무비밀번호

                string sWorkOnOffId = "";
                string sWorkOnOffNm = "";
                string sWorkOnOffGropu = "";
                string sWorkOnOffWebPosPw = "";
                //#20171107 통제관리 start, phj - 20171113
                string sWorkOnOffRfndPw = "";
                //#20171107 통제관리 end, phj - 20171113

                //캐셔마스터 조회
                IMasterUs cService = (IMasterUs)sManager.InitServiceInstance(ServiceLists.ASV_MASTER.DLL, ServiceLists.ASV_MASTER.CASHIER_MASTER);
                DataTable dtDataInfo = cService.Select(new string[] { m_cPosStatus.Base.StoreNo, sCashierNo });
                if (dtDataInfo == null || dtDataInfo.Rows.Count == 0)
                {
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0006);
                    return sRet;
                }

                foreach (DataRow drData in dtDataInfo.Rows)
                {
                    sMstCashierId = CmUtil.GetDataRowStr(drData, PosMst.MST_USER.DATA.CASHIER_ID);
                    sMstCashierTy = CmUtil.GetDataRowStr(drData, PosMst.MST_USER.DATA.CASHIER_DIV);
                    sMstCashierNm = CmUtil.GetDataRowStr(drData, PosMst.MST_USER.DATA.CASHIER_NM);
                    sMstCashierPw = CmUtil.GetDataRowStr(drData, PosMst.MST_USER.DATA.CASHIER_PWD);
                    sMstAuthGroup = CmUtil.GetDataRowStr(drData, PosMst.MST_USER.DATA.EMP_AUTH_GRP);
                    sMstCashierRfndPw = CmUtil.GetDataRowStr(drData, PosMst.MST_USER.DATA.RTN_PWD);
                    sMstCashierWebPosPw = CmUtil.GetDataRowStr(drData, PosMst.MST_USER.DATA.CASHIER_WEB_POS_PWD);
                }


                //#20171107 통제관리 start, phj
                //기존
                /*
                // 담당자 비밀번호 체크
                if (sCashierPwd != sMstCashierPw)
                {
                    // 비밀번호 틀림
                    WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0007);
                    return sRet;
                }
                */

                //변경
                if (sMenuKey == PosKey.MENU_KEY.AUTHORITY_CNTL)
                {
                    if (sCashierPwd != sMstCashierRfndPw)
                    {
                        // 비밀번호 틀림
                        WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0007);
                        return sRet;
                    }
                    else
                    {
                        sRet = UserCom.RST_OK;
                        return sRet;
                    }
                }
                else
                {
                    if (sCashierPwd != sMstCashierPw)
                    {
                        // 비밀번호 틀림
                        WinManager.ErrorMessage(POS_MESSAGE.ERROR.MSG_0007);
                        return sRet;
                    }
                }
                //#20171107 통제관리 end, phj




                // 거래데이터 초기화
                m_cDataService.DeleteTranItem();
                m_cDataService.InitTranHeader();

                m_cTrnStatus.Head.TradeDiv = ItemConst.TRAN_DIV.NORMAL;              // 거래구분 설정
                m_cTrnStatus.Head.TradeKind = ItemConst.TRAN_KIND.LOG.LOGIN;         // 거래종별 설정

                if (aParam[2] == "")
                { 
                    // 로그인 담당자정보 설정
                    m_cPosStatus.Base.CashierNo = sMstCashierId;
                    m_cPosStatus.Base.CashierName = sMstCashierNm;
                    m_cPosStatus.Base.CashierAuth = sMstAuthGroup;
                    m_cPosStatus.Base.CashierWebPosPwd = sMstCashierWebPosPw;
                    //#20171107 통제관리 start, phj - 20171113
                    m_cPosStatus.Base.CashierRtnPwd = sMstCashierRfndPw;
                    //#20171107 통제관리 end, phj - 20171113
                }
                else
                {   // 로그인 담당자정보 임시 설정
                    sWorkOnOffId = m_cPosStatus.Base.CashierNo;
                    sWorkOnOffNm = m_cPosStatus.Base.CashierName;
                    sWorkOnOffGropu = m_cPosStatus.Base.CashierAuth;
                    sWorkOnOffWebPosPw = m_cPosStatus.Base.CashierWebPosPwd;
                    //#20171107 통제관리 start, phj - 20171113
                    sWorkOnOffRfndPw = m_cPosStatus.Base.CashierRtnPwd;
                    //#20171107 통제관리 end, phj - 20171113

                    // 로그인 담당자정보 설정
                    m_cPosStatus.Base.CashierNo = sMstCashierId;
                    m_cPosStatus.Base.CashierName = sMstCashierNm;
                    m_cPosStatus.Base.CashierAuth = sMstAuthGroup;
                    m_cPosStatus.Base.CashierWebPosPwd = sMstCashierWebPosPw;
                    //#20171107 통제관리 start, phj - 20171113
                    m_cPosStatus.Base.CashierRtnPwd = sMstCashierRfndPw;
                    //#20171107 통제관리 end, phj - 20171113
                }

                // 거래 완료 처리(로그온)
                sRet = m_cSaleComplete.OpenCloseExecute(new string[] { });
                if (sRet != UserCom.RST_OK)
                {
                    // 로그인 담당자정보 해지
                    m_cPosStatus.Base.CashierNo = "";
                    m_cPosStatus.Base.CashierName = "";
                    return sRet;
                }

                // 거래데이터 완료 후 데이터 초기화 처리
                bool bIncTradeNo = m_cDataCommon.TradeNoAddCheck("");
                m_cDataService.TranComplete();
                m_cDataService.SetMsgNoInc(true, !bIncTradeNo, bIncTradeNo);      // 거래번호 증가

                // 로그인 담당자정보 저장
                CmMessage m_PosSaleInfo = CmMessage.MakeMessageFromFile(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.PosSaleInfo);
                m_PosSaleInfo.GetMessage("CASHIER").MakeMessageOverWrite("CashierNo", m_cPosStatus.Base.CashierNo);
                m_PosSaleInfo.GetMessage("CASHIER").MakeMessageOverWrite("CashierName", m_cPosStatus.Base.CashierName);
                m_PosSaleInfo.GetMessage("CASHIER").MakeMessageOverWrite("CashierAuth", m_cPosStatus.Base.CashierAuth);

                m_PosSaleInfo.MakeFileFromMessage(BaseCom.NxIniPath + PosConst.INI_FILE_NAME.PosSaleInfo);

                if(aParam[2] != "")
                { 
                    m_cPosStatus.Base.CashierNo = sWorkOnOffId;
                    m_cPosStatus.Base.CashierName = sWorkOnOffNm;
                    m_cPosStatus.Base.CashierAuth = sWorkOnOffGropu;
                    m_cPosStatus.Base.CashierWebPosPwd = sWorkOnOffWebPosPw;
                    //#20171107 통제관리 start, phj - 20171113
                    m_cPosStatus.Base.CashierRtnPwd = sWorkOnOffRfndPw;
                    //#20171107 통제관리 end, phj - 20171113
                }

                sRet = UserCom.RST_OK;
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
                m_cDataService.DeleteTranItem();              // 거래데이터 초기화
            }
            return sRet;
        }
    }
}
