count.js 7.0 KB

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