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

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

namespace Cosmos.Service
{
    class StoreCheck : IStoreCheck
    {
        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;                                     // 마스터 인터페이스
        private IDatabaseSQL m_cSqlDbService = null;                                // 데이터베이스 관리

        private ISaleCompleteUs m_cSaleComplete = null;                             // 거래완료처리

        protected IDataCommonUs m_cDataCommon = null;                               // 판매공통 모듈

        public StoreCheck()
        {
            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);
            m_cSqlDbService = (IDatabaseSQL)sManager.InitServiceInstance(ServiceLists.AGENT_DATABASE.DLL, ServiceLists.AGENT_DATABASE.DATABASE_MSSQL);
            m_cSaleComplete = (ISaleCompleteUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.SALE_COMPLETE);
        }

        /// <summary>
        /// 점포점검 스케줄 확인
        /// </summary>
        /// <returns></returns>
        public DataTable StoreCheckSchedule(string sPosStatus, string sType)
        {
            string sQuery = "";
            string sSaleDate = "";

            string sPosAlarmAppntType = "";  // POS 알람타입
            int nFromDate = 0;               // 일자 From
            int nToDate = 0;                 // 일자 To
            string sWeek = "";               // 주말
            string sDow = "";                // 요일
            string sStartHour = "";          // 시작시간
            string sFnshHour = "";           // 종료시간

            int nYYYY = 0;                   // 영업일자 년도
            int nMM = 0;                     // 영업일자 월
            int nDD = 0;                     // 영업일자 일

            string sInspctEntryGrpCd = string.Empty;   // 체크 그룹 코드

            double sSysTime = 0;       // 현재 시스템 시간

            sSaleDate = m_cPosStatus.Base.SaleDate;
            DataTable dtCheckInfo = new DataTable();
            dtCheckInfo.Columns.Add(new DataColumn("CHECK_CD", typeof(string)));

            try
            {
                sQuery += " SELECT CMP_CD ";
                sQuery += "      , INSPCT_ENTRY_GRP_CD ";
                sQuery += "      , INSPCT_ENTRY_GRP_NM ";
                sQuery += "      , POS_DIV ";
                sQuery += "      , SALE_POS_ALARM_TYPE ";
                sQuery += "      , SCORE_MNG_YN ";
                sQuery += "      , QUE ";
                sQuery += "      , SALE_POS_ALARM_DT_APPNT_TYPE ";
                sQuery += "      , DATE_1 ";
                sQuery += "      , DATE_2 ";
                sQuery += "      , WEEK ";
                sQuery += "      , DOW ";
                sQuery += "      , START_HOUR ";
                sQuery += "      , FNSH_HOUR ";
                sQuery += "      , SV_CONFRM_YN ";
                sQuery += "      , ANSWR_POSS_DAY_CNT ";
                sQuery += "  FROM POSMST..MST_STOR_INSPCT ";
                sQuery += " WHERE CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += "   AND POS_DIV = 'S' ";                                // 판매POS : S
                sQuery += "   AND SALE_POS_ALARM_TYPE = '" + sPosStatus + "'";
                sQuery += "   AND USE_YN = '1' ";

                DataTable dtDataInfo = m_cMstService.Select(new string[] { sQuery });

                if (dtDataInfo != null && dtDataInfo.Rows.Count > 0)
                {
                    if (sSaleDate.Length < 8)
                    {
                        // 스케줄의 기준은 시스템 일자가 아닌 영업일자가 기준이다.
                        // 영업일자가 빈 값이면 일자 비교를 못한다.
                        return dtCheckInfo;
                    }

                    nYYYY = Convert.ToInt32(sSaleDate.Substring(0, 4));         // 영업일자 년도
                    nMM = Convert.ToInt32(sSaleDate.Substring(4, 2));           // 영업일자 월
                    nDD = Convert.ToInt32(sSaleDate.Substring(6, 2));           // 영업일자 일

                    for (int nLoop = 0; nLoop < dtDataInfo.Rows.Count; nLoop++)
                    {
                        // 준비점검 공지사항일 경우 그냥 사용여부만 체크해서 포함시켜준다.
                        if (sPosStatus != "0")
                        {
                            sPosAlarmAppntType = CmUtil.GetDataRowStr(dtDataInfo.Rows[nLoop], PosMst.MST_STOR_INSPCT.DATA.SALE_POS_ALARM_DT_APPNT_TYPE);
                            nFromDate = CmUtil.GetDataRowInt(dtDataInfo.Rows[nLoop], PosMst.MST_STOR_INSPCT.DATA.DATE_1);
                            nToDate = CmUtil.GetDataRowInt(dtDataInfo.Rows[nLoop], PosMst.MST_STOR_INSPCT.DATA.DATE_2);
                            sWeek = CmUtil.GetDataRowStr(dtDataInfo.Rows[nLoop], PosMst.MST_STOR_INSPCT.DATA.WEEK);
                            sDow = CmUtil.GetDataRowStr(dtDataInfo.Rows[nLoop], PosMst.MST_STOR_INSPCT.DATA.DOW);
                            sStartHour = CmUtil.GetDataRowStr(dtDataInfo.Rows[nLoop], PosMst.MST_STOR_INSPCT.DATA.START_HOUR);
                            sFnshHour = CmUtil.GetDataRowStr(dtDataInfo.Rows[nLoop], PosMst.MST_STOR_INSPCT.DATA.FNSH_HOUR);

                            sInspctEntryGrpCd = CmUtil.GetDataRowStr(dtDataInfo.Rows[nLoop], PosMst.MST_STOR_INSPCT.DATA.INSPCT_ENTRY_GRP_CD);

                            sSysTime = CmUtil.GetValueToInt(DateTime.Now.ToString("HH:mm:ss").Replace(":", ""));

                            // 시간은 어느 조건이던지 체크하여 표시 여부를 판단한다.
                            // 시간이 입력되지 않았다면 무조건 포함시켜준다.

                            // 매일
                            if (sPosAlarmAppntType == "1")
                            {
                                // 시간이 입력되지 않았다면 표시한다.
                                if (sStartHour == "" || sStartHour == null || sFnshHour == "" || sFnshHour == null)
                                {
                                    // 준비점검 공지사항을 버튼으로 눌렀을 경우 그냥 더해준다.
                                    // 개점, 마감시에는 로컬부분에 저장되었는지 확인하여 더해준다.
                                    if (sType == PosKey.MENU_KEY.PREP_STOR_NOTICE)
                                    {
                                        CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                    }
                                    else
                                    {
                                        CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                    }
                                }
                                else
                                {
                                    if ((sSysTime > CmUtil.DoubleParse(sStartHour) && sSysTime < CmUtil.DoubleParse(sFnshHour)))
                                    {
                                        CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                    }
                                }
                            }

                            // 매월 지정일
                            if (sPosAlarmAppntType == "2")
                            {
                                if (nDD == nFromDate)
                                {
                                    // 시간이 입력되지 않았다면 표시한다.
                                    if (sStartHour == "" || sStartHour == null || sFnshHour == "" || sFnshHour == null)
                                    {
                                        CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                    }
                                    else
                                    {
                                        if ((sSysTime > CmUtil.DoubleParse(sStartHour) && sSysTime < CmUtil.DoubleParse(sFnshHour)))
                                        {
                                            CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                        }
                                    }
                                }
                            }

                            // 매월 최초 요일
                            // 3번 코드는 사용하지 않기로 함
                            if (sPosAlarmAppntType == "8")
                            {
                                string sChkRet = UserCom.RST_ERR;

                                DateTime dSaleDate = new DateTime(nYYYY, nMM, nDD);

                                sChkRet = GetMonthOfWeek(dSaleDate, "1", sWeek);
                                if (sChkRet == UserCom.RST_OK)
                                {
                                    // 시간이 입력되지 않았다면 표시한다.
                                    if (sStartHour == "" || sStartHour == null || sFnshHour == "" || sFnshHour == null)
                                    {
                                        CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                    }
                                    else
                                    {
                                        if ((sSysTime > CmUtil.DoubleParse(sStartHour) && sSysTime < CmUtil.DoubleParse(sFnshHour)))
                                        {
                                            CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                        }
                                    }
                                }
                            }

                            // 매주 지정요일
                            if (sPosAlarmAppntType == "4")
                            {
                                string sSaleWeek = string.Empty;

                                DateTime dSaleDate = new DateTime(nYYYY, nMM, nDD);

                                GetDayOfWeek(dSaleDate);

                                if (sSaleWeek == sWeek)
                                {
                                    // 시간이 입력되지 않았다면 표시한다.
                                    if (sStartHour == "" || sStartHour == null || sFnshHour == "" || sFnshHour == null)
                                    {
                                        CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                    }
                                    else
                                    {
                                        if ((sSysTime > CmUtil.DoubleParse(sStartHour) && sSysTime < CmUtil.DoubleParse(sFnshHour)))
                                        {
                                            CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                        }
                                    }
                                }
                            }

                            // 지정일
                            if (sPosAlarmAppntType == "5")
                            {
                                string sDd = m_cPosStatus.Base.SaleDate.Substring(6, 2);

                                if (nDD == nFromDate)
                                {
                                    // 시간이 입력되지 않았다면 표시한다.
                                    if (sStartHour == "" || sStartHour == null || sFnshHour == "" || sFnshHour == null)
                                    {
                                        CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                    }
                                    else
                                    {
                                        if ((sSysTime > CmUtil.DoubleParse(sStartHour) && sSysTime < CmUtil.DoubleParse(sFnshHour)))
                                        {
                                            CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                        }
                                    }
                                }
                            }

                            // 특정기간
                            if (sPosAlarmAppntType == "6")
                            {
                                if (nDD >= nFromDate && nDD <= nToDate)
                                {
                                    // 시간이 입력되지 않았다면 표시한다.
                                    if (sStartHour == "" || sStartHour == null || sFnshHour == "" || sFnshHour == null)
                                    {
                                        CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                    }
                                    else
                                    {
                                        if ((sSysTime > CmUtil.DoubleParse(sStartHour) && sSysTime < CmUtil.DoubleParse(sFnshHour)))
                                        {
                                            CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                        }
                                    }
                                }
                            }

                            // 매월 말일
                            if (sPosAlarmAppntType == "7")
                            {
                                DateTime dSaleDate = new DateTime(nYYYY, nMM, nDD);

                                DateTime dtLast_Day = dSaleDate.AddMonths(1).AddDays(0 - dSaleDate.Day);

                                if (nDD == dtLast_Day.Day)
                                {
                                    // 시간이 입력되지 않았다면 표시한다.
                                    if (sStartHour == "" || sStartHour == null || sFnshHour == "" || sFnshHour == null)
                                    {
                                        CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                    }
                                    else
                                    {
                                        if ((sSysTime > CmUtil.DoubleParse(sStartHour) && sSysTime < CmUtil.DoubleParse(sFnshHour)))
                                        {
                                            CheckAdd(dtCheckInfo, dtDataInfo, sType, nLoop, sSaleDate, sInspctEntryGrpCd);
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            DataRow dr = dtCheckInfo.NewRow();
                            dr["CHECK_CD"] = CmUtil.GetDataRowStr(dtDataInfo.Rows[nLoop], PosMst.MST_STOR_INSPCT.DATA.INSPCT_ENTRY_GRP_CD);
                            dtCheckInfo.Rows.Add(dr);
                        }
                    }
                }

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

        /// <summary>
        /// 체크 항목조회
        /// </summary>
        /// <param name="sINSPCT_ENTRY"></param>
        /// <returns></returns>
        private DataTable StoreCheckForm(string sINSPCT_ENTRY)
        {
            string sQuery = "";
            try
            {
                sQuery += " SELECT INSPCT_ENTRY_NM ";
                sQuery += "      , INSPCT_ENTRY ";
                sQuery += "      , SEL_ENTRY_CNT ";
                sQuery += "      , SEL_ENTRY_1 ";
                sQuery += "      , SEL_ENTRY_2 ";
                sQuery += "      , SEL_ENTRY_3 ";
                sQuery += "      , SEL_ENTRY_4 ";
                sQuery += "      , SEL_ENTRY_5 ";
                sQuery += " FROM POSMST..MST_SALESORG_INSPCT_DTL ";
                sQuery += " WHERE CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += "   AND BRAND_CD = '" + m_cPosStatus.Base.BrandCd + "'";
                //sQuery += "   AND BRAND_CD = '0001' ";
                sQuery += "   AND INSPCT_ENTRY = '" + sINSPCT_ENTRY + "'";
                sQuery += " ORDER BY QUE ASC ";

                DataTable dtDataInfo = m_cMstService.Select(new string[] { sQuery });

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

        private string GetDayOfWeek(DateTime dateTime)
        {
            DayOfWeek sDayOfWeek = dateTime.DayOfWeek;
            string sRet = string.Empty;

            try
            {
                switch (sDayOfWeek)
                {
                    case DayOfWeek.Monday:
                        sRet = "1";
                        break;
                    case DayOfWeek.Tuesday:
                        sRet = "2";
                        break;
                    case DayOfWeek.Wednesday:
                        sRet = "3";
                        break;
                    case DayOfWeek.Thursday:
                        sRet = "4";
                        break;
                    case DayOfWeek.Friday:
                        sRet = "5";
                        break;
                    case DayOfWeek.Saturday:
                        sRet = "6";
                        break;
                    case DayOfWeek.Sunday:
                        sRet = "7";
                        break;
                    default:
                        break;
                }
                return sRet;

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

        private string GetMonthOfWeek(DateTime dDateTime, string sCheckDow, string sCheckWeek)
        {
            string sRet = string.Empty;

            int sThisMonth = dDateTime.Month;      // 영업일자 월
            int sAddMonth = 0;

            DateTime dAddDate = DateTime.Today;

            int nMinusDay = 0;                     // 오늘날짜에서 빼줄 일자

            try
            {
                switch (sCheckDow)
                {
                    case "1":
                        nMinusDay = -7;
                        break;
                    case "2":
                        nMinusDay = -14;
                        break;
                    case "3":
                        nMinusDay = -21;
                        break;
                    case "4":
                        nMinusDay = -28;
                        break;
                    case "5":
                        nMinusDay = -35;
                        break;
                    case "6":
                        // 마지막 주는 더해준다.
                        nMinusDay = 7;
                        break;
                    default:
                        break;
                }

                string sWeek = GetDayOfWeek(dDateTime);

                // 요일이 체크하는 요일과 같다면
                if (sWeek == sCheckWeek)
                {   
                    // 몇번째 주(첫번째 주, 두번째 주, 세번째 주) 인지에에 따라서 마이너스 해준 후 
                    // 아래에서 월을 비교하여 이달의 몇번째 주인지 확인한다.
                    dAddDate = dDateTime.AddDays(nMinusDay);

                    sAddMonth = dAddDate.Month;

                    // 마이너스 한 결과 달이 바뀐다면 해당 주차가 맞다고 본다.
                    if (sThisMonth > sAddMonth)
                    {
                        // 지정한 주 이다.
                        // 해당하는 주 일 경우에만 달이 바뀐다.
                        sRet = UserCom.RST_OK;
                    }
                }

                return sRet;
            }
            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>
        /// <param name="sSaleDate">영업일자</param>
        /// <param name="sStoreCheckCd">점검 항목 그룹 코드</param>
        /// <returns></returns>
        public bool StoreSaveCheckLoacl(string sSaleDate, string sStoreCheckCd)
        {
            bool bRet = false;
            string sQuery = "";

            try
            {
                sQuery += " SELECT ANSWR_YN ";
                sQuery += " FROM POSLOG..ETC_STOR_INSPCT_ANSWR_STOR ";
                sQuery += " WHERE CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += "   AND STOR_CD = '" + m_cPosStatus.Base.StoreNo + "'";
                sQuery += "   AND START_DT = '" + sSaleDate + "'";
                sQuery += "   AND INSPCT_ENTRY_GRP_CD = '" + sStoreCheckCd + "'";

                DataTable dtDataInfo = m_cMstService.Select(new string[] { sQuery });

                if (dtDataInfo != null && dtDataInfo.Rows.Count > 0)
                {
                    if (CmUtil.GetDataRowStr(dtDataInfo.Rows[0], Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.ANSWR_YN) == "1")
                    {
                        bRet = true;
                        return bRet;
                    }
                }
            }
            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 bRet;
        }

        /// <summary>
        /// 체크항목
        /// </summary>
        /// <param name="sINSPCT_ENTRY"></param>
        /// <returns></returns>
        public DataTable StoreNoticeDTL(string sInspctEntryGrpCd)
        {
            string sQuery = "";
            try
            {
                sQuery += " SELECT A.INSPCT_ENTRY_GRP_CD AS INSPCT_ENTRY_GRP_CD ";
                sQuery += "      , A.INSPCT_ENTRY_L_CLSS_CD AS INSPCT_ENTRY_L_CLSS_CD ";
                sQuery += "      , A.INSPCT_ENTRY_L_CLSS_NM AS INSPCT_ENTRY_L_CLSS_NM ";
                sQuery += "      , B.INSPCT_ENTRY AS INSPCT_ENTRY ";
                sQuery += "      , B.INSPCT_ENTRY_NM AS INSPCT_ENTRY_NM ";
                sQuery += "      , B.INPUT_DIV AS INPUT_DIV ";
                sQuery += "      , B.SEL_ENTRY_CNT AS SEL_ENTRY_CNT ";
                sQuery += "      , B.SEL_ENTRY_1 AS SEL_ENTRY_1 ";
                sQuery += "      , B.SEL_ENTRY_2 AS SEL_ENTRY_2 ";
                sQuery += "      , B.SEL_ENTRY_3 AS SEL_ENTRY_3 ";
                sQuery += "      , B.SEL_ENTRY_4 AS SEL_ENTRY_4 ";
                sQuery += "      , B.SEL_ENTRY_5 AS SEL_ENTRY_5 ";
                sQuery += "      , B.SCORE_1 AS SCORE_1 ";
                sQuery += "      , B.SCORE_2 AS SCORE_2 ";
                sQuery += "      , B.SCORE_3 AS SCORE_3 ";
                sQuery += "      , B.SCORE_4 AS SCORE_4 ";
                sQuery += "      , B.SCORE_5 AS SCORE_5 ";
                sQuery += " FROM POSMST..MST_SALESORG_INSPCT_GRP AS A ";
                sQuery += "     LEFT OUTER JOIN POSMST..MST_SALESORG_INSPCT_DTL AS B ";
                sQuery += "        ON A.CMP_CD = B.CMP_CD AND A.BRAND_CD = B.BRAND_CD AND A.INSPCT_ENTRY_GRP_CD = B.INSPCT_ENTRY_GRP_CD ";
                sQuery += "       AND A.INSPCT_ENTRY_L_CLSS_CD = B.INSPCT_ENTRY_L_CLSS_CD ";
                sQuery += " WHERE A.CMP_CD = '" + m_cPosStatus.Base.CmpCd + "'";
                sQuery += "   AND A.BRAND_CD = '" + m_cPosStatus.Base.BrandCd + "'";
                sQuery += "   AND A.INSPCT_ENTRY_GRP_CD = '" + sInspctEntryGrpCd + "'";
                sQuery += "   AND A.USE_YN = '" + PosConst.MST_USE_YN.YES + "'";
                sQuery += "   AND B.USE_YN = '" + PosConst.MST_USE_YN.YES + "'";
                if (sInspctEntryGrpCd == "11")
                {
                    sQuery += "   AND A.INSPCT_ENTRY_L_CLSS_CD <> '12' ";
                }
                sQuery += " ORDER BY A.INSPCT_ENTRY_L_CLSS_CD, QUE ASC ";

                DataTable dtDataInfo = m_cMstService.Select(new string[] { sQuery });

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

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string StoreCheckSave(string sInsertUpdate, DataTable dtData1, DataTable dtData2)
        {
            string sRet = UserCom.RST_ERR;
            string sInsQuery1 = string.Empty;
            string sInsQuery2 = string.Empty;
            string sRegDateTime = string.Empty;
            string sQuery = string.Empty;

            try
            {
                if (dtData1 != null && dtData2 != null)
                {
                    if (m_cSqlDbService.SetDBConnectionString(m_cPosStatus.Base.LocalDbSource,
                                                                  m_cPosStatus.Base.LocalDbCatalog,
                                                                  m_cPosStatus.Base.LocalDbUserID,
                                                                  m_cPosStatus.Base.LocalDbPassword) == true)
                    {

                        m_cSqlDbService.Begin();

                        sRegDateTime = DateTime.Now.ToString("yyyyMMddHHmmss");

                        foreach (DataRow dr1 in dtData1.Rows)
                        {
                            if (sInsertUpdate == "0")
                            {
                                // 점포 점검 헤더 저장
                                sInsQuery1 = "";
                                sInsQuery1 += " INSERT INTO POSLOG..ETC_STOR_INSPCT_ANSWR_STOR ( ";
                                sInsQuery1 += " CMP_CD ,INSPCT_ENTRY_GRP_CD ,STOR_CD ,START_DT ,ANSWR_YN ,ETC_CTNTS_1 ,ETC_CTNTS_2 ,ETC_CTNTS_3 , SEND_YN, REG_USER_ID ";
                                sInsQuery1 += ",REG_DATE ,UPD_USER_ID ,UPD_DATE  ";
                                sInsQuery1 += " ) VALUES ( '{0}', '{1}', '{2}', '{3}', '{4}', N'{5}', N'{6}', N'{7}', '{8}' , '{9}', ";
                                sInsQuery1 += " '{10}','{11}', '{12}' )";

                                sQuery = string.Format(sInsQuery1
                                                            , m_cPosStatus.Base.CmpCd                                              // 회사코드
                                                            , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.INSPCT_ENTRY_GRP_CD]       // 점검 항목 그룹코드
                                                            , m_cPosStatus.Base.StoreNo                                            // 점포코드
                                                            , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.START_DT]                  // 점검일자              
                                                            , "1"                                                                  // 응답여부
                                                            , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.ETC_CTNTS_1]               // 기타내용1
                                                            , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.ETC_CTNTS_2]               // 기타내용2
                                                            , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.ETC_CTNTS_3]               // 기타내용3
                                                            , "0"                                                                  // 전송여부
                                                            , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.REG_USER_ID]               // 등록자
                                                            , sRegDateTime                                                         // 등록일자
                                                            , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.REG_USER_ID]               // 수정자
                                                            , sRegDateTime                                                         // 수정일자
                                                            );

                            }
                            else if (sInsertUpdate == "1")
                            {
                                // 점포 점검 헤더 업데이트
                                sInsQuery1 = "";
                                sInsQuery1 = " UPDATE POSLOG..ETC_STOR_INSPCT_ANSWR_STOR ";
                                sInsQuery1 += "    SET ANSWR_YN = '{0}' ";
                                sInsQuery1 += "       ,ETC_CTNTS_1 = N'{1}' ";
                                sInsQuery1 += "       ,ETC_CTNTS_2 = N'{2}' ";
                                sInsQuery1 += "       ,ETC_CTNTS_3 = N'{3}' ";
                                sInsQuery1 += "       ,SEND_YN = '{4}' ";
                                sInsQuery1 += "       ,UPD_USER_ID = '{5}' ";
                                sInsQuery1 += "       ,UPD_DATE = '{6}' ";
                                sInsQuery1 += " WHERE CMP_CD = '{7}' ";
                                sInsQuery1 += "   AND INSPCT_ENTRY_GRP_CD = '{8}' ";
                                sInsQuery1 += "   AND STOR_CD = '{9}' ";
                                sInsQuery1 += "   AND START_DT = '{10}' ";

                                sQuery = string.Format(sInsQuery1
                                                        , "1"                                                             // 응답여부
                                                        , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.ETC_CTNTS_1]          // 기타내용1
                                                        , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.ETC_CTNTS_2]          // 기타내용2
                                                        , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.ETC_CTNTS_3]          // 기타내용3
                                                        , "0"                                                             // 전송여부
                                                        , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.REG_USER_ID]          // 수정자
                                                        , sRegDateTime                                                    // 수정일자
                                                        , m_cPosStatus.Base.CmpCd                                         // CMP_CD
                                                        , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.INSPCT_ENTRY_GRP_CD]  // 점검항목그룹코드
                                                        , m_cPosStatus.Base.StoreNo                                       // 점포코드
                                                        , dr1[Column.ETC_STOR_INSPCT_ANSWR_STOR.NAME.START_DT]             // 점검일자
                                                        );
                            }

                        }

                        foreach (DataRow dr2 in dtData2.Rows)
                        {
                            // 점포 점검 디테일 저장
                            if (sInsertUpdate == "0")
                            {
                                sInsQuery2 = "";
                                sInsQuery2 += " INSERT INTO POSLOG..ETC_STOR_INSPCT_ANSWR ( ";
                                sInsQuery2 += " CMP_CD ,BRAND_CD ,INSPCT_ENTRY_GRP_CD ,STOR_CD ,START_DT ,INSPCT_ENTRY_L_CLSS_CD ,INSPCT_ENTRY ";
                                sInsQuery2 += " ,SEL_ENTRY_1_YN ,SEL_ENTRY_2_YN ,SEL_ENTRY_3_YN ,SEL_ENTRY_4_YN ,SEL_ENTRY_5_YN ";
                                sInsQuery2 += " ,SCORE_1 ,SCORE_2 ,SCORE_3 ,SCORE_4 ,SCORE_5 ";
                                sInsQuery2 += " ,SEL_ENTRY_1_CTNTS ,SEL_ENTRY_2_CTNTS ,SEL_ENTRY_3_CTNTS ,SEL_ENTRY_4_CTNTS ,SEL_ENTRY_5_CTNTS ";
                                sInsQuery2 += " ,NOTE ,SEND_YN ";
                                sInsQuery2 += " ,REG_USER_ID ,REG_DATE ,UPD_USER_ID ,UPD_DATE  ";
                                sInsQuery2 += " ) VALUES ( '{0}', '{1}', '{2}', '{3}', '{4}', '{5}', {6}, '{7}', '{8}' , '{9}', ";
                                sInsQuery2 += " '{10}','{11}', {12}, {13}, {14}, {15}, {16}, N'{17}', N'{18}', N'{19}', N'{20}', N'{21}', '{22}', '{23}', '{24}', '{25}', '{26}', '{27}' )";

                                sQuery += string.Format(sInsQuery2
                                                            , m_cPosStatus.Base.CmpCd                                         // 회사코드
                                                            , m_cPosStatus.Base.BrandCd                                       // 브랜드 코드
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.INSPCT_ENTRY_GRP_CD]      // 점검 항목 그룹코드
                                                            , m_cPosStatus.Base.StoreNo                                       // 점포코드
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.START_DT]                 // 점검일자 
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.INSPCT_ENTRY_L_CLSS_CD]   // 점검항목대분류코드
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.INSPCT_ENTRY]             // 점검항목
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_1_YN]           // 선택항목1 여부
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_2_YN]           // 선택항목2 여부
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_3_YN]           // 선택항목3 여부
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_4_YN]           // 선택항목4 여부
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_5_YN]           // 선택항목5 여부
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SCORE_1]                  // 점수1
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SCORE_2]                  // 점수2
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SCORE_3]                  // 점수3
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SCORE_4]                  // 점수4
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SCORE_5]                  // 점수5
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_1_CTNTS]        // 선택항목1 내용
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_2_CTNTS]        // 선택항목2 내용
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_3_CTNTS]        // 선택항목3 내용
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_4_CTNTS]        // 선택항목4 내용
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_5_CTNTS]        // 선택항목5 내용
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.NOTE]                     // 비고
                                                            , "0"                                                             // 전송여부
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.REG_USER_ID]              // 등록자
                                                            , sRegDateTime                                                    // 등록일자
                                                            , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.REG_USER_ID]              // 수정자
                                                            , sRegDateTime                                                    // 수정일자
                                                            );
                            }
                            else if (sInsertUpdate == "1")
                            {
                                // 점포점검 디테일 업데이트
                                sInsQuery2 = "";
                                sInsQuery2 = " UPDATE POSLOG..ETC_STOR_INSPCT_ANSWR ";
                                sInsQuery2 += "    SET SEL_ENTRY_1_YN = '{0}' ";
                                sInsQuery2 += "       ,SEL_ENTRY_2_YN = '{1}' ";
                                sInsQuery2 += "       ,SEL_ENTRY_3_YN = '{2}' ";
                                sInsQuery2 += "       ,SEL_ENTRY_4_YN = '{3}' ";
                                sInsQuery2 += "       ,SEL_ENTRY_5_YN = '{4}' ";
                                sInsQuery2 += "       ,SCORE_1 = '{5}' ";
                                sInsQuery2 += "       ,SCORE_2 = '{6}' ";
                                sInsQuery2 += "       ,SCORE_3 = '{7}' ";
                                sInsQuery2 += "       ,SCORE_4 = '{8}' ";
                                sInsQuery2 += "       ,SCORE_5 = '{9}' ";
                                sInsQuery2 += "       ,SEL_ENTRY_1_CTNTS = N'{10}' ";
                                sInsQuery2 += "       ,SEL_ENTRY_2_CTNTS = N'{11}' ";
                                sInsQuery2 += "       ,SEL_ENTRY_3_CTNTS = N'{12}' ";
                                sInsQuery2 += "       ,SEL_ENTRY_4_CTNTS = N'{13}' ";
                                sInsQuery2 += "       ,SEL_ENTRY_5_CTNTS = N'{14}' ";
                                sInsQuery2 += "       ,NOTE = '{15}' ";
                                sInsQuery2 += "       ,SEND_YN = '{16}' ";
                                sInsQuery2 += "       ,UPD_USER_ID = '{17}' ";
                                sInsQuery2 += "       ,UPD_DATE = '{18}' ";
                                sInsQuery2 += " WHERE CMP_CD = '{19}' ";
                                sInsQuery2 += "   AND BRAND_CD = '{20}' ";
                                sInsQuery2 += "   AND INSPCT_ENTRY_GRP_CD = '{21}' ";
                                sInsQuery2 += "   AND STOR_CD = '{22}' ";
                                sInsQuery2 += "   AND START_DT = '{23}' ";
                                sInsQuery2 += "   AND INSPCT_ENTRY_L_CLSS_CD = '{24}' ";
                                sInsQuery2 += "   AND INSPCT_ENTRY = '{25}' ";

                                sQuery += string.Format(sInsQuery2
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_1_YN]           // 선택항목1 여부
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_2_YN]           // 선택항목2 여부
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_3_YN]           // 선택항목3 여부
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_4_YN]           // 선택항목4 여부
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_5_YN]           // 선택항목5 여부
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SCORE_1]                  // 점수1
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SCORE_2]                  // 점수2
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SCORE_3]                  // 점수3
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SCORE_4]                  // 점수4
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SCORE_5]                  // 점수5
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_1_CTNTS]        // 선택항목1 내용
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_2_CTNTS]        // 선택항목2 내용
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_3_CTNTS]        // 선택항목3 내용
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_4_CTNTS]        // 선택항목4 내용
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.SEL_ENTRY_5_CTNTS]        // 선택항목5 내용
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.NOTE]                     // 비고
                                                        , "0"                  // 전송여부
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.REG_USER_ID]              // 등록자
                                                        , sRegDateTime                 // 갱신일시
                                                        , m_cPosStatus.Base.CmpCd                   // 회사코드
                                                        , m_cPosStatus.Base.BrandCd                 // 브랜드코드
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.INSPCT_ENTRY_GRP_CD]      // 점검 항목 그룹코드
                                                        , m_cPosStatus.Base.StoreNo                  // 점포코드
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.START_DT]      // 점검 항목 그룹코드
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.INSPCT_ENTRY_L_CLSS_CD]      // 점검 항목 그룹코드   // 점검항목대분류코드
                                                        , dr2[Column.ETC_STOR_INSPCT_ANSWR.NAME.INSPCT_ENTRY]             // 점검항목
                                                        );
  
                            }

                        }
                        if (m_cSqlDbService.DBExecuteNonQuery(new string[] { sQuery }) != UserCom.OK)
                        {
                            m_cSqlDbService.Rollback();
                            return sRet;
                        }
                        m_cSqlDbService.Commit();
                        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>
        /// 점포점검 등록 여부 IRT
        /// </summary>
        /// <returns></returns>
        public string StoreCheckSaveIrt(string InspctEntryGrpCd, string sCheckDate)
        {
            string sRet = UserCom.RST_ERR;

            try
            {
                Hashtable htSendData = new Hashtable();
                Hashtable htRecvData = new Hashtable();

                htSendData.Add("INQ_TYPE", ItemConst.IRT_INQ_TYPE.STORE_SAVE_CHECK);
                htSendData.Add("STOR_CD", m_cPosStatus.Base.StoreNo);
                htSendData.Add("INSPCT_ENTRY_GRP_CD", InspctEntryGrpCd);
                htSendData.Add("START_DT", sCheckDate);
                htSendData.Add("RES_CD", "");

                sRet = m_cDataCommon.ExecutePosIrt(ItemConst.COMM_MSG_TYPE.POSIRT, m_cPosStatus.Base.CommSvrIp, (int)m_cPosStatus.Base.BizInqPort, 10000, htSendData, ref htRecvData);
                if (sRet != UserCom.RST_OK || htRecvData == null)
                {
                    //WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0326));
                    return UserCom.RST_OK;
                }

                if (htRecvData[Column.IQ_SALEONCREDIT_CUSTINQ_RSP.DATA.RES_CD].ToString() != "00")
                {
                    return UserCom.RST_OK;
                }

                return UserCom.RST_ERR;
            }
            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 UserCom.RST_ERR;
        }


        private void CheckAdd(DataTable dtCheckInfo, DataTable dtDataInfo, string sType, int nLoop, string sSaleDate, string sInspctEntryGrpCd)
        {
            try
            {
                // 준비점검 공지사항을 버튼으로 눌렀을 경우 그냥 더해준다.
                // 개점, 마감시에는 로컬부분에 저장되었는지 확인하여 더해준다.
                if (sType == PosKey.MENU_KEY.PREP_STOR_NOTICE)
                {
                    DataRow dr = dtCheckInfo.NewRow();
                    dr["CHECK_CD"] = CmUtil.GetDataRowStr(dtDataInfo.Rows[nLoop], PosMst.MST_STOR_INSPCT.DATA.INSPCT_ENTRY_GRP_CD);
                    dtCheckInfo.Rows.Add(dr);
                }
                else
                {
                    if (StoreSaveCheckLoacl(sSaleDate, sInspctEntryGrpCd) == false)
                    {
                        DataRow dr = dtCheckInfo.NewRow();
                        dr["CHECK_CD"] = CmUtil.GetDataRowStr(dtDataInfo.Rows[nLoop], PosMst.MST_STOR_INSPCT.DATA.INSPCT_ENTRY_GRP_CD);
                        dtCheckInfo.Rows.Add(dr);
                    }
                }

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

    }
}
