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 LJLib.Net.SPI.Server;
using LJLib.SQLEX;

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

                    cmd.CommandText = "SELECT funid FROM st_user_power WHERE userid = " + request.userid;
                    using (var reader = cmd.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            powers.Add(Convert.ToInt32(reader["funid"]));
                        }
                    }
                }
            }
            rslt.powerList = new Power().GetAllPowers();
            foreach (var powerItem in rslt.powerList)
            {
                if (powers.Contains(powerItem.funid.Value))
                {
                    powerItem.inuse = 1;
                }
                else
                {
                    powerItem.inuse = 0;
                }
            }
        }
    }
}
