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 SaveMattressBcpExcutor : ExcutorBase<SaveMattressBcpRequest, SaveMattressBcpResponse>
    {
        protected override void ExcuteInternal(SaveMattressBcpRequest request, object state, SaveMattressBcpResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (request.mattress == null)
            {
                rslt.ErrMsg = "缺少主表信息";
                return;
            }
            if(request.mattress.mxlist == null || !request.mattress.mxlist.Any())
            {
                rslt.ErrMsg = "缺少半成品明细";
                return;
            }

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

                // 初始化属性
                AutoInit.AutoInitS(cmd, request.mattress);

                using (cmd.Transaction = con.BeginTransaction())
                {
                    try
                    {
                        var dtNow = DateTime.Now;

                        var fields = @"billdate,semi_finished_type,semi_finished_code,semi_finished_name,dscrp,production_size,hemming_process,
                                        hemmed_size,shrinkage_rate,elongation_rate,opemp,opdate,auditemp,auditdate";
                        var fieldsMx = "billid,printid,mtrlid,formulaid,chastr,thickness,qty,erp_mtrlid,usenum,usedenom";

                        if (request.mattress.billid <= 0)
                        {
                            //新建
                            fields += ",billid,billcode";

                            request.mattress.opdate = dtNow;
                            request.mattress.opemp = tokendata.username;

                            request.mattress.billid = BllHelper.GetID(cmd, "u_semi_finished_product");

                            switch(request.mattress.semi_finished_type)
                            {
                                case 901:
                                    request.mattress.billcode = "JM";
                                    break;
                                case 902:
                                    request.mattress.billcode = "JD";
                                    break;
                                case 903:
                                    request.mattress.billcode = "BC";
                                    break;
                                case 904:
                                    request.mattress.billcode = "XC";
                                    break;
                                case 905:
                                    request.mattress.billcode = "VC";
                                    break;
                            }

                            // 编号-年月日+流水
                            request.mattress.billcode = $"{request.mattress.billcode}-{dtNow.ToString("yyyyMMdd")}{(request.mattress.billid.Value % 10000).ToString("D4")}";

                            DbSqlHelper.Insert(cmd, "u_semi_finished_product", null, request.mattress, fields);
                        }
                        else
                        {
                            //修改
                            cmd.CommandText = @"DELETE FROM u_semi_finished_product_mx WHERE billid = @billid";
                            cmd.Parameters.Clear();
                            cmd.Parameters.AddWithValue("@billid", request.mattress.billid);
                            cmd.ExecuteNonQuery();

                            DbSqlHelper.Update(cmd, "u_semi_finished_product", null, request.mattress, "billid", fields);
                        }

                        var cnt = 0;
                        foreach (var mx in request.mattress.mxlist)
                        {
                            AutoInit.AutoInitS(cmd, mx);

                            mx.billid = request.mattress.billid;
                            mx.printid = ++cnt;

                            DbSqlHelper.Insert(cmd, "u_semi_finished_product_mx", null, mx, fieldsMx);
                        }

                        var hisprice = new u_his_price
                        {
                            bednetid_mattressid = request.mattress.billid,
                            typeid = 2,
                            cmpdate = dtNow,
                            cmpemp = tokendata.username,
                            nottax_dept_cost = 0,
                            dept_cost = 0,
                            foreign_cost = 0
                        };
                        var fieldsHs = "bednetid_mattressid, typeid, cmpdate, cmpemp, nottax_dept_cost, dept_cost, foreign_cost";
                        DbSqlHelper.Insert(cmd, "u_his_price", null, hisprice, fieldsHs);

                        cmd.Transaction.Commit();

                        rslt.mattress = new u_semi_finished_product() { billid = request.mattress.billid, billcode = request.mattress.billcode };
                    }
                    catch (Exception e)
                    {
                        cmd.Transaction.Rollback();
                        rslt.ErrMsg = e.ToString();
                    }
                }
            }
        }
    }
}
