123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- '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 ClassService extends CrudService {
- constructor(ctx) {
- super(ctx, 'class');
- this.model = this.ctx.model.Class;
- this.stumodel = this.ctx.model.Student;
- this.lessmodel = this.ctx.model.Lesson;
- this.umodel = this.ctx.model.User;
- this.tmodel = this.ctx.model.Trainplan;
- this.gmodel = this.ctx.model.Group;
- }
- async divide(data) {
- const { planid, termid } = data;
- assert(planid, '计划id为必填项');
- assert(termid, '期id为必填项');
- // 根据计划id与期id查询所有批次下的班级
- const newclass = await this.model.find({ planid, termid });
- if (!newclass) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '班级信息不存在');
- }
- // 根据计划和期查询所有上报的学生 并按照学校排序
- const newstudent = await this.stumodel.find({ termid }).sort({ schid: 1 });
- if (!newstudent) {
- throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '学生信息不存在');
- }
- let _students = _.map(newstudent, 'id');
- for (const _class of newclass) {
- // 取得班级人数
- const stunum = _class.number;
- // 取出班级人数下的学生id
- const beforestu = _.take(_students, stunum);
- // 将取出的数据调用更新学生的班级信息方法
- await this.studentup(_class.id, _class.batchid, beforestu);
- // 将班级自动分为组
- await this.groupcreate(termid, _class.batchid, _class.id);
- // 取出的学生数据从原数据中删除
- _students = _.difference(_students, beforestu);
- }
- }
- // 根据传入的学生列表和班级id更新学生信息
- async studentup(classid, batchid, beforestu) {
- // 循环学生id
- for (const stuid of beforestu) {
- const student = await this.stumodel.findById(stuid);
- if (student) {
- student.classid = classid;
- student.batchid = batchid;
- await student.save();
- }
- }
- }
- // 自动分组
- async groupcreate(termid, batchid, classid) {
- const group = await this.gmodel.find({ termid, batchid, classid });
- if (group.length === 0) {
- for (let i = 1; i < 8; i++) {
- const name = i + '组';
- const newdata = { name, termid, batchid, classid };
- await this.gmodel.create(newdata);
- }
- }
- }
- // 根据传入的学生列表和班级id更新学生信息
- async studentupclass({ id }, data) {
- assert(id, '班级id为必填项');
- // 循环学生id
- for (const stuid of data) {
- const student = await this.stumodel.findById(stuid);
- if (student) {
- student.classid = id;
- await student.save();
- }
- }
- }
- async notice(data) {
- for (const classid of data.classids) {
- // 根据班级id找到需要通知的班级
- const _class = await this.model.findById(classid);
- const { headteacherid } = _class;
- // 根据班级id找到对应的课程表
- const lesson = await this.lessmodel.findOne({ classid });
- if (lesson) {
- const lessons = lesson.lessons;
- const remark = '感谢您的使用';
- const date = await this.ctx.service.util.updatedate();
- const detail = '班级各项信息已确认,请注意查收';
- // 遍历班级授课教师发送通知
- for (const lessoninfo of lessons) {
- const teaid = lessoninfo.teaid;
- const _teacher = await this.umodel.findOne({ uid: teaid, type: '3' });
- if (_teacher) {
- const teaopenid = _teacher.openid;
- this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, teaopenid, '您有一个新的通知', detail, date, remark, classid);
- }
- }
- // 给班主任发送通知
- const _headteacher = await this.umodel.findOne({ uid: headteacherid, type: '1' });
- if (_headteacher) {
- const headteaopenid = _headteacher.openid;
- this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, headteaopenid, '您有一个新的通知', detail, date, remark, classid);
- }
- // 根据班级的期id查询对应的培训计划
- const trainplan = await this.tmodel.findOne({ 'termnum._id': _class.termid });
- const term = await trainplan.termnum.id(_class.termid);
- const batch = await term.batchnum.id(_class.batchid);
- const startdate = batch.startdate;
- const classname = _class.name;
- // 给班级所有学生发送邮件通知
- const students = await this.stumodel.find({ classid });
- for (const student of students) {
- const { email, name } = student;
- const subject = '吉林省高等学校毕业生就业指导中心通知';
- const text = name + '您好!\n欢迎参加由吉林省高等学校毕业生就业指导中心举办的“双困生培训会”。\n您所在的班级为:' + classname + '\n班级开课时间为:' + startdate;
- this.ctx.service.util.sendMail(email, subject, text);
- }
- }
- }
- }
- async uptea(data) {
- for (const _data of data) {
- const classInfo = await this.model.findById(_data.id);
- classInfo.headteacherid = _data.headteacherid;
- await classInfo.save();
- }
- }
- async searchDate({ classid }) {
- const classInfo = await this.model.findById(classid);
- const trainplan = await this.tmodel.findById(classInfo.planid);
- if (trainplan) {
- const term = _.filter(trainplan.termnum, item => item.id === classInfo.termid);
- if (term.length > 0) {
- const batch = _.filter(term[0].batchnum, item => item.id === classInfo.batchid);
- if (batch.length > 0) {
- return { startdate: batch[0].startdate, enddate: batch[0].enddate };
- }
- }
- }
- }
- }
- module.exports = ClassService;
|