leave.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 } = 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. for (const user of users) {
  71. const openid = user.openid;
  72. const date = await this.ctx.service.util.updatedate();
  73. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  74. }
  75. const _class = await this.cmodel.findById(student.classid);
  76. // 通知班主任学生请假通过
  77. const headteacherid = _class.headteacherid;
  78. const headuser = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  79. if (headuser) {
  80. const openid = headuser.openid;
  81. const date = await this.ctx.service.util.updatedate();
  82. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  83. }
  84. const leadstu = await this.smodel.findOne({ classid: student.classid, job: '班长' });
  85. if (leadstu) {
  86. const leaduser = await await this.umodel.findOne({ uid: leadstu.id, type: '4' });
  87. if (leaduser) {
  88. const openid = leaduser.openid;
  89. const date = await this.ctx.service.util.updatedate();
  90. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  91. }
  92. }
  93. const stuopenid = stuuser.openid;
  94. detail = '您的请假申请已通过,请及时查收!';
  95. const date = await this.ctx.service.util.updatedate();
  96. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, stuopenid, '您有一个新的通知', detail, date, remark);
  97. }
  98. if (status === '2') {
  99. const detail = '您的请假申请已被拒绝,拒绝原因为:' + refcause;
  100. const openid = stuuser.openid;
  101. const date = await this.ctx.service.util.updatedate();
  102. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  103. }
  104. }
  105. if (refcause) {
  106. leave.refcause = refcause;
  107. }
  108. return await leave.save();
  109. }
  110. }
  111. module.exports = LeaveService;