leave.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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, batchid, termid, classid, planid } = 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 (status) {
  59. leave.status = status;
  60. const student = await this.smodel.findById(leave.studentid);
  61. const remark = '感谢您的使用';
  62. const stuuser = await this.umodel.findOne({ uid: leave.studentid, type: '4' });
  63. if (status === '1') {
  64. // 通知中心管理员学生请假通过
  65. let detail = student.name + '的请假申请已通过,请及时查收!';
  66. const users = await this.umodel.find({ type: '0' });
  67. for (const user of users) {
  68. const openid = user.openid;
  69. const date = await this.ctx.service.util.updatedate();
  70. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  71. }
  72. const _class = await this.cmodel.findById(student.classid);
  73. // 通知班主任学生请假通过
  74. const headteacherid = _class.headteacherid;
  75. const headuser = await this.umodel.findOne({ uid: headteacherid, type: '1' });
  76. if (headuser) {
  77. const openid = headuser.openid;
  78. const date = await this.ctx.service.util.updatedate();
  79. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  80. }
  81. const leadstu = await this.smodel.findOne({ classid: student.classid, job: '班长' });
  82. console.log(leadstu);
  83. const leaduser = await await this.umodel.findOne({ uid: leadstu.id, type: '4' });
  84. if (leaduser) {
  85. const openid = leaduser.openid;
  86. const date = await this.ctx.service.util.updatedate();
  87. this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  88. }
  89. const stuopenid = stuuser.openid;
  90. detail = '您的请假申请已通过,请及时查收!';
  91. const date = await this.ctx.service.util.updatedate();
  92. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, stuopenid, '您有一个新的通知', detail, date, remark);
  93. }
  94. if (status === '2') {
  95. const detail = '您的请假申请已被拒绝,拒绝原因为:' + refcause;
  96. const openid = stuuser.openid;
  97. const date = await this.ctx.service.util.updatedate();
  98. await this.ctx.service.weixin.sendTemplateMsg(this.ctx.app.config.REVIEW_TEMPLATE_ID, openid, '您有一个新的通知', detail, date, remark);
  99. }
  100. }
  101. if (refcause) {
  102. leave.refcause = refcause;
  103. }
  104. return await leave.save();
  105. }
  106. }
  107. module.exports = LeaveService;