leave.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 newdata = { ...data, status: '0' };
  17. const entity = await this.model.create(newdata);
  18. const studentid = data.studentid;
  19. const student = await this.smodel.findById(studentid);
  20. const schid = student.schid;
  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 } = 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 (status) {
  47. leave.status = status;
  48. const student = await this.smodel.findById(leave.studentid);
  49. const remark = '感谢您的使用';
  50. const stuuser = await this.umodel.findOne({ uid: leave.studentid, type: '4' });
  51. if (status === '1') {
  52. // 通知中心管理员学生请假通过
  53. let detail = student.name + '的请假申请已通过,请及时查收!';
  54. const users = await this.umodel.find({ type: '0' });
  55. for (const user of users) {
  56. const openid = user.openid;
  57. const date = await this.ctx.service.util.updatedate();
  58. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  59. }
  60. const _class = await this.cmodel.findById(student.classid);
  61. // 通知班主任学生请假通过
  62. const headteacherid = _class.headteacherid;
  63. const headuser = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  64. if (headuser) {
  65. const openid = headuser.openid;
  66. const date = await this.ctx.service.util.updatedate();
  67. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  68. }
  69. const leadstu = await this.smodel.findOne({ classid: student.classid, job: '班长' });
  70. console.log(leadstu);
  71. const leaduser = await await this.umodel.findOne({ uid: leadstu.id, type: '4' });
  72. if (leaduser) {
  73. const openid = leaduser.openid;
  74. const date = await this.ctx.service.util.updatedate();
  75. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  76. }
  77. const stuopenid = stuuser.openid;
  78. detail = '您的请假申请已通过,请及时查收!';
  79. const date = await this.ctx.service.util.updatedate();
  80. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, stuopenid, '您有一个新的通知', detail, date, remark);
  81. }
  82. if (status === '2') {
  83. const detail = '您的请假申请已被拒绝,拒绝原因为:' + refcause;
  84. const openid = stuuser.openid;
  85. const date = await this.ctx.service.util.updatedate();
  86. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  87. }
  88. }
  89. if (refcause) {
  90. leave.refcause = refcause;
  91. }
  92. return await leave.save();
  93. }
  94. }
  95. module.exports = LeaveService;