using System;
using System.Collections;
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 RefreshMattressInterfaceQdExcutor : ExcutorBase<RefreshMattressInterfaceQdRequest, RefreshMattressInterfaceQdResponse>
    {
        protected override void ExcuteInternal(RefreshMattressInterfaceQdRequest request, object state, RefreshMattressInterfaceQdResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (request.mattressid == null || request.mattressid <= 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.mattressid.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_mattress_interface_qd>();
                var interfaceHelper = HelperBase.GetHelper<InterfaceHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });

                if(request.isPz == 1)
                {
                    var selectStr = @"SELECT u_mattress_interface_qd.mattressid 
	                            ,u_mattress_interface_qd.printid 
	                            ,u_mattress_interface_qd.itemname 
	                            ,u_mattress_interface_qd.bj_pzname 
	                            ,u_mattress_interface_qd.bj_pzname_mx 
	                            ,u_mattress_interface_qd.bj_pzname_mx_mx 
	                            ,u_mattress_interface_qd.mtrlid 
	                            ,u_mattress_interface_qd.erp_mtrlid 
	                            ,u_mattress_interface_qd.useqty 
	                            ,u_mattress_interface_qd.dscrp 
	                            ,u_mtrl_price.name AS mtrlname
	                            ,u_mattress_interface_qd.actual_useqty 
	                            ,u_mattress_interface_qd.qd_actual_size 
	                            ,u_mattress_interface_qd.qd_pfgroupqty 
	                            ,u_mattress_interface_qd.wrkgrpid 
	                            ,u_mattress_interface_qd.ss_rate 
	                            ,u_mattress_interface_qd.ls_rate 
	                            ,u_mattress_interface_qd.sh_rate
	                            ,u_mattress_interface_qd.wip_type
	                            ,u_mattress_interface_qd.wrkgrpcode2
	                            ,ISNULL(u_mattress_interface_qd.formulaid, 0) AS formulaid
                            FROM u_mattress_interface_qd
                            LEFT JOIN u_mtrl_price ON u_mtrl_price.mtrlid = u_mattress_interface_qd.mtrlid";
                    var outputFields = "mattressid,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,wip_type,wrkgrpcode2";
                    DbSqlHelper.SelectJoin(cmd, selectStr, "mattressid = @mattressid", new Dictionary<string, object>() { { "@mattressid", request.mattressid } }, "printid", outputFields, 0, 0, copy_list);

                } else
                {
                    interfaceHelper.ImportMattressInterfaceQdList(request.mattressid.Value, copy_list);
                }

                interfaceHelper.MattressInterfaceFindERPPrdPf(copy_list, null);
                rslt.mxList = copy_list;
            }
        }
    }
}
