namespace BLL { using System; using System.Collections.Generic; using System.Linq; using System.Web.Security; using System.Web.UI.WebControls; using Model; public static class Sys_UserService { public static Model.SGGLDB db = Funs.DB; /// /// 记录数 /// private static int count { get; set; } /// /// 定义变量 /// private static IQueryable qq = from x in db.Sys_User orderby x.UserCode select x; /// /// 用户登陆方法 /// /// 登陆名 /// 未加密密码 /// 记住我开关 /// 调用页面 /// 是否登陆成功 public static bool UserLogOn(string account, string password, bool rememberMe, System.Web.UI.Page page) { bool IsSuccess = false; var q = from y in Funs.DB.Sys_User where y.Account == account select y; List x = null; if (q.Count() > 0) { x = (from y in Funs.DB.Sys_User where y.Account == account && y.IsPost == true && y.Password == EncryptionPassword(password) select y).ToList(); if (x.Any()) { FormsAuthentication.SetAuthCookie(account, false); Model.Sys_User s = x.First(); page.Session[SessionName.CurrUser] = s; if (rememberMe) { System.Web.HttpCookie u = new System.Web.HttpCookie("UserInfo"); u["username"] = account; //u["password"] = null; // Cookies过期时间设置为一年. u.Expires = DateTime.Now.AddYears(1); page.Response.Cookies.Add(u); } else { // 当选择不提交用户名时,Cookies过期时间设置为昨天. page.Response.Cookies["UserInfo"].Expires = DateTime.Now.AddDays(-1); } IsSuccess = true; //page.Application.Lock(); //page.Application["IsValiad"] = true; //page.Application.UnLock(); } } return IsSuccess; } /// /// 登陆获取用户信息 /// /// /// /// /// /// public static bool ADUserLogOn(string account, bool rememberMe, System.Web.UI.Page page) { bool IsSuccess = false; var user = Funs.DB.Sys_User.FirstOrDefault(x => x.Account == account && x.IsPost == true); if (user != null) { FormsAuthentication.SetAuthCookie(account, false); page.Session[SessionName.CurrUser] = user; if (rememberMe) { System.Web.HttpCookie u = new System.Web.HttpCookie("UserInfo"); u["username"] = account; // u["password"] = null; // Cookies过期时间设置为一年. u.Expires = DateTime.Now.AddYears(1); page.Response.Cookies.Add(u); } else { // 当选择不提交用户名时,Cookies过期时间设置为昨天. page.Response.Cookies["UserInfo"].Expires = DateTime.Now.AddDays(-1); } IsSuccess = true; } return IsSuccess; } /// /// 加密密码 /// /// 加密前的密码 /// 加密后的密码 public static string EncryptionPassword(string password) { return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5"); } /// /// 获取用户初始密码 /// /// 单位id /// 手机号码 /// public static string GetEncryptionPassword(string unitId,string phone) { string passNo =string.Empty; var unit = BLL.Base_UnitService.GetUnit(unitId); if (unit != null && !string.IsNullOrEmpty(unit.UnitCode)) { passNo = unit.UnitCode; } passNo += '.'; if (!string.IsNullOrEmpty(phone) && phone.Length > 4) { passNo += phone.Substring(phone.Length - 4); } else { passNo += "1234"; } return EncryptionPassword(passNo); } /// /// 获取用户信息 /// /// 用户Id /// 用户信息 public static Model.Sys_User GetUsersByUserId(string userId) { Model.Sys_User m = Funs.DB.Sys_User.FirstOrDefault(e => e.UserId == userId); return m; } /// /// 获取用户账号是否存在 /// /// 用户id /// 账号 /// 项目ID /// 是否存在 public static bool IsExistUserAccount(string userId, string account) { bool isExist = false; if (!string.IsNullOrEmpty(userId)) { var user = Funs.DB.Sys_User.FirstOrDefault(x => x.Account == account && x.UserId != userId); if (user != null) { isExist = true; } } else { var user = Funs.DB.Sys_User.FirstOrDefault(x => x.Account == account); if (user != null) { isExist = true; } } return isExist; } /// /// 获取同单位用户名称是否存在 /// /// 用户id /// 账号 /// 项目ID /// 是否存在 public static bool IsExistUserName(string userId, string userName,string unitId) { bool isExist = false; var name = Funs.DB.Sys_User.FirstOrDefault(x => x.UserName == userName && x.UnitId == unitId && x.UserId != userId); if (name != null) { isExist = true; } return isExist; } /// /// 根据部门获取用户信息 /// /// /// public static List GetUsersByDep(string departId) { return (from x in Funs.DB.Sys_User where x.DepartId == departId select x).ToList(); } /// /// 根据部门Id筛选用户信息,用户名模糊查询所有用户信息 /// /// 用户名 /// public static List GetUsersByRoleId(string userName, string roleId, string projectId) { return (from x in Funs.DB.Sys_User where x.UserName.Contains(userName) && x.RoleId == roleId select x).ToList(); } /// /// 根据用户名模糊查询所有用户信息 /// /// 用户名 /// public static List GetUsersByUserNameAndProjectId(string userName, string projectId) { return (from x in Funs.DB.Sys_User where x.UserName.Contains(userName) && x.IsHeadMan == false select x).ToList(); } /// /// 根据角色,用户名模糊查询所有用户信息 /// /// 用户名 /// public static List GetUsersByUserName(string userName, string roleId) { return (from x in Funs.DB.Sys_User where x.UserName.Contains(userName) && x.RoleId == roleId select x).ToList(); } /// /// 根据用户Id查询所有用户的数量 /// /// 用户Id /// 用户的数量 public static int GetUserCount(string userId) { var q = (from x in Funs.DB.Sys_User where x.UserId == userId select x).ToList(); return q.Count(); } /// /// 根据用户获取密码 /// /// /// public static string GetPasswordByUserId(string userId) { Model.Sys_User m = Funs.DB.Sys_User.FirstOrDefault(e => e.UserId == userId); return m.Password; } /// /// 根据用户ID获取名称 /// /// /// public static string GetUserNameByUserId(string userId) { string userName = string.Empty; Model.Sys_User m = Funs.DB.Sys_User.FirstOrDefault(e => e.UserId == userId); if (m != null) { userName = m.UserName; } return userName; } /// /// 修改密码 /// /// /// public static void UpdatePassword(string userId, string password) { Model.SGGLDB db = Funs.DB; Model.Sys_User m = db.Sys_User.FirstOrDefault(e => e.UserId == userId); if (m != null) { m.Password = password; db.SubmitChanges(); } } /// /// 增加人员信息 /// /// 人员实体 public static void AddUser(Model.Sys_User user) { Model.SGGLDB db = Funs.DB; Model.Sys_User newUser = new Model.Sys_User(); newUser.UserId = user.UserId; newUser.Account = user.Account; newUser.UserCode = user.UserCode; newUser.Password = user.Password; newUser.UserName = user.UserName; newUser.UnitId = user.UnitId; newUser.RoleId = user.RoleId; newUser.IsPost = user.IsPost; newUser.DepartId = user.DepartId; newUser.Email = user.Email; newUser.EmailPassword = user.EmailPassword; newUser.AllowLoginSystem = user.AllowLoginSystem; newUser.Phone = user.Phone; newUser.SignatureUrl = user.SignatureUrl; db.Sys_User.InsertOnSubmit(newUser); db.SubmitChanges(); } /// /// 修改人员信息 /// /// 人员实体 public static void UpdateUser(Model.Sys_User user) { Model.SGGLDB db = Funs.DB; Model.Sys_User newUser = db.Sys_User.FirstOrDefault(e => e.UserId == user.UserId); if (newUser != null) { newUser.Account = user.Account; newUser.UserCode = user.UserCode; //newUser.Password = user.Password; newUser.UserName = user.UserName; newUser.UnitId = user.UnitId; newUser.RoleId = user.RoleId; newUser.IsPost = user.IsPost; newUser.DepartId = user.DepartId; newUser.Email = user.Email; if (!String.IsNullOrEmpty(user.EmailPassword)) { newUser.EmailPassword = user.EmailPassword; } newUser.AllowLoginSystem = user.AllowLoginSystem; newUser.Phone = user.Phone; newUser.SignatureUrl = user.SignatureUrl; db.SubmitChanges(); } } /// /// 根据人员Id删除一个人员信息 /// /// public static void DeleteUser(string userId) { Model.SGGLDB db = Funs.DB; Model.Sys_User user = db.Sys_User.FirstOrDefault(e => e.UserId == userId); if (user != null) { var userButtonPower = from x in db.Project_UserButtonPower where x.UserId == userId select x; if (userButtonPower.Count() > 0) { db.Project_UserButtonPower.DeleteAllOnSubmit(userButtonPower); db.SubmitChanges(); } var project_UserPower = from x in db.Project_UserPower where x.UserId == userId select x; if (project_UserPower.Count() > 0) { db.Project_UserPower.DeleteAllOnSubmit(project_UserPower); db.SubmitChanges(); } db.Sys_User.DeleteOnSubmit(user); db.SubmitChanges(); } } /// /// 根据角色获得用户数 /// /// 角色 /// public static int GetUserCountByRole(string role) { var q = (from x in Funs.DB.Sys_User where x.RoleId == role select x).ToList(); return q.Count(); } /// /// 根据部门主键获得用户数 /// /// 部门主键 /// 用户数 public static int GetUserCountByDepartId(string departId) { var q = (from x in Funs.DB.Sys_User where x.DepartId == departId select x).ToList(); return q.Count(); } /// /// 根据单位主键获得用户 /// /// 单位主键 /// public static List GetUsersByUnitId(string unitId) { var q = (from x in Funs.DB.Sys_User where x.UnitId == unitId select x).ToList(); return q; } /// /// 根据单位主键获得在岗用户 /// /// 单位主键 /// public static List GetIsPostUsersByUnitId(string unitId, string projectId) { var q = (from x in Funs.DB.Sys_User join y in Funs.DB.Project_User on x.UserId equals y.UserId where y.ProjectId == projectId && x.UnitId == unitId && x.IsPost == true orderby x.UserName select x).ToList(); return q; } /// /// 根据帐号获取用户信息 /// /// 帐号 /// 用户信息 public static Model.Sys_User GetUserByAccount(string account) { Model.Sys_User m = Funs.DB.Sys_User.FirstOrDefault(e => e.IsHeadMan == true && e.Account == account); return m; } /// /// 根据用户主键判断是否分包商业务管理员角色 /// /// /// public static bool GetIsGLByUserId(string userId) { bool isGL = false; Model.Sys_User m = Funs.DB.Sys_User.FirstOrDefault(e => e.UserId == userId); if (m != null) { if (m.RoleId == BLL.Const.GL) { isGL = true; } } return isGL; } /// /// 根据部门获得所有对应在岗的用户且参与流程 /// /// public static ListItem[] GetUserListByDepartId(string departId) { var q = (from x in Funs.DB.Sys_User join y in Funs.DB.Sys_Role on x.RoleId equals y.RoleId where x.IsPost == true && y.IsAuditFlow == true && x.DepartId == departId orderby x.UserName select x).ToList(); ListItem[] lis = new ListItem[q.Count()]; for (int i = 0; i < q.Count(); i++) { lis[i] = new ListItem(q[i].UserName ?? "", q[i].UserId.ToString()); } return lis; } /// /// 查询所有在岗的用户 /// /// public static ListItem[] GetGlUserList() { var q = (from x in Funs.DB.Sys_User where x.IsPost == true && x.RoleId == BLL.Const.GL orderby x.UserName select x).ToList(); ListItem[] lis = new ListItem[q.Count()]; for (int i = 0; i < q.Count(); i++) { lis[i] = new ListItem(q[i].UserName ?? "", q[i].UserId.ToString()); } return lis; } /// /// 查询所有在岗的用户 /// /// public static List GetUserList() { return (from x in Funs.DB.Sys_User where x.IsPost == true orderby x.UserName select x).ToList(); } /// /// 查询所有在岗的用户 /// /// public static List GetUserListByProjectId(string projectId) { var pUserId = from x in Funs.DB.Project_User where x.ProjectId == projectId select x.UserId; var q = (from x in Funs.DB.Sys_User where x.IsPost == true && (x.UserId == BLL.Const.GlyId || pUserId.Contains(x.UserId)) orderby x.UserName select x).ToList(); return q; } /// /// 获取具有流程审批的用户 /// /// public static List GetUserListHasApprove(string projectId) { var q = (from x in Funs.DB.Sys_User join y in Funs.DB.Project_User on x.UserId equals y.UserId join z in Funs.DB.Sys_Role on x.RoleId equals z.RoleId join r in Funs.DB.Sys_Role on y.RoleId equals r.RoleId where y.ProjectId == projectId && x.IsPost == true && (z.IsAuditFlow == true || r.IsAuditFlow == true) orderby x.UserName select x).ToList(); return q; } /// /// 根据角色获得所有对应在岗的用户 /// /// public static List GetUserListByRoleId(string projectId, string roleId) { var q = (from x in Funs.DB.Sys_User join y in Funs.DB.Project_User on x.UserId equals y.UserId where y.ProjectId == projectId && (x.RoleId == roleId || y.RoleId == roleId) orderby x.UserName select x).ToList(); return q; } /// /// 根据角色获得最大编号的在岗用户 /// /// public static Model.Sys_User GetMaxUserByRoleId(string roleId) { return (from x in Funs.DB.Sys_User where x.IsPost == true && x.RoleId == roleId && (x.UserCode != "" && x.UserCode != null) orderby x.UserCode descending select x).FirstOrDefault(); } /// /// 获取检测单位的用户 /// /// public static ListItem[] GetUserListByUnitType(string projectId, string unitType) { var q = (from x in Funs.DB.Sys_User join y in Funs.DB.Project_User on x.UserId equals y.UserId join z in Funs.DB.Project_Unit on x.UnitId equals z.UnitId where (x.IsPost == true && y.ProjectId == projectId && z.UnitType == unitType) orderby x.UserName select x).ToList(); ListItem[] lis = new ListItem[q.Count()]; for (int i = 0; i < q.Count(); i++) { lis[i] = new ListItem(q[i].UserName ?? "", q[i].UserId.ToString()); } return lis; } /// /// /// /// public static List GetRecipientList(string projectId) { return (from x in Funs.DB.Sys_User join y in Funs.DB.Project_User on x.UserId equals y.UserId where x.Account != BLL.Const.Gly && x.Account != BLL.Const.GlyId && y.ProjectId==projectId orderby x.UserName select x).ToList(); } public static Model.Sys_User GetUsersByUserName(string userName) { return Funs.DB.Sys_User.FirstOrDefault(e => e.UserName == userName); } } }