123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- '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 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;
- }
- // 查询
- 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 _schools = _.map(schstus, 'school_name');
- // 取得无重复的寝室号
- const schools = _.uniq(_schools);
- const schs = [];
- for (const sch of schools) {
- const _schstunum = schstus.filter(item => item.school_name === sch);
- const newdata = { schname: sch, schnum: _schstunum.length };
- schs.push(newdata);
- }
- data.schs = schs;
- // 取得计划内已培训的学生数
- 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;
- }
- // 按学校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 = CountService;
|