ExamOrgDataReportService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using Furion.DatabaseAccessor.Extensions;
  2. using Furion.JsonSerialization;
  3. using YBEE.EQM.Core;
  4. namespace YBEE.EQM.Application;
  5. /// <summary>
  6. /// 监测机构上报类型服务
  7. /// </summary>
  8. public class ExamOrgDataReportService : IExamOrgDataReportService, ITransient
  9. {
  10. private readonly IRepository<ExamOrgDataReport> _rep;
  11. private readonly IResourceFileService _resourceFileService;
  12. private readonly IExamSpecialStudentService _examSpecialStudentService;
  13. private readonly IExamAbsentReplaceService _examAbsentReplaceService;
  14. public ExamOrgDataReportService(IRepository<ExamOrgDataReport> rep, IResourceFileService resourceFileService, IExamSpecialStudentService examSpecialStudentService, IExamAbsentReplaceService examAbsentReplaceService)
  15. {
  16. _rep = rep;
  17. _resourceFileService = resourceFileService;
  18. _examSpecialStudentService = examSpecialStudentService;
  19. _examAbsentReplaceService = examAbsentReplaceService;
  20. }
  21. /// <summary>
  22. /// 提交上报
  23. /// </summary>
  24. /// <param name="input"></param>
  25. /// <returns></returns>
  26. public async Task Submit(SubmitExamOrgDataReportInput input)
  27. {
  28. var orgId = CurrentSysUserInfo.SysOrgId;
  29. var examOrg = await _rep.Change<ExamOrg>().DetachedEntities
  30. .Include(t => t.ExamPlan).ThenInclude(t => t.ExamDataReports)
  31. .FirstOrDefaultAsync(t => t.SysOrgId == orgId && t.ExamPlanId == input.ExamPlanId);
  32. var edr = examOrg.ExamPlan.ExamDataReports.FirstOrDefault(t => t.Type == input.Type) ?? throw Oops.Oh(ErrorCode.E2001);
  33. if (edr.Status != ExamStatus.ACTIVE)
  34. {
  35. throw Oops.Oh(ErrorCode.E2006);
  36. }
  37. if (edr.EndTime < DateTime.Now)
  38. {
  39. throw Oops.Oh(ErrorCode.E2008);
  40. }
  41. if (input.Type == DataReportType.SP_STUDENT)
  42. {
  43. var vspSuccess = await _examSpecialStudentService.VerifyAttachment(input.ExamPlanId);
  44. if (vspSuccess == false)
  45. {
  46. throw Oops.Oh(ErrorCode.E1012, "特殊学生明细");
  47. }
  48. }
  49. else if(input.Type == DataReportType.ABSENT_REPLACE)
  50. {
  51. var vspSuccess = await _examAbsentReplaceService.VerifyAttachment(input.ExamPlanId);
  52. if (vspSuccess == false)
  53. {
  54. throw Oops.Oh(ErrorCode.E1012, "缺测替补学生明细");
  55. }
  56. }
  57. var item = await _rep.FirstOrDefaultAsync(t => t.ExamOrg.SysOrgId == CurrentSysUserInfo.SysOrgId && t.ExamOrg.ExamPlanId == input.ExamPlanId && t.Type == input.Type);
  58. if (item == null)
  59. {
  60. item = new ExamOrgDataReport()
  61. {
  62. Type = input.Type,
  63. Status = DataReportStatus.REPORTED,
  64. ReportSysUserId = CurrentSysUserInfo.SysUserId,
  65. ReportTime = DateTime.Now,
  66. ExamOrgId = examOrg.Id,
  67. ExamPlanId = input.ExamPlanId,
  68. SysOrgId = orgId,
  69. };
  70. await _rep.InsertNowAsync(item);
  71. }
  72. else
  73. {
  74. item.Status = DataReportStatus.REPORTED;
  75. item.ReportSysUserId = CurrentSysUserInfo.SysUserId;
  76. item.ReportTime = DateTime.Now;
  77. await item.UpdateIncludeNowAsync(new[] { nameof(item.Status), nameof(item.ReportSysUserId), nameof(item.ReportTime) });
  78. }
  79. }
  80. /// <summary>
  81. /// 添加特殊学生佐证材料
  82. /// </summary>
  83. /// <param name="input"></param>
  84. /// <returns></returns>
  85. public async Task AddAttachment(AddExamOrgDataReportAttachmentInput input)
  86. {
  87. var orgId = CurrentSysUserInfo.SysOrgId;
  88. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.SourceId);
  89. bool hasItem = true;
  90. if (item == null)
  91. {
  92. hasItem = false;
  93. var examOrg = await _rep.Change<ExamOrg>().DetachedEntities.FirstOrDefaultAsync(t => t.SysOrgId == orgId && t.ExamPlanId == input.ExamPlanId) ?? throw Oops.Oh(ErrorCode.E2001, "监测机构");
  94. item = new()
  95. {
  96. Type = input.Type,
  97. Status = DataReportStatus.UNREPORT,
  98. ExamOrgId = examOrg.Id,
  99. ExamPlanId = input.ExamPlanId,
  100. SysOrgId = orgId,
  101. };
  102. }
  103. item.Attachments = AttachmentUtil.InsertInto(item.Attachments, input.Adapt<AttachmentItem>());
  104. if (hasItem)
  105. {
  106. await item.UpdateIncludeAsync(new[] { nameof(item.Attachments) });
  107. }
  108. else
  109. {
  110. await _rep.InsertNowAsync(item);
  111. }
  112. }
  113. /// <summary>
  114. /// 删除特殊学生佐证材料
  115. /// </summary>
  116. /// <param name="input"></param>
  117. /// <returns></returns>
  118. public async Task DelAttachment(DeleteAttachmentInput input)
  119. {
  120. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.SourceId) ?? throw Oops.Oh(ErrorCode.E2001);
  121. var attachments = AttachmentUtil.GetList(item.Attachments);
  122. var a = attachments.FirstOrDefault(t => t.FileId == input.FileId);
  123. if (a != null)
  124. {
  125. attachments.Remove(a);
  126. item.Attachments = JSON.Serialize(attachments);
  127. await item.UpdateIncludeAsync(new[] { nameof(item.Attachments) });
  128. await _resourceFileService.Del(new() { Id = a.FileId });
  129. if (a.ThumbFileId.HasValue && a.ThumbFileId > 0)
  130. {
  131. await _resourceFileService.Del(new() { Id = a.ThumbFileId.Value });
  132. }
  133. }
  134. }
  135. /// <summary>
  136. /// 退回机构上报
  137. /// </summary>
  138. /// <param name="input"></param>
  139. /// <returns></returns>
  140. public async Task ReportReject(BaseId input)
  141. {
  142. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id);
  143. item.Status = DataReportStatus.REJECTED;
  144. await item.UpdateIncludeAsync(new[] { nameof(item.Status) });
  145. }
  146. /// <summary>
  147. /// 获取监测机构上报类型列表
  148. /// </summary>
  149. /// <param name="examPlanId"></param>
  150. /// <param name="sysOrgId">不传取当前机构ID</param>
  151. /// <returns></returns>
  152. public async Task<List<ExamOrgDataReportUnionOutput>> GetListByExamPlanId(int examPlanId, short? sysOrgId = null)
  153. {
  154. var orgId = sysOrgId ?? CurrentSysUserInfo.SysOrgId;
  155. var pitems = await _rep.Change<ExamDataReport>().DetachedEntities.Where(t => t.ExamPlanId == examPlanId).ToListAsync();
  156. var oitems = await _rep.DetachedEntities.Include(t => t.ExamOrg).ThenInclude(t => t.SysOrg)
  157. .Include(t => t.ReportSysUser)
  158. .Where(t => t.ExamOrg.SysOrgId == orgId && t.ExamOrg.ExamPlanId == examPlanId)
  159. .ToListAsync();
  160. var items = from p in pitems
  161. join o in oitems on p.Type equals o.Type into temp
  162. from u in temp.DefaultIfEmpty()
  163. select new ExamOrgDataReportUnionOutput
  164. {
  165. ExamDataReportId = p.Id,
  166. ExamDataReport = p.Adapt<ExamDataReportOutput>(),
  167. ExamOrgDataReportId = u?.Id ?? 0,
  168. ExamOrgDataReport = u == null ? new() : u.Adapt<ExamOrgDataReportOutput>(),
  169. SysOrgId = orgId,
  170. IsExpired = p.EndTime < DateTime.Now,
  171. };
  172. return items.ToList();
  173. }
  174. /// <summary>
  175. /// 根据监测计划ID和上报类型获取机构上报信息
  176. /// </summary>
  177. /// <param name="type"></param>
  178. /// <param name="examPlanId"></param>
  179. /// <param name="sysOrgId"></param>
  180. /// <returns></returns>
  181. public async Task<ExamOrgDataReportUnionOutput> GetByTypeExamPlanId(DataReportType type, int examPlanId, short? sysOrgId = null)
  182. {
  183. var items = await GetListByExamPlanId(examPlanId, sysOrgId);
  184. var item = items.FirstOrDefault(t => t.ExamDataReport.Type == type) ?? throw Oops.Oh(ErrorCode.E2001);
  185. item.ExamPlan = (await _rep.Change<ExamPlan>().DetachedEntities.FirstOrDefaultAsync(t => t.Id == examPlanId))?.Adapt<ExamPlanOutput>();
  186. return item;
  187. }
  188. /// <summary>
  189. /// 分页查询机构上报类型列表
  190. /// </summary>
  191. /// <param name="input"></param>
  192. /// <returns></returns>
  193. public async Task<PageResult<ExamPlanOrgDataReportOutput>> QueryPageList(ExamOrgDataReportPageInput input)
  194. {
  195. string where = "T2.type = @type AND T1.sys_org_id = @sysOrgId AND (T4.status = 2 OR T4.status = 3) AND T4.is_deleted = 0";
  196. if (input.Status.HasValue)
  197. {
  198. where = $"{where} AND T3.status = {(short)input.Status.Value}";
  199. }
  200. if (input.SemesterId.HasValue)
  201. {
  202. where = $"{where} AND T4.semester_id = {(short)input.SemesterId.Value}";
  203. }
  204. if (!string.IsNullOrEmpty(input.Name?.Trim()))
  205. {
  206. where = $"{where} AND T4.full_name LIKE '%{input.Name.Trim()}%'";
  207. }
  208. var p = new
  209. {
  210. CurrentSysUserInfo.SysOrgId,
  211. input.PageSize,
  212. PageOffset = (input.PageIndex - 1) * input.PageSize,
  213. input.Type,
  214. input.Status,
  215. input.Name,
  216. input.SemesterId,
  217. };
  218. var totalCount = await _rep.SqlScalarAsync<int>($@"
  219. SELECT COUNT(1) AS total_count
  220. FROM exam_org AS T1
  221. JOIN exam_data_report AS T2 ON T1.exam_plan_id = T2.exam_plan_id
  222. INNER JOIN exam_plan AS T4 ON T1.exam_plan_id = T4.id
  223. LEFT JOIN exam_org_data_report AS T3 ON T1.id = T3.exam_org_id AND T2.type = T3.type
  224. WHERE {where}", p);
  225. var items = await _rep.SqlQueriesAsync<ExamPlanOrgDataReportOutput>($@"
  226. SELECT ROW_NUMBER() OVER (ORDER BY T1.exam_plan_id DESC, T2.type) AS `row_number`,
  227. T1.exam_plan_id, T1.sys_org_id, T2.type, T2.begin_time, T2.end_time, T2.`status` AS exam_status,
  228. IFNULL(T3.`status`, 1) AS `status`, T3.report_sys_user_id, T5.`name` AS report_sys_user_name, T3.report_time,
  229. T4.full_name AS exam_plan_full_name, T4.`name` AS exam_plan_name, T4.short_name AS exam_plan_short_name,
  230. T4.`status` AS exam_plan_status, T4.education_stage, T4.semester_id
  231. FROM exam_org AS T1
  232. JOIN exam_data_report AS T2 ON T1.exam_plan_id = T2.exam_plan_id
  233. JOIN exam_plan AS T4 ON T1.exam_plan_id = T4.id
  234. LEFT JOIN exam_org_data_report AS T3 ON T1.id = T3.exam_org_id AND T2.type = T3.type
  235. LEFT JOIN sys_user AS T5 ON T3.report_sys_user_id = T5.id
  236. WHERE {where}
  237. LIMIT @pageSize OFFSET @pageOffset;", p);
  238. PageResult<ExamPlanOrgDataReportOutput> ret = new()
  239. {
  240. PageIndex = input.PageIndex,
  241. PageSize = input.PageSize,
  242. TotalCount = totalCount,
  243. Items = items
  244. };
  245. return ret;
  246. }
  247. public async Task RefreshFileSize()
  248. {
  249. var items = await _rep.Entities.ToListAsync();
  250. foreach (var item in items)
  251. {
  252. var ats = AttachmentUtil.GetList(item.Attachments);
  253. foreach (var at in ats)
  254. {
  255. var file = await _rep.Change<ResourceFile>().DetachedEntities.FirstOrDefaultAsync(t => t.Id == at.FileId) ?? throw new Exception("fuckfuck");
  256. at.FileSize = file.FileSize;
  257. }
  258. item.Attachments = JSON.Serialize(ats);
  259. }
  260. await _rep.UpdateAsync(items);
  261. }
  262. }