using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using JLHHJSvr.BLL;
using JLHHJSvr.Com;
using JLHHJSvr.Com.Model;
using JLHHJSvr.Helper;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;
using LJLib.SQLEX;
using Newtonsoft.Json.Linq;

namespace JLHHJSvr.Excutor
{
    internal sealed class UpdateMtrlPriceExcutor : ExcutorBase<UpdateMtrlPriceRequest, UpdateMtrlPriceResponse>
    {
        Dictionary<string, object> replacements = new Dictionary<string, object>();

        protected override void ExcuteInternal(UpdateMtrlPriceRequest request, object state, UpdateMtrlPriceResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (request.mattressid <= 0)
            {
                rslt.ErrMsg = "床垫id参数为空!";
                return;
            }

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


                var mattress = new u_mattress();
                if (DbSqlHelper.SelectOne(cmd, "u_mattress", "mattressid = @mattressid", new Dictionary<string, object>() { { "mattressid", request.mattressid } }, mattress, "erp_mtrlid,erp_mtrlcode, erp_mtrlname, erp_mtrlmode, erp_mtrltypeid, erp_mtrltype, erp_mtrlunit, erp_mtrlengname,nottax_factory_cost,dept_profitrate,dept_profitrate_rangli,commission,taxes,fob,extras_cost,mattresstypeid,mattresscode,js1_flag") != 1)
                {
                    rslt.ErrMsg = "床垫报价单匹配失败";
                    return;
                }

                var interfaceList = new List<u_mattress_interface>();
                var selectStr = @"SELECT u_mattress_interface.mattressid
	                            ,printid
	                            ,itemname
	                            ,bj_pzname
	                            ,bj_namemx
	                            ,actual_size
	                            ,sb_craft
	                            ,actual_size_sb
	                            ,erp_pzid
	                            ,ss_rate
	                            ,ls_rate
	                            ,bj_inputtype
                                ,u_configure_code.pzcode
                                ,u_configure_code.name AS pzname
                            FROM u_mattress_interface
                            left outer join u_mattress on u_mattress.mattressid = u_mattress_interface.mattressid
                            left outer join u_configure_code on u_configure_code.typeid = u_mattress.erp_configcodetype 
		                            and u_mattress_interface.erp_pzid = u_configure_code.pzid";
                DbSqlHelper.SelectJoin(cmd, selectStr, "u_mattress_interface.mattressid = @mattressid AND u_mattress_interface.erp_pzid > 0 AND u_mattress_interface.bj_pzname <> '' ", new Dictionary<string, object>() { { "@mattressid", request.mattressid } }, "printid", "mattressid,printid,itemname,bj_pzname,bj_namemx,actual_size,sb_craft,actual_size_sb,erp_pzid,ss_rate,ls_rate,bj_inputtype,pzcode,pzname", 0, 0, interfaceList);

                var l1Helper = HelperBase.GetHelper<ERPHelper>(cmd);
                l1Helper.context = new HelperBase.Context() { tokendata = tokendata };

                mattress = l1Helper.GetMattressMsg(mattress, interfaceList);

                var errMsg = l1Helper.SaveMtrldef(mattress, null, "bj_mxdscrp,configname");
                if (!string.IsNullOrEmpty(errMsg))
                {
                    rslt.ErrMsg = errMsg;
                    return;
                }

                errMsg = l1Helper.UpdateL1Planprice(mattress.erp_mtrlid.Value, mattress.nottax_dept_cost.Value);
                if (!string.IsNullOrEmpty(errMsg))
                {
                    rslt.ErrMsg = errMsg;
                    return;
                }

                //更新床网半成品
                var mattressMx = new u_mattress_mx_mtrl();
                if (DbSqlHelper.SelectOne(cmd, "u_mattress_mx_mtrl", "erp_mtrlid <> 0 AND mattressid = @mattressid", new Dictionary<string, object>() { { "mattressid", request.mattressid } }, mattress, "mtrlid , erp_mtrlid") == 1)
                {
                    decimal lde_cost = 0;
                    var ll_erp_mtrlid = 0;

                    cmd.CommandText = @"select
                            isnull(total_cost,0)  - 
                            isnull(sponge_mtrl_cost,0)  -
                            isnull(felt_mtrl_cost,0)  -
                            isnull(felt_mtrl_x_cost,0)  -
                            isnull(packet_mtrl_cost,0)  - 
                            isnull(packet_hr_cost,0) AS cw_cost
                        FROM u_bednet WHERE mattressid = @deptid";
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@mattressid", mattressMx.mtrlid);
                    using (var reader = cmd.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            lde_cost = Convert.ToDecimal(reader["nottax_factory_cost"]);
                        }
                    }

                    errMsg = l1Helper.UpdateL1Planprice(mattressMx.erp_mtrlid.Value, lde_cost);
                    if (!string.IsNullOrEmpty(errMsg))
                    {
                        rslt.ErrMsg = errMsg;
                        return;
                    }

                }
            }
        }

    }
}
