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

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