shenpi_again.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. 'use strict';
  2. /**
  3. * 审批信息重新发送定时任务
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/26
  7. * @version
  8. */
  9. const Subscription = require('egg').Subscription;
  10. const projectSettingConst = require('../const/project_setting');
  11. const smsTypeConst = require('../const/sms_type');
  12. class ShenpiAgain extends Subscription {
  13. static get schedule() {
  14. return {
  15. interval: '1m',
  16. type: 'worker',
  17. env: ['prod'],
  18. disable: process.env.NODE_ENV !== 'production',
  19. };
  20. }
  21. async subscribe() {
  22. const ctx = this.ctx;
  23. const list = await ctx.service.noticeAgain.getAllDataByCondition({ where: { status: 1 } });
  24. if (list.length === 0) return;
  25. const pidList = ctx.helper._.uniq(ctx.helper._.map(list, 'pid'));
  26. const projectsNoticeSetting = await ctx.service.project.getAllDataByCondition({
  27. columns: ['id', 'notice_setting'],
  28. where: { id: pidList },
  29. });
  30. const uidList = ctx.helper._.uniq(ctx.helper._.map(list, 'uid'));
  31. const usersNoticeSetting = await ctx.service.projectAccount.getAllDataByCondition({
  32. columns: ['id', 'project_id', 'notice_again'],
  33. where: { id: uidList },
  34. });
  35. const tidList = ctx.helper._.uniq(ctx.helper._.map(list, 'tid'));
  36. const tenderList = await ctx.service.tender.getAllDataByCondition({ columns: ['id', 'name'], where: { id: tidList } });
  37. const tenderInfoList = await ctx.service.tenderInfo.getAllDataByCondition({ columns: ['id', 'tid', 'deal_info'], where: { tid: tidList } });
  38. const updateData = [];
  39. const deleteData = [];
  40. const sendData = [];
  41. for (const i of list) {
  42. if (i.times > 10) continue;// 发超过10次就不发了吧
  43. const uinfo = ctx.helper._.find(usersNoticeSetting, { id: i.uid });
  44. const notice_again = uinfo.notice_again ? JSON.parse(uinfo.notice_again) : null;
  45. if (!notice_again) continue;
  46. const pinfo = ctx.helper._.find(projectsNoticeSetting, { id: i.pid });
  47. const notice_setting = pinfo.notice_setting ? JSON.parse(pinfo.notice_setting) : ctx.helper._.cloneDeep(projectSettingConst.noticeSetting);
  48. const interval = notice_setting.mode === 'fixed' ? notice_setting.fixed : (i.times === 0 ? notice_setting.activity.first : (i.times === 1 ? notice_setting.activity.second : notice_setting.activity.later));
  49. const send_time = ctx.helper.calculateNextSendTime(i.last_time, interval, notice_setting.shield_times.start, notice_setting.shield_times.end);
  50. // console.log(i.id, new Date(), send_time);
  51. if (new Date() > send_time) {
  52. // 判断是否已经sp_type的sp_id已经完成审批或者是否存在,如果已经审批或不存在则需要删除
  53. const spInfo = await ctx.service.noticeAgain.getSpResult(i.table_name, i.sp_id);
  54. if (!spInfo) {
  55. deleteData.push(i.id);
  56. continue;
  57. } else {
  58. const status = spInfo.status || spInfo.audit_status;
  59. if (status !== 2) {
  60. updateData.push({
  61. id: i.id,
  62. status: 2,
  63. });
  64. continue;
  65. }
  66. }
  67. // 重发信息并记录到更新表里数据
  68. const t = ctx.helper._.find(tenderList, { id: i.tid });
  69. const tenderInfo = ctx.helper._.find(tenderInfoList, { tid: i.tid });
  70. const newTenderInfo = {
  71. deal_info: { buildName: '' },
  72. };
  73. if (tenderInfo) {
  74. newTenderInfo.deal_info = JSON.parse(tenderInfo.deal_info);
  75. }
  76. i.origin_url = JSON.parse(i.origin_url);
  77. const tender = {
  78. data: t,
  79. info: newTenderInfo,
  80. };
  81. // 可能会重复发送,未知原因
  82. // await ctx.helper.sendWechat(i.uid, i.sms_type, smsTypeConst.judge.approval.toString(), i.template, JSON.parse(i.wx_data), tender, { protocol: i.origin_url.protocol, host: i.origin_url.host });
  83. updateData.push({
  84. id: i.id,
  85. times: i.times + 1,
  86. last_time: ctx.helper.dateTran(new Date(), 'YYYY-MM-DD HH:mm'),
  87. });
  88. sendData.push({
  89. uid: i.uid,
  90. sms_type: i.sms_type,
  91. sp_type: smsTypeConst.judge.approval.toString(),
  92. template: i.template,
  93. wx_data: JSON.parse(i.wx_data),
  94. tender,
  95. origin_url: i.origin_url,
  96. });
  97. }
  98. }
  99. if (updateData.length > 0) await ctx.service.noticeAgain.defaultUpdateRows(updateData);
  100. if (deleteData.length > 0) await ctx.service.noticeAgain.deleteById(deleteData);
  101. if (sendData.length > 0) {
  102. for (const s of sendData) {
  103. await ctx.helper.sendWechat(s.uid, s.sms_type, s.sp_type, s.template, s.wx_data, s.tender, { protocol: s.origin_url.protocol, host: s.origin_url.host });
  104. }
  105. }
  106. }
  107. }
  108. module.exports = ShenpiAgain;