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

namespace JLHHJSvr.Excutor
{
    internal sealed class GetBedNetInterfaceListExcutor : ExcutorBase<GetBedNetInterfaceListRequest, GetBedNetInterfaceListResponse>
    {
        protected override void ExcuteInternal(GetBedNetInterfaceListRequest request, object state, GetBedNetInterfaceListResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            if (request.bednetid == null || request.bednetid <= 0)
            {
                rslt.ErrMsg = "床网id有误,请检查!";
                return;
            }

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

                cmd.CommandText = @"SELECT u_bednet.bednetid
	                                    ,u_bednet.bednettypeid
	                                    ,u_bednet.deptid
	                                    ,u_bednet.mattress_width
	                                    ,u_bednet.mattress_height
	                                    ,u_bednet.mattress_length
	                                    ,u_bednet.if_doublenet
	                                    ,u_bednet.if_doublespring
	                                    ,u_bednet.if_side_iron
	                                    ,u_bednet.if_jb
	                                    ,u_bednet.iffork
	                                    ,u_bednet.if_rsORwa
	                                    ,u_bednet.if_sponge_drilling
	                                    ,u_bednet.packtype
	                                    ,u_bednet.packqty
	                                    ,u_bednet.total_hr_cost
	                                    ,u_bednet.total_fees_cost
	                                    ,u_bednet.total_cost
	                                    ,u_bednet.taxrate
	                                    ,u_bednet.taxes
	                                    ,u_bednet.commissionrate
	                                    ,u_bednet.commission
	                                    ,u_bednet.fob
	                                    ,u_bednet.profitrate
	                                    ,u_bednet.nottax_factory_cost
	                                    ,u_bednet.nottax_dept_cost
	                                    ,u_bednet.dept_cost
	                                    ,u_bednet.foreign_cost
	                                    ,u_bednet.extras_cost
	                                    ,u_bednet.dept_profitrate
	                                    ,u_bednet.moneyrate
	                                    ,u_bednet.flag
	                                    ,u_bednet.createtime
	                                    ,u_bednet.createby
                                        ,u_bednet.erp_mtrlid
                                        ,u_bednet.js2_flag
                                        ,u_bednet.js2_auditingrep
                                        ,u_bednet.js2_auditingdate
                                        ,u_bednet.creatmtrl_flag
                                        ,u_bednet.creatmtrl_auditingrep
                                        ,u_bednet.creatmtrl_auditingdate
                                        ,u_bednet.creatmtrlqd_flag
                                        ,u_bednet.creatmtrlqd_auditingrep
                                        ,u_bednet.creatmtrlqd_auditingdate
                                        ,u_bednet.erp_mtrlcode
                                        ,u_bednet.erp_mtrlengname
                                        ,u_bednet.erp_configcodetype
                                    FROM u_bednet
                                    WHERE u_bednet.bednetid = @bednetid";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@bednetid", request.bednetid);
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader.Read())
                    {

                        //var _mtrlmode = Convert.ToString(reader["erp_mtrlmode"]);
                        //if (string.IsNullOrEmpty(_mtrlmode))
                        //{
                        //    _mtrlmode = Convert.ToInt32(reader["mattress_width"]) + "*" + Convert.ToInt32(reader["mattress_length"]) + "*" + Convert.ToInt32(reader["mattress_height"]);
                        //}
                        var _mtrlname = Convert.ToString(reader["erp_mtrlname"]);
                        if (string.IsNullOrEmpty(_mtrlname))
                        {
                            _mtrlname = Convert.ToString(reader["mattressname"]);
                        }

                        rslt.bednet = new u_bednet()
                        {
                            bednetid = request.bednetid.Value,
                            erp_mtrlid = Convert.ToInt32(reader["erp_mtrlid"]),
                            deptid = Convert.ToInt32(reader["deptid"]),
                            bednetname = Convert.ToString(reader["mattressname"]),
                            bednetcode = Convert.ToString(reader["mattresscode"]),
                            mattress_width = Convert.ToInt32(reader["mattress_width"]),
                            mattress_length = Convert.ToInt32(reader["mattress_length"]),
                            mattress_height = Convert.ToInt32(reader["mattress_height"]),
                            erp_configcodetype = Convert.ToInt32(reader["erp_configcodetype"]),
                            erp_mtrltypeid = Convert.ToInt32(reader["erp_mtrltypeid"]),
                            erp_mtrlcode = Convert.ToString(reader["erp_mtrlcode"]),
                            erp_mtrlname = _mtrlname,
                            //erp_mtrlmode = _mtrlmode,
                            erp_mtrlunit = Convert.ToString(reader["erp_mtrlunit"]),
                            erp_mtrlengname = Convert.ToString(reader["erp_mtrlengname"]),
                            erp_mtrltype = Convert.ToString(reader["erp_mtrltype"]),
                            js2_flag = Convert.ToByte(reader["js2_flag"])
                        };
                    }
                    else
                    {
                        rslt.ErrMsg = "无法找到床网报价数据，请确认";
                        return;
                    }
                }

                if (request.isEdit == 1)
                {
                    if (rslt.bednet.erp_configcodetype <= 0)
                    {
                        cmd.CommandText = @"SELECT TOP 1 u_configure_type.contfigtypeid
	                                    ,u_configure_type.contfigtypename
	                                    ,u_configure_type.contfigtype
	                                    ,u_configure_type.usechflag
	                                    ,u_configure_type.flag
	                                    ,u_configure_type.auditemp
	                                    ,u_configure_type.auditdate
                                    FROM u_configure_type";
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@empid", tokendata.empid);
                        cmd.Parameters.AddWithValue("@dwname", "dw_mattress_interface_yw_edit");
                        cmd.Parameters.AddWithValue("@itemname", "erp_configcodetype");
                        using (var readerItem = cmd.ExecuteReader())
                        {
                            if (readerItem.Read())
                            {
                                rslt.bednet.erp_configcodetype = Convert.ToInt32(readerItem["contfigtypeid"]);
                            }
                        }
                    }

                    if (rslt.bednet.erp_mtrltypeid <= 0)
                    {
                        cmd.CommandText = @"SELECT itemvalue FROM sys_user_filestring WHERE dwname = @dwname AND itemname = @itemname AND empid = @empid";
                        cmd.Parameters.Clear();
                        cmd.Parameters.AddWithValue("@empid", tokendata.empid);
                        cmd.Parameters.AddWithValue("@dwname", "dw_mattress_interface_yw_edit");
                        cmd.Parameters.AddWithValue("@itemname", "mtrltypeid");
                        using (var readerItem = cmd.ExecuteReader())
                        {
                            if (readerItem.Read())
                            {
                                rslt.bednet.erp_mtrltypeid = Convert.ToInt32(readerItem["itemvalue"]);
                            }
                        }
                    }

                    if (rslt.bednet.erp_mtrlunit == "")
                    {
                        rslt.bednet.erp_mtrlunit = "张";
                    }
                }

                var interfaceHelper = HelperBase.GetHelper<InterfaceHelper>(cmd, new HelperBase.Context() { tokendata = tokendata });

                var interfaceList = interfaceHelper.GetBedNetInterfaceList(request.bednetid.Value);
                var qdList = interfaceHelper.GetMattressInterfaceQdList(request.bednetid.Value);

                interfaceHelper.InterfaceFindERPPrdPf(qdList, interfaceList);

                rslt.interfaceList = interfaceList;
                rslt.qdList = qdList;
            }
        }
    }
}
