task.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. const { baseUrl } = _.get(this.ctx.app.config, 'mission');
  12. if (baseUrl) this.missionBase = baseUrl;
  13. }
  14. // 建立任务
  15. async toExport(body) {
  16. const { range } = body;
  17. const { fn } = await this.getFnDir(range);
  18. const data = {
  19. title: fn,
  20. params: {
  21. project: 'center',
  22. service: 'task',
  23. method: 'export',
  24. body,
  25. },
  26. };
  27. if (this.missionBase) {
  28. const url = `${this.missionBase}/api/mission`;
  29. const res = await this.ctx.curl(url, {
  30. method: 'post',
  31. headers: {
  32. 'content-type': 'application/json',
  33. },
  34. data,
  35. dataType: 'json',
  36. });
  37. if (res.status !== 200 || res.data.errcode !== 0) {
  38. throw new BusinessError(ErrorCode.SERVICE_FAULT, '创建任务失败');
  39. }
  40. } else {
  41. throw new BusinessError(ErrorCode.SERVICE_FAULT, '未找到任务项目设置');
  42. }
  43. }
  44. async export({ range, subid, missionid }) {
  45. assert(missionid, '缺少任务信息,无法执行任务');
  46. try {
  47. // 找到学生
  48. // 修改条件,termid变成数组了,需要一个一个查出来
  49. const { planid, termid, batchid, classid } = range;
  50. const queryObject = {};
  51. if (planid) queryObject.planid = planid;
  52. if (batchid) queryObject.batchid = batchid;
  53. if (classid) queryObject.classid = classid;
  54. let studentList = [];
  55. for (const t of termid) {
  56. queryObject.termid = t;
  57. const stuList = await this.ctx.service.student.query(queryObject);
  58. studentList.push(...stuList);
  59. }
  60. if (studentList.length <= 0) {
  61. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何学生信息');
  62. }
  63. studentList = JSON.parse(JSON.stringify(studentList));
  64. this.ctx.service.util.updateProcess(missionid, '25');
  65. console.log('作业导出=>25%');
  66. // 找到作业
  67. const taskList = await this.getTaskInfo(range, subid);
  68. this.ctx.service.util.updateProcess(missionid, '50');
  69. console.log('作业导出=>50%');
  70. // prefix 最少 科目名称/人 可能出现 选择到期:期/班/科目/人; 选择到批:期/批/班/科目/人; 选择到班:科目/人 ;选择计划:期/批/班/科目/人
  71. // 找到应该是哪种模式的文件夹,同时可以获得压缩包的名称
  72. const { fn, prefix: pn } = await this.getFnDir(range);
  73. // 循环作业,之后匹配所需要的信息
  74. const arr = [];
  75. for (const task of taskList) {
  76. const { lessonname, studentid, picurl } = task;
  77. const stu = studentList.find(f => f._id === studentid);
  78. if (!stu) continue;
  79. const { name: studentname, termname, batchname, classname } = stu;
  80. let picarr = [];
  81. if (_.isString(picurl)) {
  82. picarr = [ picurl ];
  83. } else {
  84. picarr = picurl;
  85. }
  86. for (const uri of picarr) {
  87. const obj = { uri };
  88. const keys = Object.keys(pn);
  89. let prefix = `${lessonname}/${studentname}/`;
  90. if (keys.includes('class')) {
  91. prefix = `${classname}/${prefix}`;
  92. }
  93. if (keys.includes('batchid')) {
  94. prefix = `${batchname}/${prefix}`;
  95. }
  96. if (keys.includes('term')) {
  97. prefix = `${termname}/${prefix}`;
  98. }
  99. obj.prefix = prefix;
  100. arr.push(obj);
  101. }
  102. }
  103. this.ctx.service.util.updateProcess(missionid, '75');
  104. console.log('作业导出=>75%');
  105. const res = await this.ctx.service.util.toZip(arr, fn);
  106. if (!res) {
  107. console.error(`${moment().format('YYYY-MM-DD HH:SS:mm')} ${fn} 导出失败`);
  108. throw new BusinessError(ErrorCode.SERVICE_FAULT, `${fn}导出失败`);
  109. }
  110. this.ctx.service.util.updateProcess(missionid, '100', '2', {
  111. uri: res,
  112. });
  113. console.log('作业导出=>100%');
  114. } catch (error) {
  115. this.ctx.service.util.updateProcess(missionid, undefined, '3');
  116. }
  117. }
  118. async getTaskInfo(range, subid) {
  119. const { planid, termid, batchid, classid } = range;
  120. let lessonList = [];
  121. // 找到指定范围的课表
  122. if (classid) {
  123. lessonList = await this.ctx.model.Lesson.find({ classid });
  124. } else if (batchid) {
  125. lessonList = await this.ctx.model.Lesson.find({ batchid });
  126. } else if (termid) {
  127. lessonList = await this.ctx.model.Lesson.find({ termid: { $in: termid } });
  128. } else if (planid) {
  129. const trainPlan = await this.ctx.model.Trainplan.findById(planid);
  130. if (!trainPlan) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息'); }
  131. let { termnum } = trainPlan;
  132. if (!termnum) {
  133. throw new BusinessError(
  134. ErrorCode.DATA_NOT_EXIST,
  135. '未找到年度计划下的期信息'
  136. );
  137. }
  138. termnum = JSON.parse(JSON.stringify(termnum));
  139. const termids = termnum.map(i => i._id);
  140. lessonList = await this.ctx.model.Lesson.find({ termid: { $in: termids } });
  141. }
  142. lessonList = JSON.parse(JSON.stringify(lessonList));
  143. // 不需要其他属性,只要课表安排
  144. lessonList = lessonList.map(i => i.lessons);
  145. lessonList = lessonList.flat();
  146. // 过滤出有subid且 !==''的课程
  147. lessonList = lessonList.filter(f => f.subid && f.subid !== '');
  148. // 如果指定某科,就把这科过滤出来
  149. if (subid) lessonList = lessonList.filter(f => f.subid === subid);
  150. // 找作业
  151. const lessonids = lessonList.map(i => i._id);
  152. let taskList = await this.ctx.model.Uploadtask.find({
  153. lessonid: { $in: lessonids },
  154. });
  155. taskList = JSON.parse(JSON.stringify(taskList));
  156. const arr = [];
  157. for (const task of taskList) {
  158. const r = arr.find(f => f.studentid === task.studentid && f.lessonid === task.lessonid);
  159. if (!r) arr.push(task);
  160. }
  161. return JSON.parse(JSON.stringify(arr));
  162. }
  163. async getFnDir(range) {
  164. const { planid, termid, batchid, classid } = range;
  165. let fn = '作业';
  166. const prefix = { class: true };
  167. // 只有班级
  168. if (classid) {
  169. const cla = await this.ctx.service.class.fetch({ id: classid });
  170. if (cla) {
  171. const { name, term } = cla;
  172. if (name) fn = `${name.includes('班') ? name : `${name}班`}${fn}`;
  173. if (term) fn = `第${term}期${fn}`;
  174. }
  175. } else if (batchid) {
  176. const tid = _.head(termid);
  177. const obj = await this.toGetFn(tid, batchid);
  178. if (obj) {
  179. const { term, batch } = obj;
  180. if (batch) fn = `第${batch}批${fn}`;
  181. if (term) fn = `第${term}期${fn}`;
  182. }
  183. prefix.term = true;
  184. prefix.batch = true;
  185. } else if (termid) {
  186. if (termid.length === 1) {
  187. const obj = await this.toGetFn(_.head(termid));
  188. if (obj) {
  189. const { term } = obj;
  190. if (term) fn = `第${term}期${fn}`;
  191. }
  192. } else {
  193. let tStr = '';
  194. for (let i = 0; i < termid.length; i++) {
  195. const tid = termid[i];
  196. const obj = await this.toGetFn(tid);
  197. if (obj) {
  198. const { term } = obj;
  199. if (term) {
  200. if (i === 0) { tStr += `${term}期`; } else { tStr += `,${term}期`; }
  201. }
  202. }
  203. }
  204. fn = `${tStr}${fn}`;
  205. }
  206. prefix.term = true;
  207. } else {
  208. const trainPlan = await this.ctx.model.Trainplan.findById(planid);
  209. if (trainPlan) {
  210. const { title } = trainPlan;
  211. if (title) fn = `${title}${fn}`;
  212. }
  213. prefix.term = true;
  214. prefix.batch = true;
  215. }
  216. return { fn, prefix };
  217. }
  218. async toGetFn(termid, batchid) {
  219. const trainPlan = await this.ctx.model.Trainplan.findOne({ 'termnum._id': ObjectId(termid) });
  220. if (!trainPlan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息');
  221. const { termnum } = trainPlan;
  222. if (!termnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划的期信息');
  223. const obj = {};
  224. if (termid) {
  225. const term = termnum.id(termid);
  226. if (term) obj.term = term.term;
  227. if (batchid) {
  228. const { batchnum } = term;
  229. const batch = batchnum.id(batchid);
  230. if (batch) obj.batch = batch.batch;
  231. }
  232. }
  233. return obj;
  234. }
  235. }
  236. module.exports = TaskService;