using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.LJException;
using JLHHJSvr.Tools;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;

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

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

            if (string.IsNullOrEmpty(request.mtrl.name))
            {
                rslt.ErrMsg = "请输入物料名称";
                return;
            }

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

                var dtNow = DateTime.Now;

                AutoInit.AutoInitS(cmd, request.mtrl);

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        if (request.mtrl.mtrlid <= 0)
                        {
                            request.mtrl.mtrlid = BllHelper.GetID(cmd, "u_mtrl_price");

                            request.mtrl.createtime = dtNow;
                            request.mtrl.createby = tokendata.username;

                            var fields = @"mtrlid,mtrltype,name,priceunit,shrinkage,gram_weight,cloth_width,if_inputqty,if_areaprice,createtime,createby,thickness,dscrp,erp_mtrlid,fjcnt,isuse,handtype,lastdate,erp_mtrlengname";
                            DbSqlHelper.Insert(cmd, "u_mtrl_price", null, request.mtrl, fields);

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

                            if(request.mtrl.mxlist != null && request.mtrl.mxlist.Any())
                            {
                                foreach (var mx in request.mtrl.mxlist)
                                {
                                    AutoInit.AutoInitS(cmd, mx);
                                    mx.mtrlid = request.mtrl.mtrlid;

                                    DbSqlHelper.Insert(cmd, "u_mtrl_price_pricelist", null, mx, fields2);
                                }
                            }
                        }
                        else
                        {
                            //修改
                            var fields = @"mtrltype,name,priceunit,shrinkage,gram_weight,cloth_width,if_inputqty,if_areaprice,thickness,dscrp,erp_mtrlid,fjcnt,isuse,handtype,lastdate,erp_mtrlengname";
                            DbSqlHelper.Update(cmd, "u_mtrl_price", null, request.mtrl, "mtrlid", fields);

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

                            if (request.mtrl.mxlist != null && request.mtrl.mxlist.Any())
                            {
                                foreach (var mx in request.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();
                    }
                }
            }
        }
    }
}
