|
@@ -75,30 +75,13 @@ class QuestionnaireService extends CrudService {
|
|
|
|
|
|
/**
|
|
|
* 导出问卷
|
|
|
- * @param {Object} Object 数据集合
|
|
|
+ * @param {Object} { range, direction, questionnaireid, modelList } 数据集合
|
|
|
* @property Object range 学生的查询范围
|
|
|
* @property String direction horizontal横向/vertical纵向
|
|
|
* @property String questionnaireid 问卷id
|
|
|
* @property Array modelList 要导出的字段,学生和问卷都在这里, 学生的table:student, 问卷的table:questionnaire
|
|
|
*/
|
|
|
async export({ range, direction, questionnaireid, modelList }) {
|
|
|
- // 获取问卷
|
|
|
- let questionnaire = await this.questionnairemodel.findById(questionnaireid);
|
|
|
- if (!questionnaire) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到问卷信息'); }
|
|
|
- questionnaire = JSON.parse(JSON.stringify(questionnaire));
|
|
|
- // 获取学生
|
|
|
- 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));
|
|
|
- // 再获取问卷
|
|
|
- let questAnswerList = await this.ctx.model.Uploadquestion.find({
|
|
|
- ...range,
|
|
|
- questionnaireid,
|
|
|
- });
|
|
|
- if (!questAnswerList) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何完成的问卷'); }
|
|
|
- questAnswerList = JSON.parse(JSON.stringify(questAnswerList));
|
|
|
- // fn,根据范围+问卷 得出文件名
|
|
|
- const fn = await this.toSetFileName(questionnaire.name, range);
|
|
|
// 将期批班转换
|
|
|
modelList = modelList.map(i => {
|
|
|
const { model } = i;
|
|
@@ -107,6 +90,29 @@ class QuestionnaireService extends CrudService {
|
|
|
else if (model === 'classid') i.model = 'classname';
|
|
|
return i;
|
|
|
});
|
|
|
+ // 获取问卷
|
|
|
+ let questionnaire = await this.questionnairemodel.findById(questionnaireid);
|
|
|
+ if (!questionnaire) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到问卷信息'); }
|
|
|
+ questionnaire = JSON.parse(JSON.stringify(questionnaire));
|
|
|
+ // 修改条件,termid变成数组了,需要一个一个查出来
|
|
|
+ const { planid, termid, batchid, classid } = range;
|
|
|
+ const queryObject = {};
|
|
|
+ if (planid) queryObject.planid = planid;
|
|
|
+ if (batchid) queryObject.batchid = batchid;
|
|
|
+ if (classid) queryObject.classid = classid;
|
|
|
+ const studentList = [];
|
|
|
+ const questAnswerList = [];
|
|
|
+ for (const t of termid) {
|
|
|
+ queryObject.termid = t;
|
|
|
+ const obj = await this.toGetData(queryObject, questionnaireid);
|
|
|
+ if (!obj) continue;
|
|
|
+ const { studentList: stuList, questAnswerList: qaList } = obj;
|
|
|
+ if (stuList) studentList.push(...stuList);
|
|
|
+ if (qaList) questAnswerList.push(...qaList);
|
|
|
+ }
|
|
|
+ // fn,根据范围+问卷 得出文件名
|
|
|
+ const fn = await this.toSetFileName(questionnaire.name, range);
|
|
|
+
|
|
|
let excelData;
|
|
|
if (direction === 'horizontal') {
|
|
|
excelData = this.horizontalSetData(studentList, questAnswerList, modelList);
|
|
@@ -121,6 +127,29 @@ class QuestionnaireService extends CrudService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取学生与学生回答的问卷
|
|
|
+ * @param {Object} condition 查询学生和学生回答问卷的条件
|
|
|
+ * @param {String} questionnaireid 问卷id
|
|
|
+ */
|
|
|
+ async toGetData(condition, questionnaireid) {
|
|
|
+ // 获取学生
|
|
|
+ let { data: studentList } = await this.ctx.service.student.query(condition);
|
|
|
+ if (studentList.length <= 0) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何学生信息'); }
|
|
|
+ studentList = JSON.parse(JSON.stringify(studentList));
|
|
|
+ // 再获取问卷答案
|
|
|
+ let questAnswerList = await this.ctx.model.Uploadquestion.find({
|
|
|
+ ...condition,
|
|
|
+ questionnaireid,
|
|
|
+ });
|
|
|
+ if (!questAnswerList) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何完成的问卷'); }
|
|
|
+ questAnswerList = JSON.parse(JSON.stringify(questAnswerList));
|
|
|
+ const obj = {};
|
|
|
+ if (studentList) obj.studentList = studentList;
|
|
|
+ if (questAnswerList) obj.questAnswerList = questAnswerList;
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获得导出文件的文件名
|
|
|
* @param {String} questName 问卷名
|
|
@@ -130,22 +159,6 @@ class QuestionnaireService extends CrudService {
|
|
|
async toSetFileName(questName, range) {
|
|
|
let fn = `-${questName}`;
|
|
|
const { planid, termid, batchid, classid } = range;
|
|
|
- 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) {
|
|
@@ -153,27 +166,70 @@ class QuestionnaireService extends CrudService {
|
|
|
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}`;
|
|
|
+ } else if (batchid) {
|
|
|
+ const tid = _.head(termid);
|
|
|
+ const obj = await this.toGetFn(tid, batchid);
|
|
|
+ if (obj) {
|
|
|
+ const { term, batch } = obj;
|
|
|
+ if (batch) fn = `第${batch}批${fn}`;
|
|
|
+ if (term) fn = `第${term}期${fn}`;
|
|
|
+ }
|
|
|
+ } else if (termid) {
|
|
|
+ if (termid.length === 1) {
|
|
|
+ const obj = await this.toGetFn(_.head(termid));
|
|
|
+ if (obj) {
|
|
|
+ const { term } = obj;
|
|
|
+ if (term) fn = `第${term}期${fn}`;
|
|
|
+ }
|
|
|
} else {
|
|
|
- fn = `${title}${fn}`;
|
|
|
+ let tStr = '';
|
|
|
+ for (let i = 0; i < termid.length; i++) {
|
|
|
+ const tid = termid[i];
|
|
|
+ const obj = await this.toGetFn(tid);
|
|
|
+ if (obj) {
|
|
|
+ const { term } = obj;
|
|
|
+ if (term) {
|
|
|
+ if (i === 0) { tStr += `${term}期`; } else { tStr += `,${term}期`; }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fn = `${tStr}${fn}`;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ const trainPlan = await this.ctx.model.Trainplan.findById(planid);
|
|
|
+ if (trainPlan) {
|
|
|
+ const { title } = trainPlan;
|
|
|
+ if (title) fn = `${title}${fn}`;
|
|
|
}
|
|
|
}
|
|
|
return fn;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取文件的期/期批
|
|
|
+ * @param {String} termid 期id
|
|
|
+ * @param {String} batchid 批id
|
|
|
+ */
|
|
|
+ async toGetFn(termid, batchid) {
|
|
|
+ const trainPlan = await this.ctx.model.Trainplan.findOne({ 'termnum._id': ObjectId(termid) });
|
|
|
+ if (!trainPlan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息');
|
|
|
+ const { termnum } = trainPlan;
|
|
|
+ if (!termnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划的期信息');
|
|
|
+ const obj = {};
|
|
|
+ if (termid) {
|
|
|
+ const term = termnum.id(termid);
|
|
|
+ if (term) obj.term = term.term;
|
|
|
+ if (batchid) {
|
|
|
+ const { batchnum } = term;
|
|
|
+ const batch = batchnum.id(batchid);
|
|
|
+ if (batch) obj.batch = batch.batch;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 横向组织数据
|
|
|
- * TODO 里面整理方法可以提取出来和纵向一起用
|
|
|
* @param {Array} studentList 学生列表
|
|
|
* @param {Array} questAnswerList 学生回答问卷列表
|
|
|
* @param {Array} modelList 需要导出的字段
|
|
@@ -224,7 +280,6 @@ class QuestionnaireService extends CrudService {
|
|
|
|
|
|
/**
|
|
|
* 纵向组织数据
|
|
|
- * TODO 里面整理方法可以提取出来和横向一起用
|
|
|
* @param {Array} studentList 学生列表
|
|
|
* @param {Array} questAnswerList 学生回答问卷列表
|
|
|
* @param {Array} modelList 需要导出的字段
|