trainplan.js 13 KB

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