using HighWayIot.Repository.service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HighWayIot.Winform.Business
{
    public class RoleBusiness
    {
        /// <summary>
        /// 当前登录人的姓名
        /// </summary>
        public static string LoginUserName = string.Empty;

        /// <summary>
        /// 用户数据业务实例
        /// </summary>
        SysUserInfoService _sysUserInfoService = SysUserInfoService.Instance;

        /// <summary>
        /// 角色数据业务实例
        /// </summary>
        SysUserRoleService _sysUserRoleService = SysUserRoleService.Instance;

        /// <summary>
        /// XML读取类
        /// </summary>
        XmlUtil xmlUtil = XmlUtil.Instance;

        /// <summary>
        /// 页面权限偏移量对应表
        /// </summary>
        List<RoleConfig> RoleConfigs;

        public RoleBusiness()
        {
            RoleConfigs = xmlUtil.ConfigReader();
        }

        /// <summary>
        /// 判断现在登录的用户是否有指定页面的访问权限
        /// </summary>
        /// <param name="pageName">页面名称</param>
        /// <returns>有权限返回true，没权限返回false</returns>
        public bool PageIsAccess(string pageName)
        {
            RoleConfig roleConfig = RoleConfigs.Where(x => x.PageName == pageName).FirstOrDefault();

            //先判断页面是否配置过，没配置过的都默认有权限
            if (roleConfig == null)
            {
                return true;
            }

            //获取当前用户的权限字符串
            string roleString = _sysUserRoleService.GetRoleInfos(
                _sysUserInfoService.GetUserInfoByUserName(LoginUserName).First().UserRole
                ).First().RoleSet;

            //转换为Char数组
            char[] chars = roleString.ToCharArray();

            //返回权限结果
            return chars[roleConfig.RoleIndex] == '1';

        }
    }
}
