using System; using System.Collections.Generic; using System.Text; using System.Linq; using YLShipBuildLandMap.Entity; using YLShipBuildLandMap.Entity.ViewModel; using System.Linq.Expressions; using Bowin.Common.Linq.Entity; using YLShipBuildLandMap.Entity.ViewModel.SystemSetting; using Bowin.Common.Linq; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using System.Security.Cryptography; using Microsoft.VisualBasic; using Z.EntityFramework.Plus; using YLShipBuildLandMap.Entity.Extensions; using Bowin.Common.Utility; namespace YLShipBuildLandMap.Services.SystemSetting { public class UserService : IUserService { private YLShipBuildLandMapContext DbContext { get; set; } public UserService(YLShipBuildLandMapContext dbContext) { DbContext = dbContext; } #region 生成相关View的查询 private IQueryable GetLoginUserQuerable(Expression> userExp) { var sql = from u in DbContext.SysUser.Where(userExp) from r in ( from role in DbContext.SysRole from userRole in role.SysUserSysRole where userRole.UserId == u.UserId select role ).Take(1).DefaultIfEmpty() select new LoginUser { UserID = u.UserId, LoginID = u.LoginId, UserName = u.Name, CurrentRoleID = r.RoleId, RoleName = r.RoleName, }; return sql; } private IQueryable GetUserViewQueryable(Expression> exp) { var sql = from u in DbContext.SysUser.Where(exp) select new UserView { UserID = u.UserId, CmWorkLinkAccountPsid = u.CmWorkLinkAccountPsid, LoginID = u.LoginId, Name = u.Name, RecordStatus = u.RecordStatus, FirstCode = YLShipBuildLandMapContext.GetPyFirst(u.Name) }; return sql; } private IQueryable GetAllUsersQueryable(Expression> exp) { var sql = from u in DbContext.SysUser.Where(exp) select new UserView { UserID = u.UserId, CmWorkLinkAccountPsid = u.CmWorkLinkAccountPsid, LoginID = u.LoginId, Name = u.Name, RecordStatus = u.RecordStatus, FirstCode = YLShipBuildLandMapContext.GetPyFirst(u.Name) }; return sql; } private IQueryable GetSysUserQueryable(Expression> exp) { var sql = from u in DbContext.SysUser.Where(exp) select new UserView { UserID = u.UserId, CmWorkLinkAccountPsid = u.CmWorkLinkAccountPsid, LoginID = u.LoginId, Name = u.Name, RecordStatus = u.RecordStatus, FirstCode = YLShipBuildLandMapContext.GetPyFirst(u.Name) }; return sql; } private IQueryable GetUserRoleViewQueryable(Expression> exp, Expression> dex) { var sql = from u in DbContext.SysUser.Where(exp) select new UserView { UserID = u.UserId, LoginID = u.LoginId, CmWorkLinkAccountPsid = u.CmWorkLinkAccountPsid, Name = u.Name, RecordStatus = u.RecordStatus, //RoleID=res.RoleId //RoleList = u.SysUserSysRole.Select(x => new RoleView //{ // RoleID = x.RoleId, // RoleName = x.Role.RoleName //}) }; return sql; } #endregion public IGridResultSet GetSysUserForSelect(Guid? prinicipalUserId, string key, int? pageIndex = 1, int? pageSize = 999, Guid?[] departIds = null, string roleName = null) { Expression> exp = (x => x.RecordStatus > 0); bool existUser = true; if (!string.IsNullOrEmpty(key)) exp = exp.And(x => x.Name.Contains(key)); if (prinicipalUserId != null && prinicipalUserId != Guid.Empty) { exp = exp.And(x => x.UserId == prinicipalUserId); existUser = false; } if (!string.IsNullOrEmpty(roleName)) { exp = exp.And(x => x.SysUserSysRole.Any(w => w.Role.RoleName.Contains(roleName))); } var sql = GetSysUserQueryable(exp); if (departIds != null && departIds.Length > 0 && existUser) sql = sql.Where(x => departIds.Contains(x.DepartmentID)); return sql.OrderBy(x => x.FirstCode).ToGridResultSet(pageIndex, pageSize); } public LoginUser Login(string loginID, string passMd5) { var sql = this.GetLoginUserQuerable(u => u.LoginId == loginID && u.Password == passMd5 && u.RecordStatus > 0); var idsql = this.GetLoginUserQuerable(u => u.CmWorkLinkAccountPsid == loginID && u.Password == passMd5 && u.RecordStatus > 0); var loginuser = sql.FirstOrDefault() != null ? sql.FirstOrDefault() : idsql.FirstOrDefault(); return loginuser; } public LoginUser GetLoginUserById(Guid userID) { var sql = this.GetLoginUserQuerable(u => u.UserId == userID); return sql.FirstOrDefault(); } public IGridResultSet GetUserViewsForSelect(string loginID, string name, int? pageIndex = null, int? pageSize = null, Guid?[] departIds = null, bool isAllUser = false) { Expression> exp = (x => x.RecordStatus > 0); if (!string.IsNullOrEmpty(loginID)) { exp = exp.And(x => x.LoginId.Contains(loginID)); } if (!string.IsNullOrEmpty(name)) { exp = exp.And(x => x.Name.Contains(name)); } IGridResultSet result = null; if (isAllUser) { result = GetAllUsersQueryable(exp).OrderBy(x => x.UserID).ToGridResultSet(pageIndex, pageSize); } return result; } public IGridResultSet GetUserViewList(string userName, Guid? roleID, int? recordStatus, List useridList, int? pageIndex = null, int? pageSize = null) { Expression> exp = (x => true); Expression> dex = (x => true); exp = exp.AndIf(useridList != null && useridList.Count > 0, e => useridList.Contains(e.UserId)); if (!string.IsNullOrEmpty(userName)) { exp = exp.And(x => x.Name.Contains(userName)); } if (roleID != null) { dex = dex.And(x => x.RoleId == roleID); var userIds = DbContext.SysUserSysRole.Where(dex).Select(x => x.UserId).ToList(); if (userIds.Count > 0) { exp = exp.And(x => userIds.Contains(x.UserId)); } } if (recordStatus.HasValue) { exp = exp.And(x => x.RecordStatus == recordStatus); } var query = GetUserRoleViewQueryable(exp, dex); var userList = query.Distinct().OrderBy(x => x.Name) .ToGridResultSet(pageIndex, pageSize); var userIDList = userList.rows.Select(x => (Guid?)x.UserID).ToList(); var roleList = DbContext.SysUserSysRole.Where(x => userIDList.Contains(x.UserId)).Select(x => new { UserID = x.UserId, RoleID = x.RoleId, RoleName = x.Role.RoleName }) .ToList(); foreach (var user in userList.rows) { user.RoleList = roleList.Where(x => x.UserID == user.UserID).Select(x => new RoleView { RoleID = x.RoleID, RoleName = x.RoleName }).ToList(); } return userList; } public UserView GetUserView(Guid userID) { return this.GetUserViewQueryable(x => x.UserId == userID).FirstOrDefault(); } public async Task Save(UserView userView, Guid operatorID) { DateTime nowTime = DateTime.Now; SysUser user = new SysUser(); if (userView.UserID.HasValue) { user = DbContext.SysUser .FirstOrDefault(x => x.UserId == userView.UserID); user.LoginId = userView.LoginID; user.Name = userView.Name; user.CmWorkLinkAccountPsid = userView.CmWorkLinkAccountPsid; user.RecordStatus = userView.RecordStatus; user.ModifyUserId = operatorID; user.ModifyTime = nowTime; this.DbContext.Update(user); } else { string pwd = "yldy@2211"; MD5 md5 = MD5.Create(); user.Password = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(pwd.Trim()))).Replace("-", null); user.UserId = Guid.NewGuid(); user.LoginId = userView.LoginID; user.Name = userView.Name; user.CmWorkLinkAccountPsid = userView.CmWorkLinkAccountPsid; user.RecordStatus = userView.RecordStatus; user.CreateUserId = operatorID; user.CreateTime = nowTime; this.DbContext.Add(user); } await this.DbContext.SaveChangesAsync(); } public async Task Delete(List userIDList) { using (var trans = DbContext.Database.BeginTransaction()) { try { foreach (Guid userId in userIDList) { } DbContext.SysUser.Where(x => userIDList.Contains(x.UserId)).Delete(); await this.DbContext.SaveChangesAsync(); trans.Commit(); } catch (Exception) { trans.Rollback(); throw new Exception("该账号已用于其他业务功能,不可删除,建议禁用该账户"); } } } public UserView GetUserViewByName(string loginID, string name) { //return this.GetUserViewQueryable(x => x.LoginId == loginID || x.Name == name.Trim()).FirstOrDefault(); //取消用户名唯一限制 return this.GetUserViewQueryable(x => x.LoginId == loginID).FirstOrDefault(); } public async Task UpdatePwd(Guid userID, string newPassword) { SysUser user = DbContext.SysUser.Where(x => x.UserId == userID).FirstOrDefault(); if (user.Password.Equals(newPassword)) { throw new Exception("新密码不可和旧密码相同!"); } user.Password = newPassword.ToUpper(); user.ModifyTime = DateTime.Now; user.ModifyUserId = userID; DbContext.Update(user); await DbContext.SaveChangesAsync(); } public async Task ResetPwd(Guid Id, Guid userID) { SysUser user = DbContext.SysUser.Where(x => x.UserId == Id).FirstOrDefault(); if (user == null) { throw new Exception("用户不存在"); } string pwd = "yldy@2211"; MD5 md5 = MD5.Create(); user.Password = BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(pwd.Trim()))).Replace("-", null); user.ModifyUserId = userID; user.ModifyTime = DateTime.Now; DbContext.Update(user); await DbContext.SaveChangesAsync(); } public List GetUserViewListByRoleName(string roleName) { var roleNames = roleName.Split(","); var curUserList = this.GetUserViewQueryable(x => x.RecordStatus > 0 && x.SysUserSysRole.Any(w => roleNames.Contains(w.Role.RoleName))).ToList(); return curUserList; } public LoginUser GetLoginUserByWeChatUserId(string weChatUserId) { var sql = this.GetLoginUserQuerable(u => u.WeChatUserId == weChatUserId); return sql.FirstOrDefault(); } public int SetWechatUser(Guid userId, string weChatUserId) { this.DbContext.SysUser.Where(u => u.UserId == userId && (string.IsNullOrEmpty(u.WeChatUserId) || u.WeChatUserId == weChatUserId)).Update(e => new SysUser { WeChatUserId = weChatUserId, WeChatLoginTime = DateTime.Now }); return this.DbContext.SaveChanges(); } public IGridResultSet GetUserRoleList() { var query = from a in DbContext.SysRole select new UserRoleView { RoleID = a.RoleId, RoleName = a.RoleName }; return query.Distinct().OrderBy(a => a.RoleName).ToGridResultSet(); } public void ChangePassword(string loginID, string password, string newPassword) { var md5Password = password.MD5(); var user = this.DbContext.SysUser.FirstOrDefault(x => x.LoginId == loginID && x.Password == md5Password && x.RecordStatus > 0); if (user != null) { user.Password = newPassword.MD5(); this.DbContext.SaveChanges(); } else { throw new Exception("原密码输入错误,请重新输入。"); } } public LoginUser GetLoginUserByCmOAId(string oaID) { var sql = this.GetLoginUserQuerable(u => u.CmWorkLinkAccountPsid == oaID && u.RecordStatus > 0); var loginuser = sql.FirstOrDefault(); return loginuser; } } }