lrf402788946 4 년 전
부모
커밋
4e874ef6db
5개의 변경된 파일67개의 추가작업 그리고 41개의 파일을 삭제
  1. 22 30
      app/controller/.score.js
  2. 1 0
      app/model/teacher.js
  3. 4 1
      app/router.js
  4. 39 9
      app/service/score.js
  5. 1 1
      app/service/teacher.js

+ 22 - 30
app/controller/.score.js

@@ -1,49 +1,41 @@
 module.exports = {
   create: {
-    requestBody: [
-      '!lessonid',
-      '!stuid',
-      '!teacherid',
-      'score',
-      'remark'
-    ]
+    requestBody: ["!lessonid", "!stuid", "!teacherid", "score", "remark"],
   },
   destroy: {
-    params: ['!id'],
-    service: 'delete'
+    params: ["!id"],
+    service: "delete",
   },
   update: {
-    params: ['!id'],
-    requestBody: [
-      'lessonid',
-      'stuid',
-      'teacherid',
-      'score',
-      'remark'
-    ]
+    params: ["!id"],
+    requestBody: ["lessonid", "stuid", "teacherid", "score", "remark"],
   },
   show: {
     parameters: {
-      params: ['!id']
+      params: ["!id"],
     },
-    service: 'fetch'
+    service: "fetch",
   },
   index: {
     parameters: {
       query: {
-        lessonid :'lessonid',
-        stuid :'stuid',
-        teacherid :'teacherid',
-        score :'score',
-        remark:'remark'
-      }
+        lessonid: "lessonid",
+        stuid: "stuid",
+        teacherid: "teacherid",
+        score: "score",
+        remark: "remark",
+      },
     },
-    service: 'query',
+    service: "query",
     options: {
-      query: ['skip', 'limit'],
-      sort: ['meta.createdAt'],
+      query: ["skip", "limit"],
+      sort: ["meta.createdAt"],
       desc: true,
-      count: true
-    }
+      count: true,
+    },
+  },
+  computedScore: {
+    parameters: { query: { teacherid: "teacherid" } },
+    service: "computedScore",
   },
 };

+ 1 - 0
app/model/teacher.js

@@ -69,6 +69,7 @@ const TeacherSchema = {
   zlscore: { type: String, required: false, maxLength: 200 }, // 资料评分
   msscore: { type: String, required: false, maxLength: 200 }, // 面试评分
   xsscore: { type: String, required: false, maxLength: 200 }, // 学生评分
+  beforescore: { type: String, required: false, maxLength: 200 }, // 以前评分的平均分
   file: { type: [ FileInfo ], select: false }, // 资料,教案PPT视频等
   status: { type: String, required: false, maxLength: 200, default: '0' },
   withpersonal: { type: String, required: false }, // 与人合作

+ 4 - 1
app/router.js

@@ -316,7 +316,7 @@ module.exports = app => {
   );
 
   // 计划自动排教师
-  router.get('apply', '/api/train/apply/arrange', controller.apply.arrange);
+  router.post('apply', '/api/train/apply/arrange', controller.apply.arrange);
   // 计划自动排教师
   router.post('apply', '/api/train/apply/sendmsg/:planid', controller.apply.sendmsg);
   // 计划自动排教师
@@ -458,6 +458,9 @@ module.exports = app => {
   router.post('/api/train/wxcheck', controller.login.wxcheck); // 微信检查登录
   router.post('/api/train/wxlogin', controller.login.wxlogin); // 登录
 
+
+  // 计算教师的分数
+  router.get('score', '/api/train/score/computed', controller.score.computedScore);
   // 评分表设置路由
   router.resources('score', '/api/train/score', controller.score); // index、create、show、destroy
   router.post('score', '/api/train/score/update/:id', controller.score.update);

+ 39 - 9
app/service/score.js

@@ -16,19 +16,49 @@ class ScoreService extends CrudService {
 
   async create(data) {
     const { teacherid, score } = data;
+    const res = await this.model.create(data);
+    this.computedScore({ teacherid });
+    return res;
+  }
+
+
+  async update({ id, score }) {
+    const r = await this.model.findById(id);
+    if (r) {
+      // 修改分数,重置状态,下面重新计算
+      r.score = score;
+      await r.save();
+    }
+    this.computedScore({ teacherid: r.teacherid });
+  }
+
+  // 根据教师id计算教师分数
+  async computedScore({ teacherid }) {
+    assert(teacherid, '没有教师信息,无法计算分数');
     const teacher = await this.tmodel.findById(teacherid);
-    let newscore;
-    if (!teacher.xsscore) {
-      newscore = score;
+    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;
+    });
+    teaScoreList = _.uniqBy(teaScoreList, 'studentid');
+    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 {
-      const xsscore = teacher.xsscore;
-      const number = await this.model.count({ teacherid });
-      newscore = (Number(xsscore) * number + Number(score)) / (number + 1);
-
+      score = _.round(_.divide(stuScoreTotal * 1, teaScoreList.length), 2);
     }
-    teacher.xsscore = newscore;
+    teacher.xsscore = score;
     await teacher.save();
-    return await this.model.create(data);
+
   }
 
 }

+ 1 - 1
app/service/teacher.js

@@ -122,7 +122,7 @@ class TeacherService extends CrudService {
         name: tea.name,
       });
       if (res) {
-        res.xsscore = tea.xsscore;
+        res.beforescore = tea.xsscore;
         await res.save();
       }
     }