count.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. // levelexit:退出人数 √
  20. // levelqj:请假人数 √
  21. // notrainstu:上传名单,但是未培训人数 student表中的数据不稳,后加的可能没有了.被人绑定更换也可能没有了.所以 这个数字用 student.total - user(type=4&&openid) 的结果 √
  22. // schstu:上传名单人数 该年度计划下,的student.total √
  23. // trainstu:已参加培训人数 user(type==4&&openid).total √
  24. // planstu:年度计划人数(重算) 根据年度计划学校的分配计算 √
  25. // schs:[{schname 学校名称,schnum 学校人数}] 学校上传人数 试用聚合做 √
  26. // 取得当前年份计划信息
  27. const setting = await this.setmodel.findOne();
  28. if (!setting) {
  29. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '没有找到默认设置');
  30. }
  31. const { planid } = setting;
  32. if (!planid) {
  33. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '没有找到默认年度计划');
  34. }
  35. const trainPlan = await this.tmodel.findById(planid);
  36. if (!trainPlan) {
  37. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '没有找到默认年度计划');
  38. }
  39. let { termnum } = trainPlan;
  40. if (!_.isArray(termnum)) {
  41. throw new BusinessError(ErrorCode.DATA_INVALID, '默认年度计划的期数据格式不正确');
  42. }
  43. // 转换下,因为之后用的期id不是ObjectId
  44. termnum = JSON.parse(JSON.stringify(termnum));
  45. const termids = termnum.map(i => i._id);
  46. const data = {};
  47. // 请假,退出
  48. const levelqj = await this.ctx.model.Leave.count({
  49. termid: { $in: termids },
  50. type: '0',
  51. status: '1',
  52. });
  53. const levelexit = await this.ctx.model.Leave.count({
  54. termid: { $in: termids },
  55. type: '1',
  56. status: '1',
  57. });
  58. data.levelqj = levelqj || 0;
  59. data.levelexit = levelexit || 0;
  60. // 参加培训的学生
  61. const trainstuRes = await this.ctx.model.Student.aggregate([
  62. { $match: { planid } },
  63. { $match: { openid: { $exists: true } } },
  64. { $count: 'total' },
  65. ]);
  66. const h = _.head(trainstuRes);
  67. if (h) {
  68. let { total: trainstu } = h;
  69. // 减去退出的学生
  70. trainstu = trainstu - data.levelexit || 0;
  71. data.trainstu = trainstu;
  72. } else {
  73. data.trainstu = 0;
  74. }
  75. // 已上传的学生总数
  76. const schstu = await this.ctx.model.Student.count({ planid });
  77. data.schstu = schstu;
  78. // 未参加培训的学生
  79. const notrainstu = schstu - data.trainstu;
  80. data.notrainstu = notrainstu;
  81. // 年度计划实际人数 这个还没用聚合,不是很优雅
  82. const mid = await this.ctx.model.Schtime.find({ planid });
  83. const planstu = mid.reduce((p, n) => p + n.arrange.reduce((np, nn) => np + (nn.number * 1 || 0), 0), 0);
  84. data.planstu = planstu;
  85. // 各个学校上传的人数
  86. const schs = await this.ctx.model.Student.aggregate([
  87. { $match: { planid } },
  88. { $group: { _id: '$schid', sum: { $sum: 1 } } },
  89. {
  90. $lookup: {
  91. from: 'school',
  92. localField: '_id',
  93. foreignField: 'code',
  94. as: 'school',
  95. },
  96. },
  97. { $unwind: '$school' },
  98. { $project: { _id: 0, schname: '$school.name', schnum: '$sum' } },
  99. ]);
  100. data.schs = schs;
  101. return data;
  102. }
  103. // 按学校id统计查询
  104. async countschstu({ id }) {
  105. // levelexit:退出人数 √
  106. // levelqj:请假人数 √
  107. // notrainstu:上传名单,但是未培训人数 student表中的数据不稳,后加的可能没有了.被人绑定更换也可能没有了.所以 这个数字用 student.total - user(type=4&&openid) 的结果 √
  108. // schstu:上传名单人数 该年度计划下,的student.total √
  109. // trainstu:已参加培训人数 user(type==4&&openid).total √
  110. // planstu:年度计划人数(重算) 根据年度计划学校的分配计算 √
  111. // 取得当前年份计划信息
  112. const setting = await this.setmodel.findOne();
  113. if (!setting) {
  114. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '没有找到默认设置');
  115. }
  116. const { planid } = setting;
  117. if (!planid) {
  118. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '没有找到默认年度计划');
  119. }
  120. const trainPlan = await this.tmodel.findById(planid);
  121. if (!trainPlan) {
  122. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '没有找到默认年度计划');
  123. }
  124. let { termnum } = trainPlan;
  125. if (!_.isArray(termnum)) {
  126. throw new BusinessError(ErrorCode.DATA_INVALID, '默认年度计划的期数据格式不正确');
  127. }
  128. // 转换下,因为之后用的期id不是ObjectId
  129. termnum = JSON.parse(JSON.stringify(termnum));
  130. const termids = termnum.map(i => i._id);
  131. const data = {};
  132. // 请假,退出
  133. const levelqj = await this.ctx.model.Leave.count({
  134. termid: { $in: termids },
  135. type: '0',
  136. status: '1',
  137. schid: id,
  138. });
  139. const levelexit = await this.ctx.model.Leave.count({
  140. termid: { $in: termids },
  141. type: '1',
  142. status: '1',
  143. schid: id,
  144. });
  145. data.levelqj = levelqj || 0;
  146. data.levelexit = levelexit || 0;
  147. // 参加培训的学生
  148. const trainstuRes = await this.ctx.model.Student.aggregate([
  149. { $addFields: { u_id: { $toString: '$_id' } } },
  150. { $match: { planid, schid: id } },
  151. {
  152. $lookup: {
  153. from: 'user',
  154. localField: 'u_id',
  155. foreignField: 'uid',
  156. as: 'user',
  157. },
  158. },
  159. { $match: { 'user.type': '4', 'user.openid': { $exists: true } } },
  160. { $count: 'total' },
  161. ]);
  162. const h = _.head(trainstuRes);
  163. if (h) {
  164. let { total: trainstu } = h;
  165. // 减去退出的学生
  166. trainstu = trainstu - data.levelexit || 0;
  167. data.trainstu = trainstu;
  168. } else {
  169. data.trainstu = 0;
  170. }
  171. // 已上传的学生总数
  172. const schstu = await this.ctx.model.Student.count({ planid, schid: id });
  173. data.schstu = schstu;
  174. // 未参加培训的学生
  175. const notrainstu = schstu - data.trainstu;
  176. data.notrainstu = notrainstu;
  177. // 年度计划实际人数 这个还没用聚合,不是很优雅
  178. const mid = await this.ctx.model.Schtime.find({ planid, schid: id });
  179. const planstu = mid.reduce((p, n) => p + n.arrange.reduce((np, nn) => np + (nn.number * 1 || 0), 0), 0);
  180. data.planstu = planstu;
  181. return data;
  182. }
  183. }
  184. module.exports = CountService;