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.LJException;
using JLHHJSvr.Tools;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;
using LJLib.SQLEX;

namespace JLHHJSvr.Excutor
{
    internal sealed class SaveConfigureBomListExcutor : ExcutorBase<SaveConfigureBomListRequest, SaveConfigureBomListResponse>
    {
        protected override void ExcuteInternal(SaveConfigureBomListRequest request, object state, SaveConfigureBomListResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }

            if (request.bomList == null || request.bomList.Count <= 0)
            {
                rslt.ErrMsg = "未提交部件选配项值清单";
                return;
            }

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

                var dtNow = DateTime.Now;

                var sb = new StringBuilder();
                var saveList = new List<u_configure_codemxbom>();
                foreach(var bom in request.bomList)
                {
                    AutoInit.AutoInitS(cmd, bom);

                    if(bom.pzid <= 0)
                    {
                        sb.AppendLine($"物料:{bom.mtrlname}配置项ID错误");
                    }

                    if(bom.printid <= 0)
                    {
                        sb.AppendLine($"物料:{bom.mtrlname}配置值ID错误");
                    }

                    if(sb.Length > 0)
                    {
                        rslt.ErrMsg = sb.ToString();
                        return;
                    }

                    saveList.Add(bom);
                }

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        var fields = @"mtrlid,sonscale,sonscale_formula,mng_cost_rate,profit_rate,realqty,sonloss,sonloss_formula,sondecloss,sondecloss_formula,default_length,default_width,default_qty";
                        foreach (var item in saveList)
                        {
                            if(item.pid <= 0)
                            {
                                fields += ",pzid,printid,pid";
                                int _pid = 0;
                                cmd.CommandText = @"SELECT ISNULL(MAX(pid),0) AS pid FROM u_configure_codemxbom WHERE pzid = @pzid AND printid = @printid";
                                cmd.Parameters.Clear();
                                cmd.Parameters.AddWithValue("@pzid", item.pzid);
                                cmd.Parameters.AddWithValue("@printid", item.printid);
                                using (var reader = cmd.ExecuteReader())
                                {
                                    if (reader.Read())
                                    {
                                        _pid = Convert.ToInt32(reader["pid"]);
                                    }
                                }
                                item.pid = _pid + 1;

                                DbSqlHelper.Insert(cmd, "u_configure_codemxbom", null, item, fields);
                            } else
                            {
                                //修改
                                DbSqlHelper.Update(cmd, "u_configure_codemxbom", null, item, "pzid,printid,pid", fields);
                            }
                        }

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