|
@@ -0,0 +1,202 @@
|
|
|
+'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 TalentedService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'Talented');
|
|
|
+ this.model = this.ctx.model.Talented;
|
|
|
+ }
|
|
|
+
|
|
|
+ async query(data, { skip, limit }) {
|
|
|
+ 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 { files } = data;
|
|
|
+ r.files = files;
|
|
|
+ res = await r.save();
|
|
|
+ } else {
|
|
|
+ res = await this.model.create(data);
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ async export(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) {
|
|
|
+ let 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, '未找到指定范围的新人才报');
|
|
|
+ r = JSON.parse(JSON.stringify(r));
|
|
|
+ const { term } = await trainPlanInfo(r.termid);
|
|
|
+ if (term) { r.term = `第${term}期`; }
|
|
|
+ res = r;
|
|
|
+ } else if (studentid) {
|
|
|
+ let 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);
|
|
|
+ r = JSON.parse(JSON.stringify(r));
|
|
|
+ if (term) { r.term = `第${term}期`; }
|
|
|
+ res = r;
|
|
|
+ } else if (classid) {
|
|
|
+ let r = await this.model.find({ classid }).populate({ path: 'studentid', select: 'name' }).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}班新人才报`;
|
|
|
+ } 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}新人才报`;
|
|
|
+ }
|
|
|
+ let arr = [];
|
|
|
+ if (res && !_.isArray(res)) {
|
|
|
+ console.log(res);
|
|
|
+ const { classid, studentid, term, files } = res;
|
|
|
+ if (term) fn = `${fn}${term}`;
|
|
|
+ if (classid) {
|
|
|
+ const { name } = classid;
|
|
|
+ if (name) fn = `${fn}${name.includes('班') ? name : `${name}班`}`;
|
|
|
+ }
|
|
|
+ if (studentid) {
|
|
|
+ const { name } = studentid;
|
|
|
+ if (name) fn = `${fn}${name}`;
|
|
|
+ }
|
|
|
+ fn = `${fn}新人才报`;
|
|
|
+ arr = files;
|
|
|
+ } else if (res && _.isArray(res)) {
|
|
|
+ for (const o of res) {
|
|
|
+ const { files } = o;
|
|
|
+ arr.push(...files);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (res.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何新人才报');
|
|
|
+ return await this.ctx.service.util.toZip(arr, fn);
|
|
|
+ }
|
|
|
+
|
|
|
+ TalentedData(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 = TalentedService;
|