'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 MaterialscoreService extends CrudService { constructor(ctx) { super(ctx, 'materialscore'); this.model = this.ctx.model.Materialscore; this.mmodel = this.ctx.model.Material; } async create(data) { const { materialid, score } = data; const material = await this.mmodel.findById(materialid); let newscore; if (!material.score) { newscore = score; } else { const _score = material.score; const number = await this.model.count({ materialid }); newscore = (Number(_score) * number + Number(score)) / (number + 1); } material.score = newscore; await material.save(); return await this.model.create(data); } } module.exports = MaterialscoreService;