materialscore.js 994 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 MaterialscoreService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'materialscore');
  10. this.model = this.ctx.model.Materialscore;
  11. this.mmodel = this.ctx.model.Material;
  12. }
  13. async create(data) {
  14. const { materialid, score } = data;
  15. const material = await this.mmodel.findById(materialid);
  16. let newscore;
  17. if (!material.score) {
  18. newscore = score;
  19. } else {
  20. const _score = material.score;
  21. const number = await this.model.count({ materialid });
  22. newscore = (Number(_score) * number + Number(score)) / (number + 1);
  23. }
  24. material.score = newscore;
  25. await material.save();
  26. return await this.model.create(data);
  27. }
  28. }
  29. module.exports = MaterialscoreService;