count.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const moment = require('moment');
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. const { BusinessError, ErrorCode } = require('naf-core').Error;
  8. class ApplyService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'count');
  11. this.smodel = this.ctx.model.Student;
  12. this.tmodel = this.ctx.model.Trainplan;
  13. this.lmodel = this.ctx.model.Leave;
  14. }
  15. // 查询
  16. async countstudent() {
  17. // 取得当前年份计划信息
  18. console.log(moment().format('YYYY'));
  19. const plan = await this.tmodel.findOne({ year: moment().format('YYYY') });
  20. if (!plan) {
  21. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '当前全年计划不存在');
  22. }
  23. // 根据计划取得当前年份下计划培训学生人数
  24. const planstu = _.chain(plan.termnum).map('batchnum').flatten()
  25. .map('number');
  26. let _planstu = 0;
  27. for (const el of planstu) {
  28. _planstu = _.add(_planstu, parseInt(el));
  29. }
  30. const data = {};
  31. data.planstu = _planstu;
  32. // 根据计划取得学校上报的学生数
  33. const schstus = await this.smodel.find({ planid: plan.id });
  34. data.schstu = schstus.length;
  35. // 统计各个学校
  36. const _schools = _.map(schstus, 'school_name');
  37. // 取得无重复的寝室号
  38. const schools = _.uniq(_schools);
  39. const schs = [];
  40. for (const sch of schools) {
  41. const _schstunum = schstus.filter(item => item.school_name === sch);
  42. const newdata = { schname: sch, schnum: _schstunum.length };
  43. schs.push(newdata);
  44. }
  45. data.schs = schs;
  46. // 取得计划内已培训的学生数
  47. const trainstu = schstus.filter(item => item.openid);
  48. data.trainstu = trainstu.length;
  49. // 取得计划内未培训的学生数
  50. const notrainstu = schstus.filter(item => !item.openid);
  51. data.notrainstu = notrainstu.length;
  52. // 取得学生请假数和退出数
  53. const levelstus = [];
  54. // 循环取得所有请假和退出的学生信息
  55. for (const elm of trainstu) {
  56. const level = await this.lmodel.findOne({ studentid: elm.id, status: '1' });
  57. if (level) {
  58. levelstus.push(level);
  59. }
  60. }
  61. // 筛选出请假数和退出数
  62. const levelqj = levelstus.filter(item => item.type === '0');
  63. const levelexit = levelstus.filter(item => item.type === '1');
  64. data.levelqj = levelqj.length;
  65. data.levelexit = levelexit.length;
  66. return data;
  67. }
  68. }
  69. module.exports = ApplyService;