schtime.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.term, elm.term);
  27. let termInfo = _.differenceBy(schtime.term, deletearray, 'id');
  28. for (const terminfo of termInfo) {
  29. const updateinfo = (_.filter(updatearray, item => item.id === terminfo.id))[0];
  30. terminfo.termnum = updateinfo.termnum;
  31. terminfo.termid = updateinfo.termid;
  32. if (updateinfo.number) terminfo.number = updateinfo.number;
  33. if (updateinfo.type) terminfo.type = updateinfo.type;
  34. if (updateinfo.carnum) terminfo.carnum = updateinfo.carnum;
  35. }
  36. termInfo = _.concat(termInfo, addarray);
  37. schtime.term = termInfo;
  38. await schtime.save();
  39. }
  40. }
  41. }
  42. async filterarray(oldarray, newarray) {
  43. const _oldarray = _.cloneDeep(oldarray);
  44. const updatearray = [];
  45. const addarray = [];
  46. for (const _newarray of newarray) {
  47. if (_newarray.id) {
  48. updatearray.push(_newarray);
  49. } else {
  50. addarray.push(_newarray);
  51. }
  52. }
  53. const deletearray = _.pullAllBy(_oldarray, updatearray, 'id');
  54. return { deletearray, updatearray, addarray };
  55. }
  56. }
  57. module.exports = SchtimeService;