using System; using System.Collections.Generic; using System.Collections; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Text; namespace RoadFlow.Platform { public class Users { /// /// 前缀 /// public const string PREFIX = "u_"; private RoadFlow.Data.Interface.IUsers dataUsers; public Users() { this.dataUsers = Data.Factory.Factory.GetUsers(); } /// /// 新增 /// public int Add(RoadFlow.Data.Model.Users model) { return dataUsers.Add(model); } /// /// 更新 /// public int Update(RoadFlow.Data.Model.Users model) { return dataUsers.Update(model); } /// /// 查询所有记录 /// public List GetAll() { return dataUsers.GetAll(); } /// /// 查询单条记录 /// public FangYar.Model.TBL.TBL_SYS_USERS_Model Get(string id) { FangYar.BLL.TBL.SysUsersBLL userbll = new FangYar.BLL.TBL.SysUsersBLL(); return userbll.GetModelByUID(id.Replace("u_","")); } /// /// 删除 /// public int Delete(string id) { return dataUsers.Delete(id); } /// /// 查询记录条数 /// public long GetCount() { return dataUsers.GetCount(); } /// /// 根据帐号查询一条记录 /// public RoadFlow.Data.Model.Users GetByAccount2(string Account2) { return Account2.IsNullOrEmpty() ? null : dataUsers.GetByAccount2(Account2); } /// /// 得到系统初始密码 /// /// public string GetInitPassword() { return RoadFlow.Utility.Config.SystemInitPassword; } /// /// 得到加密后的密码字符串 /// /// /// public string EncryptionPassword(string password) { if (password.IsNullOrEmpty()) { return ""; } RoadFlow.Utility.HashEncrypt hash = new RoadFlow.Utility.HashEncrypt(); return hash.MD5System(hash.MD5System(password)); //hash.SHA512Encrypt(hash.SHA512Encrypt(password.Trim())); } /// /// 得到一个用户加密后的密码 /// /// /// /// public string GetUserEncryptionPassword(string userID, string password) { return password.IsNullOrEmpty() || userID.IsNullOrEmpty() ? "" : EncryptionPassword(userID.Trim().ToLower() + password.Trim()); } /// /// 初始化一个用户密码 /// /// /// public bool InitPassword(Guid userID) { return dataUsers.UpdatePassword(GetUserEncryptionPassword(userID.ToString(), GetInitPassword()), userID); } /// /// 查询一个岗位下所有人员 /// /// /// public List GetAllByOrganizeID(string organizeID) { return dataUsers.GetAllByOrganizeID(organizeID); } /// /// 得到一个用户的所有岗位 /// /// /// Dictionary Guid 岗位ID bool 是否主要岗位 public Dictionary GetAllStation(string userID) { UsersRelation ur = new UsersRelation(); var urs = ur.GetAllByUserID(userID); Dictionary dict = new Dictionary(); foreach (var u in urs) { if (!dict.ContainsKey(u.OrganizeID)) { dict.Add(u.OrganizeID, u.IsMain == 1); } } return dict; } /// /// 得到一个用户的主要岗位 /// /// /// public string GetMainStation(string userID) { var ur = new UsersRelation().GetMainByUserID(userID); return ur == null ? "" : ur.OrganizeID; } /// /// 查询一组岗位下所有人员 /// /// /// public List GetAllByOrganizeIDArray(string[] organizeIDArray) { return dataUsers.GetAllByOrganizeIDArray(organizeIDArray); } ///// ///// 得到一个用户所在部门 ///// ///// ///// //public RoadFlow.Data.Model.Organize GetDeptByUserID(string userID) //{ // string stationID =GetMainStation(userID); // if(stationID=="") // { // return null; // } // var parents = new RoadFlow.Platform.Organize().GetAllParent(stationID); // parents.Reverse(); // foreach (var parent in parents) // { // if (parent.Type == 2 || parent.Type==1) // { // return parent; // } // } // return null; //} /// /// 得到一个用户所在部门 /// /// /// public FangYar.Model.TBL.TBL_SYS_EMP_Model GetDeptByUserID(string userID) { FangYar.BLL.TBL.SysEmpBLL empBll = new FangYar.BLL.TBL.SysEmpBLL(); return empBll.GetModelByUserID("", userID); //string stationID =GetMainStation(userID); //if(stationID=="") //{ // return null; //} //var parents = new RoadFlow.Platform.Organize().GetAllParent(stationID); //parents.Reverse(); //foreach (var parent in parents) //{ // if (parent.Type == 2 || parent.Type==1) // { // return parent; // } //} //return null; } /// /// 当前用户机构ID /// public static string CurrentOrgID { get { var org = new Users().GetDeptByUserID(CurrentUserID); return org == null ? "" : org.ORG_ID; } } /// /// 当前用户机构名称 /// public static string CurrentOrgName { get { var org = new Users().GetDeptByUserID(CurrentUserID); return org == null ? "" : (org.ORG_NAME == null ? "" : org.ORG_NAME); } } /// /// 当前用户部门ID /// public static string CurrentDeptID { get { var dept = new Users().GetDeptByUserID(CurrentUserID); return dept == null ? "" : dept.DEPT_ID; } } /// /// 当前用户部门名称 /// public static string CurrentDeptName { get { var dept = new Users().GetDeptByUserID(CurrentUserID); return dept == null ? "" : dept.DEPT_NAME; } } /// /// 当前用户手机号 /// public static string CurrentMobile { get { var emp = new Users().GetDeptByUserID(CurrentUserID); return emp == null ? "" : (emp.EMP_MOBILE == null ? "" : emp.EMP_MOBILE); } } /// /// 检查帐号是否重复 /// /// 帐号 /// 人员ID(此人员除外) /// public bool HasAccount2(string Account2, string userID = "") { return Account2.IsNullOrEmpty() ? false : dataUsers.HasAccount2(Account2.Trim(), userID); } /// /// 修改用户密码 /// /// 明文的密码 /// /// public bool UpdatePassword(string password, Guid userID) { return password.IsNullOrEmpty() ? false : dataUsers.UpdatePassword(GetUserEncryptionPassword(userID.ToString(), password.Trim()), userID); } /// /// 得到当前登录用户ID /// public static string CurrentUserID { get { try { object session = null; if (System.Web.HttpContext.Current.Session != null) { session = System.Web.HttpContext.Current.Session[RoadFlow.Utility.Keys.SessionKeys.UserID.ToString()]; if (session == null) { string kn_root_cookie = System.Web.HttpContext.Current.Request.Cookies["kn_root_cookie"].Value; JObject obj = (JObject)JsonConvert.DeserializeObject(kn_root_cookie); string usersUid = (string)obj["usersUid"]; return usersUid == null ? "" : usersUid; } } return session == null ? "" : session.ToString(); } catch { return ""; } } } /// /// 得到当前登录用户 /// public static FangYar.Model.TBL.TBL_SYS_USERS_Model CurrentUser { get { try { object obj = System.Web.HttpContext.Current.Session[RoadFlow.Utility.Keys.SessionKeys.User.ToString()]; if (obj == null) { string userID = CurrentUserID; if (userID == null || userID == "") { return null; } else { FangYar.BLL.TBL.SysUsersBLL userbll = new FangYar.BLL.TBL.SysUsersBLL(); var user = userbll.GetModelByUID(userID); if (user != null) { System.Web.HttpContext.Current.Session[RoadFlow.Utility.Keys.SessionKeys.User.ToString()] = user; } return user; } } else { return obj as FangYar.Model.TBL.TBL_SYS_USERS_Model; } } catch (Exception ex) { return null; } } } /// /// 当前用户名称 /// [Obsolete("由于该方法拼写错误,请使用CurrentUserName属性")] public static string CurrentUsreName { get { return CurrentUser == null ? "" : CurrentUser.USERS_NAME; } } /// /// 当前用户名称 /// public static string CurrentUserName { get { return CurrentUser == null ? "" : CurrentUser.USERS_NAME; } } /// /// 当前用户的所有角色 /// public static List CurrentUserRoles { get { string userID = CurrentUserID; if (userID == null || userID == "") { return new List(); } List list = new List(); var userRoles = new UsersRole().GetByUserIDFromCache(userID); foreach (var userRole in userRoles) { list.Add(userRole.RoleID); } return list; } } /// /// 得到一个不重复的帐号 /// /// /// public string GetAccount2(string Account2) { if (Account2.IsNullOrEmpty()) { return ""; } string newAccount2 = Account2.Trim(); int i = 0; while (HasAccount2(newAccount2)) { newAccount2 += (++i).ToString(); } return newAccount2; } /// /// 更新排序 /// public int UpdateSort(Guid userID, int sort) { return dataUsers.UpdateSort(userID, sort); } /// /// 根据ID得到名称 /// /// /// public string GetName(string id) { var user = Get(id); return user == null ? "" : user.USERS_NAME; } /// /// 去除ID前缀 /// /// /// public static string RemovePrefix(string id) { return id.IsNullOrEmpty() ? "" : id.Replace(PREFIX, ""); } /// /// 去除ID前缀 /// /// /// public string RemovePrefix1(string id) { return id.IsNullOrEmpty() ? "" : id.Replace(PREFIX, ""); } /// /// 得到一个人员的主管 /// /// /// public string GetLeader(string userID) { var mainStation = GetMainStation(userID); if (mainStation == null) { return ""; } RoadFlow.Platform.Organize borg = new Organize(); var station = borg.Get(mainStation); if (station == null) { return ""; } if (!station.Leader.IsNullOrEmpty()) { return station.Leader; } var parents = borg.GetAllParent(station.Number2); foreach (var parent in parents) { if (!parent.Leader.IsNullOrEmpty()) { return parent.Leader; } } return ""; } /// /// 得到一个人员的分管领导 /// /// /// public string GetChargeLeader(string userID) { var mainStation = GetMainStation(userID); if (mainStation == null) { return ""; } RoadFlow.Platform.Organize borg = new Organize(); var station = borg.Get(mainStation); if (station == null) { return ""; } if (!station.ChargeLeader.IsNullOrEmpty()) { return station.ChargeLeader; } var parents = borg.GetAllParent(station.Number2); foreach (var parent in parents) { if (!parent.ChargeLeader.IsNullOrEmpty()) { return parent.ChargeLeader; } } return ""; } /// /// 判断一个人员是否是部门主管 /// /// /// public bool IsLeader(string userID) { string leader = GetLeader(userID); return leader.Contains(userID.ToString(), StringComparison.CurrentCultureIgnoreCase); } /// /// 判断一个人员是否是部门分管领导 /// /// /// public bool IsChargeLeader(string userID) { string leader = GetChargeLeader(userID); return leader.Contains(userID.ToString(), StringComparison.CurrentCultureIgnoreCase); } /// /// 判断一个人员是否在一个组织机构字符串里 /// /// /// /// public bool IsContains(string userID, string memberString) { if (memberString.IsNullOrEmpty()) { return false; } var user = new Organize().GetAllUsers(memberString).Find(p => p.USERS_UID == userID); return user != null; } } public class UsersEqualityComparer : IEqualityComparer { public bool Equals(FangYar.Model.TBL.TBL_SYS_USERS_Model user1, FangYar.Model.TBL.TBL_SYS_USERS_Model user2) { return user1 == null || user2 == null || user1.USERS_UID == user2.USERS_UID; } public int GetHashCode(FangYar.Model.TBL.TBL_SYS_USERS_Model user) { return user.ToString().GetHashCode(); } } }