using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using DeiNiu.wms.Logical;
using System.Transactions;
using DeiNiu.wms.Data.Model;
using DeiNiu.Utils;
using System.Net;
namespace DeiNiu.Wcf
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“ScheduledService”。
    public class ScheduledService : basicService,IScheduledService
    {
        static int syncOperId = 3335;
        static lWmsInRequest _lir;
        static WmsGoods _wgoods;
        WmsGoods wgoods
        {
              get
            {
                if (_wgoods == null  )
                {
                    _wgoods = new WmsGoods();
                }
                return _wgoods;
            }
        }
        lWmsInRequest lir
        {
            get
            {
                if (_lir == null  )
                {
                    _lir = new lWmsInRequest(syncOperId);
                }
                return _lir;
            }
        }
        private static lWmsOutRequest _lor;
        lWmsOutRequest lor
        {
            get
            {
                if (_lor == null  )
                {
                    _lor = new lWmsOutRequest(syncOperId);
                }
                return _lor;
            }
        }
        private static lOwner _lOwner;
        lOwner lOwnerObj
        {
            get
            {
                if (_lOwner == null  )
                {
                    _lOwner = new lOwner(syncOperId);
                }
                return _lOwner;
            }
        }
        private static lCustomer _lcust;
        lCustomer lcust
        {
            get
            {
                if (_lcust == null)
                {
                    _lcust = new lCustomer(syncOperId);
                }
                return _lcust;
            }
        }
        private static lWmsOutPickRequest _lop ;
        lWmsOutPickRequest lop
        {
            get
            {
                if (_lop == null)
                {
                    _lop = new lWmsOutPickRequest(syncOperId);
                }
                return _lop;
            }
        }
        private static lWmsOutPickPort _lopp;
        lWmsOutPickPort lopp
        {
            get
            {
                if (_lopp == null )
                {
                    _lopp = new lWmsOutPickPort(syncOperId );
                }
                return _lopp;
            }
        }
      /*
        public void syncOutRequest()
        {
            lor.syncOrders();
        }
        /// 
        /// sync out request,create pickorders
        /// 
        public void preProcessOutOrders()
        {
            lor.preProcessOutOrders();
        }
        */
        /// 
        /// sync inrequests
        /// 启用入库收货、验收模块后不再自动同步入库数据
        /// 
        public void syncInRequest()
        {
           // lir.syncOrders();
        }
        public void syncGoods()
        {
            log.Debug("start syncGoods ");
            try
            {
                wgoods.syncGoods();
            }
            catch (Exception e)
            {
                log.Error(e);
            }
           
        }
        public int syncNewOwners()
        {
            log.Debug("start syncOwner ");
            try
            {
                  return lOwnerObj.syncOwner();
            }
            catch (Exception e)
            {
                log.Error(e);
            }
            return 0;
        }
        public int syncCustomer()
        {
            log.Debug("start syncCustomer ");
            try
            {
                return lcust.syncCustomer();
            }
            catch (Exception e)
            {
                log.Error(e);
            }
            return 0;
        
        }
        
        public string processOut(enumOutStockRequestStatus status)
        {
           if(lor.isErpWorking())
            {
                return "ERP transfering data to wms, sleep a while...";
            }
            if (!String.IsNullOrEmpty(WmsConstants.CURRENT_PANDIAN_ORDER))
            {
                return "盘点进行中。。。。" + WmsConstants.CURRENT_PANDIAN_ORDER;
            }
            log.Debug("start sync and create pickoutrequest ,status   " +status);
            string result = "任务完成。。。";
            try
            {
                lor.processOutOrders(status);
            }
            catch (Exception e)
            {
                log.Error(e);
                result += " | processOutOrders error:" + e.Message;
            }
            log.Debug("start createPDAPickTasks ");
            try
            {
                lop.createPDAPickTasks();
            }
            catch (Exception e)
            {
                log.Error(e);
                result += " | createPDAPickTasks error:" + e.Message;
            }
            try
            {
                log.Debug("start createPickWaves ");
                bool rt = lopp.createPickWaves(); ;
                log.Debug("end createPickWaves ,success? " + rt);
            }
            catch (Exception e)
            {
                log.Error(e);
                result += " | createPickWaves error:" + e.Message;
            }
            return result;
        }
        /*
        public void createPDAPickTasks()
        {
            log.Debug ("begin ---to create pick out pda task data");
            if (isProcessBatchWaveing)
            {
                log.Debug("!!!!!!!!!!!!!!!!! create pick out pda data ... is processing... return ....");
                return;
            }
            isProcessBatchWaveing = true;
          
            try
            {
               
                lop.createPDAPickTasks();
                LogHelper.debug(typeof(WmsOutPickRequestService), "end ---to create pick out pda task data");
                isProcessBatchWaveing = false;
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                isProcessBatchWaveing = false;
            }
            
        } 
        public void syncAndCreatePickRequest(enumOutStockRequestStatus status)
        {
            lor.preProcessOutOrdersImp(status);
        }
        static bool isCreateWaveing = false;
        public bool createPickWaves()
        {
          log.Debug(  "begin to createPickWaves...");
            if (isCreateWaveing)
            {
               log.Debug ( "!!!!!!!!!!!!!!!!! new pick wave ... is processing... return ....");
                return false;
            }
            isCreateWaveing = true;
             
            try
            {
                bool ok = lopp.createPickWaves();
              
                isCreateWaveing = false;
                return ok;
            }
            catch (Exception e)
            {
              log.Error(e);
            }
            finally
            {
                isCreateWaveing = false;
            }
            isCreateWaveing = false;
            return true;
             
        }
        */
        public int[] getJobInterVal()
        {
            _lop = new lWmsOutPickRequest(syncOperId);
            int[] a = new int[5];
             
            a[0] = WmsConstants.JOB_OUT_REQUEST_INTERVAL;
            a[1] = WmsConstants.JOB_TEST_REQUEST_INTERVAL;
            a[2] = WmsConstants.JOB_BASIC_INFO_SYNC_INTERVAL;
            a[3] = WmsConstants.JOB_OUT_REQUEST_NO_STOCK_CHECK_INTERVAL;
            a[4] = WmsConstants.JOB_NOTICE_ERP;
            
            return a;
        }
        public int getWaveInterval()
        { 
            WaveRule wr = new WaveRule() .getCurrentActiveRule(enumWaveRuleType.普通波次);
            return wr.interval;
        }
        public void truncateDataForTestOnly()
        {
            try {
#if DEBUG
                lir.createDataForTestOnly();
#endif
            }catch(Exception er)
            {
                log.Error(er);
            }
        }
        public string noticeErp()
        {
            log.Debug("start noticeErp ---------------->>");
            string rt = "";
            // to notice purch in
            try
            {
                lir.retryNoticeErpValidIn();
             
            }
            catch (Exception er)
            {
                rt = er.Message;
                log.Error(er);
            }  
            
            // to notice sale out
            try
            {
              //  lop.unloadPickrequests();  //already do it when start truck
            }
            catch (Exception er)
            {
                rt = er.Message;
                log.Error(er);
            }
            try
            {
                if (WmsConstants.ERP_NOTICE_STOCK_OUT_DOING)
                {
                    log.Debug(" 正在同步工作中,稍后再试");
                    return " 正在同步工作中,稍后再试";
                }
                lop.retryNoticeErp();
            }
            catch (Exception er)
            {
                rt = er.Message;
                log.Error(er);
            }
            try
            {
                lop.retryNoticeErpOrder();
            }
            catch (Exception er)
            {
                rt = er.Message;
                log.Error(er);
            }
            // to notice Obsolete
            // to notice 盘点
            log.Debug("end noticeErp ------------<< ");
            return rt;
        }
    }
}