'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; class TaskService extends CrudService { constructor(ctx) { super(ctx, 'task'); this.model = this.ctx.model.Task; } async export({ range, subid }) { console.log(range, subid); // 找到学生 let { data: studentList } = await this.ctx.service.student.query(range); if (studentList.length <= 0) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何学生信息'); } studentList = JSON.parse(JSON.stringify(studentList)); // 找到作业 const taskList = await this.getTaskInfo(range, subid); // prefix 最少 科目名称/人 可能出现 选择到期:期/班/科目/人; 选择到批:期/批/班/科目/人; 选择到班:科目/人 ;选择计划:期/批/班/科目/人 // 找到应该是哪种模式的文件夹,同时可以获得压缩包的名称 const { fn, prefix: pn } = await this.getFnDir(range); // 循环作业,之后匹配所需要的信息 const arr = []; for (const task of taskList) { const { lessonname, studentid, picurl } = task; const stu = studentList.find(f => f._id === studentid); if (!stu) continue; const { name: studentname, termname, batchname, classname } = stu; let picarr = []; if (_.isString(picurl)) { picarr = [ picurl ]; } else { picarr = picurl; } for (const uri of picarr) { const obj = { uri }; const keys = Object.keys(pn); let prefix = `${lessonname}/${studentname}/`; if (keys.includes('class')) { prefix = `${classname}/${prefix}`; } if (keys.includes('batchid')) { prefix = `${batchname}/${prefix}`; } if (keys.includes('term')) { prefix = `${termname}/${prefix}`; } obj.prefix = prefix; arr.push(obj); } } return await this.ctx.service.util.toZip(arr, fn); } async getTaskInfo(range, subid) { const { planid, termid, batchid, classid } = range; let lessonList = []; // 找到指定范围的课表 if (classid) { lessonList = await this.ctx.model.Lesson.find({ classid }); } else if (batchid) { lessonList = await this.ctx.model.Lesson.find({ batchid }); } else if (termid) { lessonList = await this.ctx.model.Lesson.find({ termid }); } else if (planid) { const trainPlan = await this.ctx.model.Trainplan.findById(planid); if (!trainPlan) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息'); } let { termnum } = trainPlan; if (!termnum) { throw new BusinessError( ErrorCode.DATA_NOT_EXIST, '未找到年度计划下的期信息' ); } termnum = JSON.parse(JSON.stringify(termnum)); const termids = termnum.map(i => i._id); lessonList = await this.ctx.model.Lesson.find({ termid: { $in: termids } }); } lessonList = JSON.parse(JSON.stringify(lessonList)); // 不需要其他属性,只要课表安排 lessonList = lessonList.map(i => i.lessons); lessonList = lessonList.flat(); // 过滤出有subid且 !==''的课程 lessonList = lessonList.filter(f => f.subid && f.subid !== ''); // 如果指定某科,就把这科过滤出来 if (subid) lessonList = lessonList.filter(f => f.subid === subid); // 找作业 const lessonids = lessonList.map(i => i._id); let taskList = await this.ctx.model.Uploadtask.find({ lessonid: { $in: lessonids }, }); taskList = JSON.parse(JSON.stringify(taskList)); const arr = []; for (const task of taskList) { const r = arr.find(f => f.studentid === task.studentid && f.lessonid === task.lessonid); if (r) arr.push(task); } // taskList = taskList.map(i => { // const r = lessonList.find(i => i._id === i.lessonid); // if (r) i.lessonname = r.subname; // return i; // }); return JSON.parse(JSON.stringify(arr)); } async getFnDir(range) { const { planid, termid, batchid, classid } = range; let fn = '作业'; const prefix = {}; const getData = (termid, batchid, termnum) => { let res = ''; if (termid) { const termInfo = termnum.id(termid); if (!termInfo) return res; const { term, batchnum } = termInfo; if (term) res = `第${term}期${res}`; if (batchid && batchnum) { const batchInfo = batchnum.id(batchid); if (!batchInfo) return res; const { batch } = batchInfo; res = `${res}第${batch}批`; } } return res; }; // 只有班级 if (classid) { const cla = await this.ctx.service.class.fetch({ id: classid }); if (cla) { const { name, term } = cla; if (name) fn = `${name.includes('班') ? name : `${name}班`}${fn}`; if (term) fn = `第${term}期${fn}`; } } else { const condition = {}; if (planid) condition._id = ObjectId(planid); if (termid) condition['termnum._id'] = ObjectId(termid); if (batchid) condition['termnum.batchnum._id'] = ObjectId(batchid); const trainPlan = await this.ctx.model.Trainplan.findOne(condition); const { termnum, title } = trainPlan; if (!termnum) return; if (termid || batchid) { const r = getData(termid, batchid, termnum); fn = `${r}${fn}`; prefix.class = true; if (batchid) { prefix.term = true; prefix.batch = true; } else { prefix.term = true; } } else { fn = `${title}${fn}`; prefix.term = true; prefix.batch = true; prefix.class = true; } } return { fn, prefix }; } } module.exports = TaskService;