ExamSpecialStudentService.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. using Furion.DatabaseAccessor.Extensions;
  2. using Furion.JsonSerialization;
  3. using NPOI.OpenXmlFormats.Wordprocessing;
  4. using NPOI.SS.UserModel;
  5. using NPOI.XWPF.UserModel;
  6. using System.Dynamic;
  7. using YBEE.EQM.Core;
  8. namespace YBEE.EQM.Application;
  9. /// <summary>
  10. /// 监测特殊学生上报管理服务
  11. /// </summary>
  12. public class ExamSpecialStudentService : IExamSpecialStudentService, ITransient
  13. {
  14. private readonly IRepository<ExamSpecialStudent> _rep;
  15. private readonly IExamGradeService _examGradeService;
  16. private readonly ISysDictDataService _sysDictDataService;
  17. private readonly ISchoolClassService _schoolClassService;
  18. private readonly IResourceFileService _resourceFileService;
  19. private readonly ICourseService _courseService;
  20. public ExamSpecialStudentService(IRepository<ExamSpecialStudent> rep, IExamGradeService examGradeService, ISysDictDataService sysDictDataService, ISchoolClassService schoolClassService, IResourceFileService resourceFileService, ICourseService courseService)
  21. {
  22. _rep = rep;
  23. _examGradeService = examGradeService;
  24. _sysDictDataService = sysDictDataService;
  25. _schoolClassService = schoolClassService;
  26. _resourceFileService = resourceFileService;
  27. _courseService = courseService;
  28. }
  29. #region 批量导入
  30. /// <summary>
  31. /// 上传监测特殊学生批量导入文件
  32. /// </summary>
  33. /// <param name="filePath"></param>
  34. /// <param name="examPlanId"></param>
  35. /// <returns></returns>
  36. public async Task<UploadExamDataOutput<UploadExamSpecialStudentOutput>> Upload(string filePath, int examPlanId)
  37. {
  38. UploadExamDataOutput<UploadExamSpecialStudentOutput> result = new();
  39. try
  40. {
  41. // 已认证过名单
  42. var spStus = await GetList(examPlanId);
  43. using FileStream fs = new(filePath, FileMode.Open, FileAccess.Read);
  44. IWorkbook workbook = ExcelUtil.GetWorkbook(filePath, fs);
  45. var sheet = workbook.GetSheetAt(0);
  46. var rows = sheet.GetRowEnumerator();
  47. #region 验证表结构
  48. // 少于2行验证
  49. if (sheet.LastRowNum < 2)
  50. {
  51. result.ErrorMessage.Add("第一行应为填写说明,第二行应为标题行,请勿修改模板结构。");
  52. return result;
  53. }
  54. // 跳过第一行
  55. rows.MoveNext();
  56. // 读取表头
  57. rows.MoveNext();
  58. IRow headerRow = (IRow)rows.Current;
  59. int index = 0;
  60. int GRADE_INDEX = index++;
  61. int CLASS_INDEX = index++;
  62. int NAME_INDEX = index++;
  63. int CERT_TYPE_INDEX = index++;
  64. int ID_NUM_INDEX = index++;
  65. int GENDER_INDEX = index++;
  66. int REASON_INDEX = index++;
  67. int TEL_INDEX = index++;
  68. int REMARK_INDEX = index;
  69. Dictionary<int, string> headers = new()
  70. {
  71. { GRADE_INDEX, "年级号" },
  72. { CLASS_INDEX, "班级号" },
  73. { NAME_INDEX, "姓名" },
  74. { CERT_TYPE_INDEX, "证件类型" },
  75. { ID_NUM_INDEX, "证件号码" },
  76. { GENDER_INDEX, "性别" },
  77. { REASON_INDEX, "特殊原因" },
  78. { TEL_INDEX, "家长电话" },
  79. { REMARK_INDEX, "备注" },
  80. };
  81. List<string> headerErrors = new();
  82. for (int i = 0; i <= index; i++)
  83. {
  84. if (headerRow.GetCell(i)?.ToString() != headers[i])
  85. {
  86. char letter = (char)('A' + i);
  87. headerErrors.Add(letter.ToString());
  88. }
  89. }
  90. if (headerErrors.Any())
  91. {
  92. string columnErrors = string.Join("、", headerErrors);
  93. result.ErrorMessage.Add($"第2行标题行{columnErrors}列名错误。从A列开始依次应为年级号、班级号、姓名、证件类型、证件号码、特殊原因、家长电话和备注。");
  94. return result;
  95. }
  96. result.StructureCorrect = true;
  97. #endregion
  98. #region 处理数据
  99. // 监测年级
  100. var examGrades = await _examGradeService.GetListByExamPlanId(examPlanId);
  101. // 获取证件类型
  102. var cts = await _sysDictDataService.GetListByDictTypeId(304);
  103. var certificateTypes = cts.ToDictionary(x => x.Name, y => y.Value);
  104. // 读取数据
  105. List<UploadExamSpecialStudentOutput> data = new();
  106. int rn = 0;
  107. while (rows.MoveNext())
  108. {
  109. IRow row = (IRow)rows.Current;
  110. string rv = row.GetCell(0)?.ToString().Trim() ?? "";
  111. rv += row.GetCell(1)?.ToString().Trim() ?? "";
  112. if (rv == "")
  113. {
  114. break;
  115. }
  116. UploadExamSpecialStudentOutput item = new() { RowNumber = ++rn };
  117. // 年级
  118. if (short.TryParse(row.GetCell(GRADE_INDEX)?.ToString(), out short gradeNumber))
  119. {
  120. var g = examGrades.FirstOrDefault(t => t.Grade.GradeNumber == gradeNumber);
  121. if (g == null)
  122. {
  123. item.ErrorMessage.Add($"{headers[GRADE_INDEX]}与监测年级不符");
  124. }
  125. else
  126. {
  127. item.ExamGradeId = g.Id;
  128. item.GradeId = g.GradeId;
  129. }
  130. }
  131. else
  132. {
  133. item.ErrorMessage.Add(headers[CLASS_INDEX]);
  134. }
  135. // 班级
  136. if (short.TryParse(row.GetCell(CLASS_INDEX)?.ToString(), out short classNumber))
  137. {
  138. item.ClassNumber = classNumber;
  139. if (item.ClassNumber < 1 || item.ClassNumber > 35)
  140. {
  141. item.ErrorMessage.Add($"{headers[CLASS_INDEX]}超限");
  142. }
  143. }
  144. else
  145. {
  146. item.ErrorMessage.Add(headers[CLASS_INDEX]);
  147. }
  148. // 姓名
  149. item.Name = (row.GetCell(NAME_INDEX)?.ToString() ?? "").ClearWhitespace();
  150. if (item.Name == "" || item.Name.Length > 100)
  151. {
  152. item.ErrorMessage.Add($"{headers[NAME_INDEX]}未填或超出100字");
  153. }
  154. if (item.Name.Length > 100) { item.Name = item.Name[..100]; }
  155. // 证件类型
  156. item.CertificateTypeName = (row.GetCell(CERT_TYPE_INDEX)?.ToString() ?? "").ClearWhitespace();
  157. if (!(item.CertificateTypeName != "" && certificateTypes.ContainsKey(item.CertificateTypeName)))
  158. {
  159. item.ErrorMessage.Add(headers[CERT_TYPE_INDEX]);
  160. }
  161. else
  162. {
  163. item.CertificateType = (CertificateType)certificateTypes[item.CertificateTypeName];
  164. }
  165. // 证件号码
  166. item.IdNumber = (row.GetCell(ID_NUM_INDEX)?.ToString() ?? "").ClearWhitespace().ToUpper();
  167. if (item.CertificateType == CertificateType.ID_CARD)
  168. {
  169. var idNumberValidate = CertificateNumberValidator.ValidateIdCard(item.IdNumber);
  170. if (!idNumberValidate.Success)
  171. {
  172. item.ErrorMessage.Add($"{item.CertificateTypeName}{idNumberValidate.ErrorMessage}");
  173. }
  174. else
  175. {
  176. item.GenderName = idNumberValidate.Gender == Gender.MALE ? "男" : "女";
  177. item.Gender = idNumberValidate.Gender;
  178. }
  179. }
  180. else
  181. {
  182. // 性别
  183. item.GenderName = (row.GetCell(GENDER_INDEX)?.ToString() ?? "").ClearWhitespace();
  184. if (item.CertificateType != CertificateType.ID_CARD)
  185. {
  186. item.Gender = item.GenderName == "男" ? Gender.MALE : item.GenderName == "女" ? Gender.FEMALE : Gender.UNKNOWN;
  187. }
  188. }
  189. if (item.IdNumber.Length > 50) { item.IdNumber = item.IdNumber[..50]; }
  190. if (spStus.Any(t => t.IdNumber == item.IdNumber))
  191. {
  192. item.ErrorMessage.Add("往期已认定不能重复上报");
  193. }
  194. // 特殊原因
  195. item.ApplyReason = (row.GetCell(REASON_INDEX)?.ToString() ?? "").ClearWhitespace();
  196. if (item.ApplyReason == "" || item.ApplyReason.Length > 2000)
  197. {
  198. item.ErrorMessage.Add($"{headers[REASON_INDEX]}未填或超出2000字");
  199. }
  200. if (item.ApplyReason.Length > 2000) { item.ApplyReason = item.ApplyReason[..2000]; }
  201. // 家长电话
  202. item.PatriarchTel = (row.GetCell(TEL_INDEX)?.ToString() ?? "").ClearWhitespace();
  203. if (item.PatriarchTel == "" || item.PatriarchTel.Length > 50)
  204. {
  205. item.ErrorMessage.Add($"{headers[TEL_INDEX]}未填或超出50字");
  206. }
  207. if (item.PatriarchTel.Length > 50) { item.PatriarchTel = item.PatriarchTel[..50]; }
  208. // 备注
  209. item.Remark = (row.GetCell(REMARK_INDEX)?.ToString() ?? "").ClearWhitespace();
  210. if (item.Remark.Length > 200) { item.Remark = item.Remark[..200]; }
  211. // 行是否验证通过
  212. item.IsSuccess = item.ErrorMessage.Count == 0;
  213. data.Add(item);
  214. result.TotalRowCount++;
  215. if (!item.IsSuccess)
  216. {
  217. result.ErrorRowCount++;
  218. }
  219. }
  220. result.Rows = data;
  221. #endregion
  222. workbook.Close();
  223. fs.Close();
  224. }
  225. catch (Exception ex)
  226. {
  227. throw new Exception(ex.Message);
  228. }
  229. finally
  230. {
  231. File.Delete(filePath);
  232. }
  233. return result;
  234. }
  235. /// <summary>
  236. /// 批量导入监测特殊学生
  237. /// </summary>
  238. /// <param name="input"></param>
  239. /// <returns></returns>
  240. public async Task<int> Import(ImportExamSpecialStudentInput input)
  241. {
  242. var orgId = input.SysOrgId ?? CurrentSysUserInfo.SysOrgId;
  243. var existsIdNumberList = await _rep.DetachedEntities.Where(t => t.ExamPlanId == input.ExamPlanId && t.SysOrgId == orgId).Select(t => t.IdNumber).Distinct().ToListAsync() ?? new List<string>();
  244. //// 删除同一监测计划中监测机构内所有数据
  245. //await _rep.Where(t => t.ExamPlanId == input.ExamPlanId && t.SysOrgId == orgId && t.SysOrgBranchId == input.SysOrgBranchId).ExecuteDeleteAsync();
  246. List<ExamSpecialStudent> items = new();
  247. var spStus = input.Items.Where(t => !existsIdNumberList.Contains(t.IdNumber)).ToList();
  248. var gs = spStus.Select(t => t.ExamGradeId).Distinct().ToList();
  249. var examGrades = await _rep.Change<ExamGrade>().DetachedEntities.Where(t => gs.Contains(t.Id)).Select(t => t.Adapt<ExamGradeOutput>()).ToListAsync();
  250. // 往期已认定记录
  251. var preItems = await _rep.Change<SpecialStudent>().DetachedEntities.Where(t => t.SysOrgId == orgId).ToListAsync();
  252. var ePreItems = await GetList(input.ExamPlanId);
  253. int c = 0;
  254. foreach (var eg in examGrades)
  255. {
  256. var classNumbers = spStus.Where(t => t.ExamGradeId == eg.Id).Select(t => t.ClassNumber).Distinct().ToList();
  257. var classDict = await _schoolClassService.GetImportSchoolClassList(new()
  258. {
  259. SysOrgId = orgId,
  260. SysOrgBranchId = input.SysOrgBranchId,
  261. ExamGrade = eg,
  262. ClassNumberList = classNumbers,
  263. });
  264. var citems = spStus.Where(t => t.ExamGradeId == eg.Id).ToList();
  265. foreach (var ni in citems)
  266. {
  267. var item = ni.Adapt<ExamSpecialStudent>();
  268. item.ExamPlanId = input.ExamPlanId;
  269. item.SysOrgId = orgId;
  270. item.SysOrgBranchId = input.SysOrgBranchId;
  271. item.SchoolClassId = classDict[ni.ClassNumber];
  272. item.Name = item.Name?.ClearWhitespace();
  273. item.IdNumber = item.IdNumber?.ClearWhitespace();
  274. item.IsPreIdentified = preItems.Any(t => t.CertificateType == item.CertificateType && t.IdNumber.ToUpper() == item.IdNumber) || ePreItems.Any(t => t.CertificateType == item.CertificateType && t.IdNumber.ToUpper() == item.IdNumber);
  275. if (item.IsPreIdentified)
  276. {
  277. item.Status = AuditStatus.APPROVED;
  278. }
  279. items.Add(item);
  280. c++;
  281. }
  282. }
  283. await _rep.InsertAsync(items);
  284. return c;
  285. }
  286. #endregion
  287. #region 创建编辑
  288. /// <summary>
  289. /// 添加监测特殊学生
  290. /// </summary>
  291. /// <param name="input"></param>
  292. /// <returns></returns>
  293. public async Task Add(AddExamSpecialStudentInput input)
  294. {
  295. var orgId = CurrentSysUserInfo.SysOrgId;
  296. var isPreIdentified = await _rep.AnyAsync(t => t.ExamPlanId == input.ExamPlanId && t.SysOrgId == orgId && t.IdNumber.ToUpper() == input.IdNumber);
  297. if (isPreIdentified)
  298. {
  299. throw Oops.Oh("往期以认定,不能重复添加");
  300. }
  301. // 检测同一监测计划中同机构内是否有相同证件号码的学生
  302. var sameItems = await _rep.DetachedEntities.Where(t => t.ExamPlanId == input.ExamPlanId && t.SysOrgId == orgId && t.CertificateType == input.CertificateType && t.IdNumber.ToUpper() == input.IdNumber.ToUpper()).ProjectToType<ExamSpecialStudentOutput>().ToListAsync();
  303. if (sameItems.Any())
  304. {
  305. throw Oops.Oh(ErrorCode.E2003, string.Join("、", sameItems.Select(t => $"{t.ExamGrade.Grade.Name}{t.ClassNumber}班{t.Name}")), "证件号码");
  306. }
  307. var examGrade = await _examGradeService.GetById(input.ExamGradeId);
  308. var schoolClass = await _schoolClassService.GetSchoolClass(orgId, input.SysOrgBranchId, examGrade, input.ClassNumber);
  309. var item = input.Adapt<ExamSpecialStudent>();
  310. item.SysOrgId = orgId;
  311. item.SchoolClassId = schoolClass.Id;
  312. item.Name = item.Name?.ClearWhitespace();
  313. item.IdNumber = item.IdNumber?.ClearWhitespace();
  314. if ((await _rep.Change<SpecialStudent>().DetachedEntities.AnyAsync(t => t.SysOrgId == orgId && t.CertificateType == item.CertificateType && t.IdNumber.ToUpper() == item.IdNumber)) || isPreIdentified)
  315. {
  316. item.IsPreIdentified = true;
  317. item.Status = AuditStatus.APPROVED;
  318. }
  319. ////item.Attachments = new List<AttachmentItem>() { new () { FileId = 1, FileName = "a.png", FileExtName = ".png" } }.ToJson();
  320. //var k = new List<AttachmentItem>() { new() { FileId = 1, FileName = "a.png", FileExtName = ".png" } };
  321. //item.Attachments = JSON.Serialize(k);
  322. await item.InsertAsync();
  323. }
  324. /// <summary>
  325. /// 更新监测特殊学生
  326. /// </summary>
  327. /// <param name="input"></param>
  328. /// <returns></returns>
  329. public async Task Update(UpdateExamSpecialStudentInput input)
  330. {
  331. var oitem = await _rep.DetachedEntities.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  332. var examGrade = await _examGradeService.GetById(oitem.ExamGradeId);
  333. var schoolClass = await _schoolClassService.GetSchoolClass(oitem.SysOrgId, input.SysOrgBranchId, examGrade, input.ClassNumber);
  334. var item = input.Adapt<ExamSpecialStudent>();
  335. item.SchoolClassId = schoolClass.Id;
  336. item.Name = item.Name?.ClearWhitespace();
  337. item.IdNumber = item.IdNumber?.ClearWhitespace();
  338. if ((await _rep.Change<SpecialStudent>().DetachedEntities.AnyAsync(t => t.SysOrgId == item.SysOrgId && t.CertificateType == item.CertificateType && t.IdNumber.ToUpper() == item.IdNumber)) ||
  339. (await _rep.AnyAsync(t => t.ExamPlanId == oitem.ExamPlanId && t.SysOrgId == item.SysOrgId && t.CertificateType == item.CertificateType && t.IdNumber.ToUpper() == item.IdNumber))
  340. )
  341. {
  342. item.IsPreIdentified = true;
  343. item.Status = AuditStatus.APPROVED;
  344. }
  345. //item.IsPreIdentified = await _rep.Change<SpecialStudent>().DetachedEntities.AnyAsync(t => t.SysOrgId == item.SysOrgId && t.CertificateType == item.CertificateType && t.IdNumber.ToUpper() == item.IdNumber);
  346. await item.UpdateIncludeAsync(new[] {
  347. nameof(item.SchoolClassId),
  348. nameof(item.ClassNumber),
  349. nameof(item.Name),
  350. nameof(item.CertificateType),
  351. nameof(item.IdNumber),
  352. nameof(item.Gender),
  353. nameof(item.BirthDate),
  354. nameof(item.StudentNumber),
  355. nameof(item.Remark),
  356. nameof(item.SysOrgBranchId),
  357. nameof(item.ApplyReason),
  358. nameof(item.PatriarchName),
  359. nameof(item.PatriarchTel),
  360. nameof(item.IsPreIdentified),
  361. });
  362. }
  363. /// <summary>
  364. /// 添加特殊学生佐证材料
  365. /// </summary>
  366. /// <param name="input"></param>
  367. /// <returns></returns>
  368. public async Task AddAttachment(AddAttachmentInput input)
  369. {
  370. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.SourceId) ?? throw Oops.Oh(ErrorCode.E2001);
  371. //var attachments = JSON.Deserialize<List<AttachmentItem>>(item.Attachments);
  372. //attachments.Add(input.Adapt<AttachmentItem>());
  373. //item.Attachments = JSON.Serialize(attachments);
  374. item.Attachments = AttachmentUtil.InsertInto(item.Attachments, input.Adapt<AttachmentItem>());
  375. await item.UpdateIncludeAsync(new[] { nameof(item.Attachments) });
  376. }
  377. /// <summary>
  378. /// 删除特殊学生佐证材料
  379. /// </summary>
  380. /// <param name="input"></param>
  381. /// <returns></returns>
  382. public async Task DelAttachment(DeleteAttachmentInput input)
  383. {
  384. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.SourceId) ?? throw Oops.Oh(ErrorCode.E2001);
  385. var attachments = AttachmentUtil.GetList(item.Attachments);
  386. var a = attachments.FirstOrDefault(t => t.FileId == input.FileId);
  387. if (a != null)
  388. {
  389. attachments.Remove(a);
  390. item.Attachments = JSON.Serialize(attachments);
  391. await item.UpdateIncludeAsync(new[] { nameof(item.Attachments) });
  392. await _resourceFileService.Del(new() { Id = a.FileId });
  393. if (a.ThumbFileId.HasValue && a.ThumbFileId > 0)
  394. {
  395. await _resourceFileService.Del(new() { Id = a.ThumbFileId.Value });
  396. }
  397. }
  398. }
  399. /// <summary>
  400. /// 验证特殊学生佐证材料
  401. /// </summary>
  402. /// <param name="examPlanId"></param>
  403. /// <returns></returns>
  404. public async Task<bool> VerifyAttachment(int examPlanId)
  405. {
  406. var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == examPlanId && t.SysOrgId == CurrentSysUserInfo.SysOrgId && t.IsPreIdentified == false).ProjectToType<ExamSpecialStudentOutput>().ToListAsync();
  407. if (items.Any(t => t.AttachmentList.Count == 0))
  408. {
  409. return false;
  410. }
  411. return true;
  412. }
  413. /// <summary>
  414. /// 删除监测特殊学生
  415. /// </summary>
  416. /// <param name="input"></param>
  417. /// <returns></returns>
  418. public async Task Del(BaseId input)
  419. {
  420. var item = await _rep.FirstOrDefaultAsync(t => t.Id == input.Id) ?? throw Oops.Oh(ErrorCode.E2001);
  421. if (item.IsPreIdentified)
  422. {
  423. throw Oops.Oh(ErrorCode.E2006);
  424. }
  425. var attachments = JSON.Deserialize<List<AttachmentItem>>(item.Attachments);
  426. if (attachments != null && attachments.Any())
  427. {
  428. foreach (var attachment in attachments)
  429. {
  430. await _resourceFileService.Del(new() { Id = attachment.FileId });
  431. if (attachment.ThumbFileId.HasValue && attachment.ThumbFileId > 0)
  432. {
  433. await _resourceFileService.Del(new() { Id = attachment.ThumbFileId.Value });
  434. }
  435. }
  436. }
  437. await item.DeleteAsync();
  438. }
  439. /// <summary>
  440. /// 清空监测特殊学生
  441. /// </summary>
  442. /// <param name="input"></param>
  443. /// <returns></returns>
  444. public async Task Clear(ClearExamSpecialStudentInput input)
  445. {
  446. var orgId = CurrentSysUserInfo.SysOrgId;
  447. await _rep.Where(t => t.ExamPlanId == input.ExamPlanId && t.SysOrgId == orgId && t.IsPreIdentified == false).ExecuteDeleteAsync();
  448. }
  449. /// <summary>
  450. /// 导出监测特殊学生上报打印表格
  451. /// </summary>
  452. /// <param name="examPlanId"></param>
  453. /// <returns></returns>
  454. public async Task<byte[]> ExportPrintTable(int examPlanId)
  455. {
  456. var items = await _rep.DetachedEntities.Include(t => t.Grade).Include(t => t.SysOrg).Where(t => t.ExamPlanId == examPlanId && t.SysOrgId == CurrentSysUserInfo.SysOrgId).ToListAsync();
  457. if (!items.Any())
  458. {
  459. throw Oops.Oh(ErrorCode.E2001);
  460. }
  461. // 获取证件类型
  462. var cts = await _sysDictDataService.GetListByDictTypeId(304);
  463. var certificateTypes = cts.ToDictionary(x => (CertificateType)x.Value, y => y.Name);
  464. XWPFDocument doc = new();
  465. doc.Document.body.sectPr = new();
  466. var sectPr = doc.Document.body.sectPr;
  467. sectPr.pgSz.orient = ST_PageOrientation.portrait;
  468. sectPr.pgSz.w = 11906;
  469. sectPr.pgSz.h = 16838;
  470. sectPr.pgMar.left = sectPr.pgMar.right = 1080;
  471. sectPr.pgMar.top = sectPr.pgMar.bottom = 1440;
  472. var gradeItems = items.GroupBy(t => t.GradeId).OrderBy(t => t.Key).ToList();
  473. int pi = 0;
  474. foreach (var gitem in gradeItems)
  475. {
  476. var gradeFirst = gitem.First();
  477. // 多于一个年级分多页
  478. if (0 < pi++)
  479. {
  480. doc.CreateParagraph().IsPageBreak = true;
  481. }
  482. // 标题行
  483. XWPFParagraph pHeader = doc.CreateParagraph();
  484. pHeader.Alignment = ParagraphAlignment.CENTER;
  485. pHeader.SpacingLineRule = LineSpacingRule.AUTO;
  486. pHeader.SetSpacingBetween(1.5, LineSpacingRule.AUTO);
  487. var pHeaderRun = pHeader.CreateRun();
  488. pHeaderRun.SetText($"{gradeFirst.SysOrg.FullName.Replace("重庆市", "")}特殊学生明细表({gradeFirst.Grade.Name})");
  489. pHeaderRun.FontSize = 18;
  490. pHeaderRun.IsBold = true;
  491. // 空行
  492. CreateBlanParagraph();
  493. // 签字行
  494. XWPFParagraph pSign = doc.CreateParagraph();
  495. pSign.SpacingLineRule = LineSpacingRule.AUTO;
  496. pSign.SetSpacingBetween(2, LineSpacingRule.AUTO);
  497. var pSignRun = pSign.CreateRun();
  498. pSignRun.SetText("学校盖章: 纪检负责人: 校长签字:");
  499. pSignRun.FontSize = 12;
  500. // 签字行
  501. XWPFParagraph pCount = doc.CreateParagraph();
  502. pCount.SpacingLineRule = LineSpacingRule.AUTO;
  503. pCount.SetSpacingBetween(2, LineSpacingRule.AUTO);
  504. var dt = DateTime.Now;
  505. var pCountRun = pCount.CreateRun();
  506. pCountRun.SetText($"填表人: 联系电话: 特殊学生计({gitem.Count()})人 {dt.Year}年{dt.Month}月{dt.Day}日");
  507. pCountRun.FontSize = 12;
  508. // 空行
  509. CreateBlanParagraph(1);
  510. #region 生成表格
  511. const int COLUMN_COUNT = 7;
  512. XWPFTable table = doc.CreateTable(gitem.Count() + 1, COLUMN_COUNT);
  513. var tableLayout = table.GetCTTbl().tblPr.AddNewTblLayout();
  514. tableLayout.type = ST_TblLayoutType.@fixed;
  515. table.Width = 5080;
  516. table.SetColumnWidth(0, 280);
  517. table.SetColumnWidth(1, 280);
  518. table.SetColumnWidth(2, 540);
  519. table.SetColumnWidth(3, 580);
  520. table.SetColumnWidth(4, 1070);
  521. table.SetColumnWidth(5, 1660);
  522. table.SetColumnWidth(6, 660);
  523. int ri = 0;
  524. table.GetRow(ri).GetCTRow().AddNewTrPr().AddNewTrHeight().val = 480;
  525. table.GetRow(ri).GetCell(0).SetParagraph(SetCellText(table, "序号", ParagraphAlignment.CENTER, true));
  526. table.GetRow(ri).GetCell(1).SetParagraph(SetCellText(table, "班级", ParagraphAlignment.CENTER, true));
  527. table.GetRow(ri).GetCell(2).SetParagraph(SetCellText(table, "学生姓名", ParagraphAlignment.CENTER, true));
  528. table.GetRow(ri).GetCell(3).SetParagraph(SetCellText(table, "证件类型", ParagraphAlignment.CENTER, true));
  529. table.GetRow(ri).GetCell(4).SetParagraph(SetCellText(table, "证件号码", ParagraphAlignment.CENTER, true));
  530. table.GetRow(ri).GetCell(5).SetParagraph(SetCellText(table, "特殊原因", ParagraphAlignment.CENTER, true));
  531. table.GetRow(ri).GetCell(6).SetParagraph(SetCellText(table, "家长电话", ParagraphAlignment.CENTER, true));
  532. for (int ci = 0; ci < COLUMN_COUNT; ci++)
  533. {
  534. table.GetRow(ri).GetCell(ci).GetCTTc().AddNewTcPr().AddNewVAlign().val = ST_VerticalJc.center;
  535. }
  536. int tableRowNumber = 1;
  537. foreach (var item in gitem)
  538. {
  539. ri++;
  540. table.GetRow(ri).GetCTRow().AddNewTrPr().AddNewTrHeight().val = 480;
  541. table.GetRow(ri).GetCell(0).SetParagraph(SetCellText(table, $"{tableRowNumber++}", ParagraphAlignment.CENTER));
  542. table.GetRow(ri).GetCell(1).SetParagraph(SetCellText(table, $"{item.ClassNumber}", ParagraphAlignment.CENTER));
  543. table.GetRow(ri).GetCell(2).SetParagraph(SetCellText(table, item.Name, ParagraphAlignment.CENTER));
  544. table.GetRow(ri).GetCell(3).SetParagraph(SetCellText(table, certificateTypes[item.CertificateType], ParagraphAlignment.CENTER));
  545. table.GetRow(ri).GetCell(4).SetParagraph(SetCellText(table, item.IdNumber, ParagraphAlignment.CENTER));
  546. table.GetRow(ri).GetCell(5).SetParagraph(SetCellText(table, item.ApplyReason, ParagraphAlignment.LEFT));
  547. table.GetRow(ri).GetCell(6).SetParagraph(SetCellText(table, item.PatriarchTel, ParagraphAlignment.CENTER));
  548. for (int ci = 0; ci < COLUMN_COUNT; ci++)
  549. {
  550. table.GetRow(ri).GetCell(ci).GetCTTc().AddNewTcPr().AddNewVAlign().val = ST_VerticalJc.center;
  551. }
  552. }
  553. // 空行
  554. CreateBlanParagraph();
  555. #endregion
  556. }
  557. // 生成流
  558. using MemoryStream ms = new();
  559. doc.Write(ms);
  560. doc.Close();
  561. var retDoc = ms.ToArray();
  562. ms.Close();
  563. return retDoc;
  564. // 创建空行
  565. void CreateBlanParagraph(double lineSpacing = 1.5)
  566. {
  567. var p = doc.CreateParagraph();
  568. p.SpacingLineRule = LineSpacingRule.AUTO;
  569. p.SetSpacingBetween(lineSpacing, LineSpacingRule.AUTO);
  570. p.CreateRun().FontSize = 12;
  571. }
  572. }
  573. #endregion
  574. #region 查询统计
  575. /// <summary>
  576. /// 分页查询监测特殊学生列表
  577. /// </summary>
  578. /// <param name="input"></param>
  579. /// <returns></returns>
  580. public async Task<PageResult<ExamSpecialStudentOutput>> QueryPageList(ExamSpecialStudentPageInput input)
  581. {
  582. var orgId = input.SysOrgId ?? CurrentSysUserInfo.SysOrgId;
  583. var query = GetQueryBase(input);
  584. //query = query.Where(t => t.ExamPlanId == input.ExamPlanId && t.SysOrgId == orgId);
  585. var ret = await query.OrderBy(t => t.GradeId).ThenBy(t => t.ClassNumber).ThenBy(t => t.Id).ProjectToType<ExamSpecialStudentOutput>().ToADPagedListAsync(input.PageIndex, input.PageSize);
  586. return ret;
  587. }
  588. /// <summary>
  589. /// 分页查询监测特殊学生列表
  590. /// </summary>
  591. /// <param name="input"></param>
  592. /// <returns></returns>
  593. public async Task<PageResult<ExamSpecialStudentOutput>> QueryAuditPageList(ExamSpecialStudentPageInput input)
  594. {
  595. var orgId = input.SysOrgId ?? CurrentSysUserInfo.SysOrgId;
  596. var query = GetQueryBase(input);
  597. //query = query.Where(t => t.ExamPlanId == input.ExamPlanId && t.SysOrgId == orgId);
  598. var ret = await query.OrderBy(t => t.GradeId).ThenBy(t => t.ClassNumber).ThenBy(t => t.Id).ProjectToType<ExamSpecialStudentOutput>().ToADPagedListAsync(input.PageIndex, input.PageSize);
  599. return ret;
  600. }
  601. /// <summary>
  602. /// 获取机构班级特殊学生上报人数统计列表
  603. /// </summary>
  604. /// <param name="examPlanId"></param>
  605. /// <param name="sysOrgId"></param>
  606. /// <returns></returns>
  607. public async Task<ExamSpecialStudentCountOutput> GetOrgGradeClassStudentCount(int examPlanId, short? sysOrgId = null)
  608. {
  609. var orgId = sysOrgId ?? CurrentSysUserInfo.SysOrgId;
  610. var items = await _rep.DetachedEntities.Where(t => t.ExamPlanId == examPlanId && t.SysOrgId == orgId)
  611. .GroupBy(t => new { t.GradeId, t.ClassNumber })
  612. .Select(t => new
  613. {
  614. t.Key.GradeId,
  615. t.Key.ClassNumber,
  616. t.FirstOrDefault().Grade,
  617. Count = t.Count(),
  618. })
  619. .ToListAsync();
  620. var cols = items.Select(t => t.ClassNumber).Distinct().ToList();
  621. var retItems = items.ToPivotList(c => c.ClassNumber, r => r.GradeId, d => d.Any() ? d.Sum(x => x.Count) : 0);
  622. foreach (var item in retItems)
  623. {
  624. item.Grade = items.FirstOrDefault(t => t.GradeId == item.GradeId).Grade;
  625. item.GradeTotal = items.Where(t => t.GradeId == item.GradeId).Sum(x => x.Count);
  626. }
  627. int total = retItems.Sum(x => x.GradeTotal);
  628. IDictionary<string, object> totalItem = new ExpandoObject();
  629. totalItem.Add("GradeId", 9999);
  630. totalItem.Add("Grade", new { Id = 9999, Name = "合计" });
  631. totalItem.Add("GradeTotal", total);
  632. retItems.Add(totalItem);
  633. return new()
  634. {
  635. ClassNumberList = cols,
  636. Items = retItems,
  637. Total = total,
  638. };
  639. }
  640. /// <summary>
  641. /// 获取状态数量
  642. /// </summary>
  643. /// <returns></returns>
  644. public async Task<List<StatusCount>> QueryStatusCount(ExamSpecialStudentPageInput input)
  645. {
  646. var query = GetQueryBase(input);
  647. if (query == null)
  648. {
  649. return new List<StatusCount>();
  650. }
  651. var counts = await query.GroupBy(t => t.Status).Select(t => new StatusCount { Status = (int)t.Key, Count = t.Count() }).ToListAsync();
  652. return counts;
  653. }
  654. #endregion
  655. public async Task RefreshFileSize()
  656. {
  657. var items = await _rep.Entities.ToListAsync();
  658. foreach (var item in items)
  659. {
  660. var ats = AttachmentUtil.GetList(item.Attachments);
  661. foreach (var at in ats)
  662. {
  663. var file = await _rep.Change<ResourceFile>().DetachedEntities.FirstOrDefaultAsync(t => t.Id == at.FileId) ?? throw new Exception("fuckfuck");
  664. at.FileSize = file.FileSize;
  665. }
  666. item.Attachments = JSON.Serialize(ats);
  667. }
  668. await _rep.UpdateAsync(items);
  669. }
  670. #region 私有方法
  671. /// <summary>
  672. /// 构建查询
  673. /// </summary>
  674. /// <param name="input"></param>
  675. /// <returns></returns>
  676. private IQueryable<ExamSpecialStudent> GetQueryBase(ExamSpecialStudentPageInput input)
  677. {
  678. var orgId = input.SysOrgId ?? CurrentSysUserInfo.SysOrgId;
  679. var name = !string.IsNullOrEmpty(input.Name?.Trim());
  680. var idNumber = !string.IsNullOrEmpty(input.IdNumber?.Trim());
  681. var applyReason = !string.IsNullOrEmpty(input.ApplyReason?.Trim());
  682. var patriarchTel = !string.IsNullOrEmpty(input.PatriarchTel?.Trim());
  683. var studentNumber = !string.IsNullOrEmpty(input.StudentNumber?.Trim());
  684. var query = _rep.DetachedEntities.Where(t => t.ExamPlanId == input.ExamPlanId && t.SysOrgId == orgId)
  685. .Where((name, u => EF.Functions.Like(u.Name, $"%{input.Name.Trim()}%")))
  686. .Where((idNumber, u => EF.Functions.Like(u.IdNumber, $"%{input.IdNumber.Trim()}%")))
  687. .Where((studentNumber, u => EF.Functions.Like(u.StudentNumber, $"%{input.StudentNumber.Trim()}%")))
  688. .Where((applyReason, u => EF.Functions.Like(u.ApplyReason, $"%{input.ApplyReason.Trim()}%")))
  689. .Where((patriarchTel, u => EF.Functions.Like(u.PatriarchTel, $"%{input.PatriarchTel.Trim()}%")))
  690. .Where(input.Status.HasValue, t => t.Status == input.Status)
  691. .Where(input.CertificateType.HasValue, t => t.CertificateType == input.CertificateType)
  692. .Where(input.Gender.HasValue, t => t.Gender == input.Gender)
  693. .Where(input.GradeId.HasValue, t => t.GradeId == input.GradeId)
  694. .Where(input.ClassNumber.HasValue, t => t.ClassNumber == input.ClassNumber)
  695. .Where(input.SysOrgBranchId.HasValue, t => t.SchoolClass.SysOrgBranchId == input.SysOrgBranchId);
  696. return query;
  697. }
  698. /// <summary>
  699. /// 设置单元格文本
  700. /// </summary>
  701. /// <param name="table"></param>
  702. /// <param name="text"></param>
  703. /// <param name="align"></param>
  704. /// <param name="isBold"></param>
  705. /// <returns></returns>
  706. private static XWPFParagraph SetCellText(XWPFTable table, string text, ParagraphAlignment align = ParagraphAlignment.CENTER, bool isBold = false)
  707. {
  708. CT_P para = new();
  709. XWPFParagraph pCell = new(para, table.Body)
  710. {
  711. Alignment = align,
  712. VerticalAlignment = TextAlignment.CENTER,
  713. //SpacingLineRule = LineSpacingRule.AUTO,
  714. };
  715. //pCell.setSpacingBetween(1.5, LineSpacingRule.AUTO);
  716. XWPFRun r1 = pCell.CreateRun();
  717. r1.SetText(text);
  718. r1.FontSize = 11;
  719. r1.FontFamily = "宋体";
  720. r1.IsBold = isBold;
  721. //r1c1.SetTextPosition(20);//设置高度
  722. return pCell;
  723. }
  724. /// <summary>
  725. /// 获取当前机构指定监测计划中的特殊学生列表
  726. /// </summary>
  727. /// <param name="examPlanId"></param>
  728. /// <returns></returns>
  729. private async Task<List<ExamSpecialStudent>> GetList(int examPlanId)
  730. {
  731. var items = await _rep.Where(t => t.ExamPlanId == examPlanId && t.SysOrgId == CurrentSysUserInfo.SysOrgId).ToListAsync();
  732. return items;
  733. }
  734. #endregion
  735. }