schtime.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 SchtimeService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'schtime');
  10. this.model = this.ctx.model.Schtime;
  11. }
  12. async updateschtimes(data) {
  13. assert(data, '缺少信息项');
  14. for (const elm of data) {
  15. // 循环:elm代表上传上来的每个学校
  16. const schtime = await this.model.findById(elm.id);
  17. // 找到该学校原数据
  18. if (schtime) {
  19. // // 原数据处理,可以不用吧
  20. // schtime.schid = elm.schid;
  21. // schtime.year = elm.year;
  22. // schtime.planid = elm.planid;
  23. // schtime.remark = elm.remark;
  24. // schtime.daterange = elm.daterange;
  25. // // 工具方法整理出需要删除,修改,添加的数据,都是array的形式
  26. const { deletearray, updatearray, addarray } = await this.filterarray(schtime.arrange, elm.arrange);
  27. let termInfo = _.differenceBy(schtime.arrange, deletearray, 'id');
  28. for (const terminfo of termInfo) {
  29. const updateinfo = (_.filter(updatearray, item => item.id === terminfo.id))[0];
  30. terminfo.termid = updateinfo.termid;
  31. if (updateinfo.number) terminfo.number = updateinfo.number;
  32. if (updateinfo.type) terminfo.type = updateinfo.type;
  33. if (updateinfo.carnum) terminfo.carnum = updateinfo.carnum;
  34. }
  35. termInfo = _.concat(termInfo, addarray);
  36. schtime.arrange = termInfo;
  37. await schtime.save();
  38. }
  39. }
  40. }
  41. async filterarray(oldarray, newarray) {
  42. const _oldarray = _.cloneDeep(oldarray);
  43. const updatearray = [];
  44. const addarray = [];
  45. for (const _newarray of newarray) {
  46. if (_newarray.id) {
  47. updatearray.push(_newarray);
  48. } else {
  49. addarray.push(_newarray);
  50. }
  51. }
  52. const deletearray = _.pullAllBy(_oldarray, updatearray, 'id');
  53. return { deletearray, updatearray, addarray };
  54. }
  55. }
  56. module.exports = SchtimeService;