student.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. const moment = require('moment');
  8. class StudentService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'student');
  11. this.model = this.ctx.model.Student;
  12. this.umodel = this.ctx.model.User;
  13. this.tmodel = this.ctx.model.Trainplan;
  14. this.clamodel = this.ctx.model.Class;
  15. this.upmodel = this.ctx.model.Uploadtask;
  16. this.gmodel = this.ctx.model.Group;
  17. this.psmodel = this.ctx.model.Personalscore;
  18. this.gsmodel = this.ctx.model.Groupscore;
  19. this.uqmodel = this.ctx.model.Uploadquestion;
  20. this.scoremodel = this.ctx.model.Score;
  21. this.leavemodel = this.ctx.model.Leave;
  22. this.attendmodel = this.ctx.model.Attendance;
  23. }
  24. async create(data) {
  25. const { name, phone: mobile, gender } = data;
  26. const res = await this.model.create(data);
  27. if (res) {
  28. const obj = {
  29. name,
  30. mobile,
  31. gender,
  32. type: '4',
  33. passwd: '12345678',
  34. uid: res._id,
  35. };
  36. const user = await this.ctx.service.user.create(obj);
  37. }
  38. return res;
  39. }
  40. async delete({ id }) {
  41. // 学生表,用户表,作业表,问卷表,评分,请假,考勤
  42. await this.model.deleteOne({ _id: ObjectId(id) });
  43. await this.umodel.deleteOne({ uid: id });
  44. await this.upmodel.deleteMany({ studentid: id });
  45. await this.uqmodel.deleteMany({ studentid: id });
  46. await this.scoremodel.deleteMany({ stuid: id });
  47. await this.leavemodel.deleteMany({ studentid: id });
  48. await this.attendmodel.deleteMany({ studentid: id });
  49. }
  50. // 查询
  51. async query({ skip, limit, ...info }) {
  52. const { termid, classid } = info;
  53. if (!classid) {
  54. const classes = await this.clamodel.find({ termid });
  55. for (const cla of classes) {
  56. await this.arrangeNumber(cla._id);
  57. }
  58. } else {
  59. await this.arrangeNumber(classid);
  60. }
  61. const total = await this.model.count(info);
  62. const res = await this.model
  63. .find(info)
  64. .skip(Number(skip))
  65. .limit(Number(limit));
  66. const data = [];
  67. for (const elm of res) {
  68. const plan = await this.tmodel.findOne({
  69. 'termnum._id': ObjectId(elm.termid),
  70. });
  71. const newdata = { ...JSON.parse(JSON.stringify(elm)) };
  72. if (plan) {
  73. const term = await plan.termnum.id(elm.termid);
  74. newdata.termname = term.term;
  75. if (elm.batchid) {
  76. const _batch = await term.batchnum.id(elm.batchid);
  77. newdata.batchname = _batch.batch;
  78. }
  79. }
  80. if (elm.classid) {
  81. const classs = await this.clamodel.findById(elm.classid);
  82. if (classs) {
  83. newdata.classname = classs.name;
  84. }
  85. }
  86. data.push(newdata);
  87. }
  88. const result = { total, data };
  89. return result;
  90. }
  91. // 查询
  92. async seek({ termid, type, batchid, skip, limit }) {
  93. const total = await this.model.count({
  94. termid,
  95. type,
  96. batchid,
  97. $or: [{ classid: null }, { classid: '' }],
  98. });
  99. const data = await this.model
  100. .find({
  101. termid,
  102. type,
  103. batchid,
  104. $or: [{ classid: null }, { classid: '' }],
  105. })
  106. .skip(Number(skip))
  107. .limit(Number(limit));
  108. const result = { total, data };
  109. return result;
  110. }
  111. async findbedroom(data) {
  112. const { batchid, classid } = data;
  113. const result = [];
  114. // 如果传的是批次id
  115. if (batchid) {
  116. // 查询该批次下的所有学生
  117. const students = await this.model.find({ batchid });
  118. const bedroomList = new Set();
  119. // 查询该批次的所有寝室号
  120. for (const student of students) {
  121. bedroomList.add(student.bedroom);
  122. }
  123. let studentList = [];
  124. // 查询该批次所有寝室下的学生名单
  125. for (const bedroom of bedroomList) {
  126. const newstudents = await this.model.find({ bedroom });
  127. for (const newstudent of newstudents) {
  128. studentList.push(newstudent.name);
  129. }
  130. result.push({ bedroom, studentList });
  131. studentList = [];
  132. }
  133. }
  134. // 如果传的是班级id
  135. if (classid) {
  136. // 查询该班级所有学生
  137. const students = await this.model.find({ classid });
  138. const bedroomList = new Set();
  139. // 查询该班级所有寝室号
  140. for (const student of students) {
  141. bedroomList.add(student.bedroom);
  142. }
  143. let studentList = [];
  144. // 查询该班级所有寝室的学生名单
  145. for (const bedroom of bedroomList) {
  146. const newstudents = await this.model.find({ bedroom });
  147. for (const newstudent of newstudents) {
  148. // 如果寝室中有非本班级学生(混寝),则过滤掉不予显示
  149. if (newstudent.classid === classid) {
  150. studentList.push(newstudent.name);
  151. }
  152. }
  153. result.push({ bedroom, studentList });
  154. studentList = [];
  155. }
  156. }
  157. return result;
  158. }
  159. async upjob(data) {
  160. const { stuid, job } = data;
  161. const student = await this.model.findById(stuid);
  162. student.job = job;
  163. if (job === '班长' || job === '学委') {
  164. const user = await this.umodel.findOne({ uid: stuid, type: '4' });
  165. const date = await this.ctx.service.util.updatedate();
  166. const openid = user.openid;
  167. const detail = '你已被班主任设置为' + job + ',请及时登录查看';
  168. const remark = '感谢您的使用';
  169. if (openid) {
  170. this.ctx.service.weixin.sendTemplateMsg(
  171. this.ctx.app.config.REVIEW_TEMPLATE_ID,
  172. openid,
  173. '您有一个新的通知',
  174. detail,
  175. date,
  176. remark
  177. );
  178. }
  179. }
  180. return await student.save();
  181. }
  182. // 根据学生id删除班级
  183. async deleteclass(data) {
  184. for (const el of data) {
  185. const student = await this.model.findById(el);
  186. if (student) {
  187. student.classid = '';
  188. await student.save();
  189. }
  190. }
  191. }
  192. // 根据班级id查出班级各个学生的分数
  193. async findscore({ skip, limit, ...info }) {
  194. const { classid } = info;
  195. const total = await this.model.count(info);
  196. const students = await this.model
  197. .find(info)
  198. .skip(Number(skip))
  199. .limit(Number(limit));
  200. const data = [];
  201. const groups = await this.gmodel.find({ classid });
  202. for (const student of students) {
  203. const _student = JSON.parse(JSON.stringify(student));
  204. const group = groups.find(item =>
  205. item.students.find(stuinfo => stuinfo.stuid === _student.id)
  206. );
  207. console.log(group);
  208. if (group) {
  209. _student.groupscore = group.score;
  210. }
  211. const tasks = await this.upmodel.find({ studentid: _student.id });
  212. _student.tasks = tasks;
  213. data.push(_student);
  214. }
  215. return { total, data };
  216. }
  217. async findbystuids({ data }) {
  218. const res = [];
  219. for (const stuid of data) {
  220. const stu = await this.model.findById(stuid);
  221. if (stu) res.push(stu);
  222. }
  223. return res;
  224. }
  225. // 根据学生id删除学生
  226. async deletestus(data) {
  227. for (const id of data) {
  228. await this.model.deleteOne({ _id: ObjectId(id) });
  229. await this.umodel.deleteOne({ uid: id });
  230. await this.upmodel.deleteMany({ studentid: id });
  231. await this.uqmodel.deleteMany({ studentid: id });
  232. await this.scoremodel.deleteMany({ stuid: id });
  233. await this.leavemodel.deleteMany({ studentid: id });
  234. await this.attendmodel.deleteMany({ studentid: id });
  235. }
  236. }
  237. // 批量更新寝室号
  238. async updatabedroom(data) {
  239. for (const el of data) {
  240. const student = await this.model.findById(el.id);
  241. if (student) {
  242. student.bedroom = el.bedroom;
  243. await student.save();
  244. }
  245. }
  246. }
  247. /**
  248. * 计算班级的优秀学生
  249. * 规则:班级干部(学生的job!=='普通学生'),检查是否有评优资格(is_fine_status==='0'可以评优),全部评优;
  250. * 普通学生取总成绩前10的人评优(非10位并列时,名额该占用就占用;第10名若有并列,就全都要)
  251. * @param {String} param0 {id=>班级id}
  252. */
  253. async getFineStudent({ id: classid }) {
  254. // 获取班级学生列表
  255. let studentList = await this.model.find({ classid });
  256. // 重置评优,干部全优秀
  257. studentList = studentList.map(i => {
  258. if (i.job.includes('普通')) i.is_fine = '0';
  259. else i.is_fine = '1';
  260. return i;
  261. });
  262. // 初始化后取出不需要算的人,他们就这样,没必要算
  263. const reverseList = studentList.filter(
  264. f => !(f.is_fine !== '2' && f.job.includes('普通'))
  265. );
  266. // 过滤出取消评优资格的学生和干部;干部就是优秀;被取消资格就别凑热闹了
  267. studentList = studentList.filter(
  268. f => f.is_fine !== '2' && f.job.includes('普通')
  269. );
  270. // 获取平时分
  271. const dailyScoreList = await this.psmodel.find({ classid });
  272. studentList = this.dealScoreList(dailyScoreList, studentList);
  273. // 获取作业分
  274. const taskScoreList = await this.upmodel.find({ classid });
  275. studentList = this.dealScoreList(taskScoreList, studentList);
  276. // 获取小组分,小组
  277. const groupList = await this.gmodel.find({ classid });
  278. const groupScoreList = await this.gsmodel.find({ classid });
  279. studentList = this.dealGroupScoreList(
  280. groupList,
  281. groupScoreList,
  282. studentList
  283. );
  284. studentList = studentList.sort(
  285. (a, b) => (b.score * 1 || 0) - (a.score * 1 || 0)
  286. );
  287. // 排名
  288. // eslint-disable-next-line no-unused-vars
  289. let num = 0;
  290. for (const student of studentList) {
  291. // 先判断是否超过第10位,超过就跳出
  292. if (num > 10) break;
  293. const { score, is_fine } = student;
  294. // 最开始初始化过所有人的状态,并且将干部和不能评优的人都过滤出去,所以正常学生应该都是没有评优,如果此处已经是优秀,那么就是和前人同分改的,直接跳过就好
  295. if (is_fine === '1') continue;
  296. // 没有分凑什么热闹
  297. if (!score) continue;
  298. let plus = 1; // 这轮有多少人,到这了,这个人肯定是要改了,所以默认1
  299. // 评优
  300. student.is_fine = '1';
  301. const rlist = studentList.filter(f => f.score === score);
  302. // 处理同分的人也都变成is_fine
  303. for (const stud of rlist) {
  304. stud.is_fine = '1';
  305. const sindex = studentList.findIndex(f =>
  306. ObjectId(stud._id).equals(f._id)
  307. );
  308. if (sindex >= 0) {
  309. studentList[sindex] = stud;
  310. plus++;
  311. }
  312. }
  313. // num+plus,算下num
  314. num = num + plus;
  315. }
  316. // 算完的和不用算的合并,提交
  317. const lastList = [ ...studentList, ...reverseList ];
  318. for (const student of lastList) {
  319. const res = await student.save();
  320. // const { meta, ...data } = student;
  321. // const r = await this.model.findByIdAndUpdate(
  322. // { _id: ObjectId(student._id) },
  323. // data
  324. // );
  325. console.log(res);
  326. }
  327. }
  328. /**
  329. * 将分数放到学生身上
  330. * @param {Array} scoreList 班级的分数列表
  331. * @param {Array} studentList 学生列表
  332. */
  333. dealScoreList(scoreList, studentList) {
  334. scoreList = _.groupBy(scoreList, 'studentid');
  335. studentList = studentList.map(i => {
  336. const slist = scoreList[i._id];
  337. if (slist) {
  338. i.score =
  339. (i.score * 1 || 0) +
  340. slist.reduce((p, n) => p + (n.score * 1 || 0), 0);
  341. }
  342. return i;
  343. });
  344. return studentList;
  345. }
  346. /**
  347. * 将 学生所在 组的 团队平均分 + 到学生身上
  348. * @param {Array} groupList 班级的小组的列表
  349. * @param {Array} scoreList 所有小组的分数列表
  350. * @param {Array} studentList 学生列表
  351. */
  352. dealGroupScoreList(groupList, scoreList, studentList) {
  353. // console.log(groupList);
  354. scoreList = _.groupBy(scoreList, 'groupid');
  355. // 算出每组的平均分,之后加给学生
  356. groupList = groupList.map(i => {
  357. const { students } = i;
  358. if (students.length > 0) {
  359. const slist = scoreList[i._id];
  360. if (slist) {
  361. i.score = slist.reduce((p, n) => p + (n.score * 1 || 0), 0);
  362. i.score = _.floor(_.divide(i.score, students.length), 2);
  363. }
  364. }
  365. return i;
  366. });
  367. // 每个学生加自己的组的平均分
  368. studentList = studentList.map(i => {
  369. const r = groupList.find(f =>
  370. f.students.find(sf => ObjectId(sf.stuid).equals(i._id))
  371. );
  372. if (r) i.score = (i.score * 1 || 0) + (r.score * 1 || 0);
  373. return i;
  374. });
  375. return studentList;
  376. }
  377. // 将学生排号
  378. async arrangeNumber(classid) {
  379. const studList = await this.model.find({ classid });
  380. let number = 1;
  381. // 查每个学生的编号,如果没有,就给赋上值;有,就给number赋上值,然后继续下一位
  382. for (const stu of studList) {
  383. if (!stu.number) {
  384. if (number * 1 < 10) stu.number = `0${number}`;
  385. else stu.number = number;
  386. await stu.save();
  387. } else {
  388. number = stu.number * 1;
  389. }
  390. number = number * 1 + 1;
  391. }
  392. // console.log(`last number => ${number}`);
  393. }
  394. // 导出学生 目前:拓展训练保险用
  395. async exportStudent(data) {
  396. let { data: studentList } = await this.query(data);
  397. studentList = JSON.parse(JSON.stringify(studentList));
  398. let ids = studentList.map(i => i.classid);
  399. ids = _.uniq(ids);
  400. const classList = [];
  401. for (const id of ids) {
  402. const cla = await this.ctx.service.class.fetch({ id });
  403. if (!cla) continue;
  404. cla.date = moment(cla.date).add(1, 'd').format('YYYY-MM-DD');
  405. classList.push(cla);
  406. }
  407. studentList = studentList.map(i => {
  408. const c = classList.find(f => ObjectId(i.classid).equals(f._id));
  409. if (c) i.date = c.date;
  410. return i;
  411. });
  412. const meta = this.metaBx();
  413. return await this.ctx.service.util.toExcel(studentList, meta, '学生名单');
  414. }
  415. metaBx() {
  416. const header = [
  417. {
  418. header: '姓名',
  419. key: 'name',
  420. width: 20,
  421. },
  422. {
  423. header: '拓训日期',
  424. key: 'date',
  425. width: 20,
  426. },
  427. {
  428. header: '性别',
  429. key: 'gender',
  430. width: 10,
  431. },
  432. {
  433. header: '民族',
  434. key: 'nation',
  435. width: 20,
  436. },
  437. {
  438. header: '身份证号',
  439. key: 'id_number',
  440. width: 20,
  441. },
  442. {
  443. header: '期',
  444. key: 'termname',
  445. width: 20,
  446. },
  447. {
  448. header: '批次',
  449. key: 'batchname',
  450. width: 20,
  451. },
  452. {
  453. header: '班级',
  454. key: 'classname',
  455. width: 20,
  456. },
  457. {
  458. header: '学校',
  459. key: 'school_name',
  460. width: 20,
  461. },
  462. {
  463. header: '院系',
  464. key: 'faculty',
  465. width: 20,
  466. },
  467. {
  468. header: '专业',
  469. key: 'major',
  470. width: 20,
  471. },
  472. {
  473. header: '手机号',
  474. key: 'phone',
  475. width: 20,
  476. },
  477. ];
  478. return header;
  479. }
  480. }
  481. module.exports = StudentService;