SysOrgService.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. 
  2. using Furion.DatabaseAccessor.Extensions;
  3. using YBEE.EQM.Core;
  4. namespace YBEE.EQM.Application;
  5. /// <summary>
  6. /// 机构信息服务
  7. /// </summary>
  8. public class SysOrgService : ISysOrgService, ITransient
  9. {
  10. private readonly IRepository<SysOrg> _rep;
  11. public SysOrgService(IRepository<SysOrg> rep)
  12. {
  13. _rep = rep;
  14. }
  15. /// <summary>
  16. /// 添加机构
  17. /// </summary>
  18. /// <param name="input"></param>
  19. /// <returns></returns>
  20. public async Task Add(AddSysOrgInput input)
  21. {
  22. if (await _rep.AnyAsync(t => t.FullName.ToUpper() == input.FullName.ToUpper() || t.Name.ToUpper() == input.Name.ToUpper() || t.ShortName.ToUpper() == input.ShortName.ToUpper()))
  23. {
  24. throw Oops.Oh(ErrorCode.E2003, "", "全称、名称或简称");
  25. }
  26. if (await _rep.AnyAsync(t => t.OrgType == input.OrgType && t.EducationStage == input.EducationStage && t.Code.ToUpper() == input.Code.ToUpper()))
  27. {
  28. throw Oops.Oh(ErrorCode.E2003, "", "代码");
  29. }
  30. var item = input.Adapt<SysOrg>();
  31. await item.InsertAsync();
  32. }
  33. /// <summary>
  34. /// 更新机构
  35. /// </summary>
  36. /// <param name="input"></param>
  37. /// <returns></returns>
  38. public async Task Update(UpdateSysOrgInput input)
  39. {
  40. if (await _rep.AnyAsync(t => t.Id != input.Id && (t.FullName.ToUpper() == input.FullName.ToUpper() || t.Name.ToUpper() == input.Name.ToUpper() || t.ShortName.ToUpper() == input.ShortName.ToUpper())))
  41. {
  42. throw Oops.Oh(ErrorCode.E2003, "其他机构", "的全称、名称或简称");
  43. }
  44. if (await _rep.AnyAsync(t => t.Id != input.Id && t.OrgType == input.OrgType && t.EducationStage == input.EducationStage && t.Code.ToUpper() == input.Code.ToUpper()))
  45. {
  46. throw Oops.Oh(ErrorCode.E2003, "其他机构", "的代码");
  47. }
  48. var item = input.Adapt<SysOrg>();
  49. await item.UpdateAsync();
  50. }
  51. /// <summary>
  52. /// 删除机构
  53. /// </summary>
  54. /// <param name="input"></param>
  55. /// <returns></returns>
  56. public async Task Del(BaseId<short> input)
  57. {
  58. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  59. item.Status = CommonStatus.DELETED;
  60. await item.UpdateIncludeAsync(new[] { nameof(item.Status) });
  61. await item.FakeDeleteAsync();
  62. }
  63. /// <summary>
  64. /// 启用禁用机构
  65. /// </summary>
  66. /// <param name="input"></param>
  67. /// <returns></returns>
  68. public async Task ChangeStatus(BaseId<short> input)
  69. {
  70. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  71. item.Status = item.Status == CommonStatus.ENABLE ? CommonStatus.DISABLE : CommonStatus.ENABLE;
  72. await item.UpdateIncludeAsync(new[] { nameof(item.Status) });
  73. }
  74. /// <summary>
  75. /// 根据ID获取机构
  76. /// </summary>
  77. /// <param name="id"></param>
  78. /// <returns></returns>
  79. public async Task<SysOrgOutput> GetById(short id)
  80. {
  81. var item = await _rep.FirstOrDefaultAsync(t => t.Id == id) ?? throw Oops.Oh(ErrorCode.E2001);
  82. return item.Adapt<SysOrgOutput>();
  83. }
  84. /// <summary>
  85. /// 查询机构列表
  86. /// </summary>
  87. /// <param name="input"></param>
  88. /// <returns></returns>
  89. public async Task<List<SysOrgLiteOutput>> QueryList(QuerySysOrgInput input)
  90. {
  91. var name = !string.IsNullOrEmpty(input.Name?.Trim());
  92. var code = !string.IsNullOrEmpty(input.Code?.Trim());
  93. var items = await _rep.DetachedEntities
  94. .Where((name, u => EF.Functions.Like(u.ShortName, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.FullName, $"%{input.Name.Trim()}%")))
  95. .Where((code, u => EF.Functions.Like(u.Code, $"%{input.Code.Trim()}%")))
  96. .Where(input.OrgType.HasValue, u => u.OrgType == input.OrgType)
  97. .Where(input.EducationStage.HasValue, u => u.EducationStage == input.EducationStage)
  98. .Where(input.OrgFundSource.HasValue, u => u.OrgFundSource == input.OrgFundSource)
  99. .Where(input.UrbanRuralType.HasValue, u => u.UrbanRuralType == input.UrbanRuralType)
  100. .Where(input.Status.HasValue, u => u.Status == input.Status)
  101. .Where(t => t.IsDeleted == false)
  102. .OrderBy(u => u.OrgType).ThenBy(t => t.EducationStage).ThenBy(t => t.Code)
  103. .ProjectToType<SysOrgLiteOutput>()
  104. .ToListAsync();
  105. return items;
  106. }
  107. /// <summary>
  108. /// 分页查询机构列表
  109. /// </summary>
  110. /// <param name="input"></param>
  111. /// <returns></returns>
  112. public async Task<PageResult<SysOrgOutput>> QueryPageList(SysOrgPageInput input)
  113. {
  114. var name = !string.IsNullOrEmpty(input.Name?.Trim());
  115. var code = !string.IsNullOrEmpty(input.Code?.Trim());
  116. var ret = await _rep.DetachedEntities
  117. .Where((name, u => EF.Functions.Like(u.ShortName, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.FullName, $"%{input.Name.Trim()}%")))
  118. .Where((code, u => EF.Functions.Like(u.Code, $"%{input.Code.Trim()}%")))
  119. .Where(input.OrgType.HasValue, u => u.OrgType == input.OrgType)
  120. .Where(input.EducationStage.HasValue, u => u.EducationStage == input.EducationStage)
  121. .Where(input.OrgFundSource.HasValue, u => u.OrgFundSource == input.OrgFundSource)
  122. .Where(input.UrbanRuralType.HasValue, u => u.UrbanRuralType == input.UrbanRuralType)
  123. .Where(input.Status.HasValue, u => u.Status == input.Status)
  124. .Where(t => t.IsDeleted == false)
  125. .OrderBy(u => u.OrgType).ThenBy(t => t.EducationStage).ThenBy(t => t.Code)
  126. .ProjectToType<SysOrgOutput>()
  127. .ToADPagedListAsync(input.PageIndex, input.PageSize);
  128. return ret;
  129. }
  130. /// <summary>
  131. /// 获取全部机构列表
  132. /// </summary>
  133. /// <returns></returns>
  134. public async Task<List<SysOrgLiteOutput>> GetAllList()
  135. {
  136. var items = await _rep.DetachedEntities.Where(t => t.IsDeleted == false).ProjectToType<SysOrgLiteOutput>().ToListAsync();
  137. return items;
  138. }
  139. /// <summary>
  140. /// 根据学校ID获取校区列表
  141. /// </summary>
  142. /// <param name="orgId"></param>
  143. /// <returns></returns>
  144. public async Task<List<SysOrgLiteOutput>> GetOrgBranchByOrgId(short orgId)
  145. {
  146. var items = await _rep.DetachedEntities.Where(t => t.OrgType == OrgType.SCHOOL_BRANCH && t.Pid == orgId).ProjectToType<SysOrgLiteOutput>().ToListAsync();
  147. return items;
  148. }
  149. }