12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const { ObjectId } = require('mongoose').Types;
- const moment = require('moment');
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- class ApplyService 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;
- }
- // 查询
- async countstudent() {
- // 取得当前年份计划信息
- 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 planstu = _.chain(plan.termnum).map('batchnum').flatten()
- .map('number');
- let _planstu = 0;
- for (const el of planstu) {
- _planstu = _.add(_planstu, parseInt(el));
- }
- const data = {};
- data.planstu = _planstu;
- // 根据计划取得学校上报的学生数
- const schstus = await this.smodel.find({ planid: plan.id });
- data.schstu = schstus.length;
- // 取得计划内已培训的学生数
- const trainstu = schstus.filter(item => item.openid !== null);
- data.trainstu = trainstu.length;
- // 取得计划内未培训的学生数
- const notrainstu = schstus.filter(item => item.openid === null);
- 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;
|