student.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class StudentService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'student');
  10. this.model = this.ctx.model.Student;
  11. this.umodel = this.ctx.model.User;
  12. this.tmodel = this.ctx.model.Trainplan;
  13. this.clamodel = this.ctx.model.Class;
  14. this.upmodel = this.ctx.model.Uploadtask;
  15. this.gmodel = this.ctx.model.Group;
  16. }
  17. async create(data) {
  18. const { name, phone: mobile, gender } = data;
  19. const res = await this.model.create(data);
  20. if (res) {
  21. const obj = { name, mobile, gender, type: '4', passwd: '12345678', uid: res._id };
  22. const user = await this.ctx.service.user.create(obj);
  23. }
  24. return res;
  25. }
  26. // 查询
  27. async query({ skip, limit, ...info }) {
  28. const total = await this.model.count(info);
  29. const res = await this.model
  30. .find(info)
  31. .skip(Number(skip))
  32. .limit(Number(limit));
  33. const data = [];
  34. for (const elm of res) {
  35. const plan = await this.tmodel.findOne({
  36. 'termnum._id': ObjectId(elm.termid),
  37. });
  38. const newdata = { ...JSON.parse(JSON.stringify(elm)) };
  39. if (plan) {
  40. const term = await plan.termnum.id(elm.termid);
  41. newdata.termname = term.term;
  42. if (elm.batchid) {
  43. const _batch = await term.batchnum.id(elm.batchid);
  44. newdata.batchname = _batch.batch;
  45. }
  46. }
  47. if (elm.classid) {
  48. const classs = await this.clamodel.findById(elm.classid);
  49. if (classs) {
  50. newdata.classname = classs.name;
  51. }
  52. }
  53. data.push(newdata);
  54. }
  55. const result = { total, data };
  56. return result;
  57. }
  58. // 查询
  59. async seek({ termid, type, batchid, skip, limit }) {
  60. const total = await this.model.count({
  61. termid,
  62. type,
  63. batchid,
  64. $or: [{ classid: null }, { classid: '' }],
  65. });
  66. const data = await this.model
  67. .find({ termid, type, batchid, $or: [{ classid: null }, { classid: '' }] })
  68. .skip(Number(skip))
  69. .limit(Number(limit));
  70. const result = { total, data };
  71. return result;
  72. }
  73. async findbedroom(data) {
  74. const { batchid, classid } = data;
  75. const result = [];
  76. // 如果传的是批次id
  77. if (batchid) {
  78. // 查询该批次下的所有学生
  79. const students = await this.model.find({ batchid });
  80. const bedroomList = new Set();
  81. // 查询该批次的所有寝室号
  82. for (const student of students) {
  83. bedroomList.add(student.bedroom);
  84. }
  85. let studentList = [];
  86. // 查询该批次所有寝室下的学生名单
  87. for (const bedroom of bedroomList) {
  88. const newstudents = await this.model.find({ bedroom });
  89. for (const newstudent of newstudents) {
  90. studentList.push(newstudent.name);
  91. }
  92. result.push({ bedroom, studentList });
  93. studentList = [];
  94. }
  95. }
  96. // 如果传的是班级id
  97. if (classid) {
  98. // 查询该班级所有学生
  99. const students = await this.model.find({ classid });
  100. const bedroomList = new Set();
  101. // 查询该班级所有寝室号
  102. for (const student of students) {
  103. bedroomList.add(student.bedroom);
  104. }
  105. let studentList = [];
  106. // 查询该班级所有寝室的学生名单
  107. for (const bedroom of bedroomList) {
  108. const newstudents = await this.model.find({ bedroom });
  109. for (const newstudent of newstudents) {
  110. // 如果寝室中有非本班级学生(混寝),则过滤掉不予显示
  111. if (newstudent.classid === classid) {
  112. studentList.push(newstudent.name);
  113. }
  114. }
  115. result.push({ bedroom, studentList });
  116. studentList = [];
  117. }
  118. }
  119. return result;
  120. }
  121. async upjob(data) {
  122. const { stuid, job } = data;
  123. const student = await this.model.findById(stuid);
  124. student.job = job;
  125. if (job === '班长' || job === '学委') {
  126. const user = await this.umodel.findOne({ uid: stuid, type: '4' });
  127. const date = await this.ctx.service.util.updatedate();
  128. const openid = user.openid;
  129. const detail = '你已被班主任设置为' + job + ',请及时登录查看';
  130. const remark = '感谢您的使用';
  131. if (openid) {
  132. this.ctx.service.weixin.sendTemplateMsg(
  133. this.ctx.app.config.REVIEW_TEMPLATE_ID,
  134. openid,
  135. '您有一个新的通知',
  136. detail,
  137. date,
  138. remark
  139. );
  140. }
  141. }
  142. return await student.save();
  143. }
  144. // 根据学生id删除班级
  145. async deleteclass(data) {
  146. for (const el of data) {
  147. const student = await this.model.findById(el);
  148. if (student) {
  149. student.classid = '';
  150. await student.save();
  151. }
  152. }
  153. }
  154. // 根据班级id查出班级各个学生的分数
  155. async findscore({ skip, limit, ...info }) {
  156. const { classid } = info;
  157. const total = await this.model.count(info);
  158. const students = await this.model
  159. .find(info)
  160. .skip(Number(skip))
  161. .limit(Number(limit));
  162. const data = [];
  163. const groups = await this.gmodel.find({ classid });
  164. for (const student of students) {
  165. const _student = JSON.parse(JSON.stringify(student));
  166. const group = groups.find(item =>
  167. item.students.find(stuinfo => stuinfo.stuid === _student.id)
  168. );
  169. console.log(group);
  170. if (group) {
  171. _student.groupscore = group.score;
  172. }
  173. const tasks = await this.upmodel.find({ studentid: _student.id });
  174. _student.tasks = tasks;
  175. data.push(_student);
  176. }
  177. return { total, data };
  178. }
  179. async findbystuids({ data }) {
  180. const res = [];
  181. for (const stuid of data) {
  182. const stu = await this.model.findById(stuid);
  183. if (stu)res.push(stu);
  184. }
  185. return res;
  186. }
  187. // 根据学生id删除学生
  188. async deletestus(data) {
  189. for (const el of data) {
  190. await this.model.deleteOne({ _id: ObjectId(el) });
  191. }
  192. }
  193. // 批量更新寝室号
  194. async updatabedroom(data) {
  195. for (const el of data) {
  196. const student = await this.model.findById(el.id);
  197. if (student) {
  198. student.bedroom = el.bedroom;
  199. await student.save();
  200. }
  201. }
  202. }
  203. }
  204. module.exports = StudentService;