talented.js 7.5 KB

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