|
@@ -0,0 +1,113 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+const assert = require('assert');
|
|
|
+const _ = require('lodash');
|
|
|
+const moment = require('moment');
|
|
|
+const { ObjectId } = require('mongoose').Types;
|
|
|
+const { CrudService } = require('naf-framework-mongoose/lib/service');
|
|
|
+const { BusinessError, ErrorCode } = require('naf-core').Error;
|
|
|
+
|
|
|
+class ScheduleService extends CrudService {
|
|
|
+ constructor(ctx) {
|
|
|
+ super(ctx, 'schedule');
|
|
|
+ this.model = this.ctx.model.Schedule;
|
|
|
+ this.notice = this.ctx.model.Notice;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询定时任务表,进行检查
|
|
|
+ */
|
|
|
+ async check() {
|
|
|
+ let list = await this.model.find();
|
|
|
+ if (list.length > 0) list = JSON.parse(JSON.stringify(list));
|
|
|
+ const group = _.groupBy(list, 'table');
|
|
|
+ const keys = Object.keys(group);
|
|
|
+ for (const key of keys) {
|
|
|
+ const klist = await this.ctx.model[key].find();
|
|
|
+ this.checkData(klist, group[key]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查数据
|
|
|
+ * @param {Array} data 各个表的数据
|
|
|
+ * @param {Array} columns 对应各个表需要检查的字段集合
|
|
|
+ */
|
|
|
+ async checkData(data, columns) {
|
|
|
+ const msg = [];
|
|
|
+ for (const i of data) {
|
|
|
+ for (const col of columns) {
|
|
|
+ // 检查每个数据中,需要检查的字段是否符合要求
|
|
|
+ const { column, number, unit, user_ids, table } = col;
|
|
|
+ const prop = _.get(i, column);
|
|
|
+ if (!prop) continue;
|
|
|
+ const res = this.getLimitResult(prop, number, unit);
|
|
|
+ if (!res) continue;
|
|
|
+ const obj = { effect_id: i._id, check_date: prop, column };
|
|
|
+ // 检查effect_id和check_date,column是否存在,提示过的就不再重发了
|
|
|
+ const noticeList = await this.notice.find(obj);
|
|
|
+ // 过滤出没有发送过消息的人员(可能有后加的情况)
|
|
|
+ const not_send_user = user_ids.filter(f => !noticeList.find(nf => nf.user_id === f));
|
|
|
+ // 拼message,然后发消息
|
|
|
+ const meta = await this.ctx.service.util.getModel(table);
|
|
|
+ obj.message = this.getMessage(i, col, meta);
|
|
|
+ this.addNotice(obj, not_send_user);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据3个参数+检查的时刻判断是否进入警告范围并开始提示
|
|
|
+ * @param {String} date 检查源日期
|
|
|
+ * @param {Number} number 时间-数
|
|
|
+ * @param {String} unit 时间-单位
|
|
|
+ * @return {Boolean} true=>提示;false=>不提示
|
|
|
+ */
|
|
|
+ getLimitResult(date, number, unit) {
|
|
|
+ // 不是日期就不查了
|
|
|
+ if (!moment(date).isValid()) return false;
|
|
|
+ const line = moment(date).subtract(number, unit).format('YYYY-MM-DD');
|
|
|
+ // const now = moment().format('YYYY-MM-DD');
|
|
|
+ // now 当前日期, line警告日期, date最后日期
|
|
|
+ const res = moment().isBetween(line, date, null, '[)');
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取提示信息内容
|
|
|
+ * @param {Object} data 被检查的数据
|
|
|
+ * @param {Object} column 检查的字段信息
|
|
|
+ * @param {Array} meta 该表的字段信息
|
|
|
+ */
|
|
|
+ getMessage(data, column, meta) {
|
|
|
+ const { table, column: colName } = column;
|
|
|
+ const field = meta.find(f => f.model === colName);
|
|
|
+ if (!field) return;
|
|
|
+ const { label } = field;
|
|
|
+ let to;
|
|
|
+ // 车辆表的信息编辑
|
|
|
+ if (table === 'Car') to = data.car_no;
|
|
|
+ else if (table === 'Driver') to = data.name;
|
|
|
+ else if (table === 'Order') to = data.order_no;
|
|
|
+ else if (table === 'Treaty') to = data.number;
|
|
|
+ const res = `${to}---- ${label} 于 ${_.get(
|
|
|
+ data,
|
|
|
+ colName
|
|
|
+ )} 到期,请及时处理以免造成不必要的损失!`;
|
|
|
+
|
|
|
+ // TODO 按监控管理的选择列表的选项去补充完信息
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量产生提示消息
|
|
|
+ * @param {Object} obj 整理的消息数据
|
|
|
+ * @param {Array} user_ids 要发送的用户id
|
|
|
+ */
|
|
|
+ async addNotice(obj, user_ids) {
|
|
|
+ const arr = user_ids.map(i => ({ ...obj, user_id: i }));
|
|
|
+ this.notice.insertMany(arr);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+module.exports = ScheduleService;
|