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;

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

            if (request.list == null || !request.list.Any())
            {
                rslt.ErrMsg = "未提交物料类别定义";
                return;
            }

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

                var dtNow = DateTime.Now;

                foreach(var mtrl in request.list)
                {
                    AutoInit.AutoInitS(cmd, mtrl);
                }

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        foreach (var mtrl in request.list)
                        {
                            if (mtrl.mtrlid > 0)
                            {
                                var fields = @"priceunit,shrinkage,gram_weight,cloth_width,thickness";
                                DbSqlHelper.Update(cmd, "u_mtrl_price", null, mtrl, "mtrlid", fields);

                                var fields2 = @"price,pricetype,price_formula,qty_formula";

                                if(mtrl.pricelistid > 0)
                                {
                                    var mxlist = new List<u_mtrl_price_pricelist>();
                                    DbSqlHelper.SelectJoin(cmd, "SELECT pricelistid,mtrlid,price,pricetype,price_formula,qty_formula FROM u_mtrl_price_pricelist", "mtrlid = @mtrlid AND pricelistid = @pricelistid", new Dictionary<string, object>() { { "@mtrlid", mtrl.mtrlid }, { "@pricelistid", mtrl.pricelistid } }, "mtrlid", "pricelistid,mtrlid,price,pricetype,price_formula,qty_formula", 0, 0, mxlist);
                                
                                    foreach(var mx in mxlist)
                                    {
                                        mx.price = mtrl.price;
                                    }

                                    mtrl.mxlist = mxlist; 
                                }

                                if (mtrl.mxlist != null && mtrl.mxlist.Any())
                                {
                                    foreach (var mx in mtrl.mxlist)
                                    {
                                        AutoInit.AutoInitS(cmd, mx);
                                        DbSqlHelper.Update(cmd, "u_mtrl_price_pricelist", null, mx, "mtrlid,pricelistid", fields2);
                                    }
                                }
                            }
                        }
                        
                        cmd.Transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        cmd.Transaction.Rollback();
                        rslt.ErrMsg = e.ToString();
                    }
                }
            }
        }
    }
}
