coachInBill.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. const { CrudService } = require('naf-framework-mongoose-free/lib/service');
  3. const { BusinessError, ErrorCode } = require('naf-core').Error;
  4. const _ = require('lodash');
  5. const assert = require('assert');
  6. //
  7. class CoachInBillService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'coachinbill');
  10. this.model = this.ctx.model.Business.CoachInBill;
  11. this.rcsModel = this.ctx.model.Relation.RelationCoachSchool;
  12. }
  13. // 查重,重了不加
  14. async beforeCreate(body) {
  15. const query = _.pick(body, [ 'lesson_id', 'type', 'school_id', 'coach_id' ]);
  16. const num = await this.model.count(query);
  17. if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '已经存在 该节课已经存在明细,请核对数据');
  18. return body;
  19. }
  20. // 改工资
  21. async afterCreate(body, data) {
  22. const { money } = data;
  23. const query = _.pick(data, [ 'school_id', 'coach_id' ]);
  24. // 查学校与教练的关联数据
  25. const rcsData = await this.rcsModel.findOne(query);
  26. if (!rcsData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到教练在该学校下任教的信息');
  27. // 累加工资
  28. let salary = _.get(rcsData, 'salary', 0);
  29. salary += money;
  30. rcsData.salary = salary;
  31. await rcsData.save();
  32. return data;
  33. }
  34. }
  35. module.exports = CoachInBillService;