平安校园
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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