平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

333 lines
11 KiB

  1. //
  2. namespace SafeCampus.Application;
  3. /// <summary>
  4. /// <inheritdoc cref="IUserService"/>
  5. /// </summary>
  6. public class UserService : DbRepository<SysUser>, IUserService
  7. {
  8. private readonly ISysUserService _sysUserService;
  9. private readonly ISysRoleService _sysRoleService;
  10. private readonly IImportExportService _importExportService;
  11. public UserService(ISysUserService sysUserService, ISysRoleService sysRoleService, IImportExportService importExportService)
  12. {
  13. _sysUserService = sysUserService;
  14. _sysRoleService = sysRoleService;
  15. _importExportService = importExportService;
  16. }
  17. #region 查询
  18. /// <inheritdoc/>
  19. public async Task<SqlSugarPagedList<SysUser>> Page(UserPageInput input)
  20. {
  21. var query = await GetQuery(input);
  22. //分页查询
  23. var pageInfo = await _sysUserService.Page(query);
  24. return pageInfo;
  25. }
  26. /// <inheritdoc/>
  27. public async Task<List<RoleSelectorOutPut>> OwnRole(BaseIdInput input)
  28. {
  29. return await _sysUserService.OwnRole(input);//获取角色
  30. }
  31. /// <inheritdoc/>
  32. public async Task<SqlSugarPagedList<UserSelectorOutPut>> Selector(UserSelectorInput input)
  33. {
  34. //获取数据范围
  35. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  36. if (dataScope == null)
  37. return await _sysUserService.Selector(input);//查询
  38. if (dataScope.Count > 0)
  39. {
  40. input.OrgIds = dataScope;//赋值机构列表
  41. return await _sysUserService.Selector(input);//查询
  42. }
  43. //返回自己
  44. return new SqlSugarPagedList<UserSelectorOutPut>
  45. {
  46. Total = 1,
  47. Pages = 1,
  48. HasPrevPages = false,
  49. HasNextPages = false,
  50. List = new[]
  51. {
  52. new UserSelectorOutPut
  53. {
  54. Account = UserManager.UserAccount, Id = UserManager.UserId,
  55. Name = UserManager.Name, OrgId = UserManager.OrgId
  56. }
  57. }
  58. };
  59. }
  60. /// <inheritdoc />
  61. public async Task<SqlSugarPagedList<RoleSelectorOutPut>> RoleSelector(RoleSelectorInput input)
  62. {
  63. var sysRoles = new SqlSugarPagedList<RoleSelectorOutPut>
  64. {
  65. Total = 1,
  66. Pages = 1,
  67. HasPrevPages = false,
  68. HasNextPages = false,
  69. List = new List<RoleSelectorOutPut>()
  70. };
  71. //获取数据范围
  72. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  73. if (dataScope == null)
  74. sysRoles = await _sysRoleService.RoleSelector(input);//获取角色选择器列表
  75. else if (dataScope.Count > 0)//如果有机构
  76. {
  77. input.OrgIds = dataScope;//将数据范传进去
  78. sysRoles = await _sysRoleService.RoleSelector(input);//获取角色选择器列表
  79. }
  80. return sysRoles;
  81. }
  82. /// <inheritdoc />
  83. public async Task<SysUser> Detail(BaseIdInput input)
  84. {
  85. var user = await _sysUserService.Detail(input);
  86. var errorMessage = "您没有权限查看该人员";
  87. //判断数据范围
  88. await _sysUserService.CheckApiDataScope(user.OrgId, user.CreateUserId, errorMessage);
  89. return user;
  90. }
  91. #endregion 查询
  92. #region 新增
  93. /// <inheritdoc/>
  94. public async Task Add(UserAddInput input)
  95. {
  96. await CheckInput(input, SystemConst.ADD);//检查参数
  97. await _sysUserService.Add(input);//添加
  98. }
  99. #endregion 新增
  100. #region 编辑
  101. /// <inheritdoc/>
  102. public async Task Edit(UserEditInput input)
  103. {
  104. await CheckInput(input, SystemConst.EDIT);//检查参数
  105. await _sysUserService.Edit(input);//编辑
  106. }
  107. /// <inheritdoc/>
  108. public async Task Edits(BatchEditInput input)
  109. {
  110. //获取数据范围
  111. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  112. var ids = input.Ids;
  113. var sysUsers = await GetListAsync(it => ids.Contains(it.Id),
  114. it => new SysUser { OrgId = it.OrgId, CreateUserId = it.CreateUserId });//根据用户ID获取机构id、
  115. sysUsers.ForEach(it =>
  116. {
  117. if (dataScope != null && !dataScope.Contains(it.OrgId) && it.CreateUserId != UserManager.UserId)
  118. throw Oops.Bah(ErrorCodeEnum.A0004);//如果不包含机构id并且不是自己创建的
  119. });
  120. await _sysUserService.Edits(input);
  121. }
  122. /// <inheritdoc/>
  123. public async Task DisableUser(BaseIdInput input)
  124. {
  125. await CheckInput(input.Id, SystemConst.DISABLE);//检查权限
  126. await _sysUserService.DisableUser(input);//禁用
  127. }
  128. /// <inheritdoc/>
  129. public async Task EnableUser(BaseIdInput input)
  130. {
  131. await CheckInput(input.Id, SystemConst.ENABLE);//检查权限
  132. await _sysUserService.EnableUser(input);//启用
  133. }
  134. /// <inheritdoc/>
  135. public async Task GrantRole(UserGrantRoleInput input)
  136. {
  137. await CheckInput(input.Id, SystemConst.DISABLE);//检查权限
  138. await _sysUserService.GrantRole(input);//授权
  139. }
  140. /// <inheritdoc/>
  141. public async Task ResetPassword(BaseIdInput input)
  142. {
  143. await CheckInput(input.Id, SystemConst.RESET_PWD);//检查权限
  144. await _sysUserService.ResetPassword(input);//重置密码
  145. }
  146. #endregion 编辑
  147. #region 删除
  148. /// <inheritdoc/>
  149. public async Task Delete(BaseIdListInput input)
  150. {
  151. //获取所有ID
  152. var ids = input.Ids;
  153. //获取用户下信息
  154. var users = await GetListAsync(it => ids.Contains(it.Id), it => new SysUser { OrgId = it.OrgId, Id = it.Id });
  155. //检查数据范围
  156. var orgIds = users.Select(it => it.OrgId).ToList();
  157. var createUserIds = users.Select(it => it.CreateUserId.GetValueOrDefault()).ToList();
  158. await _sysUserService.CheckApiDataScope(orgIds, createUserIds, "您没有权限删除这些人员");
  159. await _sysUserService.Delete(input);//删除
  160. }
  161. #endregion 删除
  162. #region 导入导出
  163. /// <inheritdoc/>
  164. public async Task<FileStreamResult> Template()
  165. {
  166. var templateName = "人员信息.xlsx";
  167. //var result = _importExportService.GenerateLocalTemplate(templateName);
  168. var result = await _importExportService.GenerateTemplate<BizUserImportInput>(templateName);
  169. return result;
  170. }
  171. /// <inheritdoc/>
  172. public async Task<dynamic> Preview(ImportPreviewInput input)
  173. {
  174. //获取数据范围
  175. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  176. if (dataScope == null || dataScope.Count > 0)
  177. {
  178. var importPreview = await _importExportService.GetImportPreview<BizUserImportInput>(input.File);
  179. importPreview.Data = await CheckImport(importPreview.Data, dataScope);//检查导入数据
  180. return importPreview;
  181. }
  182. throw Oops.Bah("您无权导入用户");
  183. }
  184. /// <inheritdoc/>
  185. public async Task<dynamic> Export(UserPageInput input)
  186. {
  187. var query = await GetQuery(input);
  188. var users = await _sysUserService.List(query);
  189. var data = users.Adapt<List<SysUserExportOutput>>();//转为Dto
  190. var result = await _importExportService.Export(data, "人员信息");
  191. return result;
  192. }
  193. /// <inheritdoc/>
  194. public async Task<ImportResultOutPut<BizUserImportInput>> Import(ImportResultInput<BizUserImportInput> input)
  195. {
  196. //获取数据范围
  197. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  198. if (dataScope.Count > 0)
  199. {
  200. var data = await CheckImport(input.Data, dataScope, true);//检查数据格式
  201. var result = _importExportService.GetImportResultPreview(data, out var importData);
  202. var sysUsers = importData.Adapt<List<SysUser>>();//转实体
  203. await _sysUserService.SetUserDefault(sysUsers);//设置用户默认值
  204. await InsertOrBulkCopy(sysUsers);// 数据导入
  205. return result;
  206. }
  207. throw Oops.Bah("您无权导入用户");
  208. }
  209. #endregion 导入导出
  210. #region 方法
  211. /// <summary>
  212. /// 检查输入参数
  213. /// </summary>
  214. /// <param name="sysUser"></param>
  215. /// <param name="operate">操作类型</param>
  216. private async Task CheckInput(SysUser sysUser, string operate)
  217. {
  218. var errorMessage = $"您没有权限{operate}该机构下的人员";
  219. if (sysUser.Id != SafeCampusConst.ZERO)
  220. {
  221. var user = await _sysUserService.GetUserById(sysUser.Id);
  222. sysUser.CreateUserId = user.CreateUserId;
  223. }
  224. //检查数据范围
  225. await _sysUserService.CheckApiDataScope(sysUser.OrgId, sysUser.CreateUserId.GetValueOrDefault(), errorMessage);
  226. }
  227. /// <summary>
  228. /// 检查输入参数
  229. /// </summary>
  230. /// <param name="userId">用户ID</param>
  231. /// <param name="operate">操作</param>
  232. private async Task CheckInput(long userId, string operate)
  233. {
  234. var errorMessage = $"您没有权限{operate}该机构下的人员";
  235. var sysUser = await _sysUserService.GetUserById(userId);
  236. //检查数据范围
  237. await _sysUserService.CheckApiDataScope(sysUser.OrgId, sysUser.CreateUserId.GetValueOrDefault(), errorMessage);
  238. }
  239. /// <summary>
  240. /// 检查导入数据
  241. /// </summary>
  242. /// <param name="data">数据</param>
  243. /// <param name="dataScope">数据范围ID数组</param>
  244. /// <param name="clearError">是否初始化错误</param>
  245. /// <returns></returns>
  246. public async Task<List<BizUserImportInput>> CheckImport(List<BizUserImportInput> data, List<long> dataScope, bool clearError = false)
  247. {
  248. var errorMessage = "没有权限";
  249. //先经过系统用户检查
  250. var bizUsers = await _sysUserService.CheckImport(data, clearError);
  251. bizUsers.ForEach(it =>
  252. {
  253. //如果机构没有错误
  254. if (!it.ErrorInfo.ContainsKey(nameof(it.OrgName)))
  255. {
  256. //判断是否包含数据范围,如果不包含
  257. if (dataScope != null && !dataScope.Contains(it.OrgId))
  258. {
  259. it.ErrorInfo.Add(nameof(it.OrgName), errorMessage);
  260. if (!it.ErrorInfo.ContainsKey(nameof(it.PositionName)))//如果机构没错
  261. it.ErrorInfo.Add(nameof(it.PositionName), errorMessage);
  262. }
  263. }
  264. if (it.ErrorInfo.Count > 0) it.HasError = true;//如果错误信息数量大于0则表示有错误
  265. });
  266. bizUsers = bizUsers.OrderByDescending(it => it.HasError).ToList();//排序
  267. return bizUsers;
  268. }
  269. /// <summary>
  270. /// 获取查询条件
  271. /// </summary>
  272. /// <param name="input"></param>
  273. /// <returns></returns>
  274. public async Task<UserPageInput> GetQuery(UserPageInput input)
  275. {
  276. //获取数据范围
  277. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  278. //动态查询条件
  279. var exp = Expressionable.Create<SysUser>();
  280. exp.And(u => u.Account != SysRoleConst.SUPER_ADMIN);
  281. exp.AndIF(dataScope is { Count: > 0 }, u => dataScope.Contains(u.OrgId));//用户机构在数据范围内
  282. exp.AndIF(dataScope is { Count: 0 }, u => u.Id == UserManager.UserId);//用户ID等于自己
  283. input.Expression = exp;
  284. return input;
  285. }
  286. #endregion 方法
  287. }