| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 | 'use strict';/** * 审批信息重新发送定时任务 * * @author CaiAoLin * @date 2017/10/26 * @version */const Subscription = require('egg').Subscription;const projectSettingConst = require('../const/project_setting');const smsTypeConst = require('../const/sms_type');class ShenpiAgain extends Subscription {    static get schedule() {        return {            interval: '1m',            type: 'worker',            env: ['prod'],            disable: process.env.NODE_ENV !== 'production',        };    }    async subscribe() {        const ctx = this.ctx;        const list = await ctx.service.noticeAgain.getAllDataByCondition({ where: { status: 1 } });        if (list.length === 0) return;        const pidList = ctx.helper._.uniq(ctx.helper._.map(list, 'pid'));        const projectsNoticeSetting = await ctx.service.project.getAllDataByCondition({            columns: ['id', 'notice_setting'],            where: { id: pidList },        });        const uidList = ctx.helper._.uniq(ctx.helper._.map(list, 'uid'));        const usersNoticeSetting = await ctx.service.projectAccount.getAllDataByCondition({            columns: ['id', 'project_id', 'notice_again'],            where: { id: uidList },        });        const tidList = ctx.helper._.uniq(ctx.helper._.map(list, 'tid'));        const tenderList = await ctx.service.tender.getAllDataByCondition({ columns: ['id', 'name'], where: { id: tidList } });        const tenderInfoList = await ctx.service.tenderInfo.getAllDataByCondition({ columns: ['id', 'tid', 'deal_info'], where: { tid: tidList } });        const updateData = [];        const deleteData = [];        const sendData = [];        for (const i of list) {            if (i.times > 10) continue;// 发超过10次就不发了吧            const uinfo = ctx.helper._.find(usersNoticeSetting, { id: i.uid });            const notice_again = uinfo.notice_again ? JSON.parse(uinfo.notice_again) : null;            if (!notice_again) continue;            const pinfo = ctx.helper._.find(projectsNoticeSetting, { id: i.pid });            const notice_setting = pinfo.notice_setting ? JSON.parse(pinfo.notice_setting) : ctx.helper._.cloneDeep(projectSettingConst.noticeSetting);            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));            const send_time = ctx.helper.calculateNextSendTime(i.last_time, interval, notice_setting.shield_times.start, notice_setting.shield_times.end);            // console.log(i.id, new Date(), send_time);            if (new Date() > send_time) {                // 判断是否已经sp_type的sp_id已经完成审批或者是否存在,如果已经审批或不存在则需要删除                const spInfo = await ctx.service.noticeAgain.getSpResult(i.table_name, i.sp_id);                if (!spInfo) {                    deleteData.push(i.id);                    continue;                } else {                    const status = spInfo.status || spInfo.audit_status;                    if (status !== 2) {                        updateData.push({                            id: i.id,                            status: 2,                        });                        continue;                    }                }                // 重发信息并记录到更新表里数据                const t = ctx.helper._.find(tenderList, { id: i.tid });                const tenderInfo = ctx.helper._.find(tenderInfoList, { tid: i.tid });                const newTenderInfo = {                    deal_info: { buildName: '' },                };                if (tenderInfo) {                    newTenderInfo.deal_info = JSON.parse(tenderInfo.deal_info);                }                i.origin_url = JSON.parse(i.origin_url);                const tender = {                    data: t,                    info: newTenderInfo,                };                // 可能会重复发送,未知原因                // 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 });                updateData.push({                    id: i.id,                    times: i.times + 1,                    last_time: ctx.helper.dateTran(new Date(), 'YYYY-MM-DD HH:mm'),                });                sendData.push({                    uid: i.uid,                    sms_type: i.sms_type,                    sp_type: smsTypeConst.judge.approval.toString(),                    template: i.template,                    wx_data: JSON.parse(i.wx_data),                    tender,                    origin_url: i.origin_url,                });            }        }        if (updateData.length > 0) await ctx.service.noticeAgain.defaultUpdateRows(updateData);        if (deleteData.length > 0) await ctx.service.noticeAgain.deleteById(deleteData);        if (sendData.length > 0) {            for (const s of sendData) {                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 });            }        }    }}module.exports = ShenpiAgain;
 |