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

namespace JLHHJSvr.Excutor
{
    internal sealed class GetDeptExcutor : ExcutorBase<GetDeptRequest, GetDeptResponse>
    {
        protected override void ExcuteInternal(GetDeptRequest request, object state, GetDeptResponse 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();

                rslt.list = new List<u_dept>();

                var whereList = new List<string>();

                var powerDeptList = UserHelper.getPowerDept(cmd, tokendata.empid);
                if(powerDeptList != null && powerDeptList.Count > 0)
                {
                    whereList.Add($"u_dept.deptid IN {ListEx.getString(powerDeptList)}");
                } else
                {
                    throw new LJCommonException("该用户没有相关权限部门,请检查!");
                }

                DbSqlHelper.Select(cmd, "u_dept", null, null, null, 0, 0, rslt.list, null,
                    "deptid, deptname, profitrate, pricelistid, springtypeid, flag");
            }
        }
    }
}
