ExamOrgDataReportService.cs 11 KB

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