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;

namespace JLHHJSvr.Excutor
{
    internal sealed class SaveMattressExcutor : ExcutorBase<SaveMattressRequest, SaveMattressResponse>
    {
        protected override void ExcuteInternal(SaveMattressRequest request, object state, SaveMattressResponse 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 bednetHelper = HelperBase.GetHelper<BedNetHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });

                var subspecsList = new List<u_mattress>();

                var _mattress = ObjectHelper.DeepCopy(request.mattress);
                _mattress.mxList = request.mattressMx;
                _mattress.extraList = request.extraProcesses;
                _mattress.extraCostList = request.extraCosts;
                _mattress.subspecsList = request.subspecsList;

                // 初始化属性
                AutoInit.AutoInitS(cmd, _mattress);
                foreach (var mx in _mattress.mxList)
                {
                    AutoInit.AutoInitS(cmd, mx);
                }

                // 状态判断
                if (!(request.mattress.copy_id != null && request.mattress.copy_id > 0))
                {
                    // 业务下单无法修改
                    if (_mattress.mattressid > 0)
                    {
                        var temp_mattress = mattressHelper.GetMattress(_mattress.mattressid, "flag, xd_flag, yw_flag, mattresscode, parentid");
                        if (temp_mattress.xd_flag != null && temp_mattress.xd_flag == 1)
                        {
                            throw new LJCommonException($"床垫报价单【{temp_mattress.mattresscode}】已业务下单，不能修改!");
                        }
                        if (temp_mattress.flag != null && temp_mattress.flag == 1)
                        {
                            throw new LJCommonException($"床垫报价单【{temp_mattress.mattresscode}】已财务审核，不能修改!");
                        }
                    }
                }

                #region TODO:需要优化
                List<string> message = new List<string>();
                mattressHelper.MattressCalculateCost(_mattress, _mattress.mxList, _mattress.extraList, _mattress.extraCostList);
                var mxmessageList = _mattress.mxList.Where(item => !string.IsNullOrEmpty(item.message)).ToList();
                if (mxmessageList.Any())
                {
                    foreach (var mx in mxmessageList)
                    {
                        message.Add(mx.message);
                    }
                }
                rslt.mxmessage = message;
                #endregion

                #region 复制报价初始化
                if(_mattress.copy_id != null && _mattress.copy_id > 0)
                {
                    _mattress.mattressid = 0;
                    _mattress.mattresscode = string.Empty;
                    _mattress.yw_flag = 0;
                    _mattress.yw_auditingrep = string.Empty;
                    _mattress.yw_auditingdate = null;
                    _mattress.js1_flag = 0;
                    _mattress.js1_auditingrep = string.Empty;
                    _mattress.js1_auditingdate = null;
                    _mattress.js2_flag = 0;
                    _mattress.js2_auditingrep = string.Empty;
                    _mattress.js2_auditingdate = null;
                    _mattress.xd_flag = 0;
                    _mattress.xd_auditingrep = string.Empty;
                    _mattress.xd_auditingdate = null;
                    _mattress.flag = 0;
                    _mattress.auditingrep = string.Empty;
                    _mattress.auditingdate = null;
                    _mattress.qr_auditingrep = string.Empty;
                    _mattress.qr_auditingdate = null;
                    _mattress.erp_mtrlid = 0;
                    _mattress.erp_mtrlcode = string.Empty;
                    _mattress.erp_piccode = string.Empty;
                    _mattress.erp_mtrlname = string.Empty;
                    _mattress.createby = string.Empty;

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

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

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

                    foreach(var mx in _mattress.subspecsList)
                    {
                        mx.mattressid = 0;
                        mx.mattresscode = string.Empty;
                    }
                }
                #endregion

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        mattressHelper.SaveMattressPro(_mattress);

                        rslt.mattressid = _mattress.mattressid;
                        rslt.mattresscode = _mattress.mattresscode;

                        if (_mattress.copy_id != null && _mattress.copy_id > 0)
                        {
                            mattressHelper.CopyMattressInterface(_mattress.copy_id.Value, _mattress.mattressid);
                        }

                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        rslt.ErrMsg = e.Message;
                        Trace.Write(e);
                        cmd.Transaction?.Rollback();
                    }
                }
            }
        }
    }
}
