소스 검색

增加按学校统计接口

liuyu 4 년 전
부모
커밋
3926152f36
3개의 변경된 파일54개의 추가작업 그리고 2개의 파일을 삭제
  1. 7 0
      app/controller/count.js
  2. 2 0
      app/router.js
  3. 45 2
      app/service/count.js

+ 7 - 0
app/controller/count.js

@@ -12,11 +12,18 @@ class CountController extends Controller {
     this.service = this.ctx.service.count;
   }
 
+  // 统计全年计划所有学校
   async countstudent() {
     const data = await this.service.countstudent();
     this.ctx.ok({ data });
   }
 
+  // 按学校统计学生情况
+  async countschstu() {
+    const data = await this.service.countschstu(this.ctx.params);
+    this.ctx.ok({ data });
+  }
+
 }
 
 module.exports = CountController;

+ 2 - 0
app/router.js

@@ -204,5 +204,7 @@ module.exports = app => {
 
   // 统计查询设置路由
   router.get('/api/train/count/countstudent', controller.count.countstudent);
+  // 按学校统计查询设置路由
+  router.get('/api/train/count/countschstu/:id', controller.count.countschstu);
 
 };

+ 45 - 2
app/service/count.js

@@ -8,12 +8,13 @@ const moment = require('moment');
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError, ErrorCode } = require('naf-core').Error;
 
-class ApplyService extends CrudService {
+class CountService extends CrudService {
   constructor(ctx) {
     super(ctx, 'count');
     this.smodel = this.ctx.model.Student;
     this.tmodel = this.ctx.model.Trainplan;
     this.lmodel = this.ctx.model.Leave;
+    this.schmodel = this.ctx.model.School;
   }
 
   // 查询
@@ -70,6 +71,48 @@ class ApplyService extends CrudService {
     return data;
   }
 
+  // 按学校id统计查询
+  async countschstu({ id }) {
+    console.log(id);
+    // 取得当前年份计划信息
+    console.log(moment().format('YYYY'));
+    const plan = await this.tmodel.findOne({ year: moment().format('YYYY') });
+    if (!plan) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '当前全年计划不存在');
+    }
+    // 根据计划取得当前年份下计划学校培训学生人数
+    const school = await this.schmodel.findOne({ code: id });
+    if (!school) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学校信息不存在');
+    }
+    const data = {};
+    data.planstu = school.number;
+    // 根据学校id取得学校上报的学生数
+    const schstus = await this.smodel.find({ planid: plan.id, schid: id });
+    data.schstu = schstus.length;
+    // 取得已培训的学生数
+    const trainstu = schstus.filter(item => item.openid);
+    data.trainstu = trainstu.length;
+    // 取得内未培训的学生数
+    const notrainstu = schstus.filter(item => !item.openid);
+    data.notrainstu = notrainstu.length;
+    // 取得学生请假数和退出数
+    const levelstus = [];
+    // 循环取得所有请假和退出的学生信息
+    for (const elm of trainstu) {
+      const level = await this.lmodel.findOne({ studentid: elm.id, status: '1' });
+      if (level) {
+        levelstus.push(level);
+      }
+    }
+    // 筛选出请假数和退出数
+    const levelqj = levelstus.filter(item => item.type === '0');
+    const levelexit = levelstus.filter(item => item.type === '1');
+    data.levelqj = levelqj.length;
+    data.levelexit = levelexit.length;
+    return data;
+  }
+
 }
 
-module.exports = ApplyService;
+module.exports = CountService;