ExamAbsentReplaceAuditService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using Furion.DatabaseAccessor.Extensions;
  2. using YBEE.EQM.Core;
  3. namespace YBEE.EQM.Application;
  4. /// <summary>
  5. /// 缺测替补审核服务
  6. /// </summary>
  7. public class ExamAbsentReplaceAuditService : IExamAbsentReplaceAuditService, ITransient
  8. {
  9. private readonly IRepository<ExamAbsentReplace> _rep;
  10. private readonly ISysRoleService _sysRoleService;
  11. public ExamAbsentReplaceAuditService(IRepository<ExamAbsentReplace> rep, ISysRoleService sysRoleService)
  12. {
  13. _rep = rep;
  14. _sysRoleService = sysRoleService;
  15. }
  16. /// <summary>
  17. /// 提交审核
  18. /// </summary>
  19. /// <param name="input"></param>
  20. /// <returns></returns>
  21. public async Task Submit(BaseId input)
  22. {
  23. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  24. var dtype = await _rep.Change<ExamDataReport>().Where(t => t.ExamPlanId == item.ExamPlanId && t.Type == DataReportType.ABSENT_REPLACE).FirstOrDefaultAsync();
  25. if (dtype == null || dtype.Status != ExamStatus.ACTIVE || (item.Status != AuditStatus.UNSUBMIT && item.Status != AuditStatus.REJECTED))
  26. {
  27. throw Oops.Oh(ErrorCode.E2006);
  28. }
  29. item.Status = AuditStatus.AUDIT;
  30. item.Audits = AuditUtil.InsertInto(item.Audits, AuditUtil.CreateNew(AuditActionType.SUBMIT, item.Status));
  31. await item.UpdateAsync();
  32. }
  33. /// <summary>
  34. /// 审核
  35. /// </summary>
  36. /// <param name="input"></param>
  37. /// <returns></returns>
  38. public async Task Audit(ExamAbsentReplaceAuditInput input)
  39. {
  40. var dt = DateTime.Now;
  41. var items = await _rep.Where(t => input.Ids.Contains(t.Id)).ToListAsync();
  42. foreach (var item in items)
  43. {
  44. item.Status = input.IsApproved ? AuditStatus.APPROVED : AuditStatus.REJECTED;
  45. item.Audits = AuditUtil.InsertInto(item.Audits, AuditUtil.CreateNew(input.IsApproved ? AuditActionType.APPROVE : AuditActionType.REJECT, item.Status, dt, input.Remark));
  46. }
  47. await _rep.UpdateAsync(items);
  48. }
  49. /// <summary>
  50. /// 反审
  51. /// </summary>
  52. /// <param name="input"></param>
  53. /// <returns></returns>
  54. public async Task Reaudit(BaseId input)
  55. {
  56. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  57. if (item.Status != AuditStatus.APPROVED)
  58. {
  59. throw Oops.Oh(ErrorCode.E2006);
  60. }
  61. item.Status = AuditStatus.APPROVE_CANCELED;
  62. item.Audits = AuditUtil.InsertInto(item.Audits, AuditUtil.CreateNew(AuditActionType.REAUDIT, item.Status));
  63. await item.UpdateAsync();
  64. }
  65. /// <summary>
  66. /// 获取待审核监测计划列表
  67. /// </summary>
  68. /// <param name="input"></param>
  69. /// <returns></returns>
  70. public async Task<PageResult<ExamPlanAuditOutput>> QueryExamPlanPageList(ExamPlanPageInput input)
  71. {
  72. var roleDataScope = await _sysRoleService.GetCurrentUserDataScope();
  73. if (roleDataScope == null || roleDataScope.EducationStages.Count == 0)
  74. {
  75. return new();
  76. }
  77. List<string> whereCause = new();
  78. if (roleDataScope.EducationStages.Count == 0)
  79. {
  80. whereCause.Add($"T1.education_stage = {(short)roleDataScope.EducationStages[0]}");
  81. }
  82. else
  83. {
  84. var ess = roleDataScope.EducationStages.Select(t => $"T1.education_stage = {(short)t}").ToList();
  85. string wes = string.Join(" OR ", ess);
  86. whereCause.Add($"({wes})");
  87. }
  88. if (!string.IsNullOrEmpty(input.Name?.Trim()))
  89. {
  90. whereCause.Add("T1.full_name LIKE '%@name%'");
  91. }
  92. if (input.SemesterId.HasValue)
  93. {
  94. whereCause.Add("T1.semester_id = @semesterId");
  95. }
  96. if (input.Status.HasValue)
  97. {
  98. whereCause.Add("T1.status = @status");
  99. }
  100. string whereSql = $"WHERE (T1.`status` = {(short)ExamStatus.ACTIVE} OR T1.`status` = {(short)ExamStatus.STOPPED}) AND T1.is_deleted = 0 AND EDR.type = {(short)DataReportType.ABSENT_REPLACE}";
  101. if (whereCause.Count > 0)
  102. {
  103. whereSql = $"{whereSql} AND {string.Join(" AND ", whereCause)}";
  104. }
  105. var p = new
  106. {
  107. input.PageSize,
  108. PageOffset = (input.PageIndex - 1) * input.PageSize,
  109. Type = DataReportType.ABSENT_REPLACE,
  110. input.SemesterId,
  111. input.Status,
  112. input.Name,
  113. };
  114. var totalCount = await _rep.SqlScalarAsync<int>($@"
  115. SELECT COUNT(1) AS total_count
  116. FROM
  117. (
  118. SELECT T1.id, COUNT(T1.sys_org_id) AS org_count
  119. FROM
  120. (
  121. SELECT T1.id, T2.sys_org_id
  122. FROM exam_plan AS T1
  123. JOIN exam_data_report AS EDR ON T1.id = EDR.exam_plan_id
  124. LEFT JOIN exam_org AS T2 ON T1.id = T2.exam_plan_id
  125. {whereSql}
  126. GROUP BY T1.id, t2.sys_org_id
  127. ) AS T1
  128. GROUP BY T1.id
  129. ) AS T
  130. ", p);
  131. var items = await _rep.SqlQueriesAsync<ExamPlanAuditOutput>($@"
  132. SELECT
  133. T1.id,
  134. T1.full_name,
  135. T1.short_name,
  136. T1.`name`,
  137. T1.semester_id,
  138. T1.education_stage,
  139. T1.`status`,
  140. COUNT(DISTINCT T1.sys_org_id) AS org_count,
  141. SUM(T1.total_count) AS total_count,
  142. SUM(T1.audit_count) AS audit_count,
  143. SUM(T1.approved_count) AS approved_count,
  144. SUM(T1.rejected_count) AS rejected_count
  145. FROM
  146. (
  147. SELECT
  148. T1.id,
  149. T1.full_name,
  150. T1.short_name,
  151. T1.`name`,
  152. T1.semester_id,
  153. T1.education_stage,
  154. T1.`status`,
  155. T2.sys_org_id,
  156. COUNT(CASE WHEN T3.`status` = {(short)DataReportStatus.REPORTED} OR T3.`status` = {(short)DataReportStatus.REJECTED} THEN T4.id ELSE NULL END) AS total_count,
  157. COUNT(CASE WHEN (T3.`status` = {(short)DataReportStatus.REPORTED} OR T3.`status` = {(short)DataReportStatus.REJECTED}) AND (T4.`status` ={(short)AuditStatus.AUDIT} OR T4.`status` = {(short)AuditStatus.APPROVE_CANCELED}) THEN T4.id ELSE NULL END) AS audit_count,
  158. COUNT(CASE WHEN (T3.`status` = {(short)DataReportStatus.REPORTED} OR T3.`status` = {(short)DataReportStatus.REJECTED}) AND T4.`status` = {(short)AuditStatus.APPROVED} THEN T4.id ELSE NULL END) AS approved_count,
  159. COUNT(CASE WHEN (T3.`status` = {(short)DataReportStatus.REPORTED} OR T3.`status` = {(short)DataReportStatus.REJECTED}) AND T4.`status` = {(short)AuditStatus.REJECTED} THEN T4.id ELSE NULL END) AS rejected_count
  160. FROM exam_plan AS T1
  161. JOIN exam_data_report AS EDR ON T1.id = EDR.exam_plan_id
  162. LEFT JOIN exam_org AS T2 ON T1.id = T2.exam_plan_id
  163. LEFT JOIN (SELECT * FROM exam_org_data_report WHERE type = {(short)DataReportType.ABSENT_REPLACE}) AS T3 ON T2.id = T3.exam_org_id AND EDR.type = T3.type
  164. LEFT JOIN exam_absent_replace AS T4 ON T1.id = T4.exam_plan_id AND T2.sys_org_id = T4.sys_org_id
  165. {whereSql}
  166. GROUP BY T1.id, T1.full_name, T1.short_name, T1.`name`, T1.semester_id, T1.education_stage, T1.`status`, T2.sys_org_id
  167. ) AS T1
  168. GROUP BY T1.id, T1.full_name, T1.short_name, T1.`name`, T1.semester_id, T1.education_stage, T1.`status`
  169. ORDER BY T1.id DESC
  170. LIMIT @pageSize OFFSET @pageOffset;
  171. ", p);
  172. PageResult<ExamPlanAuditOutput> ret = new()
  173. {
  174. PageIndex = input.PageIndex,
  175. PageSize = input.PageSize,
  176. TotalCount = totalCount,
  177. Items = items
  178. };
  179. return ret;
  180. }
  181. /// <summary>
  182. /// 获取待审核机构列表
  183. /// </summary>
  184. /// <param name="input"></param>
  185. /// <returns></returns>
  186. public async Task<PageResult<ExamPlanOrgAuditOutput>> QueryOrgAuditPageList(ExamOrgDataReportAuditPageInput input)
  187. {
  188. var roleDataScope = await _sysRoleService.GetCurrentUserDataScope();
  189. if (roleDataScope == null || roleDataScope.EducationStages.Count == 0)
  190. {
  191. return new();
  192. }
  193. var p = new
  194. {
  195. input.PageSize,
  196. PageOffset = (input.PageIndex - 1) * input.PageSize,
  197. input.ExamPlanId,
  198. input.SearchValue,
  199. input.SysOrgName,
  200. input.ExamPlanName,
  201. Type = (short)DataReportType.ABSENT_REPLACE,
  202. };
  203. List<string> whereCause = new();
  204. if (input.ExamPlanId.HasValue)
  205. {
  206. whereCause.Add("T1.id = @examPlanId");
  207. }
  208. if (roleDataScope.EducationStages.Count == 0)
  209. {
  210. whereCause.Add($"T1.education_stage = {(short)roleDataScope.EducationStages[0]}");
  211. }
  212. else
  213. {
  214. var ess = roleDataScope.EducationStages.Select(t => $"T1.education_stage = {(short)t}").ToList();
  215. string wes = string.Join(" OR ", ess);
  216. whereCause.Add($"({wes})");
  217. }
  218. if (input.SemesterId.HasValue)
  219. {
  220. whereCause.Add("T1.semester_id = @semesterId");
  221. }
  222. if (!string.IsNullOrEmpty(input.ExamPlanName?.Trim()))
  223. {
  224. whereCause.Add("T1.full_name LIKE '%@examPlanName%'");
  225. }
  226. if (!string.IsNullOrEmpty(input.SysOrgName?.Trim()))
  227. {
  228. whereCause.Add("ORG.full_name LIKE '%@sysOrgName%'");
  229. }
  230. string whereSql = $"WHERE (T1.`status` = {(short)ExamStatus.ACTIVE} OR T1.`status` = {(short)ExamStatus.STOPPED})";
  231. if (whereCause.Count > 0)
  232. {
  233. whereSql = $"{whereSql} AND {string.Join(" AND ", whereCause)}";
  234. }
  235. string fromSql = $@"
  236. FROM exam_plan AS T1
  237. JOIN exam_org AS T2 ON T1.id = T2.exam_plan_id
  238. JOIN sys_org AS ORG ON T2.sys_org_id = ORG.id
  239. LEFT JOIN (SELECT * FROM exam_org_data_report WHERE type = {(short)DataReportType.ABSENT_REPLACE}) AS T3 ON T2.id = T3.exam_org_id
  240. LEFT JOIN exam_absent_replace AS T4 ON T1.id = T4.exam_plan_id AND T2.sys_org_id = T4.sys_org_id
  241. ";
  242. string groupSql = @"GROUP BY T2.exam_plan_id, T1.full_name, T1.`name`, T1.semester_id, T1.education_stage, T1.`status`, T2.sys_org_id, ORG.full_name, ORG.`name`, ORG.`code`, T3.`status`, T3.report_time";
  243. var totalCount = await _rep.SqlScalarAsync<int>($@"
  244. SELECT COUNT(1) AS total_count
  245. FROM
  246. (
  247. SELECT COUNT(1) AS total_count
  248. {fromSql}
  249. {whereSql}
  250. {groupSql}
  251. ) AS T", p);
  252. var items = await _rep.SqlQueriesAsync<ExamPlanOrgAuditOutput>($@"
  253. SELECT ROW_NUMBER() OVER (ORDER BY T.exam_plan_id DESC, T.audit_count DESC, T.report_time) AS `row_number`, T.*
  254. FROM
  255. (
  256. SELECT
  257. T2.exam_plan_id,
  258. T1.full_name AS exam_plan_full_name,
  259. T1.`name` AS exam_plan_name,
  260. T1.semester_id,
  261. T1.education_stage,
  262. T1.`status` AS exam_status,
  263. T2.sys_org_id,
  264. ORG.full_name AS sys_org_full_name,
  265. ORG.`name` AS sys_org_name,
  266. ORG.`code` AS sys_org_code,
  267. IFNULL(T3.`status`, 1) AS data_report_status,
  268. T3.report_time,
  269. COUNT(CASE WHEN (T3.`status` = {(short)DataReportStatus.REPORTED} OR T3.`status` = {(short)DataReportStatus.REJECTED}) THEN T4.id ELSE NULL END) AS total_count,
  270. COUNT(CASE WHEN (T3.`status` = {(short)DataReportStatus.REPORTED} OR T3.`status` = {(short)DataReportStatus.REJECTED}) AND (T4.`status` = {(short)AuditStatus.AUDIT} OR T4.`status` = {(short)AuditStatus.APPROVE_CANCELED}) THEN T4.id ELSE NULL END) AS audit_count,
  271. COUNT(CASE WHEN (T3.`status` = {(short)DataReportStatus.REPORTED} OR T3.`status` = {(short)DataReportStatus.REJECTED}) AND T4.`status` = {(short)AuditStatus.APPROVED} THEN T4.id ELSE NULL END) AS approved_count,
  272. COUNT(CASE WHEN (T3.`status` = {(short)DataReportStatus.REPORTED} OR T3.`status` = {(short)DataReportStatus.REJECTED}) AND T4.`status` = {(short)AuditStatus.REJECTED} THEN T4.id ELSE NULL END) AS rejected_count
  273. {fromSql}
  274. {whereSql}
  275. {groupSql}
  276. ) AS T
  277. ORDER BY T.exam_plan_id DESC, T.audit_count DESC, T.data_report_status DESC, T.report_time DESC
  278. LIMIT @pageSize OFFSET @pageOffset;
  279. ", p);
  280. PageResult<ExamPlanOrgAuditOutput> ret = new()
  281. {
  282. PageIndex = input.PageIndex,
  283. PageSize = input.PageSize,
  284. TotalCount = totalCount,
  285. Items = items
  286. };
  287. return ret;
  288. }
  289. }