count.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 CountService 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. this.schmodel = this.ctx.model.School;
  15. }
  16. // 查询
  17. async countstudent() {
  18. // 取得当前年份计划信息
  19. console.log(moment().format('YYYY'));
  20. const plan = await this.tmodel.findOne({ year: moment().format('YYYY') });
  21. if (!plan) {
  22. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '当前全年计划不存在');
  23. }
  24. // 根据计划取得当前年份下计划培训学生人数
  25. const planstu = _.chain(plan.termnum).map('batchnum').flatten()
  26. .map('number');
  27. let _planstu = 0;
  28. for (const el of planstu) {
  29. _planstu = _.add(_planstu, parseInt(el));
  30. }
  31. const data = {};
  32. data.planstu = _planstu;
  33. // 根据计划取得学校上报的学生数
  34. const schstus = await this.smodel.find({ planid: plan.id });
  35. data.schstu = schstus.length;
  36. // 统计各个学校
  37. const _schools = _.map(schstus, 'school_name');
  38. // 取得无重复的寝室号
  39. const schools = _.uniq(_schools);
  40. const schs = [];
  41. for (const sch of schools) {
  42. const _schstunum = schstus.filter(item => item.school_name === sch);
  43. const newdata = { schname: sch, schnum: _schstunum.length };
  44. schs.push(newdata);
  45. }
  46. data.schs = schs;
  47. // 取得计划内已培训的学生数
  48. const trainstu = schstus.filter(item => item.openid);
  49. data.trainstu = trainstu.length;
  50. // 取得计划内未培训的学生数
  51. const notrainstu = schstus.filter(item => !item.openid);
  52. data.notrainstu = notrainstu.length;
  53. // 取得学生请假数和退出数
  54. const levelstus = [];
  55. // 循环取得所有请假和退出的学生信息
  56. for (const elm of trainstu) {
  57. const level = await this.lmodel.findOne({ studentid: elm.id, status: '1' });
  58. if (level) {
  59. levelstus.push(level);
  60. }
  61. }
  62. // 筛选出请假数和退出数
  63. const levelqj = levelstus.filter(item => item.type === '0');
  64. const levelexit = levelstus.filter(item => item.type === '1');
  65. data.levelqj = levelqj.length;
  66. data.levelexit = levelexit.length;
  67. return data;
  68. }
  69. // 按学校id统计查询
  70. async countschstu({ id }) {
  71. console.log(id);
  72. // 取得当前年份计划信息
  73. console.log(moment().format('YYYY'));
  74. const plan = await this.tmodel.findOne({ year: moment().format('YYYY') });
  75. if (!plan) {
  76. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '当前全年计划不存在');
  77. }
  78. // 根据计划取得当前年份下计划学校培训学生人数
  79. const school = await this.schmodel.findOne({ code: id });
  80. if (!school) {
  81. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学校信息不存在');
  82. }
  83. const data = {};
  84. data.planstu = school.number;
  85. // 根据学校id取得学校上报的学生数
  86. const schstus = await this.smodel.find({ planid: plan.id, schid: id });
  87. data.schstu = schstus.length;
  88. // 取得已培训的学生数
  89. const trainstu = schstus.filter(item => item.openid);
  90. data.trainstu = trainstu.length;
  91. // 取得内未培训的学生数
  92. const notrainstu = schstus.filter(item => !item.openid);
  93. data.notrainstu = notrainstu.length;
  94. // 取得学生请假数和退出数
  95. const levelstus = [];
  96. // 循环取得所有请假和退出的学生信息
  97. for (const elm of trainstu) {
  98. const level = await this.lmodel.findOne({ studentid: elm.id, status: '1' });
  99. if (level) {
  100. levelstus.push(level);
  101. }
  102. }
  103. // 筛选出请假数和退出数
  104. const levelqj = levelstus.filter(item => item.type === '0');
  105. const levelexit = levelstus.filter(item => item.type === '1');
  106. data.levelqj = levelqj.length;
  107. data.levelexit = levelexit.length;
  108. return data;
  109. }
  110. }
  111. module.exports = CountService;