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 RefreshBedNetInterfaceExcutor : ExcutorBase<RefreshBedNetInterfaceRequest, RefreshBedNetInterfaceResponse>
    {
        protected override void ExcuteInternal(RefreshBedNetInterfaceRequest request, object state, RefreshBedNetInterfaceResponse 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 copy_list = new List<u_mattress_interface>();

                var selectStr = @"SELECT bednetid
	                            ,printid
	                            ,itemname
	                            ,bj_pzname
	                            ,bj_namemx
	                            ,actual_size
	                            ,erp_pzid
	                            ,Space(200) AS erp_pzcode
	                            ,Space(200) AS erp_pzname
	                            ,bj_inputtype AS erp_inputtype
                            FROM u_bednet_interface";
                DbSqlHelper.SelectJoin(cmd, selectStr, "bednetid = @bednetid", new Dictionary<string, object>() { { "@bednetid", request.bednetid } }, "printid", "bednetid,printid,itemname,bj_pzname,bj_namemx,actual_size,erp_pzid,ss_rate,ls_rate,bj_inputtype", 0, 0, copy_list);

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

                var interfaceList = interfaceHelper.RefreshBedNetInterfaceList(request.bednetid.Value, request.configcodetype.Value);

                if (request.isPz != null && request.isPz == 1)
                {
                    var copyDict = copy_list.Where(mx2 => mx2.erp_pzid > 0)
                                .ToDictionary(mx2 => mx2.erp_pzid, mx2 => mx2);

                    foreach (var mx in interfaceList)
                    {
                        if (mx.erp_pzid > 0 && copyDict.TryGetValue(mx.erp_pzid, out var mx2))
                        {
                            if (mx.bj_inputtype != 2 || (mx.bj_inputtype == 2 && mx2.bj_inputtype == 1))
                            {
                                mx.bj_namemx = mx2.bj_namemx;
                                mx.actual_size = mx2.actual_size;

                                if (mx.bj_inputtype == 2 && mx2.bj_inputtype == 1)
                                {
                                    mx.bj_inputtype = 1;
                                }
                            }
                            else
                            {
                                mx.actual_size = mx2.actual_size;
                            }
                        }
                    }
                }

                rslt.mxList = interfaceList;
            }
        }
    }
}
