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;

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

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

                cmd.CommandText = @"SELECT u_mattress.mattressid
	                                ,u_mattress.erp_mtrlid
	                                ,u_mattress.mattressname
	                                ,u_mattress.erp_configcodetype
	                                ,u_mattress.mattresscode
	                                ,u_mattress.mattressrelcode
                                    ,mattress_width
									,mattress_length
									,mattress_height
	                                ,erp_mtrltypeid
	                                ,erp_mtrlcode
	                                ,erp_mtrlname
	                                ,erp_mtrlmode
	                                ,erp_mtrlunit
	                                ,erp_mtrltype
	                                ,CASE 
		                                WHEN isnull(erp_mtrlengname, '') = ''
			                                THEN mattressrelcode
		                                ELSE erp_mtrlengname
		                                END AS erp_mtrlengname
	                                ,old_mtrlname AS old_mtrlname
                                    ,deptid
                                    ,yw_flag
                                    ,js1_flag
                                    ,js2_flag
                                FROM u_mattress
                                WHERE u_mattress.mattressid = @mattressid";
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@mattressid", request.mattressid);
                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.mattress = new u_mattress()
                        {
                            mattressid = request.mattressid.Value,
                            erp_mtrlid = Convert.ToInt32(reader["erp_mtrlid"]),
                            deptid = Convert.ToInt32(reader["deptid"]),
                            mattressname = Convert.ToString(reader["mattressname"]),
                            mattresscode = Convert.ToString(reader["mattresscode"]),
                            mattressrelcode = Convert.ToString(reader["mattressrelcode"]),
                            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"]),
                            old_mtrlname = Convert.ToString(reader["old_mtrlname"]),
                            yw_flag = Convert.ToInt32(reader["yw_flag"]),
                            js1_flag = Convert.ToInt32(reader["js1_flag"]),
                            js2_flag = Convert.ToInt32(reader["js2_flag"])
                        };
                    }
                }

                if(request.isEdit == 1)
                {
                    if (rslt.mattress.erp_configcodetype <= 0)
                    {
                        cmd.CommandText = @"SELECT CASE
                                            WHEN itemvalue > 0 THEN itemvalue
                                            ELSE(
                                                SELECT itemvalue
                                                FROM sys_user_filestring
                                                WHERE empid = 0
                                                    AND dwname = @dwname 
                                                    AND itemname = @itemname
                                            )
                                        END AS 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", "erp_configcodetype");
                        using (var readerItem = cmd.ExecuteReader())
                        {
                            if (readerItem.Read())
                            {
                                rslt.mattress.erp_configcodetype = Convert.ToInt32(readerItem["itemvalue"]);
                            }
                        }
                    }

                    if (rslt.mattress.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.mattress.erp_mtrltypeid = Convert.ToInt32(readerItem["itemvalue"]);
                            }
                        }
                    }
                }

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

                var interfaceList = interfaceHelper.GetMattressInterfaceList(request.mattressid.Value);
                var qdList = interfaceHelper.GetMattressInterfaceQdList(request.mattressid.Value);

                interfaceHelper.MattressInterfaceFindERPPrdPf(qdList, interfaceList);

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