'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 ExperienceService extends CrudService { constructor(ctx) { super(ctx, 'experience'); this.model = this.ctx.model.Experience; } async query(data, { skip = 0, limit = 0 } = {}) { let res = await this.model.find(data).populate({ path: 'studentid', select: 'name job' }).skip(parseInt(skip)) .limit(parseInt(limit)); if (res.length > 0) { res = JSON.parse(JSON.stringify(res)); res = res.map(i => { const { studentid } = i; if (studentid && _.isObject(studentid)) { const { name, job } = studentid; i.stuname = name; i.stujob = job; } return i; }); } return res; } async create(data) { const { studentid } = data; let res; const r = await this.model.findOne({ studentid }); if (r) { const { title, content } = data; r.title = title; r.content = content; res = await r.save(); } else { res = await this.model.create(data); } return res; } async exportDocx(data) { const { planid, termid, batchid, classid, studentid, id } = data; // 从小到大判断 // res是最后的结果,可能是Object也可能是Array // 作者拼接:能直接获取学生姓名,班级名称,需要单独查下期数 let res; let fn = ''; 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 (id) { const r = await this.model.findById(id).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' }); if (!r) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定范围的培训心得'); const { term } = await trainPlanInfo(r.termid); if (term) { r.term = `第${term}期`; } res = r; } else if (studentid) { const r = await this.model.findOne({ studentid }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' }); if (!r) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定范围的培训心得'); const { term } = await trainPlanInfo(r.termid); if (term) { r.term = `第${term}期`; } res = r; } else if (classid) { let r = await this.model.find({ classid }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' }); if (r.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定范围的培训心得'); r = JSON.parse(JSON.stringify(r)); const h = _.head(r); const { term } = await trainPlanInfo(h.termid); r = r.map(i => { i.term = `第${term}期`; return i; }); res = r; fn = `第${term}期${h.classid.name.includes('班') ? h.classid.name : `${h.classid.name}班`}培训心得`; } else if (batchid) { let r = await this.model.find({ batchid }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' }); if (r.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定范围的培训心得'); r = JSON.parse(JSON.stringify(r)); const h = _.head(r); const { term, batch } = await trainPlanInfo(h.termid, h.batchid); r = r.map(i => { i.term = `第${term}期`; i.batch = `第${batch}批`; return i; }); res = r; fn = `第${term}期第${batch}批培训心得`; } else if (termid) { let r = await this.model.find({ termid }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' }); if (r.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定范围的培训心得'); r = JSON.parse(JSON.stringify(r)); const h = _.head(r); const { term } = await trainPlanInfo(h.termid, h.batchid); r = r.map(i => { i.term = `第${term}期`; return i; }); res = r; fn = `第${term}期培训心得`; } else if (planid) { const trainPlan = await this.ctx.model.Trainplan.findById(planid); if (!trainPlan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定年度计划信息'); const { termnum } = trainPlan; if (!termnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定年度计划下的各期信息'); if (!_.isArray(termnum)) throw new BusinessError(ErrorCode.DATA_INVALID, '年度计划下的期信息数据格式错误'); const arr = []; for (const t of termnum) { const dup = JSON.parse(JSON.stringify(t)); let r = await this.model.find({ termid: dup._id }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' }); if (r.length <= 0) continue; r = JSON.parse(JSON.stringify(r)); const h = _.head(r); const { term } = await trainPlanInfo(h.termid); r = r.map(i => { i.term = `第${term}期`; return i; }); arr.push(...r); } res = arr; fn = `${trainPlan.title}培训心得`; } const arr = []; if (res && !_.isArray(res)) { const docxObj = this.experienceData(res); arr.push(docxObj); const { author } = docxObj; if (author !== '') { fn = `${author}培训心得`; } } else if (res && _.isArray(res)) { for (const o of res) { const docxObj = this.experienceData(o); arr.push(docxObj); } } if (res.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何培训心得'); return await this.ctx.service.util.toDocx(arr, fn); } experienceData(data) { const { title, content, classid, studentid, term } = data; let docxObj = { title }; const cArr = content.split('\n'); const ncArr = []; // cArr内容按回车分行,检查每行内容开始是不是有4个空格(首行缩进),没有就加上 for (let c of cArr) { if (_.isString(c) && !c.startsWith(' ')) { c = ` ${_.trim(c)}`; } ncArr.push(c); } docxObj = Object.assign(docxObj, { content: ncArr }); let author = term; if (_.isObject(classid)) { const { name } = classid; if (name) author = `${author}${name.includes('班') ? name : `${name}班`}`; } if (_.isObject(studentid)) { const { name } = studentid; if (name) author = `${author}${name.includes('班') ? name : `${name}`}`; } if (author !== '') { docxObj = Object.assign(docxObj, { author }); } return docxObj; } } module.exports = ExperienceService;