12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- class SchtimeService extends CrudService {
- constructor(ctx) {
- super(ctx, 'schtime');
- this.model = this.ctx.model.Schtime;
- }
- async updateschtimes(data) {
- assert(data, '缺少信息项');
- for (const elm of data) {
- // 循环:elm代表上传上来的每个学校
- const schtime = await this.model.findById(elm.id);
- // 找到该学校原数据
- if (schtime) {
- // // 原数据处理,可以不用吧
- // schtime.schid = elm.schid;
- // schtime.year = elm.year;
- // schtime.planid = elm.planid;
- // schtime.remark = elm.remark;
- // schtime.daterange = elm.daterange;
- // // 工具方法整理出需要删除,修改,添加的数据,都是array的形式
- const { deletearray, updatearray, addarray } = await this.filterarray(schtime.arrange, elm.arrange);
- let termInfo = _.differenceBy(schtime.arrange, deletearray, 'id');
- for (const terminfo of termInfo) {
- const updateinfo = (_.filter(updatearray, item => item.id === terminfo.id))[0];
- terminfo.termid = updateinfo.termid;
- if (updateinfo.term) terminfo.term = updateinfo.term;
- if (updateinfo.batch) terminfo.batch = updateinfo.batch;
- if (updateinfo.batchid) terminfo.batchid = updateinfo.batchid;
- if (updateinfo.number) terminfo.number = updateinfo.number;
- if (updateinfo.type) terminfo.type = updateinfo.type;
- if (updateinfo.carnum) terminfo.carnum = updateinfo.carnum;
- }
- termInfo = _.concat(termInfo, addarray);
- schtime.arrange = termInfo;
- await schtime.save();
- }
- }
- }
- async filterarray(oldarray, newarray) {
- const _oldarray = _.cloneDeep(oldarray);
- const updatearray = [];
- const addarray = [];
- for (const _newarray of newarray) {
- if (_newarray.id) {
- updatearray.push(_newarray);
- } else {
- addarray.push(_newarray);
- }
- }
- const deletearray = _.pullAllBy(_oldarray, updatearray, 'id');
- return { deletearray, updatearray, addarray };
- }
- }
- module.exports = SchtimeService;
|