123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- using Attribute;
- using Base;
- using Common;
- using Dto;
- using Extensions;
- using Feign;
- using Infrastructure;
- using IPTools.Core;
- using Model;
- using Model.Base;
- using Repository;
- using Service;
- using System.Collections;
- using Vo;
- namespace Services
- {
- /// <summary>
- /// 系统用户
- /// </summary>
- [AppService(ServiceType = typeof(ISysUserService), ServiceLifetime = LifeTime.Transient)]
- public class SysUserService : BaseService<SysUser>, ISysUserService
- {
- private readonly ISysRoleService RoleService;
- private readonly ISysPostService PostService;
- private readonly ISysUserRoleService UserRoleService;
- private readonly ISysUserPostService UserPostService;
- public SysUserService(
- ISysRoleService sysRoleService,
- ISysPostService sysPostService,
- ISysUserRoleService userRoleService,
- ISysUserPostService userPostService)
- {
- RoleService = sysRoleService;
- PostService = sysPostService;
- UserRoleService = userRoleService;
- UserPostService = userPostService;
- }
- /// <summary>
- /// 根据条件分页查询用户列表
- /// </summary>
- /// <returns></returns>
- public PagedInfo<SysUser> SelectUserList(SysUserQueryDto user, PagerInfo pager)
- {
- var exp = Expressionable.Create<SysUser>();
- exp.AndIF(!string.IsNullOrEmpty(user.username), u => u.username.Contains(user.username));
- exp.AndIF(!string.IsNullOrEmpty(value: user.nickname), u => u.username.Contains(user.nickname));
- exp.AndIF(!string.IsNullOrEmpty(user.name), u => u.username.Contains(user.name));
- exp.AndIF(!string.IsNullOrEmpty(user.phone), u => u.username.Contains(user.phone));
- exp.And(u => u.DelFlag == "0");
- var query = Queryable()
- .Where(exp.ToExpression())
- .OrderByDescending(m => m.userId);
- PagedInfo<SysUser> result = query.ToPage(pager);
- foreach(SysUser sub in result.Records)
- {
- List<long> userRoles = UserRoleService.GetList(m => m.UserId == sub.userId).Select(m => m.RoleId).ToList();
- sub.RoleList = RoleService.GetList(m => userRoles.Contains(m.roleId));
- List<long> userPost = UserPostService.GetList(m => m.UserId == sub.userId).Select(m => m.PostId).ToList();
- sub.PostList = PostService.GetList(m => userPost.Contains(m.postId));
- }
- return result;
- }
- /// <summary>
- /// 通过用户ID查询用户
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- public SysUser SelectUserById(long userId)
- {
- var user = Queryable().Filter(null, true).WithCache(60 * 5)
- .Where(f => f.userId == userId).First();
- // if (user != null && user.UserId > 0)
- // {
- // user.Roles = RoleService.SelectUserRoleListByUserId(userId);
- // user.RoleIds = user.Roles.Select(x => x.RoleId).ToArray();
- // }
- return user;
- }
- /// <summary>
- /// 校验用户名称是否唯一
- /// </summary>
- /// <param name="userName"></param>
- /// <returns></returns>
- public string CheckUserNameUnique(string userName)
- {
- int count = Count(it => it.username == userName);
- if (count > 0)
- {
- return UserConstants.NOT_UNIQUE;
- }
- return UserConstants.UNIQUE;
- }
- /// <summary>
- /// 新增保存用户信息
- /// </summary>
- /// <param name="sysUser"></param>
- /// <returns></returns>
- public SysUser InsertUser(SysUser sysUser)
- {
- sysUser.CreateTime = DateTime.Now;
- sysUser.UpdateTime = DateTime.Now;
- var result = UseTran(() =>
- {
- sysUser.userId = Insertable(sysUser).ExecuteReturnIdentity();
- //新增用户角色信息
- UserRoleService.InsertUserRole(sysUser);
- //新增用户岗位信息
- UserPostService.InsertUserPost(sysUser);
- });
- if (!result.IsSuccess)
- {
- throw new Exception("提交数据异常," + result.ErrorMessage, result.ErrorException);
- }
- return sysUser;
- }
- /// <summary>
- /// 修改用户信息
- /// </summary>
- /// <param name="user"></param>
- /// <returns></returns>
- public int UpdateUser(SysUser user)
- {
- var roleIds = RoleService.SelectUserRoles(user.userId);
- var diffArr = roleIds.Where(c => !((IList)user.RoleIds).Contains(c)).ToArray();
- var diffArr2 = user.RoleIds.Where(c => !((IList)roleIds).Contains(c)).ToArray();
- bool result = UseTran2(() =>
- {
- if (diffArr.Length > 0 || diffArr2.Length > 0)
- {
- //删除用户与角色关联
- UserRoleService.DeleteUserRoleByUserId((int)user.userId);
- //新增用户与角色关联
- UserRoleService.InsertUserRole(user);
- }
- // 删除用户与岗位关联
- UserPostService.Delete(user.userId);
- // 新增用户与岗位管理
- UserPostService.InsertUserPost(user);
- ChangeUser(user);
- });
- return result ? 1 : 0;
- }
- public int ChangeUser(SysUser user)
- {
- user.UpdateTime = DateTime.Now;
- return Update(user, true);
- }
- /// <summary>
- /// 重置密码
- /// </summary>
- /// <param name="userid"></param>
- /// <param name="password"></param>
- /// <returns></returns>
- public int ResetPwd(long userid, string password)
- {
- return Update(new SysUser() { userId = userid, password = password }, it => new { it.password }, f => f.userId == userid);
- }
- /// <summary>
- /// 修改用户状态
- /// </summary>
- /// <param name="user"></param>
- /// <returns></returns>
- public int ChangeUserStatus(SysUser user)
- {
- CheckUserAllowed(user);
- return Update(user, it => new { it.DelFlag }, f => f.userId == user.userId);
- }
- /// <summary>
- /// 删除用户
- /// </summary>
- /// <param name="userid"></param>
- /// <returns></returns>
- public int DeleteUser(long userid)
- {
- CheckUserAllowed(new SysUser() { userId = userid });
- //删除用户与角色关联
- UserRoleService.DeleteUserRoleByUserId((int)userid);
- // 删除用户与岗位关联
- UserPostService.Delete(userid);
- return Update(new SysUser() { userId = userid, DelFlag = "2" }, it => new { it.DelFlag }, f => f.userId == userid);
- }
- /// <summary>
- /// 修改用户头像
- /// </summary>
- /// <param name="user"></param>
- /// <returns></returns>
- public int UpdatePhoto(SysUser user)
- {
- return Update(user, it => new { it.avatar }, f => f.userId == user.userId); ;
- }
- /// <summary>
- /// 注册用户
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- public SysUser Register(RegisterDto dto)
- {
- if (!Tools.PasswordStrength(dto.Password))
- {
- throw new CustomException("密码强度不符合要求");
- }
- if (!Tools.CheckUserName(dto.Username))
- {
- throw new CustomException("用户名不符合要求");
- }
- //密码md5
- string password = Function.MD532(dto.Password);
- var ip_info = IpTool.Search(dto.UserIP);
- SysUser user = new()
- {
- CreateTime = DateTime.Now,
- username = dto.Username,
- nickname = dto.Username,
- password = password,
- deptId = 0,
- };
- if (UserConstants.NOT_UNIQUE.Equals(CheckUserNameUnique(dto.Username)))
- {
- throw new CustomException($"保存用户{dto.Username}失败,注册账号已存在");
- }
- user.userId = Insertable(user).ExecuteReturnIdentity();
- return user;
- }
- /// <summary>
- /// 校验角色是否允许操作
- /// </summary>
- /// <param name="user"></param>
- public void CheckUserAllowed(SysUser user)
- {
- if (user.IsAdmin())
- {
- throw new CustomException("不允许操作超级管理员角色");
- }
- }
- /// <summary>
- /// 校验用户是否有数据权限
- /// </summary>
- /// <param name="userid"></param>
- /// <param name="loginUserId"></param>
- public void CheckUserDataScope(long userid, long loginUserId)
- {
- if (!SysUser.IsAdmin(loginUserId))
- {
- SysUser user = new SysUser() { userId = userid };
- //TODO 判断用户是否有数据权限
- }
- }
- /// <summary>
- /// 导入数据
- /// </summary>
- /// <param name="users"></param>
- /// <returns></returns>
- public (string, object, object) ImportUsers(List<SysUser> users)
- {
- users.ForEach(x =>
- {
- x.CreateTime = DateTime.Now;
- x.DelFlag = "0";
- x.password = "E10ADC3949BA59ABBE56E057F20F883E";
- });
- var x = Context.Storageable(users)
- .SplitInsert(it => !it.Any())
- .SplitIgnore(it => it.Item.username == GlobalConstant.AdminRole)
- .SplitError(x => x.Item.username.IsEmpty(), "用户名不能为空")
- .SplitError(x => !Tools.CheckUserName(x.Item.username), "用户名不符合规范")
- .WhereColumns(it => it.username)//如果不是主键可以这样实现(多字段it=>new{it.x1,it.x2})
- .ToStorage();
- var result = x.AsInsertable.ExecuteCommand();//插入可插入部分;
- string msg = string.Format(" 插入{0} 更新{1} 错误数据{2} 不计算数据{3} 删除数据{4} 总共{5}",
- x.InsertList.Count,
- x.UpdateList.Count,
- x.ErrorList.Count,
- x.IgnoreList.Count,
- x.DeleteList.Count,
- x.TotalList.Count);
- //输出统计
- Console.WriteLine(msg);
- //输出错误信息
- foreach (var item in x.ErrorList)
- {
- Console.WriteLine("userName为" + item.Item.username + " : " + item.StorageMessage);
- }
- foreach (var item in x.IgnoreList)
- {
- Console.WriteLine("userName为" + item.Item.username + " : " + item.StorageMessage);
- }
- return (msg, x.ErrorList, x.IgnoreList);
- }
- /// <summary>
- /// 登录
- /// </summary>
- /// <param name="user">登录实体</param>
- /// <returns></returns>
- public SysUser Login(LoginBodyDto user)
- {
- return GetFirst(it => it.username == user.username && it.password.ToLower() == user.password.ToLower());
- }
- /// <summary>
- /// 修改登录信息
- /// </summary>
- /// <param name="userIP"></param>
- /// <param name="userId"></param>
- /// <returns></returns>
- public void UpdateLoginInfo(string userIP, long userId)
- {
- }
- public PagedInfo<SysUserVo> TestList(SysDeptFeign SysDept)
- {
- //开始拼装查询条件
- var predicate = Expressionable.Create<SysUser>();
- var response = Queryable()
- .Where(predicate.ToExpression())
- .OrderByDescending(m => m.userId)
- .Includes(m => m.deptJoin)
- // .CrossQuery(typeof(Users), "tel")
- // .Includes(m => m.user)
- .ToPage<SysUser, SysUserVo>(new PagerInfo());
- return response;
- }
- }
- }
|