'use strict'; const assert = require('assert'); const _ = require('lodash'); const { ObjectId } = require('mongoose').Types; const { CrudService } = require('naf-framework-mongoose/lib/service'); const { BusinessError, ErrorCode } = require('naf-core').Error; class StudentService extends CrudService { constructor(ctx) { super(ctx, 'student'); this.model = this.ctx.model.Student; this.umodel = this.ctx.model.User; this.tmodel = this.ctx.model.Trainplan; this.clamodel = this.ctx.model.Class; this.upmodel = this.ctx.model.Uploadtask; this.gmodel = this.ctx.model.Group; } // 查询 async query({ skip, limit, ...info }) { const total = await this.model.count(info); const res = await this.model .find(info) .skip(Number(skip)) .limit(Number(limit)); const data = []; for (const elm of res) { const plan = await this.tmodel.findOne({ 'termnum._id': ObjectId(elm.termid), }); const newdata = { ...JSON.parse(JSON.stringify(elm)) }; if (plan) { const term = await plan.termnum.id(elm.termid); newdata.termname = term.term; if (elm.batchid) { const _batch = await term.batchnum.id(elm.batchid); newdata.batchname = _batch.batch; } } if (elm.classid) { const classs = await this.clamodel.findById(elm.classid); if (classs) { newdata.classname = classs.name; } } data.push(newdata); } const result = { total, data }; return result; } // 查询 async seek({ termid, type, batchid, skip, limit }) { const total = await this.model.count({ termid, type, batchid, $or: [{ classid: null }, { classid: '' }], }); const data = await this.model .find({ termid, type, batchid, $or: [{ classid: null }, { classid: '' }] }) .skip(Number(skip)) .limit(Number(limit)); const result = { total, data }; return result; } async findbedroom(data) { const { batchid, classid } = data; const result = []; // 如果传的是批次id if (batchid) { // 查询该批次下的所有学生 const students = await this.model.find({ batchid }); const bedroomList = new Set(); // 查询该批次的所有寝室号 for (const student of students) { bedroomList.add(student.bedroom); } let studentList = []; // 查询该批次所有寝室下的学生名单 for (const bedroom of bedroomList) { const newstudents = await this.model.find({ bedroom }); for (const newstudent of newstudents) { studentList.push(newstudent.name); } result.push({ bedroom, studentList }); studentList = []; } } // 如果传的是班级id if (classid) { // 查询该班级所有学生 const students = await this.model.find({ classid }); const bedroomList = new Set(); // 查询该班级所有寝室号 for (const student of students) { bedroomList.add(student.bedroom); } let studentList = []; // 查询该班级所有寝室的学生名单 for (const bedroom of bedroomList) { const newstudents = await this.model.find({ bedroom }); for (const newstudent of newstudents) { // 如果寝室中有非本班级学生(混寝),则过滤掉不予显示 if (newstudent.classid === classid) { studentList.push(newstudent.name); } } result.push({ bedroom, studentList }); studentList = []; } } return result; } async upjob(data) { const { stuid, job } = data; const student = await this.model.findById(stuid); student.job = job; if (job === '班长' || job === '学委') { const user = await this.umodel.findOne({ uid: stuid, type: '4' }); const date = await this.ctx.service.util.updatedate(); const openid = user.openid; const detail = '你已被班主任设置为' + job + ',请及时登录查看'; const remark = '感谢您的使用'; if (openid) { this.ctx.service.weixin.sendTemplateMsg( this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark ); } } return await student.save(); } // 根据学生id删除班级 async deleteclass(data) { for (const el of data) { const student = await this.model.findById(el); if (student) { student.classid = ''; await student.save(); } } } // 根据班级id查出班级各个学生的分数 async findscore({ skip, limit, ...info }) { const { classid } = info; const total = await this.model.count(info); const students = await this.model .find(info) .skip(Number(skip)) .limit(Number(limit)); const data = []; const groups = await this.gmodel.find({ classid }); for (const student of students) { const _student = JSON.parse(JSON.stringify(student)); const group = groups.find(item => item.students.find(stuinfo => stuinfo.stuid === _student.id) ); console.log(group); if (group) { _student.groupscore = group.score; } const tasks = await this.upmodel.find({ studentid: _student.id }); _student.tasks = tasks; data.push(_student); } return { total, data }; } async findbystuids({ data }) { const res = []; for (const stuid of data) { const stu = await this.model.findById(stuid); if (stu)res.push(stu); } return res; } // 根据学生id删除学生 async deletestus(data) { for (const el of data) { await this.model.deleteOne({ _id: ObjectId(el) }); } } // 批量更新寝室号 async updatabedroom(data) { for (const el of data) { const student = await this.model.findById(el.id); if (student) { student.bedroom = el.bedroom; await student.save(); } } } } module.exports = StudentService;