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 SchShipment : IDataServiceUs
    {
        #region 변수 선언
        private SManager sManager = new SManager();                                 // 이 객체를 통해 업무 Service 호출
        private PosStatus m_cPosStatus = new PosStatus();                           //기본정보 참조
        private StateServer StateObject = (StateServer)StateServer.GetInstance();   // StateObject : StateServer Object (객체)

        private ISaleCompleteUs m_cSaleComplete = null;                             // 거래완료처리
        protected IDataCommonUs m_cDataCommon = null;                               // 판매공통 모듈

        protected Column.POS_REQ_SERVER_INTERFACE.DATA m_cCustItem = null;

        private ArrayList m_sPrintData = null;

        private ArrayList aCustItem = null;
        private IPosPrinterUs m_cPrinterUs = null;

        #endregion 변수 선언

        #region 생성자 & 소멸자
        public SchShipment()
        {
            m_cPosStatus = (PosStatus)StateObject.POS;      // POS 기본정보
            m_cDataCommon = (IDataCommonUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.DATA_COMMON);
            m_cPrinterUs = (IPosPrinterUs)sManager.InitServiceInstance(ServiceLists.AGENT_OLEDEVICE.DLL, ServiceLists.AGENT_OLEDEVICE.DEVICE_POSPRINTER);
            m_cSaleComplete = (ISaleCompleteUs)sManager.InitServiceInstance(ServiceLists.ASV_DATA_PROCESS.DLL, ServiceLists.ASV_DATA_PROCESS.SALE_COMPLETE);
        }
        #endregion 생성자 & 소멸자

        /// <summary>
        /// 출하/예정 내역 조회
        /// </summary>
        /// <param name="aParam"></param>
        /// <returns></returns>
        public object GetData(string[] aParam)
        {
            string sRet = string.Empty;

            try
            {
                sRet = SearchShipmentDetail( aParam[0]);
                return m_sPrintData;
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
            return sRet;
        }

        /// <summary>
        /// 출하/예정 내역 조회 조회 상세(출력메세지)
        /// </summary>
        /// <returns></returns>
        private string SearchShipmentDetail(string sDateFrom)
        {
            string sRet = UserCom.RST_ERR;

            string[] sCol = null;
            string[] sRow = null;

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

                m_sPrintData = new ArrayList();

                //INQ_TYPE
                htSendData.Add("INQ_TYPE", ItemConst.IRT_INQ_TYPE.SERVER_INTERFACE_SCH);
                // STOR_CD
                htSendData.Add("STOR_CD", m_cPosStatus.Base.StoreNo);
                // POS_NO
                htSendData.Add("POS_NO", m_cPosStatus.Base.PosNo);
                // STOR_TY
                htSendData.Add("STOR_TY", "10");
                // PRT_DIV
                htSendData.Add("PRT_DIV", ItemConst.IRT_PRT_DIV.SHIPMENT_LIST);
                // START_DT
                htSendData.Add("START_DT", sDateFrom);
                // END_DT
                htSendData.Add("END_DT", sDateFrom);
                // ORD_SEQ
                htSendData.Add("ORD_SEQ", "");
                // ORD_GRP
                htSendData.Add("ORD_GRP", "");
                // DATA_CNT
                htSendData.Add("DATA_CNT", "0");
                // RES_DATA
                htSendData.Add("RES_DATA", "");

                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)
                {
                    sRet = UserCom.RST_ERR;
                    WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0326));
                    return sRet;
                }

                if (htRecvData["DATA_CNT"].ToString() != "0")
                {
                    if (htRecvData.Count > 0)
                    {
                        sCol = htRecvData["RES_DATA"].ToString().Split(new string[] { "#~" }, StringSplitOptions.RemoveEmptyEntries);

                        aCustItem = (ArrayList)StateObject.GetItemObject(Column.POS_REQ_SERVER_INTERFACE.ITEM);

                        aCustItem.Clear();

                        for (int i = 0; i < sCol.Count(); i++)
                        {
                            m_cCustItem = new Column.POS_REQ_SERVER_INTERFACE.DATA();

                            sRow = sCol[i].Split('|');

                            m_cCustItem.FIELD_01 = sRow[0];
                            m_cCustItem.FIELD_02 = sRow[1];
                            m_cCustItem.FIELD_03 = sRow[2];
                            m_cCustItem.FIELD_04 = sRow[3];
                            m_cCustItem.FIELD_05 = sRow[4];
                            m_cCustItem.FIELD_06 = sRow[5];

                            aCustItem.Add(m_cCustItem);
                        }

                        sRet = DisplayShipmentDetail(sDateFrom);

                        return sRet;
                    }
                }
                else
                {
                    WinManager.ErrorMessage(MessageManager.GetErrorMessage(POS_MESSAGE.ERROR.MSG_0302));
                    sRet = UserCom.RST_ERR;
                    return sRet;
                }

                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 DisplayShipmentDetail(string sSchDate)
        {
            string sRet = UserCom.RST_ERR;

            string sPrintData = string.Empty;
            string sPrtDate = string.Empty;

            try
            {
                if (sSchDate != "" && sSchDate.Length >= 8)
                {
                    sPrtDate = sSchDate.Substring(0, 4) + "." + sSchDate.Substring(4, 2) + "." + sSchDate.Substring(6, 2);
                }

                // 프린트 헤더
                AddPrintData(PosConst.PRT_HDR.PRT_NOR, PosConst.PRT_FMT.F103, new string[] { "**" + MessageManager.GetPrintMessage(POS_MESSAGE.PRINT.MSG_0086) + "**" });
                AddPrintData(PosConst.PRT_HDR.PRT_NOR, PosConst.PRT_FMT.F103, new string[] { "[ " + MessageManager.GetPrintMessage(POS_MESSAGE.PRINT.MSG_0075) + " ]   " + sPrtDate });
                //AddPrintData(PosConst.PRT_HDR.PRT_NOR, PosConst.PRT_FMT.F100, new string[] { "-" });
                //AddPrintData(PosConst.PRT_HDR.PRT_NOR, "", new string[] { MessageManager.GetPrintMessage(POS_MESSAGE.PRINT.MSG_0087), MessageManager.GetPrintMessage(POS_MESSAGE.PRINT.MSG_0088), 
                //    MessageManager.GetPrintMessage(POS_MESSAGE.PRINT.MSG_0089), MessageManager.GetPrintMessage(POS_MESSAGE.PRINT.MSG_0090), MessageManager.GetPrintMessage(POS_MESSAGE.PRINT.MSG_0091)});
                //AddPrintData(PosConst.PRT_HDR.PRT_NOR, PosConst.PRT_FMT.F100, new string[] { "-" });

                for (int iRow = 0; iRow < aCustItem.Count; iRow++)
                {
                    Column.POS_REQ_SERVER_INTERFACE.DATA cCustItem = (Column.POS_REQ_SERVER_INTERFACE.DATA)aCustItem[iRow];
                    if (iRow == 0)
                    {
                        AddPrintData(PosConst.PRT_HDR.PRT_NOR, PosConst.PRT_FMT.F100, new string[] { "=" });
                        AddPrintData(PosConst.PRT_HDR.PRT_NOR, "", new string[] { cCustItem.FIELD_01, cCustItem.FIELD_02, cCustItem.FIELD_03, 
                        cCustItem.FIELD_04, cCustItem.FIELD_05});
                        AddPrintData(PosConst.PRT_HDR.PRT_NOR, PosConst.PRT_FMT.F100, new string[] { "=" });
                    }
                    else
                    {
                        if (cCustItem.FIELD_06 == "1")
                        {
                            AddPrintData(PosConst.PRT_HDR.PRT_NOR, PosConst.PRT_FMT.F100, new string[] { "-" });
                        }
                        AddPrintData(PosConst.PRT_HDR.PRT_NOR, "", new string[] { cCustItem.FIELD_01, cCustItem.FIELD_02, cCustItem.FIELD_03, 
                        cCustItem.FIELD_04, m_cPosStatus.Global.NumericTOCurrency(Convert.ToDouble(cCustItem.FIELD_05))});
                        if (cCustItem.FIELD_06 == "1" && iRow != aCustItem.Count - 1)
                        {
                            AddPrintData(PosConst.PRT_HDR.PRT_NOR, PosConst.PRT_FMT.F100, new string[] { "-" });
                        }
                    }

                    
                }

                AddPrintData(PosConst.PRT_HDR.PRT_NOR, PosConst.PRT_FMT.F100, new string[] { "=" });
                AddPrintData(PosConst.PRT_HDR.PRT_NOR, PosConst.PRT_FMT.F103, new string[] { "** " + MessageManager.GetPrintMessage(POS_MESSAGE.PRINT.MSG_0075) + " : " + DateTime.Now.ToString("yyyyMMddHHmmss") + " **" });
                //AddPrintData(PosConst.PRT_HDR.PRT_CUT, PosConst.PRT_FMT.F100, new string[] { " " });

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

        #region 영수증 출력데이터 생성
        /// <summary>
        /// 영수증 출력데이터 생성
        /// </summary>
        /// <param name="sPrtHead">출력형태</param>
        /// <param name="sPrtFmt">출력포맷</param>
        /// <param name="sData">Data</param>
        private void AddPrintData(string sPrtHead, string sPrtFmt, string[] aData)
        {
            try
            {
                string sPrtData = "";

                if (sPrtFmt == PosConst.PRT_FMT.F100)               // 선귿기
                {
                    for (int i = 1; i <= 42; i++) sPrtData += aData[0];
                }

                else if (sPrtFmt == PosConst.PRT_FMT.F101)          // 왼쪽정렬
                {
                    sPrtData = aData[0];
                }
                else if (sPrtFmt == PosConst.PRT_FMT.F102)          // 오른쪽 정렬
                {
                    sPrtData = CmUtil.LPadH(aData[0], 42);
                }
                else if (sPrtFmt == PosConst.PRT_FMT.F103)          // 중앙정렬
                {
                    if (sPrtHead == PosConst.PRT_HDR.PRT_HOR || sPrtHead == PosConst.PRT_HDR.PRT_BIG)
                    {
                        if (CmUtil.LenH(aData[0]) <= 21) sPrtData = CmUtil.LPadH("", 10 - (CmUtil.LenH(aData[0]) / 2)) + aData[0];
                    }
                    else
                    {
                        if (CmUtil.LenH(aData[0]) <= 42) sPrtData = CmUtil.LPadH("", 21 - (CmUtil.LenH(aData[0]) / 2)) + aData[0];
                    }
                }
                else
                {
                    switch (aData.Length)
                    {
                        case 5:
                            //sPrtData = CmUtil.RPadH(aData[0].ToString(), 14) + CmUtil.LPadH(aData[1].ToString(), 5) + CmUtil.RPadH(aData[2].ToString(), 5) + CmUtil.RPadH(aData[3].ToString(), 5) + CmUtil.RPadH(aData[4].ToString(), 13);
                            sPrtData = CmUtil.RPadH(aData[0].ToString(), 14) + CmUtil.LPadH(aData[1].ToString(), 5) + CmUtil.LPadH(aData[2].ToString(), 5) + CmUtil.LPadH(aData[3].ToString(), 5) + CmUtil.LPadH(aData[4].ToString(), 13);
                            break;
                        case 4:
                            sPrtData = CmUtil.RPadH(aData[0].ToString(), 12) + CmUtil.RPadH(aData[1].ToString(), 12) + CmUtil.LPadH(aData[2].ToString(), 7) + CmUtil.LPadH(aData[3].ToString(), 11);
                            break;
                        case 3:
                            sPrtData = CmUtil.RPadH(aData[0].ToString(), 23) + CmUtil.LPadH(aData[1].ToString(), 7) + CmUtil.LPadH(aData[2].ToString(), 11);
                            break;
                        default:
                            sPrtData = aData[0].ToString();
                            break;
                    }
                }
                m_sPrintData.Add(sPrtHead + sPrtData);
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }
        }
        #endregion 

        #region 영수증 프린터 출력
        /// <summary>
        /// 영수증 프린터 출력
        /// </summary>
        /// <param name="alPrintData"></param>
        /// <returns></returns>
        public string SetData(object oParam)
        {
            string bReturn = UserCom.RST_ERR;
            try
            {
                ArrayList alPrintData = (ArrayList)oParam;

                string[] arPrintData = (string[])alPrintData.ToArray(typeof(string));

                m_cPrinterUs.ReceiptPrinter(arPrintData);
            }
            catch (Exception ex)
            {
                UserLog.WriteLogFile(UserCom.LOG_ERROR,
                                     System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name,      // Project Name (프로젝트명)
                                     System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "." +  // Class Name (Class Name (클래스명))
                                     System.Reflection.MethodBase.GetCurrentMethod().Name + "()",                // Function Name (Function Name (함수명))
                                     ex.Message);
            }

            return bReturn;
        }
        #endregion

        public string Execute(string[] aParam)
        {
            throw new NotImplementedException();
        }
    }
}
