Bladeren bron

添加还原和记录

lrf 3 jaren geleden
bovenliggende
commit
0aa6d81ff7
1 gewijzigde bestanden met toevoegingen van 26 en 1 verwijderingen
  1. 26 1
      app/service/updateapply.js

+ 26 - 1
app/service/updateapply.js

@@ -3,13 +3,38 @@ const { CrudService } = require('naf-framework-mongoose-free/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 const _ = require('lodash');
 const assert = require('assert');
+const { ObjectId } = require('mongoose').Types;
+const { isNullOrUndefined, trimData } = require('naf-core').Util;
+const moment = require('moment');
 
 // 绩效修改申请表
 class UpdateapplyService extends CrudService {
   constructor(ctx) {
-    super(ctx, "updateapply");
+    super(ctx, 'updateapply');
     this.model = this.ctx.model.Updateapply;
   }
+
+  async update(filter, update, { projection } = {}) {
+    assert(filter);
+    assert(update);
+    const { _id, id } = filter;
+    if (_id || id) filter = { _id: ObjectId(_id || id) };
+    // TODO:检查数据是否存在
+    const entity = await this.model.findOne(filter).exec();
+    if (isNullOrUndefined(entity)) throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
+    // 当1=>3时,进行还原操作.将原数据状态变为0,数据可修改状态
+    if (entity.status === '1' && update.status === '3') {
+      const { project_id, record = [] } = entity;
+      const rData = { desc: '基础处通过修改绩效申请', examine_date: moment().format('YYYY-MM-DD HH:mm:ss'), status: '0' };
+      record.push(rData);
+      if (!project_id) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到申请修改的原数据');
+      await this.ctx.model.Apply.updateOne({ _id: ObjectId(project_id) }, { status: '0', record });
+    }
+    // TODO: 修改数据
+    entity.set(trimData(update));
+    await entity.save();
+    return await this.model.findOne(filter, projection).exec();
+  }
 }
 
 module.exports = UpdateapplyService;