count.js 7.1 KB

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