leave.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const { ObjectId } = require('mongoose').Types;
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. class LeaveService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx, 'leave');
  10. this.model = this.ctx.model.Leave;
  11. this.smodel = this.ctx.model.Student;
  12. this.umodel = this.ctx.model.User;
  13. this.cmodel = this.ctx.model.Class;
  14. }
  15. async create(data) {
  16. const studentid = data.studentid;
  17. const student = await this.smodel.findById(studentid);
  18. const schid = student.schid;
  19. const newdata = { ...data, status: '0', schid };
  20. let sch = await this.ctx.model.School.findOne({ code: schid });
  21. if (!sch) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找到该学生的学校信息');
  22. sch = JSON.parse(JSON.stringify(sch));
  23. const { _id } = sch;
  24. const user = await this.umodel.findOne({ uid: _id, type: '2' });
  25. const entity = await this.model.create(newdata);
  26. console.log(user);
  27. if (user) {
  28. const openid = 'ocPqjswkUejZHq2ANriNrFFC7A3I'; // user.openid
  29. const date = await this.ctx.service.util.updatedate();
  30. const detail = student.name + '发起了请假请求(点击消息审核请假)';
  31. const remark = '感谢您的使用';
  32. const to_uri = `${this.app.config.baseUrl}/msgconfirm/leave/status?id=${entity.id}`;
  33. if (openid) { await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个学生请假通知', detail, date, remark, to_uri); }
  34. }
  35. return await this.fetch({ id: entity.id });
  36. }
  37. async update({ id }, data) {
  38. const leave = await this.model.findById(id);
  39. const { studentid, starttime, endtime, reason, status, refcause, batchid, termid, classid, planid, stuname } = data;
  40. if (studentid) {
  41. leave.studentid = studentid;
  42. }
  43. if (starttime) {
  44. leave.starttime = starttime;
  45. }
  46. if (endtime) {
  47. leave.endtime = endtime;
  48. }
  49. if (reason) {
  50. leave.reason = reason;
  51. }
  52. if (batchid) {
  53. leave.batchid = batchid;
  54. }
  55. if (termid) {
  56. leave.termid = termid;
  57. }
  58. if (classid) {
  59. leave.classid = classid;
  60. }
  61. if (planid) {
  62. leave.planid = planid;
  63. }
  64. if (stuname) {
  65. leave.stuname = stuname;
  66. }
  67. if (status) {
  68. leave.status = status;
  69. const student = await this.smodel.findById(leave.studentid);
  70. const remark = '感谢您的使用';
  71. const stuuser = await this.umodel.findOne({ uid: leave.studentid, type: '4' });
  72. if (status === '1') {
  73. // 通知中心管理员学生请假通过
  74. let detail = student.name + '的请假申请已通过,请及时查收!';
  75. const users = await this.umodel.find({ type: '0' });
  76. let toOtherMsg = `${student.name}的${leave.type === '1' ? '退出' : '请假'}申请已通过,请及时查收!`;
  77. if (leave.starttime) toOtherMsg = `${toOtherMsg}\n 时间:${leave.starttime} 至 ${leave.endtime || ''}`;
  78. if (leave.reason) toOtherMsg = `${toOtherMsg}\n ${leave.type === '1' ? '退出' : '请假'}原因:${leave.reason}`;
  79. if (leave.refcause) toOtherMsg = `${toOtherMsg}\n 学校反馈:${leave.refcause}`;
  80. for (const user of users) {
  81. const openid = user.openid;
  82. const date = await this.ctx.service.util.updatedate();
  83. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, 'ocPqjswkUejZHq2ANriNrFFC7A3I', '您有一个新的通知', toOtherMsg, date, remark);
  84. }
  85. const _class = await this.cmodel.findById(student.classid);
  86. // 通知班主任学生请假通过
  87. const headteacherid = _class.headteacherid;
  88. const headuser = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  89. if (headuser) {
  90. const openid = headuser.openid;
  91. const date = await this.ctx.service.util.updatedate();
  92. // TODO,如果是退出,需要班主任确认,加上最后一个参数
  93. if (leave.type === '1') {
  94. const to_uri = `${this.app.config.baseUrl}/msgconfirm/leave/confirm?id=${leave.id}`;
  95. const remark = '点击消息确认学生是否已经退出';
  96. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, 'ocPqjswkUejZHq2ANriNrFFC7A3I', '您有一个新的通知', toOtherMsg, date, remark, to_uri);
  97. } else {
  98. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, 'ocPqjswkUejZHq2ANriNrFFC7A3I', '您有一个新的通知', toOtherMsg, date, remark);
  99. }
  100. }
  101. const leadstu = await this.smodel.findOne({ classid: student.classid, job: '班长' });
  102. if (leadstu) {
  103. const leaduser = await await this.umodel.findOne({ uid: leadstu.id, type: '4' });
  104. if (leaduser) {
  105. const openid = leaduser.openid;
  106. const date = await this.ctx.service.util.updatedate();
  107. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, 'ocPqjswkUejZHq2ANriNrFFC7A3I', '您有一个新的通知', toOtherMsg, date, remark);
  108. }
  109. }
  110. const stuopenid = stuuser.openid;
  111. detail = '您的请假申请已通过,请及时查收!';
  112. const date = await this.ctx.service.util.updatedate();
  113. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, stuopenid, '您有一个新的通知', detail, date, remark);
  114. }
  115. if (status === '2') {
  116. const detail = '您的请假申请已被拒绝,拒绝原因为:' + refcause;
  117. const openid = stuuser.openid;
  118. const date = await this.ctx.service.util.updatedate();
  119. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  120. }
  121. }
  122. if (refcause) {
  123. leave.refcause = refcause;
  124. }
  125. return await leave.save();
  126. }
  127. }
  128. module.exports = LeaveService;