'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 ScoreService extends CrudService { constructor(ctx) { super(ctx, 'score'); this.model = this.ctx.model.Score; this.tmodel = this.ctx.model.Teacher; } async create(data) { const { teacherid, score } = data; const teacher = await this.tmodel.findById(teacherid); let newscore; if (!teacher.xsscore) { newscore = score; } else { const xsscore = teacher.xsscore; const number = await this.model.count({ teacherid }); newscore = (Number(xsscore) * number + Number(score)) / (number + 1); } teacher.xsscore = newscore; await teacher.save(); return await this.model.create(data); } } module.exports = ScoreService;