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

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


namespace Cosmos.Service
{
    class TranResend : IDataServiceUs
    {
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)
        private PosStatus m_cPosStatus = new PosStatus();                           // 기본정보 참조

        private IDatabaseSQL m_cSqlDbService = null;                                

        public TranResend()
        {
            m_cPosStatus = (PosStatus)StateObject.POS;

            m_cSqlDbService = (IDatabaseSQL)sManager.InitServiceInstance(ServiceLists.AGENT_DATABASE.DLL, ServiceLists.AGENT_DATABASE.DATABASE_MSSQL);
        }

        /// <summary>
        /// 테이터 조회
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public object GetData(string[] aParam)
        {
            string sQuery = "";

            try
            {
                sQuery  = " SELECT COUNT(1) AS TOTAL_CNT ";
                sQuery +=      " , SUM(CASE WHEN SEND_YN = '1' ";
                sQuery +=                 " THEN 1 ";
                sQuery +=                 " ELSE 0 ";
                sQuery +=            " END) AS OK_CNT ";
                sQuery +=      " , SUM(CASE WHEN SEND_YN = '9' ";
                sQuery +=                 " THEN 1 ";
                sQuery +=                 " ELSE 0 ";
                sQuery +=            " END) AS FAIL_CNT ";
                sQuery +=   " FROM POSLOG..TR_SALE_HEADER ";
                sQuery +=  " WHERE CMP_CD = '" + aParam[0] + "' ";
                sQuery +=    " AND SALE_DT = '" + aParam[1] + "' ";
                sQuery +=    " AND STOR_CD = '" + aParam[2] + "' ";

                m_cSqlDbService.SetDBConnectionString(m_cPosStatus.Base.LocalDbSource, m_cPosStatus.Base.LocalDbCatalog, m_cPosStatus.Base.LocalDbUserID, m_cPosStatus.Base.LocalDbPassword);

                DataTable dtDataInfo = null;

                m_cSqlDbService.DBSelect(sQuery, out dtDataInfo);

                m_cSqlDbService.Close();

                return dtDataInfo;
            }
            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;
        }

        public string SetData(object oParam)
        {
            return UserCom.RST_ERR;
        }

        /// <summary>
        /// 매출 전송 Flag Update
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public string Execute(string[] aParam)
        {
            string sQuery = "";
            string sRet = UserCom.RST_ERR;
            try
            {
                sQuery  = " UPDATE POSLOG..TR_SALE_HEADER ";
                sQuery += "    SET SEND_YN = '0' ";
                sQuery += "  WHERE CMP_CD = '{0}' ";
                sQuery += "    AND SALE_DT = '{1}' ";
                sQuery += "    AND STOR_CD = '{2}' ";

                string[] saQuery = new string[1];
                saQuery[0] = string.Format(sQuery, aParam[0], aParam[1], aParam[2]);

                m_cSqlDbService.SetDBConnectionString(m_cPosStatus.Base.LocalDbSource, m_cPosStatus.Base.LocalDbCatalog, m_cPosStatus.Base.LocalDbUserID, m_cPosStatus.Base.LocalDbPassword);

                if (m_cSqlDbService.DBExecuteNonQuery(saQuery) == UserCom.OK)
                {
                    sRet = UserCom.RST_OK;
                }

                m_cSqlDbService.Close();
            }
            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;
        }
    }
}
