shenpi_again.js 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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: 'all',
  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. for (const i of list) {
  39. if (i.times > 10) continue;// 发超过10次就不发了吧
  40. const uinfo = ctx.helper._.find(usersNoticeSetting, { id: i.uid });
  41. const notice_again = uinfo.notice_again ? JSON.parse(uinfo.notice_again) : null;
  42. if (!notice_again) continue;
  43. const pinfo = ctx.helper._.find(projectsNoticeSetting, { id: i.pid });
  44. const notice_setting = pinfo.notice_setting ? JSON.parse(pinfo.notice_setting) : ctx.helper._.cloneDeep(projectSettingConst.noticeSetting);
  45. 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));
  46. const send_time = ctx.helper.calculateNextSendTime(i.last_time, interval, notice_setting.shield_times.start, notice_setting.shield_times.end);
  47. console.log(i.id, new Date(), send_time);
  48. if (new Date() > send_time) {
  49. // 判断是否已经sp_type的sp_id已经完成审批或者是否存在,如果已经审批或不存在则需要删除
  50. const spInfo = await ctx.service.noticeAgain.getSpResult(i.table_name, i.sp_id);
  51. if (!spInfo) {
  52. deleteData.push(i.id);
  53. continue;
  54. } else {
  55. const status = spInfo.status || spInfo.audit_status;
  56. if (status !== 2) {
  57. updateData.push({
  58. id: i.id,
  59. status: 2,
  60. });
  61. continue;
  62. }
  63. }
  64. // 重发信息并记录到更新表里数据
  65. const t = ctx.helper._.find(tenderList, { id: i.tid });
  66. const tenderInfo = ctx.helper._.find(tenderInfoList, { tid: i.tid });
  67. const newTenderInfo = {
  68. deal_info: { buildName: '' },
  69. };
  70. if (tenderInfo) {
  71. newTenderInfo.deal_info = JSON.parse(tenderInfo.deal_info);
  72. }
  73. i.origin_url = JSON.parse(i.origin_url);
  74. const tender = {
  75. data: t,
  76. info: newTenderInfo,
  77. };
  78. 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 });
  79. updateData.push({
  80. id: i.id,
  81. times: i.times + 1,
  82. last_time: ctx.helper.dateTran(new Date(), 'YYYY-MM-DD HH:mm'),
  83. });
  84. }
  85. }
  86. if (updateData.length > 0) await ctx.service.noticeAgain.defaultUpdateRows(updateData);
  87. if (deleteData.length > 0) await ctx.service.noticeAgain.deleteById(deleteData);
  88. }
  89. }
  90. module.exports = ShenpiAgain;