SysUserService.cs 12 KB

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