1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- '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 { lessonid, stuid, teacherid, score, remark } = data;
- let res = await this.model.findOne({ stuid, lessonid, teacherid });
- if (res) {
- if (score) res.score = score;
- if (remark)res.remark = remark;
- res = await res.save();
- } else {
- res = await this.model.create(data);
- }
- // const res = await this.model.create(data);
- this.computedScore({ teacherid });
- return res;
- }
- async update({ id, score, remark }) {
- const r = await this.model.findById(id);
- if (r) {
- // 修改分数,重置状态,下面重新计算
- r.score = score;
- r.remark = remark;
- await r.save();
- }
- this.computedScore({ teacherid: r.teacherid });
- }
- // 根据教师id计算教师分数
- async computedScore({ teacherid }) {
- assert(teacherid, '没有教师信息,无法计算分数');
- const teacher = await this.tmodel.findById(teacherid);
- if (!teacher) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '没有找到教师信息');
- const { beforescore } = teacher;
- let teaScoreList = await this.model.find({ teacherid });
- // 判断有没有学生给评分
- if (teaScoreList.length <= 0) return;
- teaScoreList = teaScoreList.map(i => {
- i.score = i.score * 1;
- return i;
- });
- // 2021-06-04修改:过滤6分以下的恶意评分
- teaScoreList = teaScoreList.filter(f => f.score > 6);
- teaScoreList = _.uniqBy(teaScoreList, 'stuid');
- teaScoreList = _.orderBy(teaScoreList, [ 'score' ], [ 'desc' ]);
- teaScoreList = _.drop(teaScoreList);
- teaScoreList = _.dropRight(teaScoreList);
- const stuScoreTotal = teaScoreList.reduce((p, n) => p + (n.score * 1 || 0), 0);
- let score = 0;
- if (beforescore) {
- score = _.round(_.divide(beforescore * 1 + stuScoreTotal * 1, (teaScoreList.length || 0) + 1), 2);
- } else {
- score = _.round(_.divide(stuScoreTotal * 1, teaScoreList.length), 2);
- }
- teacher.xsscore = score;
- await teacher.save();
- }
- // 手动触发,重新计算所有的教师的分数
- async computedAllTeacherScore() {
- const teacherList = await this.tmodel.find();
- for (const t of teacherList) {
- const { _id } = t;
- await this.computedScore({ teacherid: _id });
- }
- }
- }
- module.exports = ScoreService;
|