using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using DirectService.Tools;
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 GetMtrlTypeListExcutor : ExcutorBase<GetMtrlTypeListRequest, GetMtrlTypeListResponse>
    {
        protected override void ExcuteInternal(GetMtrlTypeListRequest request, object state, GetMtrlTypeListResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }

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

                var selectStr = @"SELECT
	                                    u_mtrltype.mtrltypeid, 
	                                    u_mtrltype.typecode, 
	                                    u_mtrltype.mtrltype, 
	                                    u_mtrltype.inuse, 
	                                    u_mtrltype.parentid,
                                        u_mtrltype.dannum1_rate,
                                        u_mtrltype.dannum2_rate,
                                        u_mtrltype.dannum3_rate,
                                        u_mtrltype.dannum4_rate
                                    FROM
	                                    u_mtrltype";
                var list = new List<u_mtrltype>();
                DbSqlHelper.SelectJoin(cmd, selectStr, null, null, "parentid", "mtrltypeid,typecode,mtrltype,inuse,parentid,dannum1_rate,dannum2_rate,dannum3_rate,dannum4_rate", 0, 0, list);

                var pk = new PkName()
                {
                    MainField = "mtrltypeid",
                    MainName = "mtrltype",
                    ParentField = "parentid"
                };

                ListEx.GetRecursions(list, pk, out var resultList);

                rslt.reList = resultList;
            }
        }
    }
}
