leave.js 6.3 KB

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