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.LJFramework.Tools;
using JLHHJSvr.Tools;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;

namespace JLHHJSvr.Excutor
{
    internal sealed class GetComputeSpringExcutor : ExcutorBase<GetComputeSpringRequest, GetComputeSpringResponse>
    {
        Dictionary<string, object> replacements = new Dictionary<string, object>();

        protected override void ExcuteInternal(GetComputeSpringRequest request, object state, GetComputeSpringResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (request.bednet == null)
            {
                rslt.ErrMsg = "缺少主表信息";
                return;
            }

            if (request.bednet.bednetid > 0 && request.bednetMx == null)
            {
                // 床垫报价-主副规格，计算弹簧排列长宽
            }
            else
            {
                if (request.bednet.bednettypeid <= 0)
                {
                    rslt.ErrMsg = "请选择床网类型";
                    return;
                }
                if (request.bednetMx == null || !request.bednetMx.Any())
                {
                    rslt.ErrMsg = "缺少报价明细";
                    return;
                }

                // 检查是否填写床网高度

                foreach (var mx in request.bednetMx)
                {
                    if (mx.bednet_height == null || mx.bednet_height <= 0)
                    {
                        rslt.ErrMsg = "请填写床网高";
                        return;
                    }
                    else
                    {
                        request.bednet.mattress_height = mx.bednet_height;
                    }
                }
            }

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

                var helper = HelperBase.GetHelper<BedNetHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });
                var _bednet = request.bednet;
                var springList = new List<u_bednetmx_spring>();

                if (request.bednet.bednetid > 0 && request.bednetMx == null)
                {
                    // 床垫报价-主副规格，计算弹簧排列长宽
                    _bednet = helper.GetBedNet(request.bednet.bednetid.Value);
                    _bednet.pricelistid = request.bednet.pricelistid;
                    request.bednetMx = helper.GetBedNetMxList(request.bednet.bednetid.Value);

                }

                if (request.spring != null && request.spring.Count > 0)
                {
                    springList = request.spring;
                }
                else if (request.bednetMx[0].if_part != null && request.bednetMx[0].if_part.Value > 0)
                {
                    springList = helper.GetBedNetSpringList(request.bednet.bednetid.Value);
                }

                // 初始化属性
                AutoInit.AutoInitS(cmd, _bednet);

                foreach (var mx in request.bednetMx)
                {
                    AutoInit.AutoInitS(cmd, mx);
                }

                try
                {
                    _bednet.mattress_width = request.bednet.mattress_width;
                    _bednet.mattress_length = request.bednet.mattress_length;

                    helper.CalCulateSpringQty(_bednet, request.bednetMx, springList);
                }
                catch (Exception e)
                {
                    rslt.message = e.Message;
                }
                rslt.replace = helper.Replacements;

                rslt.formulas = helper.FormulaReplacements;

                rslt.bednet = _bednet;
                rslt.bednetMx = request.bednetMx;
            }
        }
    }
}
