shenpi_again.js 5.2 KB

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