task.js 8.8 KB

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