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

namespace JLHHJSvr.Excutor
{
    internal sealed class RefreshBedNetInterfaceQdExcutor : ExcutorBase<RefreshBedNetInterfaceQdRequest, RefreshBedNetInterfaceQdResponse>
    {
        protected override void ExcuteInternal(RefreshBedNetInterfaceQdRequest request, object state, RefreshBedNetInterfaceQdResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (request.bednetid == null || request.bednetid <= 0)
            {
                rslt.ErrMsg = "床网id有误,请检查!";
                return;
            }

            using (var con = GlobalVar.ConnectionString.NewSqlConnection())
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                var mattress = new u_mattress() { mattressid = request.bednetid.Value };
                DbSqlHelper.SelectOne(cmd, mattress, "js2_flag");

                if (request.isCheck == 1 && mattress.js2_flag == 1)
                {
                    throw new LJCommonException("床网已技术2审核，不能重新生成!");
                }

                var copy_list = new List<u_bednet_interface_qd>();

                var selectStr = @"SELECT u_bednet_interface_qd.bednetid
	                                ,u_bednet_interface_qd.printid
	                                ,u_bednet_interface_qd.itemname
	                                ,u_bednet_interface_qd.bj_pzname
	                                ,u_bednet_interface_qd.bj_pzname_mx
	                                ,u_bednet_interface_qd.bj_pzname_mx_mx
	                                ,u_bednet_interface_qd.mtrlid
	                                ,u_bednet_interface_qd.erp_mtrlid
	                                ,u_bednet_interface_qd.useqty
	                                ,u_bednet_interface_qd.dscrp
	                                ,u_mtrl_price.name AS mtrlname
	                                ,u_bednet_interface_qd.actual_useqty
	                                ,u_bednet_interface_qd.qd_actual_size
	                                ,u_bednet_interface_qd.wrkgrpid
	                                ,Space(200) AS erp_mtrlcode
	                                ,Space(200) AS erp_mtrlname
	                                ,Space(200) AS erp_mtrlmode
	                                ,Space(200) AS erp_unit
	                                ,Space(200) AS mtrlengname
                                FROM u_bednet_interface_qd
                                LEFT JOIN u_mtrl_price ON u_mtrl_price.mtrlid = u_bednet_interface_qd.mtrlid";
                var outputFields = "bednetid,printid,itemname,bj_pzname,bj_pzname_mx,bj_pzname_mx_mx,mtrlid,erp_mtrlid,useqty,dscrp,mtrlname,actual_useqty,qd_actual_size,qd_pfgroupqty,wrkgrpid,ss_rate,ls_rate,sh_rate,formulaid";
                DbSqlHelper.SelectJoin(cmd, selectStr, "bednetid = @bednetid", new Dictionary<string, object>() { { "@bednetid", request.bednetid } }, "printid", outputFields, 0, 0, copy_list);

                var interfaceHelper = HelperBase.GetHelper<InterfaceHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });

                var qdList = interfaceHelper.RefreshBedNetInterfaceQdList(request.bednetid.Value);

                if (request.isPz != null && request.isPz == 1)
                {
                    var copyDict = new Dictionary<Tuple<string, string, string>, u_bednet_interface_qd>();

                    foreach (var mx2 in copy_list)
                    {
                        var key = Tuple.Create(mx2.itemname, mx2.bj_pzname, mx2.bj_pzname_mx);
                        if (!copyDict.ContainsKey(key))
                        {
                            copyDict[key] = mx2;
                        }
                    }

                    // 遍历qdList
                    foreach (var mx in qdList)
                    {
                        var key = Tuple.Create(mx.itemname, mx.bj_pzname, mx.bj_pzname_mx); // 创建复合键
                        if (copyDict.TryGetValue(key, out var mx2)) // 高效查找对应的mx2
                        {
                            // 更新mx属性
                            mx.erp_mtrlid = mx2.erp_mtrlid;
                            mx.erp_mtrlcode = mx2.erp_mtrlcode;
                            mx.erp_mtrlname = mx2.erp_mtrlname;
                            mx.erp_mtrlmode = mx2.erp_mtrlmode;
                            mx.erp_unit = mx2.erp_unit;
                            mx.useqty = mx2.useqty;
                            mx.actual_useqty = mx2.actual_useqty;
                            mx.wrkgrpid = mx2.wrkgrpid;
                            mx.qd_actual_size = mx2.qd_actual_size;
                            mx.dscrp = mx2.dscrp;
                        }
                    }
                }

                interfaceHelper.InterfaceFindERPPrdPf<u_bednet_interface_qd,u_bednet_interface>(qdList);

                rslt.mxList = qdList;
            }
        }
    }
}
