task.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // 修改条件,termid变成数组了,需要一个一个查出来
  16. const { planid, termid, batchid, classid } = range;
  17. const queryObject = {};
  18. if (planid) queryObject.planid = planid;
  19. if (batchid) queryObject.batchid = batchid;
  20. if (classid) queryObject.classid = classid;
  21. let studentList = [];
  22. for (const t of termid) {
  23. queryObject.termid = t;
  24. const { data: stuList } = await this.ctx.service.student.query(queryObject);
  25. studentList.push(...stuList);
  26. }
  27. if (studentList.length <= 0) {
  28. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何学生信息');
  29. }
  30. studentList = JSON.parse(JSON.stringify(studentList));
  31. // 找到作业
  32. const taskList = await this.getTaskInfo(range, subid);
  33. // prefix 最少 科目名称/人 可能出现 选择到期:期/班/科目/人; 选择到批:期/批/班/科目/人; 选择到班:科目/人 ;选择计划:期/批/班/科目/人
  34. // 找到应该是哪种模式的文件夹,同时可以获得压缩包的名称
  35. const { fn, prefix: pn } = await this.getFnDir(range);
  36. // 循环作业,之后匹配所需要的信息
  37. const arr = [];
  38. for (const task of taskList) {
  39. const { lessonname, studentid, picurl } = task;
  40. const stu = studentList.find(f => f._id === studentid);
  41. if (!stu) continue;
  42. const { name: studentname, termname, batchname, classname } = stu;
  43. let picarr = [];
  44. if (_.isString(picurl)) {
  45. picarr = [ picurl ];
  46. } else {
  47. picarr = picurl;
  48. }
  49. for (const uri of picarr) {
  50. const obj = { uri };
  51. const keys = Object.keys(pn);
  52. let prefix = `${lessonname}/${studentname}/`;
  53. if (keys.includes('class')) {
  54. prefix = `${classname}/${prefix}`;
  55. }
  56. if (keys.includes('batchid')) {
  57. prefix = `${batchname}/${prefix}`;
  58. }
  59. if (keys.includes('term')) {
  60. prefix = `${termname}/${prefix}`;
  61. }
  62. obj.prefix = prefix;
  63. arr.push(obj);
  64. }
  65. }
  66. return await this.ctx.service.util.toZip(arr, fn);
  67. }
  68. async getTaskInfo(range, subid) {
  69. const { planid, termid, batchid, classid } = range;
  70. let lessonList = [];
  71. // 找到指定范围的课表
  72. if (classid) {
  73. lessonList = await this.ctx.model.Lesson.find({ classid });
  74. } else if (batchid) {
  75. lessonList = await this.ctx.model.Lesson.find({ batchid });
  76. } else if (termid) {
  77. lessonList = await this.ctx.model.Lesson.find({ termid: { $in: termid } });
  78. } else if (planid) {
  79. const trainPlan = await this.ctx.model.Trainplan.findById(planid);
  80. if (!trainPlan) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息'); }
  81. let { termnum } = trainPlan;
  82. if (!termnum) {
  83. throw new BusinessError(
  84. ErrorCode.DATA_NOT_EXIST,
  85. '未找到年度计划下的期信息'
  86. );
  87. }
  88. termnum = JSON.parse(JSON.stringify(termnum));
  89. const termids = termnum.map(i => i._id);
  90. lessonList = await this.ctx.model.Lesson.find({ termid: { $in: termids } });
  91. }
  92. lessonList = JSON.parse(JSON.stringify(lessonList));
  93. // 不需要其他属性,只要课表安排
  94. lessonList = lessonList.map(i => i.lessons);
  95. lessonList = lessonList.flat();
  96. // 过滤出有subid且 !==''的课程
  97. lessonList = lessonList.filter(f => f.subid && f.subid !== '');
  98. // 如果指定某科,就把这科过滤出来
  99. if (subid) lessonList = lessonList.filter(f => f.subid === subid);
  100. // 找作业
  101. const lessonids = lessonList.map(i => i._id);
  102. let taskList = await this.ctx.model.Uploadtask.find({
  103. lessonid: { $in: lessonids },
  104. });
  105. taskList = JSON.parse(JSON.stringify(taskList));
  106. const arr = [];
  107. for (const task of taskList) {
  108. const r = arr.find(f => f.studentid === task.studentid && f.lessonid === task.lessonid);
  109. if (!r) arr.push(task);
  110. }
  111. return JSON.parse(JSON.stringify(arr));
  112. }
  113. async getFnDir(range) {
  114. const { planid, termid, batchid, classid } = range;
  115. let fn = '作业';
  116. const prefix = { class: true };
  117. // 只有班级
  118. if (classid) {
  119. const cla = await this.ctx.service.class.fetch({ id: classid });
  120. if (cla) {
  121. const { name, term } = cla;
  122. if (name) fn = `${name.includes('班') ? name : `${name}班`}${fn}`;
  123. if (term) fn = `第${term}期${fn}`;
  124. }
  125. } else if (batchid) {
  126. const tid = _.head(termid);
  127. const obj = await this.toGetFn(tid, batchid);
  128. if (obj) {
  129. const { term, batch } = obj;
  130. if (batch) fn = `第${batch}批${fn}`;
  131. if (term) fn = `第${term}期${fn}`;
  132. }
  133. prefix.term = true;
  134. prefix.batch = true;
  135. } else if (termid) {
  136. if (termid.length === 1) {
  137. const obj = await this.toGetFn(_.head(termid));
  138. if (obj) {
  139. const { term } = obj;
  140. if (term) fn = `第${term}期${fn}`;
  141. }
  142. } else {
  143. let tStr = '';
  144. for (let i = 0; i < termid.length; i++) {
  145. const tid = termid[i];
  146. const obj = await this.toGetFn(tid);
  147. if (obj) {
  148. const { term } = obj;
  149. if (term) {
  150. if (i === 0) { tStr += `${term}期`; } else { tStr += `,${term}期`; }
  151. }
  152. }
  153. }
  154. fn = `${tStr}${fn}`;
  155. }
  156. prefix.term = true;
  157. } else {
  158. const trainPlan = await this.ctx.model.Trainplan.findById(planid);
  159. if (trainPlan) {
  160. const { title } = trainPlan;
  161. if (title) fn = `${title}${fn}`;
  162. }
  163. prefix.term = true;
  164. prefix.batch = true;
  165. }
  166. return { fn, prefix };
  167. }
  168. async toGetFn(termid, batchid) {
  169. const trainPlan = await this.ctx.model.Trainplan.findOne({ 'termnum._id': ObjectId(termid) });
  170. if (!trainPlan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息');
  171. const { termnum } = trainPlan;
  172. if (!termnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划的期信息');
  173. const obj = {};
  174. if (termid) {
  175. const term = termnum.id(termid);
  176. if (term) obj.term = term.term;
  177. if (batchid) {
  178. const { batchnum } = term;
  179. const batch = batchnum.id(batchid);
  180. if (batch) obj.batch = batch.batch;
  181. }
  182. }
  183. return obj;
  184. }
  185. }
  186. module.exports = TaskService;