平安校园
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.
 
 
 
 
 
 

141 lines
5.6 KiB

  1. namespace SafeCampus.Application;
  2. /// <inheritdoc cref="IOrgService"/>
  3. public class OrgService : DbRepository<SysOrg>, IOrgService
  4. {
  5. private readonly ISysOrgService _sysOrgService;
  6. private readonly ISysUserService _sysUserService;
  7. public OrgService(ISysOrgService sysOrgService, ISysUserService sysUserService)
  8. {
  9. _sysOrgService = sysOrgService;
  10. _sysUserService = sysUserService;
  11. }
  12. /// <inheritdoc/>
  13. public async Task<SqlSugarPagedList<SysOrg>> Page(SysOrgPageInput input)
  14. {
  15. //获取数据范围
  16. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  17. dataScope?.Remove(UserManager.OrgId);
  18. //构建查询
  19. var query = Context.Queryable<SysOrg>()
  20. .WhereIF(dataScope != null, it => dataScope.Contains(it.Id))//机构ID查询
  21. .WhereIF(input.ParentId > 0,
  22. it => it.ParentId == input.ParentId || SqlFunc.JsonLike(it.ParentIdList, input.ParentId.ToString()))//父级
  23. .WhereIF(!string.IsNullOrEmpty(input.Name), it => it.Name.Contains(input.Name))//根据名称查询
  24. .WhereIF(!string.IsNullOrEmpty(input.Code), it => it.Code.Contains(input.Code))//根据编码查询
  25. .WhereIF(!string.IsNullOrEmpty(input.Status), it => it.Status == input.Status)//根据状态查询
  26. .OrderByIF(!string.IsNullOrEmpty(input.SortField), $"{input.SortField} {input.SortOrder}").OrderBy(it => it.SortCode)
  27. .OrderBy(it => it.CreateTime);//排序
  28. var pageInfo = await query.ToPagedListAsync(input.PageNum, input.PageSize);//分页
  29. return pageInfo;
  30. }
  31. /// <inheritdoc />
  32. public async Task<List<SysOrg>> Tree(SysOrgTreeInput input = null)
  33. {
  34. //获取数据范围
  35. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  36. //构建机构树
  37. var result = await _sysOrgService.Tree(dataScope, input);
  38. return result;
  39. }
  40. /// <inheritdoc />
  41. public async Task Add(SysOrgAddInput input)
  42. {
  43. await CheckInput(input, SystemConst.ADD);//检查参数
  44. await _sysOrgService.Add(input, ApplicationConst.BIZ_ORG);
  45. }
  46. /// <inheritdoc />
  47. public async Task Edit(SysOrgEditInput input)
  48. {
  49. await CheckInput(input, SystemConst.EDIT);//检查参数
  50. await _sysOrgService.Edit(input, ApplicationConst.BIZ_ORG);
  51. }
  52. /// <inheritdoc />
  53. public async Task Copy(SysOrgCopyInput input)
  54. {
  55. //获取数据范围
  56. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  57. if (dataScope == null || dataScope.Count > 0)//如果有机构
  58. {
  59. if (dataScope is { Count: > 0 } && (!dataScope.ContainsAll(input.Ids) || !dataScope.Contains(input.TargetId)))//判断目标机构和需要复制的机构是否都在数据范围里面
  60. throw Oops.Bah("您没有权限复制这些机构");
  61. await _sysOrgService.Copy(input);//复制操作
  62. }
  63. }
  64. /// <inheritdoc />
  65. public async Task Delete(BaseIdListInput input)
  66. {
  67. //获取所有ID
  68. var ids = input.Ids;
  69. //获取要删除的机构列表
  70. var orgList = (await _sysOrgService.GetListAsync()).Where(it => ids.Contains(it.Id)).ToList();
  71. if (orgList.Any(it => it.Category == CateGoryConst.ORG_COMPANY))//如果有公司{
  72. throw Oops.Bah("不能删除根机构");
  73. //检查数据范围
  74. var orgIds = orgList.Select(it => it.Id).ToList();
  75. var createUserIds = orgList.Select(it => it.CreateUserId.GetValueOrDefault()).ToList();
  76. await _sysUserService.CheckApiDataScope(orgIds, createUserIds, "您没有权限删除这些机构");
  77. await _sysOrgService.Delete(input, ApplicationConst.BIZ_ORG);//删除操作
  78. }
  79. /// <inheritdoc />
  80. public async Task<SysOrg> Detail(BaseIdInput input)
  81. {
  82. var org = await _sysOrgService.GetSysOrgById(input.Id);//获取机构
  83. var errorMessage = "您没有权限查看该机构";
  84. //判断数据范围
  85. await _sysUserService.CheckApiDataScope(org.Id, org.CreateUserId.GetValueOrDefault(), errorMessage);
  86. return org;
  87. }
  88. #region 方法
  89. /// <summary>
  90. /// 检查输入参数
  91. /// </summary>
  92. /// <param name="sysOrg">参数</param>
  93. /// <param name="operate">操作名称</param>
  94. private async Task CheckInput(SysOrg sysOrg, string operate)
  95. {
  96. sysOrg.Category = CateGoryConst.ORG_DEPT;//设置分类为部门,业务只能操作部门
  97. if (sysOrg.ParentId == SafeCampusConst.ZERO)
  98. {
  99. throw Oops.Bah($"不能{operate}根机构");
  100. }
  101. var errorMessage = $"您没有权限{operate}该机构";
  102. //获取数据范围
  103. var dataScope = await _sysUserService.GetLoginUserApiDataScope();
  104. if (dataScope is { Count: > 0 })//如果有机构
  105. {
  106. if (sysOrg.Id > 0 && !dataScope.Contains(sysOrg.Id))//如果id不为0判断是否在数据范围
  107. throw Oops.Bah(errorMessage);
  108. if (!dataScope.Contains(sysOrg.ParentId))//判断父ID是否在数据范围
  109. throw Oops.Bah($"{errorMessage}下的机构");
  110. }
  111. else if (dataScope is { Count: 0 })
  112. {
  113. //如果id大于0表示编辑
  114. if (sysOrg.Id > 0)
  115. {
  116. var org = await _sysOrgService.GetSysOrgById(sysOrg.Id);//获取机构
  117. if (org.CreateUserId != UserManager.UserId)
  118. throw Oops.Bah(errorMessage);//机构的创建人不是自己则报错
  119. }
  120. else
  121. {
  122. throw Oops.Bah(errorMessage);
  123. }
  124. }
  125. }
  126. #endregion 方法
  127. }