using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Data;

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

namespace Cosmos.Service
{
    class PosNoticeCom : IDataServiceUs
    {
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private PosStatus m_cPosStatus = new PosStatus();                           //기본정보 참조
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private IMasterUs m_cMstService = null;                                     // 마스터 관리
        protected IDataCommonUs m_cDataCommon = null;                               // 판매공통 모듈
        
        public PosNoticeCom()
        {
            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            m_cDataCommon = (IDataCommonUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_COMMON);
            m_cMstService = (IMasterUs)sManager.InitServiceInstance(ServiceLists.ASV_MASTER.DLL, ServiceLists.ASV_MASTER.POS_MASTER);
        }

        /// <summary>
        /// 서비스 실행
        /// </summary>
        public string Execute(string[] aParam)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                string sQuery = "";

                sQuery += " UPDATE POSMST.dbo.MST_EMG_NOTE ";
                sQuery += "    SET READ_YN = '" + aParam[3] + "' ";
                sQuery += "      , UPD_DT  = '" + DateTime.Now.ToString("yyyyMMddHHmmss") + "' ";
                sQuery += "      , SEND_YN = '0' ";
                sQuery += "  WHERE 1 = 1 ";
                sQuery += "	   AND CMP_CD   = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += "	   AND STOR_CD  = '" + m_cPosStatus.Base.StoreNo + "'";
                sQuery += "	   AND POS_NO   = '" + m_cPosStatus.Base.PosNo + "'";
                sQuery += "    AND START_DT = '" + aParam[0] + "'";
                sQuery += "    AND MSG_CODE = '" + aParam[1] + "'";
                sQuery += "    AND MSG_DIV  = '" + aParam[2] + "'";

                sQuery = sQuery.Replace("\t", " ");

                int iRet = m_cMstService.Update(new string[] { sQuery });

                if (iRet == 1)
                    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);
            }
            return sRet;
        }

        /// <summary>
        /// 데이터 획득
        /// </summary>
        public object GetData(string[] aParam)
        {
            try
            {
                string sQuery = "";

                if (aParam[0] == PosMst.MST_EMG_NOTE.TABLE_NAME)
                {

                    sQuery += " SELECT *";
                    sQuery += " FROM POSMST.dbo.MST_EMG_NOTE ";
                    sQuery += " WHERE 1 = 1 ";
                    sQuery += "   AND CMP_CD   = '" + m_cPosStatus.Base.CmpCd + "'";
                    sQuery += "   AND STOR_CD  = '" + m_cPosStatus.Base.StoreNo + "'";
                    sQuery += "   AND POS_NO   = '" + m_cPosStatus.Base.PosNo + "'";

                    if (aParam[1] == ItemConst.NOTE_MSG_DIV.NRL)    //일반
                    {
                        sQuery += "   AND START_DT <= '" + DateTime.Now.ToString("yyyyMMdd") + "'";
                        sQuery += "   AND MSG_DIV   = '" + ItemConst.NOTE_MSG_DIV.NRL + "'";
                        sQuery += "   AND FNSH_HOUR >= '" + DateTime.Now.ToString("yyyyMMddHHmmss") + "'";
                    }
                    else if (aParam[1] == ItemConst.NOTE_MSG_DIV.EMG)   //긴급
                    {
                        sQuery += "   AND START_DT <= '" + DateTime.Now.ToString("yyyyMMdd") + "'";
                        sQuery += "   AND MSG_DIV   = '" + ItemConst.NOTE_MSG_DIV.EMG + "'";
                        sQuery += "   AND FNSH_HOUR >= '" + DateTime.Now.ToString("yyyyMMddHHmmss") + "'";
                    }
                    else if (aParam[1] == ItemConst.NOTE_MSG_DIV.BIZ)   //영업
                    {
                        sQuery += "   AND START_DT <= '" + DateTime.Now.ToString("yyyyMMdd") + "'";
                        sQuery += "   AND MSG_DIV   = '" + ItemConst.NOTE_MSG_DIV.BIZ + "'";
                        sQuery += "   AND FNSH_HOUR >= '" + DateTime.Now.ToString("yyyyMMddHHmmss") + "'";
                    }
                    else if (aParam[1] == ItemConst.NOTE_MSG_DIV.ALL)   //전체
                    {
                        sQuery += "   AND START_DT <= '" + DateTime.Now.ToString("yyyyMMdd") + "'";
                        sQuery += "   AND FNSH_HOUR >= '" + DateTime.Now.ToString("yyyyMMddHHmmss") + "'";
                    }
                    else if (aParam[1] == ItemConst.NOTE_MSG_DIV.ROL)   //롤링
                    {
                        sQuery += "   AND START_DT <= '" + DateTime.Now.ToString("yyyyMMdd") + "'";
                        sQuery += "   AND MSG_DIV   = '" + ItemConst.NOTE_MSG_DIV.EMG + "'";

                        sQuery += "   AND MSG_TYPE NOT IN ('1','2') ";  //프로그램 또는 마스터가 아닌것
                        sQuery += "   AND POPUP_YN  = '0'";    //팝업구분
                        sQuery += "   AND FNSH_HOUR >= '" + DateTime.Now.ToString("yyyyMMddHHmmss") + "'";
                        sQuery += "   AND USE_YN = '1' ";

                    }
                    else if (aParam[1] == ItemConst.NOTE_MSG_DIV.POP)   // 팝업 또는 프로그램/마스터
                    {
                        sQuery += "   AND START_DT = '" + DateTime.Now.ToString("yyyyMMdd") + "'";
                        sQuery += "   AND MSG_DIV  = '" + ItemConst.NOTE_MSG_DIV.EMG + "'";
                        sQuery += "   AND (MSG_TYPE IN ('1','2') OR POPUP_YN = '1')";    //프로그램 또는 마스터 또는 팝업
                        sQuery += "   AND READ_YN  IN ('0','1') ";    //
                        sQuery += "   AND USE_YN = '1' ";
                    }

                    sQuery += " ORDER BY CMP_CD ,STOR_CD ,POS_NO, MSG_CODE DESC ";


                }
                else if (aParam[0] == PosMst.MST_CPI.TABLE_NAME)
                {
                    sQuery = " SELECT ROW_NUMBER() OVER( ORDER BY CPI_CD ASC) AS NO,CPI_CD,CPI_NM,CPI_EXPLN,CPI_INFO ";
                    sQuery += "  FROM POSMST.dbo.MST_CPI WITH(NOLOCK) ";
                    sQuery += " WHERE 1 = 1 ";
                    sQuery += "  AND CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'  ";
                    sQuery += "  AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "' ";
                    sQuery += "  AND '" + DateTime.Now.ToString("yyyyMMdd") + "' BETWEEN STARTDAY AND FNSHDAY ";
                    sQuery += "  AND PRGRS_STATUS  = 'C'";
                    sQuery += "  AND USE_YN = '" + PosConst.MST_USE_YN.YES + "' ";
                }

                sQuery = sQuery.Replace("\t", " ");

                return m_cMstService.Select(new string[] { sQuery });
            }
            catch (Exception ex)
            {
                WinManager.ExceptionMessage(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,
                    System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + "()", ex.Message);
            }
            return null;
        }

        /// <summary>
        /// 프로그램 관련 데이터 업데이트
        /// </summary>
        public string SetData(object oParam)
        {
            string sRet = UserCom.RST_ERR;
            try
            {
                string sQuery = "";

                // 재기동시 POS 프로그램은 무조건 최신버전으로 수신하기에 
                // 긴급메시지로 프로그램 다운로드 하라는 메시지 읽음으로 처리한다.
                sQuery += " UPDATE POSMST.dbo.MST_EMG_NOTE \n";
                sQuery += "    SET READ_YN = '2' \n";
                sQuery += "      , SEND_YN = '0' \n";
                sQuery += "      , DOWN_YN = '1' \n";
                sQuery += "  WHERE 1 = 1 \n";
                sQuery += "	   AND CMP_CD   = '" + m_cPosStatus.Base.CmpCd + "' \n";
                sQuery += "	   AND STOR_CD  = '" + m_cPosStatus.Base.StoreNo + "' \n";
                sQuery += "	   AND POS_NO   = '" + m_cPosStatus.Base.PosNo + "' \n";
                sQuery += "    AND READ_YN <> '2' \n";
                sQuery += "    AND MSG_TYPE = '1' \n";
                sQuery += "    AND MSG_DIV  = '" + ItemConst.NOTE_MSG_DIV.EMG + "' \n";

                sQuery = sQuery.Replace("\t", " ");

                int iRet = m_cMstService.Update(new string[] { sQuery });

                if (iRet == 1)
                    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);
            }
            return sRet;
        }

    }
}
