using System;
using System.Collections;
using DeiNiu.Utils;
using DeiNiu.wms.Data.Model;
using System.Data;
using System.Threading;
namespace DeiNiu.wms.Logical
{
    [Serializable]
    public class LEmployee
    {
        Employee _obj;
        private DeptEmp _deptEmpObj = new DeptEmp();
        //  ArrayList _authList = new ArrayList();
        private bool _canYuShen;
        private bool _canShenHe;
        private bool _canKaoHe;
        private bool _canManageProject;
        
        public LEmployee()
        {
            Initialize();
             
        }
        public DeptEmp GetDeptEmp
        {
            get
            {
                return _deptEmpObj;
            }
        }
        public Employee GetEmployee
        {
            get
            {
                return _obj;
            }
        }
        public ArrayList AuthList
        {
            get
            {
                // return _authList;
                if (_obj != null) return new LRoleAuthority().GetAuthorityList(_obj.ID);  //权限即时有效,不需登录
                return new ArrayList();
            }
        }
        public DataTable AuthTable
        {
            get
            {
                if (_obj != null) return new LRoleAuthority().GetAuthByEm(_obj.ID);  //权限即时有效,不需登录
                return new DataTable();
            }
        }
        /// 
        /// get all data
        /// 
        public DataSet GetAllData()
        {
            return _obj.Query();
        }
        /// 
        /// get all data
        /// 
        public DataSet GetAllActiveData()
        {
            return _obj.QueryActived();
        }
        /// 
        /// 取得用户部门信息
        /// 
        public DataTable GetActivedEmpDept()
        {
            return _deptEmpObj.GetActivedEmpDept();
        }
        /// 
        /// get a record by id
        /// 
        public void Initialize(int id)
        {
            _obj = id != 0 ? new Employee(id) : new Employee();
            setApproveProperty(_obj.ID);
        }
        /// 
        /// get a record by id 0
        /// 
        public void Initialize()
        {
            Initialize(0);
        }
        public DataTable GetDepartments()
        {
            return new Department().QueryActived().Tables[0];
        }
       
        /// 
        /// login, success then load authoritylist
        /// 
        /// 
        /// 
        /// 
        public bool Login(string userAccount, string passwd)
        {
            int id = _obj.ValidUser(userAccount,Util.Encrypt(passwd) );
            if ( id >0)
            {
                //  _authList = new LRoleAuthority().GetAuthorityList(_obj.ID);
               // setApproveProperty(_obj.ID);
                Initialize(id);
                return true;
            }
            return false;
        }
        protected void setApproveProperty(int empId)
        {
            LRoleAuthority ra = new LRoleAuthority();
            ArrayList al = ra.GetApproveRoleList(empId);
            _canKaoHe = al.Contains(ApproveRoles.KaoHe);
            _canManageProject = al.Contains(ApproveRoles.项目维护);
            if (GetCanApproveDeptList().Count > 0) //只有部门负责人才有预审、审核的权利。
            {
                _canShenHe = al.Contains(ApproveRoles.ShenHe);
                _canYuShen = al.Contains(ApproveRoles.YuShen);
            }
        }
        /// 
        /// 当前用户项目权限authType列表,0,100,200..
        /// 
        public ArrayList ProjectAuthlist
        {
            get
            { 
                const string prjlink = "~/Project/ProjectMain.aspx?authType=";
                ArrayList al = AuthList;
                ArrayList prjAuthlist = new ArrayList();
                for (int i = 0; i < al.Count; i++)
                {
                    if (al[i].ToString().Contains(prjlink.ToUpper()))
                    {
                        prjAuthlist.Add(al[i].ToString().Substring(prjlink.Length));
                    } 
                }
                return prjAuthlist;
            }
        }
        /// 
        /// 当前用户项目最大权限authType, Example 200..
        /// 
        public int  ProjectMaxAuth 
        {
            get
            {
                int authType = 0; 
                int index = -1;
                ArrayList al = ProjectAuthlist;
                for (int i = 0; i < al.Count; i++)
                { 
                        if (Convert.ToInt32(al[i].ToString() ) > authType)
                        {
                            authType = Convert.ToInt32(al[i].ToString() );
                            index = i;
                        } 
                } 
                return index>-1 ?Convert.ToInt32(al[index].ToString()):index ;
            }
        }
        /// 
        /// 当前用户项目最大权限页面, Example 200..
        /// 
        public string ProjectMaxAuthPage
        {
            get
            {
                const string prjlink = "~/Project/ProjectMain.aspx?authType=";
                return ProjectMaxAuth>-1?prjlink +ProjectMaxAuth : string.Empty;
            }
        }
        public bool CanShenHe
        {
            get { return _canShenHe; }
        }
        public bool CanYuShen
        {
            get { return _canYuShen; }
            set { _canYuShen = value; }
        }
        public bool CanKaoHe
        {
            get { return _canKaoHe; }
        }
        public bool CanManageProjects
        {
            get { return _canManageProject; }
        }
        public DataTable GetLessons(int empId)
        {
            return _obj.GetLessons(empId);
        }
        /// 
        /// 当前用户可进行项目审批的部门列表
        /// 
        /// 
        public ArrayList GetCanApproveDeptList()
        {
            
            return new Department().QueryDepts(GetEmployee.ID);
            
        }
        /// 
        /// 取得当前员工的部门列表
        /// 
        /// 
        /// 
        public Array GetEmDepartments()
        {
           
            DataTable dt=  _obj.GetDepartments( );
            string[] al = new string[1]    ;
            if(dt !=null)
            {
                al = new string[dt.Rows.Count];
                int i = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    al[i] = dr["DE_DEPT"].ToString();
                    i++;
                }
            }
            return al;
        }
        /// 
        /// 取得员工的部门列表
        /// 
        /// 
        /// 
        public DataTable GetDepartments(int empId)
        {
            return _obj.GetDepartments(empId);
        }
        /// 
        /// 取得部门的员工列表
        /// 
        /// 
        /// 
        
        public DataTable GetActiveEmpByDept(int deptId)
        {
            return _obj.GetActivedEmplyeesByDept(deptId);
        }
        public bool AddEmployee()
        {
            if(!_obj.ValidUser(_obj.em_account))
            {
                _obj.Add();
                return true;
            }
            return false;
        }
        public ArrayList OnLineUsers
        {
            get
            {
                return _obj.GetUsersOnLine();
            }
        }
        public void test()
        {
            int i = 0;
            while (i < 1000000)
            {
                i++;
                Console.WriteLine(string.Format("current user id is {0}", LoginInfo.UserId));
                Thread.Sleep(1000);
            }
        }
    }
    
   
}