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 GetComputeMattressExcutor : ExcutorBase<GetComputeMattressRequest, GetComputeMattressResponse>
    {
        protected override void ExcuteInternal(GetComputeMattressRequest request, object state, GetComputeMattressResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (request.mattress == null)
            {
                rslt.ErrMsg = "缺少主表信息";
                return;
            }

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

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

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

                using (cmd.Transaction = con.BeginTransaction())
                {
                    var helper = HelperBase.GetHelper<MattressHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });
                    var helperOld = HelperBase.GetHelper<MattressHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });

                    var mattressMx = helper.GetMattressMxMtrl(request.mattress.mattressid);

                    if (request.mattress.copy_id != null && request.mattress.copy_id > 0)
                    {
                        request.mattress.mattressid = 0;
                        request.mattress.mattresscode = string.Empty;

                        foreach (var mx in mattressMx)
                        {
                            mx.mattressid = 0;
                            mx.mattressmxid = 0;
                        }
                    }

                    if (request.mattress.dannum_type.Value == 0)
                    {
                        request.mattress.dannum_type = 2; // 默认标准
                    }
                    try
                    {
                        var differArr = new List<u_mattress_computed>();
                        for (var i = 1; i < 5; i++)
                        {
                            var differItem = new u_mattress_computed();
                            differItem.type = i;
                            var _mattress = ObjectHelper.DeepCopy(request.mattress);
                            if (request.mattress.dannum_type.Value == i)
                            {
                                helper.CalCulateFormula(_mattress, mattressMx, false);
                                rslt.replace = helper.Replacements;
                                rslt.formulas = helper.FormulaReplacements;
                                rslt.formulas_bednet = helper.BendetFormulasReplace;
                            }
                            else
                            {
                                _mattress.dannum_type = i;
                                helper.CalCulateFormula(_mattress, mattressMx, false);
                            }

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

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

                                helperOld.CalCulateFormula(_mattress, mattressMx, false, false);
                                differItem.replace_origin = helperOld.Replacements;
                                rslt.formulas_origin = helperOld.FormulaReplacements;
                            }
                            differArr.Add(differItem);
                        }
                        rslt.differ = differArr;

                        //helper.CalCulateFormula(request.mattress, request.mattressMx, false);
                        //rslt.replace = helper.Replacements;

                        //rslt.formulas = helper.FormulaReplacements;
                    }
                    catch (Exception e)
                    {
                        rslt.message = e.Message;
                    }
                }
            }
        }
    }
}
