ExamOrgService.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using Furion.DatabaseAccessor.Extensions;
  2. using YBEE.EQM.Core;
  3. namespace YBEE.EQM.Application;
  4. /// <summary>
  5. /// 被监测机构管理服务
  6. /// </summary>
  7. public class ExamOrgService : IExamOrgService, ITransient
  8. {
  9. private readonly IRepository<ExamOrg> _rep;
  10. public ExamOrgService(IRepository<ExamOrg> rep)
  11. {
  12. _rep = rep;
  13. }
  14. /// <summary>
  15. /// 添加机构
  16. /// </summary>
  17. /// <param name="input"></param>
  18. /// <returns></returns>
  19. public async Task AddList(AddExamOrgListInput input)
  20. {
  21. var existsOrgIdList = await _rep.Where(t => t.ExamPlanId == input.ExamPlanId).Select(t => t.SysOrgId).ToListAsync();
  22. var addOrgIdList = from t in input.SysOrgIdList where !(from rid in existsOrgIdList select rid).Contains(t) select t;
  23. var roles = addOrgIdList.Select(u => new ExamOrg
  24. {
  25. ExamPlanId = input.ExamPlanId,
  26. SysOrgId = u,
  27. }).ToList();
  28. await _rep.InsertAsync(roles);
  29. }
  30. /// <summary>
  31. /// 移出机构
  32. /// </summary>
  33. /// <param name="input"></param>
  34. /// <returns></returns>
  35. public async Task Remove(DelExamOrgInput input)
  36. {
  37. var roleUsers = await _rep.Where(t => input.IdList.Contains(t.Id)).ToListAsync();
  38. await _rep.DeleteAsync(roleUsers);
  39. }
  40. /// <summary>
  41. /// 切换机构是否参与区统一监测
  42. /// </summary>
  43. /// <param name="input"></param>
  44. /// <returns></returns>
  45. public async Task SwitchRequiredSample(SwitchExamOrgRequiredSampleInput input)
  46. {
  47. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  48. item.IsRequiredExam = input.IsRequiredExam;
  49. await item.UpdateIncludeAsync(new[] { nameof(item.IsRequiredExam) });
  50. }
  51. /// <summary>
  52. /// 根据监测计划ID获取监测机构列表
  53. /// </summary>
  54. /// <param name="examPlanId"></param>
  55. /// <returns></returns>
  56. public async Task<List<ExamOrgOutput>> GetListByExamPlanId(int examPlanId)
  57. {
  58. var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == examPlanId).ProjectToType<ExamOrgOutput>().ToListAsync();
  59. var odrs = await _rep.Change<ExamOrgDataReport>().Where(t => t.ExamOrg.ExamPlanId == examPlanId).ToListAsync();
  60. foreach (var item in items)
  61. {
  62. item.DataReports = odrs.Where(t => t.ExamOrgId == item.Id).ToDictionary(t => (short)t.Type, u => u.Status);
  63. }
  64. return items;
  65. }
  66. /// <summary>
  67. /// 分页查询监测机构列表
  68. /// </summary>
  69. /// <param name="input"></param>
  70. /// <returns></returns>
  71. public async Task<PageResult<ExamOrgOutput>> QueryPageList(ExamOrgPageInput input)
  72. {
  73. var orgName = !string.IsNullOrEmpty(input.OrgName?.Trim());
  74. var ret = await _rep.DetachedEntities.Where(t => t.ExamPlanId == input.ExamPlanId)
  75. .Where((orgName, u => EF.Functions.Like(u.SysOrg.FullName, $"%{input.OrgName.Trim()}%")))
  76. .ProjectToType<ExamOrgOutput>()
  77. .ToADPagedListAsync(input.PageIndex, input.PageSize);
  78. var odrs = await _rep.Change<ExamOrgDataReport>().Where(t => t.ExamOrg.ExamPlanId == input.ExamPlanId).ProjectToType<ExamOrgDataReportOutput>().ToListAsync();
  79. foreach (var item in ret.Items)
  80. {
  81. item.DataReportList = odrs.Where(t => t.ExamOrgId == item.Id).ToList();
  82. item.DataReports = item.DataReportList.ToDictionary(t => (short)t.Type, u => u.Status);
  83. }
  84. return ret;
  85. }
  86. /// <summary>
  87. /// 分页查询未加入的机构
  88. /// </summary>
  89. /// <param name="input"></param>
  90. /// <returns></returns>
  91. public async Task<PageResult<SysOrgLiteOutput>> QueryNotInSysOrgPageList(ExamOrgNotInPageInput input)
  92. {
  93. var code = !string.IsNullOrEmpty(input.Code?.Trim());
  94. var name = !string.IsNullOrEmpty(input.Name?.Trim());
  95. var examPlan = await _rep.Change<ExamPlan>().DetachedEntities.FirstOrDefaultAsync(t => t.Id == input.ExamPlanId);
  96. var inOrgIds = await _rep.DetachedEntities.Where(t => t.ExamPlanId == input.ExamPlanId).Select(t => t.SysOrgId).ToListAsync();
  97. var ret = await _rep.Change<SysOrg>().DetachedEntities
  98. .Where((code, u => EF.Functions.Like(u.Code, $"%{input.Code.Trim()}%")))
  99. .Where((name, u => EF.Functions.Like(u.FullName, $"%{input.Name.Trim()}%")))
  100. .Where(input.UrbanRuralType.HasValue, t => t.UrbanRuralType == input.UrbanRuralType)
  101. .Where(input.Status.HasValue, t => t.Status == input.Status)
  102. .Where(t => t.OrgType == OrgType.SCHOOL && t.EducationStage == examPlan.EducationStage && t.UrbanRuralType != UrbanRuralType.NONE && !inOrgIds.Contains(t.Id))
  103. .ProjectToType<SysOrgLiteOutput>()
  104. .ToADPagedListAsync(input.PageIndex, input.PageSize);
  105. return ret;
  106. }
  107. /// <summary>
  108. /// 分页查询机构监测计划列表
  109. /// </summary>
  110. /// <param name="input"></param>
  111. /// <returns></returns>
  112. public async Task<PageResult<ExamPlanOutput>> QueryExamPlanPageList(ExamPlanPageInput input)
  113. {
  114. var name = !string.IsNullOrEmpty(input.Name?.Trim());
  115. var searchBeginTime = !string.IsNullOrEmpty(input.SearchBeginTime?.Trim());
  116. var searchEndTime = !string.IsNullOrEmpty(input.SearchEndTime?.Trim());
  117. var ret = await _rep.DetachedEntities.Where(t => t.SysOrgId == CurrentSysUserInfo.SysOrgId && (t.ExamPlan.Status == ExamStatus.ACTIVE || t.ExamPlan.Status == ExamStatus.STOPPED) && t.ExamPlan.IsDeleted == false)
  118. .Where((name, u => EF.Functions.Like(u.ExamPlan.Name, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.ExamPlan.FullName, $"%{input.Name.Trim()}%") || EF.Functions.Like(u.ExamPlan.ShortName, $"%{input.Name.Trim()}%")))
  119. .Where(input.EducationStage.HasValue, t => t.ExamPlan.EducationStage == input.EducationStage)
  120. //.Where(input.ExamPeriodType.HasValue, t => t.ExamPlan.ExamPeriodType == input.ExamPeriodType)
  121. //.Where(input.ExamType.HasValue, t => t.ExamPlan.ExamType == input.ExamType)
  122. .Where(input.SemesterId.HasValue, t => t.ExamPlan.SemesterId == input.SemesterId)
  123. .Where(t => (t.ExamPlan.Status == ExamStatus.ACTIVE || t.ExamPlan.Status == ExamStatus.STOPPED) && (!input.Status.HasValue || input.Status.HasValue && t.ExamPlan.Status == input.Status))
  124. .Where((searchBeginTime, u => u.ExamPlan.CreateTime >= DateTime.Parse(input.SearchBeginTime)))
  125. .Where((searchEndTime, u => u.ExamPlan.CreateTime <= DateTime.Parse(input.SearchEndTime)))
  126. .OrderByDescending(t => t.ExamPlanId)
  127. .Select(t => t.ExamPlan).ProjectToType<ExamPlanOutput>().ToADPagedListAsync(input.PageIndex, input.PageSize);
  128. return ret;
  129. }
  130. }