lrf 1 year ago
parent
commit
8960f30436
1 changed files with 14 additions and 30 deletions
  1. 14 30
      app/service/count.js

+ 14 - 30
app/service/count.js

@@ -1,6 +1,5 @@
 'use strict';
 
-
 const assert = require('assert');
 const _ = require('lodash');
 const { ObjectId } = require('mongoose').Types;
@@ -43,11 +42,9 @@ class CountService extends CrudService {
     }
     let { termnum } = trainPlan;
     if (!_.isArray(termnum)) {
-      throw new BusinessError(
-        ErrorCode.DATA_INVALID,
-        '默认年度计划的期数据格式不正确'
-      );
+      throw new BusinessError(ErrorCode.DATA_INVALID, '默认年度计划的期数据格式不正确');
     }
+
     // 转换下,因为之后用的期id不是ObjectId
     termnum = JSON.parse(JSON.stringify(termnum));
     const termids = termnum.map(i => i._id);
@@ -65,19 +62,11 @@ class CountService extends CrudService {
     });
     data.levelqj = levelqj || 0;
     data.levelexit = levelexit || 0;
+
     // 参加培训的学生
     const trainstuRes = await this.ctx.model.Student.aggregate([
-      { $addFields: { u_id: { $toString: '$_id' } } },
       { $match: { planid } },
-      {
-        $lookup: {
-          from: 'user',
-          localField: 'u_id',
-          foreignField: 'uid',
-          as: 'user',
-        },
-      },
-      { $match: { 'user.type': '4', 'user.openid': { $exists: true } } },
+      { $match: { openid: { $exists: true } } },
       { $count: 'total' },
     ]);
     const h = _.head(trainstuRes);
@@ -104,12 +93,14 @@ class CountService extends CrudService {
     const schs = await this.ctx.model.Student.aggregate([
       { $match: { planid } },
       { $group: { _id: '$schid', sum: { $sum: 1 } } },
-      { $lookup: {
-        from: 'school',
-        localField: '_id',
-        foreignField: 'code',
-        as: 'school',
-      } },
+      {
+        $lookup: {
+          from: 'school',
+          localField: '_id',
+          foreignField: 'code',
+          as: 'school',
+        },
+      },
       { $unwind: '$school' },
       { $project: { _id: 0, schname: '$school.name', schnum: '$sum' } },
     ]);
@@ -140,10 +131,7 @@ class CountService extends CrudService {
     }
     let { termnum } = trainPlan;
     if (!_.isArray(termnum)) {
-      throw new BusinessError(
-        ErrorCode.DATA_INVALID,
-        '默认年度计划的期数据格式不正确'
-      );
+      throw new BusinessError(ErrorCode.DATA_INVALID, '默认年度计划的期数据格式不正确');
     }
     // 转换下,因为之后用的期id不是ObjectId
     termnum = JSON.parse(JSON.stringify(termnum));
@@ -197,14 +185,10 @@ class CountService extends CrudService {
     data.notrainstu = notrainstu;
     // 年度计划实际人数 这个还没用聚合,不是很优雅
     const mid = await this.ctx.model.Schtime.find({ planid, schid: id });
-    const planstu = mid.reduce(
-      (p, n) => p + n.arrange.reduce((np, nn) => np + (nn.number * 1 || 0), 0),
-      0
-    );
+    const planstu = mid.reduce((p, n) => p + n.arrange.reduce((np, nn) => np + (nn.number * 1 || 0), 0), 0);
     data.planstu = planstu;
     return data;
   }
-
 }
 
 module.exports = CountService;