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

namespace JLHHJSvr.Excutor
{
    internal sealed class CreatMtrldefExcutor : ExcutorBase<CreatMtrldefRequest, CreatMtrldefResponse>
    {
        Dictionary<string, object> replacements = new Dictionary<string, object>();

        protected override void ExcuteInternal(CreatMtrldefRequest request, object state, CreatMtrldefResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (request.list.Count <= 0)
            {
                rslt.ErrMsg = "床垫id参数为空!";
                return;
            }

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

                foreach (var mattressid in request.list)
                {
                    var mattress = new u_mattress();
                    if (DbSqlHelper.SelectOne(cmd, "u_mattress", "mattressid = @mattressid", new Dictionary<string, object>() { { "mattressid", mattressid } }, mattress, "erp_mtrlid,erp_mtrlcode, erp_mtrlname, erp_mtrlmode, erp_mtrltypeid, erp_mtrltype, erp_mtrlunit, erp_mtrlengname,nottax_factory_cost,dept_profitrate,dept_profitrate_rangli,commission,taxes,fob,extras_cost,mattresstypeid,erp_configcodetype,dijia_cost1,dijia_cost2,dijia_cost3,dijia_cost4") != 1)
                    {
                        rslt.ErrMsg = "床垫报价单匹配失败";
                        return;
                    }

                    var interfaceList = new List<u_mattress_interface>();
                    var selectStr = @"SELECT u_mattress_interface.mattressid
	                                ,printid
	                                ,itemname
	                                ,bj_pzname
	                                ,bj_namemx
	                                ,actual_size
	                                ,sb_craft
	                                ,actual_size_sb
	                                ,erp_pzid
	                                ,ss_rate
	                                ,ls_rate
	                                ,bj_inputtype
                                    ,u_configure_code.pzcode
                                    ,u_configure_code.name AS pzname
                                FROM u_mattress_interface
                                left outer join u_mattress on u_mattress.mattressid = u_mattress_interface.mattressid
                                left outer join u_configure_code on u_configure_code.typeid = u_mattress.erp_configcodetype 
		                                and u_mattress_interface.erp_pzid = u_configure_code.pzid";
                    DbSqlHelper.SelectJoin(cmd, selectStr, "u_mattress_interface.mattressid = @mattressid AND u_mattress_interface.erp_pzid > 0 AND u_mattress_interface.bj_pzname <> '' ", new Dictionary<string, object>() { { "@mattressid", mattressid } }, "printid", "mattressid,printid,itemname,bj_pzname,bj_namemx,actual_size,sb_craft,actual_size_sb,erp_pzid,ss_rate,ls_rate,bj_inputtype,pzcode,pzname", 0, 0, interfaceList);


                    var l1Helper = HelperBase.GetHelper<ERPHelper>(cmd);
                    l1Helper.context = new HelperBase.Context() { tokendata = tokendata };

                    l1Helper.CheckLogin();

                    mattress = l1Helper.GetMattressMsg(mattress, interfaceList);

                    var errMsg = l1Helper.SaveMtrldef(mattress, interfaceList);
                    if (!string.IsNullOrEmpty(errMsg))
                    {
                        rslt.ErrMsg = errMsg;
                        return;
                    }
                    mattress.mattressid = mattressid;
                    mattress.creatmtrl_flag = 1;

                    DbSqlHelper.Update(cmd, mattress, "erp_mtrlid, creatmtrl_flag, erp_mtrlcode");

                    rslt.mattress = mattress;
                }
            }
        }

        /// <summary>
        /// 保存物料清单请求参数,没有传入的参数按默认值
        /// </summary>
        private class SavePrdPfRequest
        {
            public string token { get; set; }
            /// <summary>
            /// 物料清单主表 mtrlid,pfcode,flag,affirmflag,Ifdft,inuse,wrkid
            /// </summary>
            public u_mtrl_pf mtrl_pf { get; set; }
            /// <summary>
            /// 物料清单明细表 mtrlid,pfcode,sonmtrlid,printid,wrkgrpid,sonpfcode,Sonscale,sonloss,sondecloss
            /// pfgroup,dscrp,pfgroupqty,pfklmode,azcode,promode
            /// </summary>
            public List<u_PrdPF> prdPfs { get; set; }
        }
    }
}
