SysUserService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. using Attribute;
  2. using Base;
  3. using Common;
  4. using Dto;
  5. using Extensions;
  6. using Feign;
  7. using Infrastructure;
  8. using IPTools.Core;
  9. using Model;
  10. using Model.Base;
  11. using Repository;
  12. using Service;
  13. using System.Collections;
  14. using Vo;
  15. namespace Services
  16. {
  17. /// <summary>
  18. /// 系统用户
  19. /// </summary>
  20. [AppService(ServiceType = typeof(ISysUserService), ServiceLifetime = LifeTime.Transient)]
  21. public class SysUserService : BaseService<SysUser>, ISysUserService
  22. {
  23. private readonly ISysRoleService RoleService;
  24. private readonly ISysPostService PostService;
  25. private readonly ISysUserRoleService UserRoleService;
  26. private readonly ISysUserPostService UserPostService;
  27. public SysUserService(
  28. ISysRoleService sysRoleService,
  29. ISysPostService sysPostService,
  30. ISysUserRoleService userRoleService,
  31. ISysUserPostService userPostService)
  32. {
  33. RoleService = sysRoleService;
  34. PostService = sysPostService;
  35. UserRoleService = userRoleService;
  36. UserPostService = userPostService;
  37. }
  38. /// <summary>
  39. /// 根据条件分页查询用户列表
  40. /// </summary>
  41. /// <returns></returns>
  42. public PagedInfo<SysUser> SelectUserList(SysUserQueryDto user, PagerInfo pager)
  43. {
  44. var exp = Expressionable.Create<SysUser>();
  45. exp.AndIF(!string.IsNullOrEmpty(user.username), u => u.username.Contains(user.username));
  46. exp.AndIF(!string.IsNullOrEmpty(value: user.nickname), u => u.username.Contains(user.nickname));
  47. exp.AndIF(!string.IsNullOrEmpty(user.name), u => u.username.Contains(user.name));
  48. exp.AndIF(!string.IsNullOrEmpty(user.phone), u => u.username.Contains(user.phone));
  49. exp.And(u => u.DelFlag == "0");
  50. var query = Queryable()
  51. .Where(exp.ToExpression())
  52. .OrderByDescending(m => m.userId);
  53. PagedInfo<SysUser> result = query.ToPage(pager);
  54. foreach(SysUser sub in result.Records)
  55. {
  56. List<long> userRoles = UserRoleService.GetList(m => m.UserId == sub.userId).Select(m => m.RoleId).ToList();
  57. sub.RoleList = RoleService.GetList(m => userRoles.Contains(m.roleId));
  58. List<long> userPost = UserPostService.GetList(m => m.UserId == sub.userId).Select(m => m.PostId).ToList();
  59. sub.PostList = PostService.GetList(m => userPost.Contains(m.postId));
  60. }
  61. return result;
  62. }
  63. /// <summary>
  64. /// 通过用户ID查询用户
  65. /// </summary>
  66. /// <param name="userId"></param>
  67. /// <returns></returns>
  68. public SysUser SelectUserById(long userId)
  69. {
  70. var user = Queryable().Filter(null, true).WithCache(60 * 5)
  71. .Where(f => f.userId == userId).First();
  72. // if (user != null && user.UserId > 0)
  73. // {
  74. // user.Roles = RoleService.SelectUserRoleListByUserId(userId);
  75. // user.RoleIds = user.Roles.Select(x => x.RoleId).ToArray();
  76. // }
  77. return user;
  78. }
  79. /// <summary>
  80. /// 校验用户名称是否唯一
  81. /// </summary>
  82. /// <param name="userName"></param>
  83. /// <returns></returns>
  84. public string CheckUserNameUnique(string userName)
  85. {
  86. int count = Count(it => it.username == userName);
  87. if (count > 0)
  88. {
  89. return UserConstants.NOT_UNIQUE;
  90. }
  91. return UserConstants.UNIQUE;
  92. }
  93. /// <summary>
  94. /// 新增保存用户信息
  95. /// </summary>
  96. /// <param name="sysUser"></param>
  97. /// <returns></returns>
  98. public SysUser InsertUser(SysUser sysUser)
  99. {
  100. sysUser.CreateTime = DateTime.Now;
  101. sysUser.UpdateTime = DateTime.Now;
  102. var result = UseTran(() =>
  103. {
  104. sysUser.userId = Insertable(sysUser).ExecuteReturnIdentity();
  105. //新增用户角色信息
  106. UserRoleService.InsertUserRole(sysUser);
  107. //新增用户岗位信息
  108. UserPostService.InsertUserPost(sysUser);
  109. });
  110. if (!result.IsSuccess)
  111. {
  112. throw new Exception("提交数据异常," + result.ErrorMessage, result.ErrorException);
  113. }
  114. return sysUser;
  115. }
  116. /// <summary>
  117. /// 修改用户信息
  118. /// </summary>
  119. /// <param name="user"></param>
  120. /// <returns></returns>
  121. public int UpdateUser(SysUser user)
  122. {
  123. var roleIds = RoleService.SelectUserRoles(user.userId);
  124. var diffArr = roleIds.Where(c => !((IList)user.RoleIds).Contains(c)).ToArray();
  125. var diffArr2 = user.RoleIds.Where(c => !((IList)roleIds).Contains(c)).ToArray();
  126. bool result = UseTran2(() =>
  127. {
  128. if (diffArr.Length > 0 || diffArr2.Length > 0)
  129. {
  130. //删除用户与角色关联
  131. UserRoleService.DeleteUserRoleByUserId((int)user.userId);
  132. //新增用户与角色关联
  133. UserRoleService.InsertUserRole(user);
  134. }
  135. // 删除用户与岗位关联
  136. UserPostService.Delete(user.userId);
  137. // 新增用户与岗位管理
  138. UserPostService.InsertUserPost(user);
  139. ChangeUser(user);
  140. });
  141. return result ? 1 : 0;
  142. }
  143. public int ChangeUser(SysUser user)
  144. {
  145. user.UpdateTime = DateTime.Now;
  146. return Update(user, true);
  147. }
  148. /// <summary>
  149. /// 重置密码
  150. /// </summary>
  151. /// <param name="userid"></param>
  152. /// <param name="password"></param>
  153. /// <returns></returns>
  154. public int ResetPwd(long userid, string password)
  155. {
  156. return Update(new SysUser() { userId = userid, password = password }, it => new { it.password }, f => f.userId == userid);
  157. }
  158. /// <summary>
  159. /// 修改用户状态
  160. /// </summary>
  161. /// <param name="user"></param>
  162. /// <returns></returns>
  163. public int ChangeUserStatus(SysUser user)
  164. {
  165. CheckUserAllowed(user);
  166. return Update(user, it => new { it.DelFlag }, f => f.userId == user.userId);
  167. }
  168. /// <summary>
  169. /// 删除用户
  170. /// </summary>
  171. /// <param name="userid"></param>
  172. /// <returns></returns>
  173. public int DeleteUser(long userid)
  174. {
  175. CheckUserAllowed(new SysUser() { userId = userid });
  176. //删除用户与角色关联
  177. UserRoleService.DeleteUserRoleByUserId((int)userid);
  178. // 删除用户与岗位关联
  179. UserPostService.Delete(userid);
  180. return Update(new SysUser() { userId = userid, DelFlag = "2" }, it => new { it.DelFlag }, f => f.userId == userid);
  181. }
  182. /// <summary>
  183. /// 修改用户头像
  184. /// </summary>
  185. /// <param name="user"></param>
  186. /// <returns></returns>
  187. public int UpdatePhoto(SysUser user)
  188. {
  189. return Update(user, it => new { it.avatar }, f => f.userId == user.userId); ;
  190. }
  191. /// <summary>
  192. /// 注册用户
  193. /// </summary>
  194. /// <param name="dto"></param>
  195. /// <returns></returns>
  196. public SysUser Register(RegisterDto dto)
  197. {
  198. if (!Tools.PasswordStrength(dto.Password))
  199. {
  200. throw new CustomException("密码强度不符合要求");
  201. }
  202. if (!Tools.CheckUserName(dto.Username))
  203. {
  204. throw new CustomException("用户名不符合要求");
  205. }
  206. //密码md5
  207. string password = Function.MD532(dto.Password);
  208. var ip_info = IpTool.Search(dto.UserIP);
  209. SysUser user = new()
  210. {
  211. CreateTime = DateTime.Now,
  212. username = dto.Username,
  213. nickname = dto.Username,
  214. password = password,
  215. deptId = 0,
  216. };
  217. if (UserConstants.NOT_UNIQUE.Equals(CheckUserNameUnique(dto.Username)))
  218. {
  219. throw new CustomException($"保存用户{dto.Username}失败,注册账号已存在");
  220. }
  221. user.userId = Insertable(user).ExecuteReturnIdentity();
  222. return user;
  223. }
  224. /// <summary>
  225. /// 校验角色是否允许操作
  226. /// </summary>
  227. /// <param name="user"></param>
  228. public void CheckUserAllowed(SysUser user)
  229. {
  230. if (user.IsAdmin())
  231. {
  232. throw new CustomException("不允许操作超级管理员角色");
  233. }
  234. }
  235. /// <summary>
  236. /// 校验用户是否有数据权限
  237. /// </summary>
  238. /// <param name="userid"></param>
  239. /// <param name="loginUserId"></param>
  240. public void CheckUserDataScope(long userid, long loginUserId)
  241. {
  242. if (!SysUser.IsAdmin(loginUserId))
  243. {
  244. SysUser user = new SysUser() { userId = userid };
  245. //TODO 判断用户是否有数据权限
  246. }
  247. }
  248. /// <summary>
  249. /// 导入数据
  250. /// </summary>
  251. /// <param name="users"></param>
  252. /// <returns></returns>
  253. public (string, object, object) ImportUsers(List<SysUser> users)
  254. {
  255. users.ForEach(x =>
  256. {
  257. x.CreateTime = DateTime.Now;
  258. x.DelFlag = "0";
  259. x.password = "E10ADC3949BA59ABBE56E057F20F883E";
  260. });
  261. var x = Context.Storageable(users)
  262. .SplitInsert(it => !it.Any())
  263. .SplitIgnore(it => it.Item.username == GlobalConstant.AdminRole)
  264. .SplitError(x => x.Item.username.IsEmpty(), "用户名不能为空")
  265. .SplitError(x => !Tools.CheckUserName(x.Item.username), "用户名不符合规范")
  266. .WhereColumns(it => it.username)//如果不是主键可以这样实现(多字段it=>new{it.x1,it.x2})
  267. .ToStorage();
  268. var result = x.AsInsertable.ExecuteCommand();//插入可插入部分;
  269. string msg = string.Format(" 插入{0} 更新{1} 错误数据{2} 不计算数据{3} 删除数据{4} 总共{5}",
  270. x.InsertList.Count,
  271. x.UpdateList.Count,
  272. x.ErrorList.Count,
  273. x.IgnoreList.Count,
  274. x.DeleteList.Count,
  275. x.TotalList.Count);
  276. //输出统计
  277. Console.WriteLine(msg);
  278. //输出错误信息
  279. foreach (var item in x.ErrorList)
  280. {
  281. Console.WriteLine("userName为" + item.Item.username + " : " + item.StorageMessage);
  282. }
  283. foreach (var item in x.IgnoreList)
  284. {
  285. Console.WriteLine("userName为" + item.Item.username + " : " + item.StorageMessage);
  286. }
  287. return (msg, x.ErrorList, x.IgnoreList);
  288. }
  289. /// <summary>
  290. /// 登录
  291. /// </summary>
  292. /// <param name="user">登录实体</param>
  293. /// <returns></returns>
  294. public SysUser Login(LoginBodyDto user)
  295. {
  296. return GetFirst(it => it.username == user.username && it.password.ToLower() == user.password.ToLower());
  297. }
  298. /// <summary>
  299. /// 修改登录信息
  300. /// </summary>
  301. /// <param name="userIP"></param>
  302. /// <param name="userId"></param>
  303. /// <returns></returns>
  304. public void UpdateLoginInfo(string userIP, long userId)
  305. {
  306. }
  307. public PagedInfo<SysUserVo> TestList(SysDeptFeign SysDept)
  308. {
  309. //开始拼装查询条件
  310. var predicate = Expressionable.Create<SysUser>();
  311. var response = Queryable()
  312. .Where(predicate.ToExpression())
  313. .OrderByDescending(m => m.userId)
  314. .Includes(m => m.deptJoin)
  315. // .CrossQuery(typeof(Users), "tel")
  316. // .Includes(m => m.user)
  317. .ToPage<SysUser, SysUserVo>(new PagerInfo());
  318. return response;
  319. }
  320. }
  321. }