leave.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. const openid = user.openid;
  23. const date = await this.ctx.service.util.updatedate();
  24. const detail = student.name + '发起了请假请求,请及时处理';
  25. const remark = '感谢您的使用';
  26. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  27. return await this.fetch({ id: entity.id });
  28. }
  29. async update({ id }, data) {
  30. const leave = await this.model.findById(id);
  31. const { studentid, starttime, endtime, reason, status, refcause } = data;
  32. if (studentid) {
  33. leave.studentid = studentid;
  34. }
  35. if (starttime) {
  36. leave.starttime = starttime;
  37. }
  38. if (endtime) {
  39. leave.endtime = endtime;
  40. }
  41. if (reason) {
  42. leave.reason = reason;
  43. }
  44. if (status) {
  45. leave.status = status;
  46. const student = await this.smodel.findById(studentid);
  47. const remark = '感谢您的使用';
  48. const stuuser = await this.umodel.findOne({ uid: studentid, type: '4' });
  49. if (status === '1') {
  50. let detail = student.name + '的请假申请已通过,请及时查收!';
  51. const users = await this.umodel.find({ type: '0' });
  52. for (const user of users) {
  53. const openid = user.openid;
  54. const date = await this.ctx.service.util.updatedate();
  55. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  56. }
  57. const _class = await this.cmodel.findById(student.classid);
  58. const headteacherid = _class.headteacherid;
  59. const headuser = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  60. const openid = headuser.openid;
  61. const date = await this.ctx.service.util.updatedate();
  62. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  63. const stuopenid = stuuser.openid;
  64. detail = '您的请假申请已通过,请及时查收!';
  65. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, stuopenid, '您有一个新的通知', detail, date, remark);
  66. }
  67. if (status === '2') {
  68. const detail = '您的请假申请已被拒绝,拒绝原因为:' + refcause;
  69. const openid = stuuser.openid;
  70. const date = await this.ctx.service.util.updatedate();
  71. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  72. }
  73. }
  74. if (refcause) {
  75. leave.refcause = refcause;
  76. }
  77. return await leave.save();
  78. }
  79. }
  80. module.exports = LeaveService;