schedule.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const moment = require('moment');
  5. const { ObjectId } = require('mongoose').Types;
  6. const { CrudService } = require('naf-framework-mongoose/lib/service');
  7. const { BusinessError, ErrorCode } = require('naf-core').Error;
  8. class ScheduleService extends CrudService {
  9. constructor(ctx) {
  10. super(ctx, 'schedule');
  11. this.model = this.ctx.model.Schedule;
  12. this.notice = this.ctx.model.Notice;
  13. }
  14. /**
  15. * 查询定时任务表,进行检查
  16. */
  17. async check() {
  18. let list = await this.model.find();
  19. if (list.length > 0) list = JSON.parse(JSON.stringify(list));
  20. const group = _.groupBy(list, 'table');
  21. const keys = Object.keys(group);
  22. for (const key of keys) {
  23. const klist = await this.ctx.model[key].find();
  24. this.checkData(klist, group[key]);
  25. }
  26. }
  27. /**
  28. * 检查数据
  29. * @param {Array} data 各个表的数据
  30. * @param {Array} columns 对应各个表需要检查的字段集合
  31. */
  32. async checkData(data, columns) {
  33. const msg = [];
  34. for (const i of data) {
  35. for (const col of columns) {
  36. // 检查每个数据中,需要检查的字段是否符合要求
  37. const { column, number, unit, user_ids, table } = col;
  38. const prop = _.get(i, column);
  39. if (!prop) continue;
  40. const res = this.getLimitResult(prop, number, unit);
  41. if (!res) continue;
  42. const obj = { effect_id: i._id, check_date: prop, column };
  43. // 检查effect_id和check_date,column是否存在,提示过的就不再重发了
  44. const noticeList = await this.notice.find(obj);
  45. // 过滤出没有发送过消息的人员(可能有后加的情况)
  46. const not_send_user = user_ids.filter(f => !noticeList.find(nf => nf.user_id === f));
  47. // 拼message,然后发消息
  48. const meta = await this.ctx.service.util.getModel(table);
  49. obj.message = this.getMessage(i, col, meta);
  50. this.addNotice(obj, not_send_user);
  51. }
  52. }
  53. }
  54. /**
  55. * 根据3个参数+检查的时刻判断是否进入警告范围并开始提示
  56. * @param {String} date 检查源日期
  57. * @param {Number} number 时间-数
  58. * @param {String} unit 时间-单位
  59. * @return {Boolean} true=>提示;false=>不提示
  60. */
  61. getLimitResult(date, number, unit) {
  62. // 不是日期就不查了
  63. if (!moment(date).isValid()) return false;
  64. const line = moment(date).subtract(number, unit).format('YYYY-MM-DD');
  65. // const now = moment().format('YYYY-MM-DD');
  66. // now 当前日期, line警告日期, date最后日期
  67. const res = moment().isBetween(line, date, null, '[)');
  68. return res;
  69. }
  70. /**
  71. * 获取提示信息内容
  72. * @param {Object} data 被检查的数据
  73. * @param {Object} column 检查的字段信息
  74. * @param {Array} meta 该表的字段信息
  75. */
  76. getMessage(data, column, meta) {
  77. const { table, column: colName } = column;
  78. const field = meta.find(f => f.model === colName);
  79. if (!field) return;
  80. const { label } = field;
  81. let to;
  82. // 车辆表的信息编辑
  83. if (table === 'Car') to = data.car_no;
  84. else if (table === 'Driver') to = data.name;
  85. else if (table === 'Order') to = data.order_no;
  86. else if (table === 'Treaty') to = data.number;
  87. const res = `${to}---- ${label} 于 ${_.get(
  88. data,
  89. colName
  90. )} 到期,请及时处理以免造成不必要的损失!`;
  91. // TODO 按监控管理的选择列表的选项去补充完信息
  92. return res;
  93. }
  94. /**
  95. * 批量产生提示消息
  96. * @param {Object} obj 整理的消息数据
  97. * @param {Array} user_ids 要发送的用户id
  98. */
  99. async addNotice(obj, user_ids) {
  100. const arr = user_ids.map(i => ({ ...obj, user_id: i }));
  101. this.notice.insertMany(arr);
  102. }
  103. }
  104. module.exports = ScheduleService;