'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 BedroomService extends CrudService { constructor(ctx) { super(ctx, 'bedroom'); this.model = this.ctx.model.Bedroom; this.smodel = this.ctx.model.Student; this.tmodel = this.ctx.model.Trainplan; this.cmodel = this.ctx.model.Class; this.umodel = this.ctx.model.User; this.ctmodel = this.ctx.model.Classtype; } // 根据班级id查找班级下所有寝室列表并查询出寝室下学生信息 async roomstu({ id }) { // 通过班级id查询学生表信息 const students = await this.smodel.find({ classid: id }); const _bedrooms = _.map(students, 'bedroom'); // 取得无重复的寝室号 const bedrooms = _.uniq(_bedrooms); console.log(bedrooms); const data = []; // 根据寝室号 取得相应的学生信息 for (const elm of bedrooms) { const stus = students.filter(item => item.bedroom === elm); const newdata = { bedroom: elm, stus }; data.push(newdata); } return data; } async ibeacon(data) { assert(data.openid, '用户信息不能为空'); // 通过openid取得学生信息 const user = await this.ctx.service.user.findByOpenid(data.openid); if (!user) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '用户不存在'); } const student = await this.smodel.findById(user.uid); if (!student) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在'); } const beedroom = await this.model.findOne({ code: student.bedroom }); if (!beedroom) { throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '寝室信息不存在'); } return { data: { ibeacon: beedroom.ibeacon } }; } // 一键分寝 async apart(data) { const { trainplanid, termid, batchid } = data; assert(trainplanid, 'trainplanid不能为空'); assert(termid, 'termid不能为空'); assert(batchid, 'batchid不能为空'); // 根据计划id取得当前计划 const trainplan = await this.tmodel.findById(trainplanid); // 根据期id取得当前期信息 const term = trainplan.termnum.find(p => p.id === termid); // 根据批次id查询批次信息 const _batch = term.batchnum.find(p => p.id === batchid); // 查询所有寝室列表 const bedroomList = await this.model.find({ batch: _batch.batch, status: '0' }).sort({ floor: -1 }); // 循环所有当前批次下的寝室列表进行分寝处理 const studentList = await this.getstudents(termid, batchid); console.log('stulen-->' + studentList.length); for (const bedroom of bedroomList) { console.log(bedroom.code); // 判断当前寝室号是否已有 // 根据期id查找所有当期学生列表 const _stu = _.filter(studentList, { bedroom: bedroom.code }); console.log(_stu.length); if (bedroom.number !== _stu.length) { let i = 0; let _gender = ''; for (const stud of studentList) { console.log('stu---' + stud.bedroom); if (stud.bedroom) { if (stud.bedroom === bedroom.code) { i = i + 1; } continue; } console.log('i--->' + i); if (i === 0) { if (!bedroom.gender) { stud.bedroomid = bedroom.id; stud.bedroom = bedroom.code; await stud.save(); i = i + 1; _gender = stud.gender; } else { if (bedroom.gender === stud.gender) { stud.bedroomid = bedroom.id; stud.bedroom = bedroom.code; await stud.save(); i = i + 1; _gender = stud.gender; } } } else if (i < bedroom.number) { if (_gender === stud.gender) { stud.bedroomid = bedroom.id; stud.bedroom = bedroom.code; await stud.save(); i = i + 1; } } else if (i === bedroom.number) { i = 0; break; } } } } // 取得当前批次的所有班级 const classes = await this.cmodel.find({ batchid }); for (const _class of classes) { // 取得每个班级的班主任id const headteacherid = _class.headteacherid; const headteacher = await this.umodel.findOne({ uid: headteacherid, type: '1' }); if (headteacher && headteacher.openid) { const openid = headteacher.openid; const remark = '感谢您的使用'; const date = await this.ctx.service.util.updatedate(); const detail = '班级学生名单与寝室安排已确认,请及时查收'; this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark, _class.id); } } } // 取得符合条件的学生列表 async getstudents(termid, batchid) { // 根据期id查找所有当期学生列表 const cltype = await this.ctmodel.find({ bedroom: '0' }); const types = _.map(cltype, 'code'); console.log('-----types-start--'); console.log(types); console.log('-----types-end--'); const studentList = await this.smodel.find({ termid, batchid, type: { $in: types } }).sort({ gender: -1 }); return studentList; } // 取得符合条件的学生列表 async getbedroomstudents(termid, batchid, bedroom) { // 根据期id查找所有当期学生列表 const studentList = await this.smodel.find({ termid, batchid, bedroom }); return studentList; } } module.exports = BedroomService;