'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 LeaveService extends CrudService { constructor(ctx) { super(ctx, 'leave'); this.model = this.ctx.model.Leave; this.smodel = this.ctx.model.Student; this.umodel = this.ctx.model.User; this.cmodel = this.ctx.model.Class; } async create(data) { const { type } = data; const studentid = data.studentid; const student = await this.smodel.findById(studentid); const schid = student.schid; const newdata = { ...data, status: '0', schid }; let sch = await this.ctx.model.School.findOne({ code: schid }); if (!sch) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该学生的学校信息'); sch = JSON.parse(JSON.stringify(sch)); const { _id } = sch; const user = await this.umodel.findOne({ uid: _id, type: '2' }); let entity; if (type === '1') { // 退出只能有一个 entity = await this.model.findOne({ studentid, type: '1' }); if (entity) { const { starttime, endtime, reason } = data; if (starttime) entity.starttime = starttime; if (endtime) entity.endtime = endtime; if (reason) entity.reason = reason; await entity.save(); } else { entity = await this.model.create(newdata); } } else { entity = await this.model.create(newdata); } if (user) { const openid = user.openid; const date = await this.ctx.service.util.updatedate(); const detail = student.name + '发起了请假请求(点击消息审核请假)'; const remark = '感谢您的使用'; const to_uri = `${this.app.config.baseUrl}/msgconfirm/leave/status?id=${entity.id}`; if (openid) { await this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个学生请假通知', detail, date, remark, to_uri); } } return await this.fetch({ id: entity.id }); } async update({ id }, data) { const leave = await this.model.findById(id); const { studentid, starttime, endtime, reason, status, refcause, batchid, termid, classid, planid, stuname } = data; if (studentid) { leave.studentid = studentid; } if (starttime) { leave.starttime = starttime; } if (endtime) { leave.endtime = endtime; } if (reason) { leave.reason = reason; } if (batchid) { leave.batchid = batchid; } if (termid) { leave.termid = termid; } if (classid) { leave.classid = classid; } if (planid) { leave.planid = planid; } if (stuname) { leave.stuname = stuname; } if (status) { leave.status = status; const student = await this.smodel.findById(leave.studentid); const remark = '感谢您的使用'; const stuuser = await this.umodel.findOne({ uid: leave.studentid, type: '4' }); if (status === '1') { // 通知中心管理员学生请假通过 let detail = student.name + '的请假申请已通过,请及时查收!'; const users = await this.umodel.find({ type: '0' }); let toOtherMsg = `${student.name}的${leave.type === '1' ? '退出' : '请假'}申请已通过,请及时查收!`; if (leave.starttime) toOtherMsg = `${toOtherMsg}\n 时间:${leave.starttime} 至 ${leave.endtime || ''}`; if (leave.reason) toOtherMsg = `${toOtherMsg}\n ${leave.type === '1' ? '退出' : '请假'}原因:${leave.reason}`; if (leave.refcause) toOtherMsg = `${toOtherMsg}\n 学校反馈:${leave.refcause}`; for (const user of users) { const openid = user.openid; const date = await this.ctx.service.util.updatedate(); this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', toOtherMsg, date, remark); } const _class = await this.cmodel.findById(student.classid); // 通知班主任学生请假通过 const headteacherid = _class.headteacherid; const headuser = await this.umodel.findOne({ uid: headteacherid, type: '1' }); if (headuser) { const openid = headuser.openid; const date = await this.ctx.service.util.updatedate(); // TODO,如果是退出,需要班主任确认,加上最后一个参数 if (leave.type === '1') { const to_uri = `${this.app.config.baseUrl}/msgconfirm/leave/confirm?id=${leave.id}`; const remark = '点击消息确认学生是否已经退出'; this.ctx.service.weixin.sendTemplateDesign(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', toOtherMsg, date, remark, to_uri); } else { this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', toOtherMsg, date, remark); } } const leadstu = await this.smodel.findOne({ classid: student.classid, job: '班长' }); if (leadstu) { const leaduser = await await this.umodel.findOne({ uid: leadstu.id, type: '4' }); if (leaduser) { const openid = leaduser.openid; const date = await this.ctx.service.util.updatedate(); this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', toOtherMsg, date, remark); } } const stuopenid = stuuser.openid; detail = '您的请假申请已通过,请及时查收!'; const date = await this.ctx.service.util.updatedate(); await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, stuopenid, '您有一个新的通知', detail, date, remark); } if (status === '2') { const detail = '您的请假申请已被拒绝,拒绝原因为:' + refcause; const openid = stuuser.openid; const date = await this.ctx.service.util.updatedate(); await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark); } } if (refcause) { leave.refcause = refcause; } return await leave.save(); } } module.exports = LeaveService;