trainplan.js 13 KB

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