experience.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class ExperienceService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'experience');
  10. this.model = this.ctx.model.Experience;
  11. }
  12. async query(data, { skip, limit }) {
  13. const res = await this.model.find().populate({ path: 'studentid', select: 'name' }).skip(parseInt(skip))
  14. .limit(parseInt(limit));
  15. return res;
  16. }
  17. async create(data) {
  18. const { studentid } = data;
  19. let res;
  20. const r = await this.model.findOne({ studentid });
  21. if (r) {
  22. const { title, content } = data;
  23. r.title = title;
  24. r.content = content;
  25. res = await r.save();
  26. } else {
  27. res = await this.model.create(data);
  28. }
  29. return res;
  30. }
  31. async exportDocx(data) {
  32. const { planid, termid, batchid, classid, id } = data;
  33. // 从小到大判断
  34. // res是最后的结果,可能是Object也可能是Array
  35. // 作者拼接:能直接获取学生姓名,班级名称,需要单独查下期数
  36. let res;
  37. let fn = '';
  38. const trainPlanInfo = async (termid, batchid) => {
  39. const trainPlan = await this.ctx.model.Trainplan.findOne({ 'termnum._id': ObjectId(termid) });
  40. if (!trainPlan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划信息');
  41. const { termnum } = trainPlan;
  42. if (!termnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到年度计划的期信息');
  43. const obj = {};
  44. if (termid) {
  45. const term = termnum.id(termid);
  46. if (term) obj.term = term.term;
  47. if (batchid) {
  48. const { batchnum } = term;
  49. const batch = batchnum.id(batchid);
  50. if (batch) obj.batch = batch.batch;
  51. }
  52. }
  53. return obj;
  54. };
  55. if (id) {
  56. const r = await this.model.findById(id).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' });
  57. if (!r) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定范围的培训心得');
  58. const { term } = await trainPlanInfo(r.termid);
  59. if (term) { r.term = `第${term}期`; }
  60. res = r;
  61. } else if (classid) {
  62. let r = await this.model.find({ classid }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' });
  63. if (r.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定范围的培训心得');
  64. r = JSON.parse(JSON.stringify(r));
  65. const h = _.head(r);
  66. const { term } = await trainPlanInfo(h.termid);
  67. r = r.map(i => {
  68. i.term = `第${term}期`;
  69. return i;
  70. });
  71. res = r;
  72. fn = `第${term}期${h.classid.name}班培训心得`;
  73. } else if (batchid) {
  74. let r = await this.model.find({ batchid }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' });
  75. if (r.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定范围的培训心得');
  76. r = JSON.parse(JSON.stringify(r));
  77. const h = _.head(r);
  78. const { term, batch } = await trainPlanInfo(h.termid, h.batchid);
  79. r = r.map(i => {
  80. i.term = `第${term}期`;
  81. i.batch = `第${batch}批`;
  82. return i;
  83. });
  84. res = r;
  85. fn = `第${term}期第${batch}批培训心得`;
  86. } else if (termid) {
  87. let r = await this.model.find({ termid }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' });
  88. if (r.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定范围的培训心得');
  89. r = JSON.parse(JSON.stringify(r));
  90. const h = _.head(r);
  91. const { term } = await trainPlanInfo(h.termid, h.batchid);
  92. r = r.map(i => {
  93. i.term = `第${term}期`;
  94. return i;
  95. });
  96. res = r;
  97. fn = `第${term}期培训心得`;
  98. } else if (planid) {
  99. const trainPlan = await this.ctx.model.Trainplan.findById(planid);
  100. if (!trainPlan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定年度计划信息');
  101. const { termnum } = trainPlan;
  102. if (!termnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定年度计划下的各期信息');
  103. if (!_.isArray(termnum)) throw new BusinessError(ErrorCode.DATA_INVALID, '年度计划下的期信息数据格式错误');
  104. const arr = [];
  105. for (const t of termnum) {
  106. const dup = JSON.parse(JSON.stringify(t));
  107. let r = await this.model.find({ termid: dup._id }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' });
  108. if (r.length <= 0) continue;
  109. r = JSON.parse(JSON.stringify(r));
  110. const h = _.head(r);
  111. const { term } = await trainPlanInfo(h.termid);
  112. r = r.map(i => {
  113. i.term = `第${term}期`;
  114. return i;
  115. });
  116. arr.push(...r);
  117. }
  118. res = arr;
  119. fn = `${trainPlan.title}培训心得`;
  120. }
  121. const arr = [];
  122. if (res && !_.isArray(res)) {
  123. const docxObj = this.experienceData(res);
  124. arr.push(docxObj);
  125. const { author } = docxObj;
  126. if (author !== '') { fn = `${author}培训心得`; }
  127. } else if (res && _.isArray(res)) {
  128. for (const o of res) {
  129. const docxObj = this.experienceData(o);
  130. arr.push(docxObj);
  131. }
  132. }
  133. if (res.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何培训心得');
  134. return await this.ctx.service.util.toDocx(arr, fn);
  135. }
  136. experienceData(data) {
  137. const { title, content, classid, studentid, term } = data;
  138. let docxObj = { title };
  139. const cArr = content.split('\n');
  140. const ncArr = [];
  141. // cArr内容按回车分行,检查每行内容开始是不是有4个空格(首行缩进),没有就加上
  142. for (let c of cArr) {
  143. if (_.isString(c) && !c.startsWith(' ')) {
  144. c = ` ${_.trim(c)}`;
  145. }
  146. ncArr.push(c);
  147. }
  148. docxObj = Object.assign(docxObj, { content: ncArr });
  149. let author = term;
  150. if (_.isObject(classid)) {
  151. const { name } = classid;
  152. if (name) author = `${author}${name.includes('班') ? name : `${name}班`}`;
  153. }
  154. if (_.isObject(studentid)) {
  155. const { name } = studentid;
  156. if (name) author = `${author}${name.includes('班') ? name : `${name}`}`;
  157. }
  158. if (author !== '') {
  159. docxObj = Object.assign(docxObj, { author });
  160. }
  161. return docxObj;
  162. }
  163. }
  164. module.exports = ExperienceService;