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 CopyConfigureCodeMxListExcutor : ExcutorBase<CopyConfigureCodeMxListRequest, CopyConfigureCodeMxListResponse>
    {
        protected override void ExcuteInternal(CopyConfigureCodeMxListRequest request, object state, CopyConfigureCodeMxListResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if(request.subject == null)
            {
                rslt.ErrMsg = "未提交部件选配项";
                return;
            }

            if(request.subject.typeid == null || request.subject.typeid <= 0)
            {
                rslt.ErrMsg = "部件选配类型ID错误";
                return;
            }

            if (request.subject.pzid == null || request.subject.pzid <= 0)
            {
                rslt.ErrMsg = "部件选配选配项ID错误";
                return;
            }

            if (request.mxList == null || !request.mxList.Any())
            {
                rslt.ErrMsg = "未提交部件选配值";
                return;
            }

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

                var c_type = new u_configure_type() { contfigtypeid = request.subject.typeid };
                DbSqlHelper.SelectOne(cmd, c_type, "flag");

                if(c_type.flag == 1)
                {
                    throw new LJCommonException("已经技审,不能修改!");
                }

                foreach(var mx in request.mxList)
                {
                    AutoInit.AutoInitS(cmd, mx);
                }

                using (cmd.Transaction = con.BeginTransaction(System.Data.IsolationLevel.Serializable))
                {
                    try
                    {
                        int _printid = 0;

                        if (request.type == 1)
                        {
                            // 覆盖
                            cmd.CommandText = @"DELETE FROM u_configure_codemx WHERE pzid = @pzid";
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@pzid", request.subject.pzid);
                            cmd.ExecuteNonQuery();
                        }
                        else
                        {
                            // 补充
                            var codeMxList = new List<u_configure_codemx>();
                            DbSqlHelper.Select(cmd, "u_configure_codemx", "pzid = @pzid", new Dictionary<string, object>() { { "@pzid", request.subject.pzid } }, "printid", 0, 0, codeMxList, null, "pzid,printid,pzcodemx,namemx,gradestr,mtrlcode,price,ifdft,MCostRate,ProfitRate,dscrp,ifuse,ifnoch,pricerate,packqty,packvol,price_pz,grade");

                            cmd.CommandText = @"SELECT ISNULL(MAX(printid),0) AS printid FROM u_configure_codemx WITH (UPDLOCK) WHERE pzid = @pzid";
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@pzid", request.subject.pzid);
                            using (var reader = cmd.ExecuteReader())
                            {
                                if (reader.Read())
                                {
                                    _printid = Convert.ToInt32(reader["printid"]);
                                }
                            }
                        }

                        foreach (var mx in request.mxList)
                        {
                            mx.printid = ++_printid;
                            mx.pzid = request.subject.pzid;

                            var fields = @"pzid,printid,pzcodemx,namemx,gradestr,mtrlcode,price,ifdft,MCostRate,ProfitRate,dscrp,ifuse,ifnoch,pricerate,packqty,packvol,price_pz,grade";
                            DbSqlHelper.Insert(cmd, "u_configure_codemx", null, mx, fields);
                        }

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