|
@@ -476,34 +476,104 @@ class StudentService extends CrudService {
|
|
|
}
|
|
|
|
|
|
// 导出学生 目前:拓展训练保险用
|
|
|
- async exportStudent({ type = 'insurance', ...data }) {
|
|
|
- console.log(type, data);
|
|
|
+ async exportStudent(body) {
|
|
|
+ let { model, ...data } = body;
|
|
|
+ // 整理model
|
|
|
+ model = model.map(i => {
|
|
|
+ const { zh, model } = i;
|
|
|
+ if (zh && model) {
|
|
|
+ const obj = {};
|
|
|
+ obj.header = zh;
|
|
|
+ if (model === 'termid') obj.key = 'termname';
|
|
|
+ else if (model === 'batchid') obj.key = 'batchname';
|
|
|
+ else if (model === 'classid') obj.key = 'classname';
|
|
|
+ else obj.key = model;
|
|
|
+ obj.width = 20;
|
|
|
+ return obj;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ model = _.compact(model);
|
|
|
+ // 请求数据
|
|
|
let { data: studentList } = await this.query(data);
|
|
|
studentList = JSON.parse(JSON.stringify(studentList));
|
|
|
let ids = studentList.map(i => i.classid);
|
|
|
ids = _.uniq(ids);
|
|
|
- const classList = [];
|
|
|
- // 此处可以优化,将班级整理,查出来,循环中find
|
|
|
- for (const id of ids) {
|
|
|
- const cla = await this.ctx.service.class.fetch({ id });
|
|
|
+ ids = _.compact(ids);
|
|
|
+ ids = ids.map(i => ObjectId(i));
|
|
|
+ const classList = await this.ctx.model.Class.find({ _id: { $in: ids } });
|
|
|
+ for (const stu of studentList) {
|
|
|
+ const { classid } = stu;
|
|
|
+ if (!classid) continue;
|
|
|
+ const cla = classList.find(f => ObjectId(classid).equals(f._id));
|
|
|
if (!cla) continue;
|
|
|
- if (type === 'insurance') { cla.date = moment(cla.startdate).add(1, 'd').format('YYYY-MM-DD'); }
|
|
|
- classList.push(cla);
|
|
|
+ const { startdate } = cla;
|
|
|
+ if (!startdate) continue;
|
|
|
+ stu.insurance = moment(startdate).add(1, 'd').format('YYYY-MM-DD');
|
|
|
}
|
|
|
- studentList = studentList.map(i => {
|
|
|
- const c = classList.find(f => ObjectId(i.classid).equals(f._id));
|
|
|
- if (c) i.date = c.date;
|
|
|
- return i;
|
|
|
- });
|
|
|
- const meta = this.metaBx(type);
|
|
|
let fn = '学生名单';
|
|
|
- const head = _.head(classList);
|
|
|
- if (head) {
|
|
|
- if (type === 'class') { fn = `${head.term}期-${head.batch}批-${head.name}${fn}`; } else fn = `${head.term}期-${head.batch}批${fn}`;
|
|
|
+ // 因为是递进下来, batchid和classid并列,并非递进
|
|
|
+ const { planid, termid, batchid, classid } = data;
|
|
|
+ const trainPlanInfo = async (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;
|
|
|
+ };
|
|
|
+ if (classid) {
|
|
|
+ // 文件名称: 期, 班
|
|
|
+ // 班级名上面有直接拽来
|
|
|
+ const cla = classList.find(f => ObjectId(classid).equals(f._id));
|
|
|
+ if (cla) {
|
|
|
+ const { name, termid } = cla;
|
|
|
+ if (name) fn = `${name.includes('班') ? name : `${name}班`}${fn}`;
|
|
|
+ if (termid) {
|
|
|
+ const obj = await trainPlanInfo(termid);
|
|
|
+ if (obj) {
|
|
|
+ const { term } = obj;
|
|
|
+ console.log(obj);
|
|
|
+ fn = `第${term}期${fn}`;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else if (batchid) {
|
|
|
+ // 文件名称,期,批
|
|
|
+ const obj = await trainPlanInfo(termid, batchid);
|
|
|
+ if (obj) {
|
|
|
+ const { term, batch } = obj;
|
|
|
+ if (batch) fn = `第${batch}批${fn}`;
|
|
|
+ if (term) fn = `第${term}期${fn}`;
|
|
|
+ }
|
|
|
+ } else if (termid) {
|
|
|
+ // 文件名称: 期
|
|
|
+ const obj = await trainPlanInfo(termid);
|
|
|
+ if (obj) {
|
|
|
+ const { term } = obj;
|
|
|
+ if (term) fn = `第${term}期${fn}`;
|
|
|
+ }
|
|
|
+ } else if (planid) {
|
|
|
+ // 文件名称:该计划标题
|
|
|
+ const trainPlan = await this.ctx.model.Trainplan.findById(planid);
|
|
|
+ if (trainPlan) {
|
|
|
+ const { title } = trainPlan;
|
|
|
+ if (title) fn = `${title}${fn}`;
|
|
|
+ }
|
|
|
}
|
|
|
- return await this.ctx.service.util.toExcel(studentList, meta, fn);
|
|
|
+ return await this.ctx.service.util.toExcel(studentList, model, fn);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ // excel导出表头设置
|
|
|
metaBx(type) {
|
|
|
const header = [
|
|
|
{
|
|
@@ -511,64 +581,7 @@ class StudentService extends CrudService {
|
|
|
key: 'name',
|
|
|
width: 20,
|
|
|
},
|
|
|
- {
|
|
|
- header: '性别',
|
|
|
- key: 'gender',
|
|
|
- width: 10,
|
|
|
- },
|
|
|
- {
|
|
|
- header: '民族',
|
|
|
- key: 'nation',
|
|
|
- width: 20,
|
|
|
- },
|
|
|
- {
|
|
|
- header: '身份证号',
|
|
|
- key: 'id_number',
|
|
|
- width: 20,
|
|
|
- },
|
|
|
- {
|
|
|
- header: '期',
|
|
|
- key: 'termname',
|
|
|
- width: 20,
|
|
|
- },
|
|
|
- {
|
|
|
- header: '批次',
|
|
|
- key: 'batchname',
|
|
|
- width: 20,
|
|
|
- },
|
|
|
- {
|
|
|
- header: '班级',
|
|
|
- key: 'classname',
|
|
|
- width: 20,
|
|
|
- },
|
|
|
- {
|
|
|
- header: '学校',
|
|
|
- key: 'school_name',
|
|
|
- width: 20,
|
|
|
- },
|
|
|
- {
|
|
|
- header: '院系',
|
|
|
- key: 'faculty',
|
|
|
- width: 20,
|
|
|
- },
|
|
|
- {
|
|
|
- header: '专业',
|
|
|
- key: 'major',
|
|
|
- width: 20,
|
|
|
- },
|
|
|
- {
|
|
|
- header: '手机号',
|
|
|
- key: 'phone',
|
|
|
- width: 20,
|
|
|
- },
|
|
|
];
|
|
|
- if (type === 'insurance') {
|
|
|
- header.splice(1, 0, {
|
|
|
- header: '拓训日期',
|
|
|
- key: 'date',
|
|
|
- width: 20,
|
|
|
- });
|
|
|
- }
|
|
|
return header;
|
|
|
}
|
|
|
}
|