trainplan.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. 'use strict';
  2. const _ = require('lodash');
  3. const { CrudService } = require('naf-framework-mongoose/lib/service');
  4. const XLSX = require('xlsx');
  5. const utils = require('../utils/utils.js');
  6. class TrainplanService extends CrudService {
  7. constructor(ctx) {
  8. super(ctx, 'trainplan');
  9. this.model = this.ctx.model.Trainplan;
  10. this.clamodel = this.ctx.model.Class;
  11. this.umodel = this.ctx.model.User;
  12. this.smodel = this.ctx.model.School;
  13. this.tmodel = this.ctx.model.Teacher;
  14. this.stumodel = this.ctx.model.Student;
  15. }
  16. // async create(data) {
  17. // const terminfo = await data.termnum;
  18. // console.log(terminfo);
  19. // const { batchnum: { type, name, number }, term } = terminfo;
  20. // console.log(type);
  21. // if (type === 1) {
  22. // const classdata = { name, number, term, type };
  23. // await this.clamodel.create(classdata);
  24. // }
  25. // if (type === 0) {
  26. // for (let i = 0; i < class; i++) {
  27. // const name = '第' + term + '期' + batch + '批次' + i + '班';
  28. // const classdate = { name, number, term, type, newbatch };
  29. // await this.clamodel.create(classdate);
  30. // }
  31. // }
  32. // return this.tpmodel.create(data);
  33. // }
  34. // }
  35. async update({ id }, data) {
  36. const trainplan = await this.model.findById(id);
  37. // 保存原数据
  38. const trainplanold = _.cloneDeep(trainplan);
  39. const { year, title, termnum, festivals, status, school } = data;
  40. if (year) {
  41. trainplan.year = year;
  42. }
  43. if (title) {
  44. trainplan.title = title;
  45. }
  46. if (termnum) {
  47. trainplan.termnum = termnum;
  48. }
  49. if (school) {
  50. trainplan.school = school;
  51. }
  52. if (festivals) {
  53. trainplan.festivals = festivals;
  54. }
  55. if (status === '1') {
  56. trainplan.status = status;
  57. }
  58. // 如果培训计划状态改为发布,发送培训计划信息,并自动生成班级
  59. const res = await trainplan.save();
  60. if (res) {
  61. if (status === '1') {
  62. // 自动生成班级
  63. await this.autoclass(res, trainplanold);
  64. // 将生成的班级重新将班级排班名
  65. // await this.autoclassname(res);
  66. // 发送培训计划信息通知给相应人员
  67. // 查询所有入库的教师
  68. const teachers = await this.tmodel.find({ status: '4' });
  69. for (const teacher of teachers) {
  70. const teacherid = teacher._id;
  71. const _teacher = await this.umodel.findOne({ uid: teacherid, type: '3' });
  72. const openid = _teacher.openid;
  73. const detail = trainplan.title + '已发布,请注意查收!';
  74. const date = await this.ctx.service.util.updatedate();
  75. const remark = '感谢您的使用';
  76. if (openid) {
  77. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  78. }
  79. }
  80. // 查询所有学校用户
  81. const schools = await this.umodel.find({ type: '2' });
  82. for (const school of schools) {
  83. const openid = school.openid;
  84. const detail = trainplan.title + '已发布,请注意查收!';
  85. const date = await this.ctx.service.util.updatedate();
  86. const remark = '感谢您的使用';
  87. if (openid) {
  88. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  89. }
  90. }
  91. }
  92. }
  93. return res;
  94. }
  95. // 自动生成班级私有方法
  96. async autoclass(res, trainplanold) {
  97. // 首先比较当前数据和原数据的值是否有不同
  98. // 保存后所有期id
  99. const tremid_res = _.map(res.termnum, 'id');
  100. // 保存前所有期id
  101. const tremid_old = _.map(trainplanold.termnum, 'id');
  102. // 取得要删除的期id,进行班级中删除已删除期的班级
  103. const deltrem = _.difference(tremid_old, tremid_res);
  104. // 循环删除已经删除期的所有班级
  105. for (const elm of deltrem) {
  106. await this.clamodel.deleteMany({ termid: elm });
  107. }
  108. // 取得所有新加期id
  109. const addtrem = _.difference(tremid_res, tremid_old);
  110. // 清空后循环取得所有期进行批次操作
  111. const terms = res.termnum;
  112. for (const el of terms) {
  113. // 判断是否新加期
  114. if (_.indexOf(addtrem, el.id) !== -1) {
  115. // 循环当前新加期的批次列表,根据批次id和班级数生成班级信息
  116. const batchnums = el.batchnum;
  117. for (const batchnum of batchnums) {
  118. // 取得当前批次的班级数
  119. const classnum = batchnum.class;
  120. for (const cla of classnum) {
  121. const newdata = { name: cla.name, number: cla.number, batchid: batchnum.id, termid: el.id, planid: res.id, type: cla.type };
  122. await this.clamodel.create(newdata);
  123. }
  124. }
  125. } else {
  126. // 不是新加期,更新期信息
  127. // 保存后所有期id
  128. const batchid_res = _.map(el.batchnum, 'id');
  129. // 保存前所有期id
  130. const batchid_old = _.map(trainplanold.termnum.id(el.id).batchnum, 'id');
  131. // 取得要删除的期id,进行班级中删除已删除期的班级
  132. const delbatchs = _.difference(batchid_old, batchid_res);
  133. // 循环删除已经删除期的所有班级
  134. for (const delba of delbatchs) {
  135. await this.clamodel.deleteMany({ termid: el.id, batchid: delba });
  136. }
  137. // 取得所有新加期id
  138. const addbatch = _.difference(batchid_res, batchid_old);
  139. const batchnums = el.batchnum;
  140. for (const batchnum of batchnums) {
  141. // 取得当前批次是否有删除
  142. // 判断是否新加期
  143. if (_.indexOf(addbatch, batchnum.id) !== -1) {
  144. // 取得当前批次的班级数
  145. const classnum = batchnum.class;
  146. for (const cla of classnum) {
  147. const newdata = { name: cla.name, number: cla.number, batchid: batchnum.id, termid: el.id, planid: res.id, type: cla.type };
  148. await this.clamodel.create(newdata);
  149. }
  150. } else {
  151. if (batchnum.class === trainplanold.termnum.id(el.id).batchnum.id(batchnum.id).class) {
  152. // 编辑只会针对班级人数进行修改。
  153. const _class = await this.clamodel.find({ termid: el.id, batchid: batchnum.id });
  154. if (_class.length !== 0) {
  155. for (const ee of _class) {
  156. ee.number = batchnum.number;
  157. await ee.save();
  158. }
  159. } else {
  160. const classnum = batchnum.class;
  161. for (const cla of classnum) {
  162. const newdata = { name: cla.name, number: cla.number, batchid: batchnum.id, termid: el.id, planid: res.id, type: cla.type };
  163. await this.clamodel.create(newdata);
  164. }
  165. }
  166. } else {
  167. // 当班级数有更改时
  168. // 删除所有班级 并重新生成班级
  169. await this.clamodel.deleteMany({ termid: el.id, batchid: batchnum.id });
  170. const classnum = batchnum.class;
  171. for (const cla of classnum) {
  172. const newdata = { name: cla.name, number: cla.number, batchid: batchnum.id, termid: el.id, planid: res.id, type: cla.type };
  173. await this.clamodel.create(newdata);
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. // // 将分好的班级重新编排名字
  182. // async autoclassname(res) {
  183. // // 取得所有期id
  184. // const tremid_res = _.map(res.termnum, 'id');
  185. // for (const termid of tremid_res) {
  186. // const classs = await this.clamodel.find({ planid: res.id, termid });
  187. // let i = 0;
  188. // for (const cla of classs) {
  189. // i = i + 1;
  190. // cla.name = i;
  191. // await cla.save();
  192. // }
  193. // }
  194. // }
  195. async exportExcel({ trainplanIds }) {
  196. const nowDate = new Date().getTime();
  197. const path =
  198. 'D:\\wwwroot\\service\\service-file\\upload\\train\\' +
  199. nowDate +
  200. '.xlsx';
  201. const respath =
  202. 'http://free.liaoningdoupo.com:80/files/train/' + nowDate + '.xlsx';
  203. for (let i = 0; i < trainplanIds.length; i++) {
  204. // 批次期次都在这里面
  205. const trainplan = await this.model.findOne({ _id: trainplanIds[i] });
  206. // 这个计划下所有的学生
  207. const studentList = await this.stumodel.find({ planid: trainplanIds[i] });
  208. // 计划名称
  209. const trainplandName = trainplan.title;
  210. // 在计划中找到这个学生在哪期以及哪期下的哪批次
  211. for (const student of studentList) {
  212. student.trainplandName = trainplandName;
  213. // 期次
  214. const term = trainplan.termnum.filter(term => {
  215. return term.id === student.termid;
  216. });
  217. if (term.length > 0) { student.termName = term[0].term; }
  218. // 批次
  219. if (term.length !== 0) {
  220. const batch = term[0].batchnum.filter(batch => {
  221. return batch.id === student.batchid;
  222. });
  223. if (batch.length > 0) {
  224. student.batchName = JSON.parse(JSON.stringify(batch[0])).name;
  225. }
  226. }
  227. student.is_fine = utils.getIsNot(student.is_fine);
  228. }
  229. const _headers = [
  230. { key: 'trainplandName', title: '计划标题' },
  231. { key: 'termName', title: '期次' },
  232. { key: 'batchName', title: '批次' },
  233. { key: 'school_name', title: '学校' },
  234. { key: 'faculty', title: '院系' },
  235. { key: 'major', title: '专业' },
  236. { key: 'name', title: '姓名' },
  237. { key: 'id_number', title: '身份证号' },
  238. { key: 'phone', title: '手机号' },
  239. { key: 'gender', title: '性别' },
  240. { key: 'nation', title: '民族' },
  241. { key: 'edua_level', title: '学历层次' },
  242. { key: 'edua_system', title: '学制' },
  243. { key: 'entry_year', title: '入学年份' },
  244. { key: 'finish_year', title: '毕业年份' },
  245. { key: 'school_job', title: '在校职务' },
  246. { key: 'qq', title: 'QQ号' },
  247. { key: 'email', title: '邮箱' },
  248. { key: 'openid', title: '微信openid' },
  249. { key: 'family_place', title: '家庭位置' },
  250. { key: 'family_is_hard', title: '是否困难' },
  251. { key: 'have_grant', title: ' 是否获得过助学金' },
  252. { key: 'job', title: '职务' },
  253. { key: 'bedroom', title: '寝室号' },
  254. { key: 'is_fine', title: '是否优秀' },
  255. // { key: 'selfscore', title: '个人分' },
  256. // { key: 'score', title: '总分' },
  257. // { key: 'diy', title: '自定义' },
  258. ];
  259. // 需要打出的列表
  260. const _data = studentList;
  261. const headers = _headers
  262. .map(({ title }) => title)
  263. .map((v, i) =>
  264. Object.assign({}, { v, position: String.fromCharCode(65 + i) + 1 })
  265. )
  266. .reduce(
  267. (prev, next) =>
  268. Object.assign({}, prev, { [next.position]: { v: next.v } }),
  269. {}
  270. );
  271. const data = _data
  272. .map((v, i) =>
  273. _headers.map(({ key }, j) =>
  274. Object.assign(
  275. {},
  276. { v: v[key], position: String.fromCharCode(65 + j) + (i + 2) }
  277. )
  278. )
  279. )
  280. .reduce((prev, next) => prev.concat(next))
  281. .reduce(
  282. (prev, next) =>
  283. Object.assign({}, prev, { [next.position]: { v: next.v } }),
  284. {}
  285. );
  286. // 合并 headers 和 data
  287. const output = Object.assign({}, headers, data);
  288. // 获取所有单元格的位置
  289. const outputPos = Object.keys(output);
  290. // 计算出范围
  291. const ref = outputPos[0] + ':' + outputPos[outputPos.length - 1];
  292. // 构建 workbook 对象
  293. const wb = {
  294. SheetNames: [ 'sheet1' ],
  295. Sheets: {
  296. sheet1: Object.assign({}, output, { '!ref': ref }),
  297. },
  298. };
  299. // 导出 Excel
  300. XLSX.writeFile(wb, path);
  301. }
  302. return respath;
  303. }
  304. }
  305. module.exports = TrainplanService;