experience.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. let res = await this.model.find().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, 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 (classid) {
  74. let r = await this.model.find({ classid }).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 } = await trainPlanInfo(h.termid);
  79. r = r.map(i => {
  80. i.term = `第${term}期`;
  81. return i;
  82. });
  83. res = r;
  84. fn = `第${term}期${h.classid.name}班培训心得`;
  85. } else if (batchid) {
  86. let r = await this.model.find({ batchid }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' });
  87. if (r.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定范围的培训心得');
  88. r = JSON.parse(JSON.stringify(r));
  89. const h = _.head(r);
  90. const { term, batch } = await trainPlanInfo(h.termid, h.batchid);
  91. r = r.map(i => {
  92. i.term = `第${term}期`;
  93. i.batch = `第${batch}批`;
  94. return i;
  95. });
  96. res = r;
  97. fn = `第${term}期第${batch}批培训心得`;
  98. } else if (termid) {
  99. let r = await this.model.find({ termid }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' });
  100. if (r.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定范围的培训心得');
  101. r = JSON.parse(JSON.stringify(r));
  102. const h = _.head(r);
  103. const { term } = await trainPlanInfo(h.termid, h.batchid);
  104. r = r.map(i => {
  105. i.term = `第${term}期`;
  106. return i;
  107. });
  108. res = r;
  109. fn = `第${term}期培训心得`;
  110. } else if (planid) {
  111. const trainPlan = await this.ctx.model.Trainplan.findById(planid);
  112. if (!trainPlan) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定年度计划信息');
  113. const { termnum } = trainPlan;
  114. if (!termnum) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到指定年度计划下的各期信息');
  115. if (!_.isArray(termnum)) throw new BusinessError(ErrorCode.DATA_INVALID, '年度计划下的期信息数据格式错误');
  116. const arr = [];
  117. for (const t of termnum) {
  118. const dup = JSON.parse(JSON.stringify(t));
  119. let r = await this.model.find({ termid: dup._id }).populate({ path: 'studentid', select: 'name job' }).populate({ path: 'classid', select: 'name' });
  120. if (r.length <= 0) continue;
  121. r = JSON.parse(JSON.stringify(r));
  122. const h = _.head(r);
  123. const { term } = await trainPlanInfo(h.termid);
  124. r = r.map(i => {
  125. i.term = `第${term}期`;
  126. return i;
  127. });
  128. arr.push(...r);
  129. }
  130. res = arr;
  131. fn = `${trainPlan.title}培训心得`;
  132. }
  133. const arr = [];
  134. if (res && !_.isArray(res)) {
  135. const docxObj = this.experienceData(res);
  136. arr.push(docxObj);
  137. const { author } = docxObj;
  138. if (author !== '') { fn = `${author}培训心得`; }
  139. } else if (res && _.isArray(res)) {
  140. for (const o of res) {
  141. const docxObj = this.experienceData(o);
  142. arr.push(docxObj);
  143. }
  144. }
  145. if (res.length <= 0) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到任何培训心得');
  146. return await this.ctx.service.util.toDocx(arr, fn);
  147. }
  148. experienceData(data) {
  149. const { title, content, classid, studentid, term } = data;
  150. let docxObj = { title };
  151. const cArr = content.split('\n');
  152. const ncArr = [];
  153. // cArr内容按回车分行,检查每行内容开始是不是有4个空格(首行缩进),没有就加上
  154. for (let c of cArr) {
  155. if (_.isString(c) && !c.startsWith(' ')) {
  156. c = ` ${_.trim(c)}`;
  157. }
  158. ncArr.push(c);
  159. }
  160. docxObj = Object.assign(docxObj, { content: ncArr });
  161. let author = term;
  162. if (_.isObject(classid)) {
  163. const { name } = classid;
  164. if (name) author = `${author}${name.includes('班') ? name : `${name}班`}`;
  165. }
  166. if (_.isObject(studentid)) {
  167. const { name } = studentid;
  168. if (name) author = `${author}${name.includes('班') ? name : `${name}`}`;
  169. }
  170. if (author !== '') {
  171. docxObj = Object.assign(docxObj, { author });
  172. }
  173. return docxObj;
  174. }
  175. }
  176. module.exports = ExperienceService;