using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace Core.Mes.ClientPurviewManager
{
    public sealed class RightsComm
    {
        private static string _ChildDpt = "";
        public static string GetChildDpt()
        {
            string err = "";
            object obj = Core.Mes.ClientFrameWork.ClientCommon._RemotingHelp.ExecuteMethod("PurviewManager", "Core.Mes.PurviewManager.UAM_DEPARTMENT",
                  "GetAllChildDpt", new object[] { Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserDepID }, out err);
            _ChildDpt = CheckNullStr(obj);
            return _ChildDpt;
        }
        public static string CheckNullStr(object obj)
        {
            if (obj == null || obj.GetType() == typeof(System.DBNull))
            {
                return "";
            }
            else
            {
                return Convert.ToString(obj);
            }
        }
        public static bool CheckRole(string OpDptID)
        {
            OpDptID = "'" + OpDptID + "'";
            string UserID = Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserID;
            string LoginID = Core.Mes.ClientFrameWork.ClientCommon._UserInfo.LoginID;
            string UserRoleID = Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserRoleID;

            string UserRoleName = Core.Mes.ClientFrameWork.ClientCommon._UserInfo.UserRoleName;

            string UserChildDpt = GetChildDpt();
            if (UserID.ToUpper() == "ROOT" || UserRoleID.ToUpper() == "ADMIN001" || LoginID.ToUpper() == "ROOT" || LoginID.ToUpper() == "ZH017" || LoginID.ToUpper() == "OY")
                return true;
            if (UserRoleID.IndexOf("Admin") < 0 && UserRoleName.IndexOf("管理员") < 0 )
            {
                MessageBox.Show("执行该命令需要管理员权限，请与管理员联系", "系统提示");
                return false;
            }
            if (UserChildDpt.Length == 0)
            {
                MessageBox.Show("权限不够，不能执行该命令，请与系统管理员联系", "系统提示");
                return false;
            }
            if (UserChildDpt.IndexOf(OpDptID) < 0)
            {
                MessageBox.Show("权限不够，不能执行该命令，请与系统管理员联系", "系统提示");
                return false;
            }
            return true;
        }
    }
}
