|
@@ -39,6 +39,9 @@ class TrainplanService extends CrudService {
|
|
|
|
|
|
async update({ id }, data) {
|
|
|
const trainplan = await this.model.findById(id);
|
|
|
+ // 保存原数据
|
|
|
+ const trainplanold = _.cloneDeep(trainplan);
|
|
|
+
|
|
|
const { year, title, termnum, festivals, status } = data;
|
|
|
if (year) {
|
|
|
trainplan.year = year;
|
|
@@ -52,35 +55,122 @@ class TrainplanService extends CrudService {
|
|
|
if (festivals) {
|
|
|
trainplan.festivals = festivals;
|
|
|
}
|
|
|
- // 如果培训计划状态改为发布
|
|
|
if (status === '1') {
|
|
|
trainplan.status = status;
|
|
|
- // 查询所有入库的教师
|
|
|
- const teachers = await this.tmodel.find({ status: '4' });
|
|
|
- for (const teacher of teachers) {
|
|
|
- const teacherid = teacher._id;
|
|
|
- const _teacher = await this.umodel.findOne({ uid: teacherid, type: '3' });
|
|
|
- const openid = _teacher.openid;
|
|
|
- const detail = trainplan.title + '已发布,请注意查收!';
|
|
|
- const date = await this.ctx.service.util.updatedate();
|
|
|
- const remark = '感谢您的使用';
|
|
|
- if (openid) {
|
|
|
- this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
|
|
|
+ }
|
|
|
+ // 如果培训计划状态改为发布,发送培训计划信息,并自动生成班级
|
|
|
+ const res = await trainplan.save();
|
|
|
+ if (res) {
|
|
|
+ if (status === '1') {
|
|
|
+ // 自动生成班级
|
|
|
+ await this.autoclass(res, trainplanold);
|
|
|
+ // 发送培训计划信息通知给相应人员
|
|
|
+ // 查询所有入库的教师
|
|
|
+ const teachers = await this.tmodel.find({ status: '4' });
|
|
|
+ for (const teacher of teachers) {
|
|
|
+ const teacherid = teacher._id;
|
|
|
+ const _teacher = await this.umodel.findOne({ uid: teacherid, type: '3' });
|
|
|
+ const openid = _teacher.openid;
|
|
|
+ const detail = trainplan.title + '已发布,请注意查收!';
|
|
|
+ const date = await this.ctx.service.util.updatedate();
|
|
|
+ const remark = '感谢您的使用';
|
|
|
+ if (openid) {
|
|
|
+ this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 查询所有学校用户
|
|
|
+ const schools = await this.umodel.find({ type: '2' });
|
|
|
+ for (const school of schools) {
|
|
|
+ const openid = school.openid;
|
|
|
+ const detail = trainplan.title + '已发布,请注意查收!';
|
|
|
+ const date = await this.ctx.service.util.updatedate();
|
|
|
+ const remark = '感谢您的使用';
|
|
|
+ if (openid) {
|
|
|
+ this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
- // 查询所有学校用户
|
|
|
- const schools = await this.umodel.find({ type: '2' });
|
|
|
- for (const school of schools) {
|
|
|
- const openid = school.openid;
|
|
|
- const detail = trainplan.title + '已发布,请注意查收!';
|
|
|
- const date = await this.ctx.service.util.updatedate();
|
|
|
- const remark = '感谢您的使用';
|
|
|
- if (openid) {
|
|
|
- this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 自动生成班级私有方法
|
|
|
+ async autoclass(res, trainplanold) {
|
|
|
+ // 首先比较当前数据和原数据的值是否有不同
|
|
|
+ // 保存后所有期id
|
|
|
+ const tremid_res = _.map(res.termnum, 'id');
|
|
|
+ // 保存前所有期id
|
|
|
+ const tremid_old = _.map(trainplanold.termnum, 'id');
|
|
|
+ // 取得要删除的期id,进行班级中删除已删除期的班级
|
|
|
+ const deltrem = _.difference(tremid_old, tremid_res);
|
|
|
+ // 循环删除已经删除期的所有班级
|
|
|
+ for (const elm of deltrem) {
|
|
|
+ await this.clamodel.deleteMany({ termid: elm });
|
|
|
+ }
|
|
|
+ // 取得所有新加期id
|
|
|
+ const addtrem = _.difference(tremid_res, tremid_old);
|
|
|
+ // 清空后循环取得所有期进行批次操作
|
|
|
+ const terms = res.termnum;
|
|
|
+ for (const el of terms) {
|
|
|
+ // 判断是否新加期
|
|
|
+ if (_.indexOf(addtrem, el.id) !== -1) {
|
|
|
+ // 循环当前新加期的批次列表,根据批次id和班级数生成班级信息
|
|
|
+ const batchnums = el.batchnum;
|
|
|
+ for (const batchnum of batchnums) {
|
|
|
+ // 取得当前批次的班级数
|
|
|
+ const classnum = batchnum.class;
|
|
|
+ for (let i = 1; i <= classnum; i++) {
|
|
|
+ const newdata = { name: i + '班', number: batchnum.number, batchid: batchnum.id, termid: el.id };
|
|
|
+ await this.clamodel.create(newdata);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 不是新加期,更新期信息
|
|
|
+ // 保存后所有期id
|
|
|
+ const batchid_res = _.map(el.batchnum, 'id');
|
|
|
+ // 保存前所有期id
|
|
|
+ const batchid_old = _.map(trainplanold.termnum.id(el.id).batchnum, 'id');
|
|
|
+ // 取得要删除的期id,进行班级中删除已删除期的班级
|
|
|
+ const delbatchs = _.difference(batchid_old, batchid_res);
|
|
|
+ // 循环删除已经删除期的所有班级
|
|
|
+ for (const delba of delbatchs) {
|
|
|
+ await this.clamodel.deleteMany({ termid: el.id, batchid: delba });
|
|
|
+ }
|
|
|
+ // 取得所有新加期id
|
|
|
+ const addbatch = _.difference(batchid_res, batchid_old);
|
|
|
+ const batchnums = el.batchnum;
|
|
|
+ for (const batchnum of batchnums) {
|
|
|
+ // 取得当前批次是否有删除
|
|
|
+ // 判断是否新加期
|
|
|
+ if (_.indexOf(addbatch, batchnum.id) !== -1) {
|
|
|
+ // 取得当前批次的班级数
|
|
|
+ const classnum = batchnum.class;
|
|
|
+ for (let i = 1; i <= classnum; i++) {
|
|
|
+ const newdata = { name: i + '班', number: batchnum.number, batchid: batchnum.id, termid: el.id };
|
|
|
+ await this.clamodel.create(newdata);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 编辑只会针对班级人数进行修改。
|
|
|
+ const _class = await this.clamodel.find({ termid: el.id, batchid: batchnum.id });
|
|
|
+ if (_class.length !== 0) {
|
|
|
+ for (const ee of _class) {
|
|
|
+ ee.number = batchnum.number;
|
|
|
+ await ee.save();
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ const classnum = batchnum.class;
|
|
|
+ for (let i = 1; i <= classnum; i++) {
|
|
|
+ const newdata = { name: i + '班', number: batchnum.number, batchid: batchnum.id, termid: el.id };
|
|
|
+ await this.clamodel.create(newdata);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- return await trainplan.save();
|
|
|
+
|
|
|
}
|
|
|
+
|
|
|
}
|
|
|
module.exports = TrainplanService;
|