experience.js 7.4 KB

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