using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms.VisualStyles;
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;
using Newtonsoft.Json;
using static JLHHJSvr.Helper.MattressHelper;

namespace JLHHJSvr.Excutor
{
    internal sealed class GetComputeMattressByIdExcutor : ExcutorBase<GetComputeMattressByIdRequest, GetComputeMattressByIdResponse>
    {
        protected override void ExcuteInternal(GetComputeMattressByIdRequest request, object state, GetComputeMattressByIdResponse 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 mattressHelper = HelperBase.GetHelper<MattressHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });
                var helperOld = HelperBase.GetHelper<MattressHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });
                var mattress = mattressHelper.GetMattress(request.mattressid.Value);
                var mxList = mattressHelper.GetMattressMxMtrl(request.mattressid.Value);

                
                if (mattress.dannum_type == null || mattress.dannum_type.Value == 0)
                {
                    mattress.dannum_type = 2; // 默认标准
                }

                var differArr = new List<u_mattress_computed>();
                foreach (DannumType value in Enum.GetValues(typeof(MattressHelper.DannumType)))
                {
                    int dannum_type = (int)value;
                    var differItem = new u_mattress_computed() { type = dannum_type };
                    var _mattress = ObjectHelper.DeepCopy(mattress);
                    if (mattress.dannum_type.Value == dannum_type)
                    {
                        mattressHelper.CalCulateFormula(_mattress, mxList, false);
                        rslt.replace = ObjectHelper.DeepCopy(mattressHelper.Replacements);
                        rslt.formulas = ObjectHelper.DeepCopy(mattressHelper.FormulaReplacements);
                        rslt.formulas_bednet = ObjectHelper.DeepCopy(mattressHelper.BendetFormulasReplace);
                    }
                    else
                    {
                        _mattress.dannum_type = dannum_type;
                        mattressHelper.CalCulateFormula(_mattress, mxList, false);
                    }

                    differItem.replace = mattressHelper.Replacements;
                    differItem.replace_bednet = mattressHelper.BendetReplace;

                    if (request.check_original == 1)
                    {
                        _mattress.dannum_type = dannum_type;

                        helperOld.CalCulateFormula(_mattress, mxList, false, false);
                        differItem.replace_origin = ObjectHelper.DeepCopy(mattressHelper.Replacements);
                        rslt.formulas_origin = ObjectHelper.DeepCopy(mattressHelper.FormulaReplacements);

                    }
                    differArr.Add(differItem);
                }

                rslt.differ = differArr;
                rslt.mattress = mattress;
                rslt.mattressMx = mattressHelper.SortMxList(mxList);
            }
        }
    }
}
