using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
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;
using NPOI.SS.Formula.Functions;

namespace JLHHJSvr.Excutor
{
    internal sealed class SaveMattressSubSpecsExcutor : ExcutorBase<SaveMattressSubSpecsRequest, SaveMattressSubSpecsResponse>
    {
        protected override void ExcuteInternal(SaveMattressSubSpecsRequest request, object state, SaveMattressSubSpecsResponse 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();

                var mattressHelper = HelperBase.GetHelper<MattressHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });
                var _mattress = mattressHelper.GetMattress(request.mattress.mattressid);
                _mattress.mxList = mattressHelper.GetMattressMxMtrl(request.mattress.mattressid);
                _mattress.extraList = mattressHelper.GetMattressMxExtra(request.mattress.mattressid,1);
                _mattress.extraCostList = mattressHelper.GetMattressMxExtra(request.mattress.mattressid, 2);
                _mattress.subspecsList = request.subspecsList;

                var existing = mattressHelper.GetMattressSubspecs(request.mattress.mattressid, "mattressid,mattresscode,flag,js1_flag") ?? new List<u_mattress>();
                var oldIdSet = new HashSet<int>(existing.Where(t => t.flag == 0 && t.js1_flag == 0).Select(x => x.mattressid));
                var toUpdate = request.subspecsList.Where(x => oldIdSet.Contains(x.mattressid)).ToList();
                var toInsert = request.subspecsList.Where(x => x.mattressid == 0).ToList();

                var subspecsList = new List<u_mattress>();
                foreach (var child in toInsert)
                {
                    subspecsList.Add(child);
                }
                foreach (var child in toUpdate)
                {
                    subspecsList.Add(child);
                }

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        mattressHelper.SaveSubSpecs(_mattress, subspecsList, false);
                        rslt.mattressid = _mattress.mattressid;
                        rslt.mattresscode = _mattress.mattresscode;
                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        rslt.ErrMsg = e.Message;
                        Trace.Write(e);
                        cmd.Transaction?.Rollback();
                    }
                }
            }
        }
    }
}
