Browse Source

新建评分时更新教师学生评分字段

reloaded 5 years ago
parent
commit
73436a93c3
1 changed files with 18 additions and 0 deletions
  1. 18 0
      app/service/score.js

+ 18 - 0
app/service/score.js

@@ -11,6 +11,24 @@ 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);
   }
 
 }