task.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class TaskService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'task');
  10. this.model = this.ctx.model.Task;
  11. }
  12. async export({ range, subid }) {
  13. console.log(range, subid);
  14. // 找到学生
  15. let { data: studentList } = await this.ctx.service.student.query(range);
  16. if (studentList.length <= 0) {
  17. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何学生信息');
  18. }
  19. studentList = JSON.parse(JSON.stringify(studentList));
  20. // 找到作业
  21. const taskList = await this.getTaskInfo(range, subid);
  22. // prefix 最少 科目名称/人 可能出现 选择到期:期/班/科目/人; 选择到批:期/批/班/科目/人; 选择到班:科目/人 ;选择计划:期/批/班/科目/人
  23. // 找到应该是哪种模式的文件夹,同时可以获得压缩包的名称
  24. const { fn, prefix: pn } = await this.getFnDir(range);
  25. // 循环作业,之后匹配所需要的信息
  26. const arr = [];
  27. for (const task of taskList) {
  28. const { lessonname, studentid, picurl } = task;
  29. const stu = studentList.find(f => f._id === studentid);
  30. if (!stu) continue;
  31. const { name: studentname, termname, batchname, classname } = stu;
  32. let picarr = [];
  33. if (_.isString(picurl)) {
  34. picarr = [ picurl ];
  35. } else {
  36. picarr = picurl;
  37. }
  38. for (const uri of picarr) {
  39. const obj = { uri };
  40. const keys = Object.keys(pn);
  41. let prefix = `${lessonname}/${studentname}/`;
  42. if (keys.includes('class')) {
  43. prefix = `${classname}/${prefix}`;
  44. }
  45. if (keys.includes('batchid')) {
  46. prefix = `${batchname}/${prefix}`;
  47. }
  48. if (keys.includes('term')) {
  49. prefix = `${termname}/${prefix}`;
  50. }
  51. obj.prefix = prefix;
  52. arr.push(obj);
  53. }
  54. }
  55. return await this.ctx.service.util.toZip(arr, fn);
  56. }
  57. async getTaskInfo(range, subid) {
  58. const { planid, termid, batchid, classid } = range;
  59. let lessonList = [];
  60. // 找到指定范围的课表
  61. if (classid) {
  62. lessonList = await this.ctx.model.Lesson.find({ classid });
  63. } else if (batchid) {
  64. lessonList = await this.ctx.model.Lesson.find({ batchid });
  65. } else if (termid) {
  66. lessonList = await this.ctx.model.Lesson.find({ termid });
  67. } else if (planid) {
  68. const trainPlan = await this.ctx.model.Trainplan.findById(planid);
  69. if (!trainPlan) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息'); }
  70. let { termnum } = trainPlan;
  71. if (!termnum) {
  72. throw new BusinessError(
  73. ErrorCode.DATA_NOT_EXIST,
  74. '未找到年度计划下的期信息'
  75. );
  76. }
  77. termnum = JSON.parse(JSON.stringify(termnum));
  78. const termids = termnum.map(i => i._id);
  79. lessonList = await this.ctx.model.Lesson.find({ termid: { $in: termids } });
  80. }
  81. lessonList = JSON.parse(JSON.stringify(lessonList));
  82. // 不需要其他属性,只要课表安排
  83. lessonList = lessonList.map(i => i.lessons);
  84. lessonList = lessonList.flat();
  85. // 过滤出有subid且 !==''的课程
  86. lessonList = lessonList.filter(f => f.subid && f.subid !== '');
  87. // 如果指定某科,就把这科过滤出来
  88. if (subid) lessonList = lessonList.filter(f => f.subid === subid);
  89. // 找作业
  90. const lessonids = lessonList.map(i => i._id);
  91. let taskList = await this.ctx.model.Uploadtask.find({
  92. lessonid: { $in: lessonids },
  93. });
  94. taskList = JSON.parse(JSON.stringify(taskList));
  95. const arr = [];
  96. for (const task of taskList) {
  97. const r = arr.find(f => f.studentid === task.studentid && f.lessonid === task.lessonid);
  98. if (!r) arr.push(task);
  99. }
  100. return JSON.parse(JSON.stringify(arr));
  101. }
  102. async getFnDir(range) {
  103. const { planid, termid, batchid, classid } = range;
  104. let fn = '作业';
  105. const prefix = {};
  106. const getData = (termid, batchid, termnum) => {
  107. let res = '';
  108. if (termid) {
  109. const termInfo = termnum.id(termid);
  110. if (!termInfo) return res;
  111. const { term, batchnum } = termInfo;
  112. if (term) res = `第${term}期${res}`;
  113. if (batchid && batchnum) {
  114. const batchInfo = batchnum.id(batchid);
  115. if (!batchInfo) return res;
  116. const { batch } = batchInfo;
  117. res = `${res}第${batch}批`;
  118. }
  119. }
  120. return res;
  121. };
  122. // 只有班级
  123. if (classid) {
  124. const cla = await this.ctx.service.class.fetch({ id: classid });
  125. if (cla) {
  126. const { name, term } = cla;
  127. if (name) fn = `${name.includes('班') ? name : `${name}班`}${fn}`;
  128. if (term) fn = `第${term}期${fn}`;
  129. }
  130. } else {
  131. const condition = {};
  132. if (planid) condition._id = ObjectId(planid);
  133. if (termid) condition['termnum._id'] = ObjectId(termid);
  134. if (batchid) condition['termnum.batchnum._id'] = ObjectId(batchid);
  135. const trainPlan = await this.ctx.model.Trainplan.findOne(condition);
  136. const { termnum, title } = trainPlan;
  137. if (!termnum) return;
  138. if (termid || batchid) {
  139. const r = getData(termid, batchid, termnum);
  140. fn = `${r}${fn}`;
  141. prefix.class = true;
  142. if (batchid) {
  143. prefix.term = true;
  144. prefix.batch = true;
  145. } else {
  146. prefix.term = true;
  147. }
  148. } else {
  149. fn = `${title}${fn}`;
  150. prefix.term = true;
  151. prefix.batch = true;
  152. prefix.class = true;
  153. }
  154. }
  155. return { fn, prefix };
  156. }
  157. }
  158. module.exports = TaskService;