score.js 961 B

123456789101112131415161718192021222324252627282930313233343536
  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 ScoreService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'score');
  10. this.model = this.ctx.model.Score;
  11. this.tmodel = this.ctx.model.Teacher;
  12. }
  13. async create(data) {
  14. const { teacherid, score } = data;
  15. const teacher = await this.tmodel.findById(teacherid);
  16. let newscore;
  17. if (!teacher.xsscore) {
  18. newscore = score;
  19. } else {
  20. const xsscore = teacher.xsscore;
  21. const number = await this.model.count({ teacherid });
  22. newscore = (Number(xsscore) * number + Number(score)) / (number + 1);
  23. }
  24. teacher.xsscore = newscore;
  25. await teacher.save();
  26. return await this.model.create(data);
  27. }
  28. }
  29. module.exports = ScoreService;