leave.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. const entity = await this.model.create(newdata);
  21. const user = await this.umodel.findOne({ uid: schid, type: '2' });
  22. if (user) {
  23. const openid = user.openid;
  24. const date = await this.ctx.service.util.updatedate();
  25. const detail = student.name + '发起了请假请求,请及时处理';
  26. const remark = '感谢您的使用';
  27. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  28. }
  29. return await this.fetch({ id: entity.id });
  30. }
  31. async update({ id }, data) {
  32. const leave = await this.model.findById(id);
  33. const { studentid, starttime, endtime, reason, status, refcause, batchid, termid, classid, planid, stuname, type } = data;
  34. if (studentid) {
  35. leave.studentid = studentid;
  36. }
  37. if (starttime) {
  38. leave.starttime = starttime;
  39. }
  40. if (endtime) {
  41. leave.endtime = endtime;
  42. }
  43. if (reason) {
  44. leave.reason = reason;
  45. }
  46. if (batchid) {
  47. leave.batchid = batchid;
  48. }
  49. if (termid) {
  50. leave.termid = termid;
  51. }
  52. if (classid) {
  53. leave.classid = classid;
  54. }
  55. if (planid) {
  56. leave.planid = planid;
  57. }
  58. if (stuname) {
  59. leave.stuname = stuname;
  60. }
  61. if (status) {
  62. leave.status = status;
  63. const student = await this.smodel.findById(leave.studentid);
  64. const remark = '感谢您的使用';
  65. const stuuser = await this.umodel.findOne({ uid: leave.studentid, type: '4' });
  66. if (status === '1') {
  67. // 通知中心管理员学生请假通过
  68. let detail = student.name + '的请假申请已通过,请及时查收!';
  69. const users = await this.umodel.find({ type: '0' });
  70. let toOtherMsg = `${student.name}的${type === '1' ? '退出' : '请假'}申请已通过,请及时查收!`;
  71. if (starttime) toOtherMsg = `${toOtherMsg}\n 时间:${starttime} 至 ${endtime}`;
  72. if (reason) toOtherMsg = `${toOtherMsg}\n ${type === '1' ? '退出' : '请假'}原因:${reason}`;
  73. if (refcause) toOtherMsg = `${toOtherMsg}\n 学校反馈:${refcause}`;
  74. for (const user of users) {
  75. const openid = user.openid;
  76. const date = await this.ctx.service.util.updatedate();
  77. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', toOtherMsg, date, remark);
  78. }
  79. const _class = await this.cmodel.findById(student.classid);
  80. // 通知班主任学生请假通过
  81. const headteacherid = _class.headteacherid;
  82. const headuser = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  83. if (headuser) {
  84. const openid = headuser.openid;
  85. const date = await this.ctx.service.util.updatedate();
  86. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', toOtherMsg, date, remark);
  87. }
  88. const leadstu = await this.smodel.findOne({ classid: student.classid, job: '班长' });
  89. if (leadstu) {
  90. const leaduser = await await this.umodel.findOne({ uid: leadstu.id, type: '4' });
  91. if (leaduser) {
  92. const openid = leaduser.openid;
  93. const date = await this.ctx.service.util.updatedate();
  94. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', toOtherMsg, date, remark);
  95. }
  96. }
  97. const stuopenid = stuuser.openid;
  98. detail = '您的请假申请已通过,请及时查收!';
  99. const date = await this.ctx.service.util.updatedate();
  100. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, stuopenid, '您有一个新的通知', detail, date, remark);
  101. }
  102. if (status === '2') {
  103. const detail = '您的请假申请已被拒绝,拒绝原因为:' + refcause;
  104. const openid = stuuser.openid;
  105. const date = await this.ctx.service.util.updatedate();
  106. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  107. }
  108. }
  109. if (refcause) {
  110. leave.refcause = refcause;
  111. }
  112. return await leave.save();
  113. }
  114. }
  115. module.exports = LeaveService;