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.DBA.DBModle;
using LJLib.DAL.SQL;
using LJLib.Net.SPI.Server;

namespace JLHHJSvr.Excutor
{
    internal sealed class GetSysFuncPwrExcutor : ExcutorBase<GetSysFuncPwrRequest, GetSysFuncPwrResponse>
    {
        protected override void ExcuteInternal(GetSysFuncPwrRequest request, object state, GetSysFuncPwrResponse rslt)
        {
            var tokendata = BllHelper.GetToken(request.token);
            if (tokendata == null)
            {
                rslt.ErrMsg = "会话已经中断，请重新登录";
                return;
            }
            using (var con = new SqlConnection(GlobalVar.ConnectionString))
            using (var cmd = con.CreateCommand())
            {
                con.Open();

                rslt.list = new List<sys_func_pwr>();
                var result = new List<sys_func_pwr>();
                var sysList = new List<sys_func_pwr>();
                DbSqlHelper.Select(cmd, "sys_func_pwr", "if_use = 1", null, null, 0, 0, sysList, null,
                    "funcid, parentid, treename, menuname, if_use");

                GetChildrenData(sysList, 0, ref result);
                rslt.list = result;
            }
        }

        public void GetChildrenData (List<sys_func_pwr>  data, int funcid, ref List<sys_func_pwr> reslist)
        {
            reslist = data.Where(t => t.parentid == funcid).ToList();
            foreach (var mx in reslist)
            {
                var list = data.Where(t => t.parentid == mx.funcid).ToList();
                if (list.Count > 0)
                {
                    var result = new List<sys_func_pwr>();
                    GetChildrenData(data, mx.funcid, ref result);
                    mx.children = result;
                }
            }
        }
    }
}
