count.js 4.3 KB

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