using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.Com.Model;
using JLHHJSvr.Helper;
using JLHHJSvr.Tools;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;
using LJLib.SQLEX;
using Newtonsoft.Json.Linq;

namespace JLHHJSvr.Excutor
{
    internal sealed class UpdateL1BasicinfoExcutor : ExcutorBase<UpdateL1BasicinfoRequest, UpdateL1BasicinfoResponse>
    {
        Dictionary<string, object> replacements = new Dictionary<string, object>();

        protected override void ExcuteInternal(UpdateL1BasicinfoRequest request, object state, UpdateL1BasicinfoResponse rslt)
        {
            UpdateL1Basic(rslt);
        }

        private static void UpdateL1Basic(UpdateL1BasicinfoResponse rslt)
        {
            using (var con = GlobalVar.ConnectionString.NewSqlConnection())
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                #region 同步u_mtrldef物料资料
                var maxMtrlModdate = "";

                cmd.CommandText = @"
                    SELECT MAX(MaxAmount) AS moddate
                    FROM (
                        SELECT MAX(moddate) AS MaxAmount
                        FROM u_mtrldef
                        UNION ALL
                        SELECT MAX(opdate)
                        FROM u_mtrldef
                        UNION ALL
                        SELECT MAX(secauditdate)
                        FROM u_mtrldef
                    ) AS SubQuery;
                ";
                cmd.Parameters.Clear();
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        maxMtrlModdate = Convert.ToDateTime(reader["moddate"]).ToString("yyyy-MM-dd HH:mm:ss.fff");
                    }
                }

                var mtrldefList = new List<u_mtrldef>();

                var erpHelper = HelperBase.GetHelper<ERPHelper>(null);

                var parameters = new JObject();
                parameters.Add("moddate", JToken.FromObject(maxMtrlModdate));
                parameters.Add("flag", 2);
                parameters.Add("pageindex", JToken.FromObject(0));
                parameters.Add("pagesize", JToken.FromObject(0));

                mtrldefList = erpHelper.GetERPList<u_mtrldef>("GetL1Mtrldef", parameters);

                if (mtrldefList.Count > 0)
                {
                    var outputFields =
                        "mtrlid,mtrlcode,mtrlname,mtrlorigin,mtrltype,unit,mtrlmode,mtrlsectype,zxmtrlmode,flag,secauditdate" +
                        ",fjcnt,status_config,woodcode_config,pcode_config,statustype,woodcodetype,pcodetype,scid" +
                        ",handtype,mtrlengname,barcode,dscrp,issuliao,isuse,iflimitprice,net_weight,gross_weight,cubage,prdpackcode,opdate,opemp,moddate,lspacklimit,planprice";
                    foreach (var mtrl in mtrldefList)
                    {
                        DbSqlHelper.InsertOrUpdate(cmd, mtrl, outputFields);
                    }
                }

                rslt.mtrlnum = mtrldefList.Count;
                #endregion

                #region 同步工组资料
                var maxWrkModdate = "";

                cmd.CommandText = @"
                    SELECT MAX(MaxAmount) AS moddate
                    FROM (
                        SELECT MAX(moddate) AS MaxAmount
                        FROM u_sc_workgroup_erp
                        UNION ALL
                        SELECT MAX(opdate)
                        FROM u_sc_workgroup_erp
                    ) AS SubQuery;
                ";
                cmd.Parameters.Clear();
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {
                        maxWrkModdate = Convert.ToDateTime(reader["moddate"]).ToString("yyyy-MM-dd HH:mm:ss.fff");
                    }
                }

                parameters = new JObject();
                parameters.Add("moddate", JToken.FromObject(maxWrkModdate));

                var workgroupList = erpHelper.GetERPList<u_sc_workgroup_erp>("GetSCWorkgroupList", parameters);

                if (workgroupList.Count > 0)
                {
                    var outputFields =
                        "wrkGrpid,wrkgrpcode,wrkGrpName,ifjj,ifprint,handtype,sonflag,parentid,inuse,storageid,opdate,opemp,moddate," +
                        "modemp,ifwkpmtrlware,dscrp,empnum,avggz,jdtype,jdnum,tasktype,sys_changetime,userate,deptid,OTHour,shortrate," +
                        "iflastfinish,barcode_check,ifauto_mode,ifbj,jjamt,finflowop";
                    foreach (var item in workgroupList)
                    {
                        DbSqlHelper.InsertOrUpdate(cmd, item, outputFields);
                    }
                }

                rslt.wkgnum = workgroupList.Count;
                #endregion

                #region 同步u_mtrltype物料类型

                parameters = new JObject();

                var mtrltypeList = erpHelper.GetERPList<u_mtrltype_erp>("GetL1Mtrltype", parameters);

                if (mtrltypeList.Count > 0)
                {

                    cmd.CommandText = @"DELETE u_mtrltype_erp";
                    cmd.Parameters.Clear();
                    cmd.ExecuteNonQuery();

                    var outputFields = "mtrltypeid,mtrltype,handtype,sonflag,parentid,mtrlprp,typecode,typecodestr";
                    foreach (var item in mtrltypeList)
                    {
                        DbSqlHelper.InsertOrUpdate(cmd, item, outputFields);
                    }
                }

                rslt.mtrltypenum = mtrltypeList.Count;
                #endregion

            }
        }
    }
}
