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 SaveBedNetExcutor : ExcutorBase<SaveBedNetRequest, SaveBedNetResponse>
    {
        Dictionary<string, object> replacements = new Dictionary<string, object>();

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

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

                if (request.bednet.copy_id != null && request.bednet.copy_id > 0)
                {
                    request.bednet.bednetid = 0;
                    request.bednet.bednetcode = string.Empty;

                    foreach (var mx in request.bednetMx)
                    {
                        mx.bednetid = 0;
                        mx.bednetmxid = 0;
                    }

                    foreach (var mx in request.spring)
                    {
                        mx.bednetmxid = 0;
                        mx.bednetmx_partid = 0;
                    }
                }

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

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

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

                try
                {
                    helper.CalCulateFormula(request.bednet, request.bednetMx, request.spring);
                }
                catch (Exception e)
                {
                    rslt.message = e.Message;
                }


                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        helper.SaveBedNet(request.bednet, request.bednetMx, request.spring);

                        cmd.Transaction.Commit();

                        rslt.bednet = new u_bednet() { 
                            bednetid = request.bednet.bednetid, 
                            bednetcode = request.bednet.bednetcode,
                            typename = request.bednet.typename,
                            mattress_width = request.bednet.mattress_width,
                            mattress_length = request.bednet.mattress_length,
                            mattress_height = request.bednet.mattress_height,
                            nottax_factory_cost = request.bednet.nottax_factory_cost,
                            total_cost = request.bednet.total_cost
                        };
                    }
                    catch (Exception e)
                    {
                        cmd.Transaction.Rollback();
                        rslt.ErrMsg = e.ToString();
                    }
                }
            }
        }
    }
}
