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

namespace JLHHJSvr.Excutor
{
    internal sealed class GetMattressSubspecsExcutor : ExcutorBase<GetMattressSubspecsRequest, GetMattressSubspecsResponse>
    {
        protected override void ExcuteInternal(GetMattressSubspecsRequest request, object state, GetMattressSubspecsResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (request.mattressid == 0)
            {
                rslt.ErrMsg = "床垫id不能为0";
                return;
            }

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

                using (cmd.Transaction = con.BeginTransaction())
                {

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

                    var subspecsList = new List<u_mattress>();
                    var result = new List<u_mattress_mx_subspecs>();
                    var fields = "mattressid,mattress_width,mattress_length,mattress_height,mattressname,mattressrelcode,version,extras_cost";
                    if (request.type == 0)
                    {
                        fields += ",mattresscode,parentid,erp_mtrlid,erp_mtrlcode,erp_configcodetype, erp_mtrlengname, erp_mtrlmode, erp_mtrlname, erp_mtrltype, erp_mtrltypeid, erp_mtrlunit, erp_piccode, xd_flag, flag, js1_flag";
                    }

                    //获取副规格列表
                    subspecsList = helper.GetMattressSubspecs(request.mattressid, fields);

                    if (subspecsList.Count > 0)
                    {
                        rslt.bednetMxs = new List<u_mattress_mx_mtrl>();
                        rslt.mtrllist = new List<u_mattress_mx_mtrl>();

                        foreach (var mattrss in subspecsList)
                        {
                            //获取弹簧排列
                            List<u_mattress_mx_mtrl> bednetmxList = helper.GetMattressSubspecsBednet(mattrss.mattressid);
                            rslt.bednetMxs = rslt.bednetMxs.Concat(bednetmxList).ToList();

                            //判断是否有主副规格调整物料
                            List<u_mattress_mx_mtrl> mtrlList = helper.GetMattressMxSubspecsMtrl(mattrss.mattressid);
                            rslt.mtrllist = rslt.mtrllist.Concat(mtrlList).ToList();
                        }
                    }

                    rslt.mattresses = subspecsList;
                }
            }
        }
    }
}
