using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
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 NPOI.OpenXml4Net.OPC.Internal;

namespace JLHHJSvr.Excutor
{
    internal sealed class ReCalculateERPCostExcutor : ExcutorBase<ReCalculateERPCostRequest, ReCalculateERPCostResponse>
    {
        protected override void ExcuteInternal(ReCalculateERPCostRequest request, object state, ReCalculateERPCostResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (request.list == null)
            {
                rslt.ErrMsg = "提交缺少列表参数,请检查!";
                return;
            }
            if (!request.list.Any())
            {
                rslt.ErrMsg = "至少提交一条需要重算的记录";
                return;
            }

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

                var log_sb = new StringBuilder();
                var helper = HelperBase.GetHelper<MattressHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });
                var l1Helper = HelperBase.GetHelper<ERPHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });

                foreach (var mattress in request.list)
                {
                    DbSqlHelper.SelectOne(cmd, mattress, "flag,deptid");

                    //if (mattress.flag == 1) throw new LJCommonException("存在报价单已审核,不能重算!");
                    if (mattress.deptid <= 0) throw new LJCommonException("存在报价单部门id错误,不能重算!");

                    var dept = new u_dept() { deptid = mattress.deptid.Value };
                    DbSqlHelper.SelectOne(cmd, dept, "pricelistid");
                    if (dept.pricelistid <= 0) throw new LJCommonException("存在报价单部门pricelistid错误,不能重算!");
                }

                using (cmd.Transaction = con.BeginTransaction())
                {
                    foreach (var mattress in request.list)
                    {

                        var mattress_temp = helper.GetMattress(mattress.mattressid);
                        var mxList = helper.GetMattressMxMtrl(mattress.mattressid);
                        var mxExtra1List = helper.GetMattressMxExtra(mattress.mattressid, 1);
                        var mxExtra2List = helper.GetMattressMxExtra(mattress.mattressid, 2);
                        var subspecsList = helper.GetMattressSubspecs(mattress.mattressid);
                        try
                        {
                            helper.MattressCalculateCost(mattress_temp, mxList, mxExtra1List, mxExtra2List);
                            helper.SaveMattress(mattress_temp, mxList, mxExtra1List, mxExtra2List);

                            if(mattress_temp.erp_mtrlid > 0)
                            {
                                string errmsg = l1Helper.SaveMtrldef(mattress_temp);
                                if (!string.IsNullOrEmpty(errmsg)) throw new LJCommonException(errmsg);
                            }

                            cmd.Transaction.Commit();
                            log_sb.Append($"床垫唯一码:{mattress_temp.mattresscode}重算成功\r\n");
                        }
                        catch (Exception ex)
                        {
                            cmd.Transaction.Rollback();
                            log_sb.Append($"床垫唯一码:{mattress_temp.mattresscode}重算失败\r\n");
                        }
                    }

                    if(log_sb.Length > 0) rslt.logMsg = log_sb.ToString();
                }
            }
        }
    }
}
