123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- 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<LoginUser> GetLoginUserQuerable(Expression<Func<SysUser, bool>> 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<UserView> GetUserViewQueryable(Expression<Func<SysUser, bool>> 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<UserView> GetAllUsersQueryable(Expression<Func<SysUser, bool>> 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<UserView> GetSysUserQueryable(Expression<Func<SysUser, bool>> 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<UserView> GetUserRoleViewQueryable(Expression<Func<SysUser, bool>> exp, Expression<Func<SysUserSysRole, bool>> 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<UserView> GetSysUserForSelect(Guid? prinicipalUserId, string key, int? pageIndex = 1, int? pageSize = 999, Guid?[] departIds = null, string roleName = null)
- {
- Expression<Func<SysUser, bool>> 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<UserView> GetUserViewsForSelect(string loginID, string name, int? pageIndex = null, int? pageSize = null, Guid?[] departIds = null, bool isAllUser = false)
- {
- Expression<Func<SysUser, bool>> 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<UserView> result = null;
- if (isAllUser)
- {
- result = GetAllUsersQueryable(exp).OrderBy(x => x.UserID).ToGridResultSet(pageIndex, pageSize);
- }
- return result;
- }
- public IGridResultSet<UserView> GetUserViewList(string userName, Guid? roleID, int? recordStatus, List<Guid> useridList, int? pageIndex = null, int? pageSize = null)
- {
- Expression<Func<SysUser, bool>> exp = (x => true);
- Expression<Func<SysUserSysRole, bool>> 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<Guid> 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<UserView> 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<UserRoleView> 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;
- }
- }
- }
|