using RMYY_CallCenter_Api.Models.Dto;
using RMYY_CallCenter_Api.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Web;

namespace RMYY_CallCenter_Api
{
    public static class Common
    {
        /// <summary>
        /// 获取授权action
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAuthActions()
        {
            var actions = CacheHelper.Get<List<string>>("AuthActions");
            if (actions != null && actions.Count > 0)
            {
                return actions;
            }
            else
            {
                var dt = DB.DbHelperSQL.Query("select F_Action FROM T_Sys_Menu with(nolock) where F_State=1 and isnull(F_Action,'')!='' ").Tables[0];
                var actions1 = (from DataRow dr in dt.Rows select dr["F_Action"].ToString().ToLower()).ToList();
                CacheHelper.Insert("AuthActions", actions1, 10);
                return actions1;
            }
        }

        /// <summary>
        /// 获取角色授权action
        /// </summary>
        /// <returns></returns>
        public static List<string> GetRoleActions(int roleId)
        {
            var actions = CacheHelper.Get<List<string>>("RoleActions_" + roleId);
            if (actions!=null && actions.Count>0)
            {
                return actions;
            }
            else
            {
                var dt = DB.DbHelperSQL.Query("select m.F_Action FROM T_Sys_RoleMenu rm with(nolock) left join T_Sys_Menu m with(nolock) on rm.F_MenuId=m.F_MenuId where m.F_State=1 and rm.F_RoleId=" + roleId + " and isnull(m.F_Action,'')!='' ").Tables[0];
                var actions1 = (from DataRow dr in dt.Rows select dr["F_Action"].ToString().ToLower()).ToList();
                CacheHelper.Insert("RoleActions_" + roleId, actions1, 10);
                return actions1;
            }
        }

        /// <summary>
        /// 获取角色对应按钮
        /// </summary>
        /// <returns></returns>
        public static List<string> GetRoleButtons(int roleId)
        {
            var actions = CacheHelper.Get<List<string>>("RoleButtons1_" + roleId);
            if (actions != null && actions.Count > 0)
            {
                return actions;
            }
            else
            {
                var dt = DB.DbHelperSQL.Query("select a.F_MenuId as F_MenuId from T_Sys_RoleMenu as a join T_Sys_Menu as b on b.F_MenuId = a.F_MenuId where b.F_Type = 3 and b.F_State = 1 and F_RoleId = '"+roleId+"'").Tables[0];
                var actions1 = (from DataRow dr in dt.Rows select dr["F_MenuId"].ToString().ToLower()).ToList();
               CacheHelper.Insert("RoleButtons_" + roleId, actions1);
                return actions1;
            }
        }



        /// <summary>
        /// 获取工单枚举状态
        /// </summary>
        /// <param name="eErrorDetailCode"></param>
        /// <returns></returns>
        public static List<TypeState> GetGongDanState<T>()
        {
            List<TypeState> slist = new List<TypeState>();
            foreach (int myCode in Enum.GetValues(typeof(T)))
            {
                TypeState model = new TypeState();
                model.StateId = myCode.ToString();//获取值
                model.StateValue = Enum.GetName(typeof(T), myCode);//获取名称
                FieldInfo fieldinfo = typeof(T).GetField(model.StateValue);
                Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
                DescriptionAttribute des = (DescriptionAttribute)objs[0];
                model.StateRemark = des?.Description ?? "";
                slist.Add(model);//添加到list
            }
            return slist;
        }
        /// <summary>
        /// 是否为ip
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIP(string ip)
        {
            return Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
        /// <summary>
        /// 获得当前页面客户端的IP
        /// </summary>
        /// <returns>当前页面客户端的IP</returns>
        public static string GetIP()
        {
            string result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            if (string.IsNullOrEmpty(result))
                result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(result))
                result = HttpContext.Current.Request.UserHostAddress;
            if (string.IsNullOrEmpty(result) || !IsIP(result))
                return "127.0.0.1";
            return result;
        }
    }
}