leave.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 schoolid = student.schoolid;
  21. const user = await this.umodel.findOne({ uid: schoolid, 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(studentid);
  49. const remark = '感谢您的使用';
  50. const stuuser = await this.umodel.findOne({ uid: studentid, type: '4' });
  51. if (status === '1') {
  52. let detail = student.name + '的请假申请已通过,请及时查收!';
  53. const users = await this.umodel.find({ type: '0' });
  54. for (const user of users) {
  55. const openid = user.openid;
  56. const date = await this.ctx.service.util.updatedate();
  57. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  58. }
  59. const _class = await this.cmodel.findById(student.classid);
  60. const headteacherid = _class.headteacherid;
  61. const headuser = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  62. if (headuser) {
  63. const openid = headuser.openid;
  64. const date = await this.ctx.service.util.updatedate();
  65. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  66. const stuopenid = stuuser.openid;
  67. detail = '您的请假申请已通过,请及时查收!';
  68. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, stuopenid, '您有一个新的通知', detail, date, remark);
  69. }
  70. }
  71. if (status === '2') {
  72. const detail = '您的请假申请已被拒绝,拒绝原因为:' + refcause;
  73. const openid = stuuser.openid;
  74. const date = await this.ctx.service.util.updatedate();
  75. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  76. }
  77. }
  78. if (refcause) {
  79. leave.refcause = refcause;
  80. }
  81. return await leave.save();
  82. }
  83. }
  84. module.exports = LeaveService;