/// 
///INTERFACE CLASS FOR TABLE t_Employee
///By wm with codesmith. 
///on 04/18/2017
/// 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using DeiNiu.Utils;
namespace DeiNiu.wms.Data.Model
{
    [Serializable]
    public class Employee : Employee_base
    {
        public Employee()
        {
        }
        public Employee(int id): base(id)
        {
        }
        protected override void getImp()
        {
            model_imp = new Employee_Imp();
        }
        //begin cust db operation, query, excute sql etc.
        public DataSet QueryByName(string name)
        {
            cmdParameters[0] = name;
            return  CustQuery(600);
        }
        private ArrayList _departments;
        public ArrayList Departments
        {
            get
            {
                return _departments;
            }
            set
            {
                _departments = value;
            }
        }
        private List _authForms =new List();
        private List _authSpecials = new List();
        private Dictionary> _authMap  ; 
        public List AuthForms
        {
            get
            {
                return _authForms;
            }
            set
            {
                _authForms = value;
            }
        }
        public List AuthSpecials
        {
            get
            {
                return _authSpecials;
            }
            set
            {
                _authSpecials = value;
            }
        }
        public Dictionary> AuthMap { get
            {
                if( _authMap == null)
                {
                    _authMap = new Dictionary>();
                    string key200 = WmsConstants.PDA_MENU_UTILS;
                    string key300 = WmsConstants.PDA_MENU_STK_IN;
                    string key400 = WmsConstants.PDA_MENU_STK_OUT;
                    string key500 = WmsConstants.PDA_MENU_PLATE;
                 
                        if (!_authMap.ContainsKey(key200))
                        {
                             _authMap.Add(key200, new List());
                        }
                        if (!_authMap.ContainsKey(key300))
                        {
                            _authMap.Add(key300, new List());
                        }
                        if (!_authMap.ContainsKey(key400))
                        {
                            _authMap.Add(key400, new List());
                        }
                        if (!_authMap.ContainsKey(key500))
                        {
                            _authMap.Add(key500, new List());
                        }
                  
                      foreach (Authority au in AuthForms)
                        {
                            if (au.auth_order <= 200)
                            {
                                _authMap[key200].Add(au);
                            }
                            else if (au.auth_order < 400)
                            {  
                                  _authMap[key300].Add(au);
                             }
                            else if (au.auth_order < 500)
                            {
                            _authMap[key400].Add(au);
                         
                            }
                            else
                            {
                            _authMap[key500].Add(au);
                         
                            }
                        }
                    }
 
                return _authMap;
            }
            
            set => _authMap = value; }
        public DataTable GetActivedEmplyeesByDept(int deptId)
        {
            cmdParameters[0] = deptId;
            return CustQuery(100).Tables[0];
        }
        /// 
        /// 验证用户,成功则初始化该用户
        /// 
        /// 
        /// 
        /// 
        public int ValidUser(string userName, string passwd)
        {
            cmdParameters[0] = userName;
            cmdParameters[1] = passwd;
            DataTable dt = CustQuery(200).Tables[0];
            if (dt.Rows.Count == 1)
            {
                return Convert.ToInt32(dt.Rows[0]["id"].ToString());
 
            }
            return -1;
            
           
        }
        /// 
        /// 登录
        /// 
        /// 
        /// md5加密的密码 
        /// 
        public bool login(string userName, string passwd)
        {
            cmdParameters[0] = userName;
            cmdParameters[1] = passwd ;  
            DataTable dt = CustQuery(200).Tables[0];
            if (dt.Rows.Count == 1)
            {
              //  _id = Convert.ToInt32(dt.Rows[0]["id"].ToString());
                getModel(dt.Rows[0]);
                this.token = Util.Encrypt((_em_account + DateTime.Now));
                this.Update();
                return true;
            }
            return false;
        }
        /// 
        /// 根据token 登录
        /// 
        /// 
        /// 
        public bool login(string token)
        {
            cmdParameters[0] = token;
            
            DataTable dt = CustQuery(210).Tables[0];
            if (dt.Rows.Count == 1)
            {
               // _id = Convert.ToInt32(dt.Rows[0]["id"].ToString());
                getModel(dt.Rows[0]);
 
                return true;
            }
            return false;
        }
        /// 
        /// 验证用户,成功则初始化该用户
        /// 
        /// 
        ///  
        /// 
        public bool ValidUser(string userName)
        {
            cmdParameters[0] = userName;
            DataTable dt = CustQuery(201).Tables[0];
            return dt.Rows.Count > 0;
        }
        /// 
        /// 取 用户的lesson
        /// 
        /// 
        /// 
        public DataTable GetLessons(int empId)
        {
            cmdParameters[0] = empId;
            return CustQuery(300).Tables[0];
        }
        /// 
        /// 取用户所在部门的列表
        /// 
        /// 
        /// 
        public DataTable GetDepartments(int empId)
        {
            _departments = new ArrayList();
            cmdParameters[0] = empId;
            return CustQuery(400).Tables[0];
        }
        /// 
        /// 取用户所在部门的列表
        /// 
        /// 
        /// 
        public DataTable GetDepartments()
        {
            if (_id != 0)
                return GetDepartments(_id);
            return null;
        }
        /// 
        /// 新增用户,并保存默认部门,职务信息
        /// 
        public new void Add()
        {
            //         using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    base.Add();
                    DeptEmp deptemObj = new DeptEmp();
                    deptemObj.de_dept = Convert.ToInt32(_em_dept);
                    deptemObj.de_employee = _id;
                    deptemObj.de_title = _em_jobcode;
                    deptemObj.Add();
                    //                   trans.Complete();
                }
                catch (MyException)
                {
                    throw;
                }
                catch (Exception er)
                {
                    throw er;
                    // throw new Exception("出现异常,保存失败。");
                }
            }
        }
        public new void Update()
        {
            base.Update();
        }
        public new void Delete()
        {
            base.Delete();
            DeptEmp de = new DeptEmp();
            de.Delete(Convert.ToInt32(_id));
        }
        /// 
        /// 在线统计
        /// 
        /// 
        public ArrayList GetUsersOnLine()
        {
            ArrayList al = new ArrayList();
            DataTable dt = CustQuery(500).Tables[0];
            foreach (DataRow drow in dt.Rows)
            {
                al.Add(drow[0].ToString());
            }
            return al;
        }
        private string _extrMsg = "";
        public string extrMsg
        {
            get { return _extrMsg; }
            set { _extrMsg = value; }
        }
    }
}