DaySubJob.java 10.8 KB
Newer Older
吴泽佳's avatar
吴泽佳 committed
1 2 3 4 5 6 7 8 9 10 11
package com.tanpu.feo.feojob.jobs;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tanpu.common.constant.BizStatus;
import com.tanpu.feo.feojob.config.MorningPaperProperties;
吴泽佳's avatar
吴泽佳 committed
12 13
import com.tanpu.feo.feojob.dao.user.entity.*;
import com.tanpu.feo.feojob.dao.user.mapper.*;
吴泽佳's avatar
吴泽佳 committed
14 15
import com.tanpu.feo.feojob.feign.wxcp.FeignClientForWxCp;
import com.tanpu.feo.feojob.feign.wxmp.FeignClientForWxMp;
16
import com.tanpu.feo.feojob.service.RedirectService;
吴泽佳's avatar
吴泽佳 committed
17 18 19 20 21 22 23 24
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
吴泽佳's avatar
吴泽佳 committed
25
import java.util.Date;
吴泽佳's avatar
吴泽佳 committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zejia zj wu
 * @date 2021-08-10
 * @Description 早报推送
 */
@Component
@Slf4j
public class DaySubJob {

    @Resource
    private DaySubjectMapper daySubjectMapper;
    @Resource
    private UserInfoMapper userInfoMapper;
    @Resource
    private OrgExtMapper orgExtMapper;
    @Resource
    MorningPaperProperties morningPaperProperties;
    @Value("${daySubject.daySubjectUrl}")
    private String daySubjectUrl;
    @Resource
    private FeignClientForWxCp feignClientForWxCp;
    @Resource
    private FeignClientForWxMp feignClientForWxMp;
吴泽佳's avatar
吴泽佳 committed
54 55
    @Resource
    private DaySelectionMapper daySelectionMapper;
吴泽佳's avatar
吴泽佳 committed
56 57
    @Resource
    private OrgMapper orgMapper;
58
    @Resource
59
    private RedirectService redirectService;
吴泽佳's avatar
吴泽佳 committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79


    @Scheduled(cron = "0 30 7 * * ? ")   //
    public void execute() {
        log.info("====== 开始执行 DaySubJob ======");
        try {
            sendDaySubjectMessage();
        } catch (Exception e) {
            log.error("====== 异常结束执行 DaySubJob ,错误:{}=======", e);
        }
        log.info("====== 结束执行 DaySubJob ======");
    }

    public void sendDaySubjectMessage() {
        List<DaySubjectEntity> daySubjectList = getDaySubList(DateUtil.today());
        if (CollectionUtils.isEmpty(daySubjectList)) {
            log.info("====== DaySubJob 暂无需要推送的早报");
            return;
        }
        Map<String, List<DaySubjectEntity>> stringListMap = daySubjectList.stream().collect(Collectors.groupingBy(DaySubjectEntity::getDsPt));
王亚雷's avatar
王亚雷 committed
80
        String mainTitle = getOneMorningTitle();
81 82 83 84 85 86 87
        for (String orgId : stringListMap.keySet()) {
            log.info("开始推送机构(orgId={})的早报", orgId);
            //查询orgCode
            OrgEntity orgEntity = orgMapper.selectOne(new LambdaQueryWrapper<OrgEntity>().eq(OrgEntity::getId, orgId).last("limit 1"));
            if (ObjectUtil.isNull(orgEntity)) {
                log.error("找不到机构(orgId={})的信息", orgId);
                return;
吴泽佳's avatar
吴泽佳 committed
88
            }
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
            List<DaySubjectEntity> daySubjectEntities = stringListMap.get(orgId);
            if (CollectionUtils.isEmpty(daySubjectEntities)) {
                log.warn("当前机构(orgCode={})没有要推送的早报", orgEntity.getOrgCode());
                return;
            }
            sendDaySubject(orgId, daySubjectEntities, mainTitle);
        }
    }

    public void sendDaySubject(String orgId, List<DaySubjectEntity> daySubjectList,String mainTitle){
        if (CollectionUtils.isEmpty(daySubjectList) || StringUtils.isBlank(orgId) || StringUtils.isBlank(mainTitle)) {
            return;
        }
        OrgEntity orgEntity = orgMapper.selectOne(new LambdaQueryWrapper<OrgEntity>().eq(OrgEntity::getId, orgId).last("limit 1"));
        if (orgEntity == null) {
            return;
        }
        // 查询 机构配置信息
        OrgExtEntity orgExtEntity = orgExtMapper.selectOne(new LambdaQueryWrapper<OrgExtEntity>()
                .eq(OrgExtEntity::getOrgId, orgId)
109
                .eq(OrgExtEntity::getDeleteTag, BizStatus.DeleteTag.tag_init_str));
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
        String jsonKey = ObjectUtil.isNotNull(orgExtEntity) ? orgExtEntity.getJsonWxcpKey() : null;
        if (StringUtils.isBlank(jsonKey)) {
            return;
        }

        //在orgExt 表中有查到 jsonKey 执行消息推送任务
        JSONObject jsonKeyInfo = JSONUtil.parseObj(jsonKey);
        // 查询该机构下 用户
        LambdaQueryWrapper<UserInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserInfoEntity::getDeletetag, BizStatus.DeleteTag.tag_init_str)
                .eq(UserInfoEntity::getOrgId, orgId)
                .eq(UserInfoEntity::getLevel, "2");
        List<UserInfoEntity> userInfoEntities = userInfoMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(userInfoEntities)) {
            log.info("机构(orgCode={})没有需要推送早报的人员", orgEntity.getOrgCode());
            return;
        }
        log.info("机构(orgCode={})本次推送{}人,早报文章{}篇", orgEntity.getOrgCode(), userInfoEntities.size(), daySubjectList.size());

吴泽佳's avatar
吴泽佳 committed
129

130 131 132 133 134 135 136 137 138 139 140 141
        for (UserInfoEntity userInfoEntity : userInfoEntities) {
            for (DaySubjectEntity daySubjectEntity : daySubjectList) {
                HashMap<String, String> parmMap = new HashMap<>();
                parmMap.put("type", "text");
                String wxOpenId = userInfoEntity.getUiOpenid();
                String wxcpUId = userInfoEntity.getWxcpUid();
                if (StringUtils.isNotBlank(wxcpUId)) {
                    // 1 企业微信 推送
                    String agentId = jsonKeyInfo.getStr("agentId");
                    String corpId = jsonKeyInfo.getStr("corpId");
                    // ww=corpId&orgCode=orgCode
                    String link = String.format(daySubjectUrl, corpId, orgEntity.getOrgCode(), daySubjectEntity.getId());
142 143
                    String url = redirectService.getRedirectLink(link);
                    log.info("早报推送使用重定向链接, redirect-url: {}, origin-url: {}", url, link);
144 145 146 147 148 149 150 151 152 153 154 155
                    StringBuilder sb = new StringBuilder();
                    sb.append(mainTitle).append("\n\n")
                            .append(morningPaperProperties.getSubTitle()).append("\n\n")
                            .append("<a href=\"")
                            .append(url).append("\">")
                            .append("快去转发获客吧")
                            .append("</a>\n\n");
                    parmMap.put("content", sb.toString());
                    log.info("===企业微信 推送===userID:{} agentId:{} corpId:{} wxcpUId:{} parmMap:{}", userInfoEntity.getId(), agentId, corpId, wxcpUId, JSONUtil.toJsonStr(parmMap));
                    if (StrUtil.isNotBlank(agentId) && StrUtil.isNotBlank(corpId) && StrUtil.isNotBlank(wxcpUId) && StrUtil.isNotBlank(JSONUtil.toJsonStr(parmMap))) {
                        feignClientForWxCp.sendMessage(agentId, corpId, wxcpUId, JSONUtil.toJsonStr(parmMap));
                    }
156
                }else if (StringUtils.isNotBlank(wxOpenId)) {
157 158 159 160
                    // 2 公众号推送
                    String wxAppID = jsonKeyInfo.getStr("sendMessageAppId");
                    // ww=wxAppID&orgCode=orgCode
                    String link = String.format(daySubjectUrl, wxAppID, orgEntity.getOrgCode(), daySubjectEntity.getId());
161 162
                    String url = redirectService.getRedirectLink(link);
                    log.info("早报推送使用重定向链接, redirect-url: {}, origin-url: {}", url, link);
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
                    StringBuilder sb = new StringBuilder();
                    sb.append(mainTitle).append("\n\n")
                            .append(morningPaperProperties.getSubTitle()).append("\n\n")
                            .append("<a href=\"")
                            .append(url).append("\">")
                            .append("快去转发获客吧")
                            .append("</a>\n\n");
                    parmMap.put("content", sb.toString());
                    log.info("===公众号 推送=== userID:{} wxAppID:{} wxOpenId:{} parmMap:{}", userInfoEntity.getId(), wxAppID, wxOpenId, JSONUtil.toJsonStr(parmMap));
                    if (StrUtil.isNotBlank(wxAppID) && StrUtil.isNotBlank(wxOpenId) && StrUtil.isNotBlank(JSONUtil.toJsonStr(parmMap))) {
                        feignClientForWxMp.sendMessage(wxAppID, wxOpenId, JSONUtil.toJsonStr(parmMap));
                    }
                } else {
                    if (userInfoEntity.getUiTelphone() != null && userInfoEntity.getUiTelphone().startsWith("1700000")) {
                        log.info("内部测试账号,跳过, uid: {}, telphone: {}", userInfoEntity.getId(), userInfoEntity.getUiTelphone());
                        continue;
                    }
                    log.error("======用户ID:{} 没有微信或企业微信id======", userInfoEntity.getId());
                }
            }
        }
吴泽佳's avatar
吴泽佳 committed
184
    }
185 186

    public List<DaySubjectEntity> getDaySubList(String today) {
吴泽佳's avatar
吴泽佳 committed
187 188 189 190
        //查询今日早报 已上架
        LambdaQueryWrapper<DaySubjectEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DaySubjectEntity::getDeletetag, BizStatus.DeleteTag.tag_init_str)
                .eq(DaySubjectEntity::getDsDate, today)
吴泽佳's avatar
吴泽佳 committed
191
                .le(DaySubjectEntity::getDsTaskuptime, new Date())
吴泽佳's avatar
吴泽佳 committed
192
                .eq(DaySubjectEntity::getDsStatus, "1");
吴泽佳's avatar
吴泽佳 committed
193 194 195 196 197 198 199 200 201
        List<DaySubjectEntity> daySubjectEntities = daySubjectMapper.selectList(queryWrapper);
        List<DaySubjectEntity> daySubjectEntityList = daySubjectEntities.stream().filter(daySubjectEntity -> {
            // 过滤掉 早报下没有文章的
            List<DaySelection> daySelections = daySelectionMapper.selectList(new LambdaQueryWrapper<DaySelection>()
                    .eq(DaySelection::getDsSubjectId, daySubjectEntity.getId())
                    .eq(DaySelection::getDeletetag, BizStatus.DeleteTag.tag_init_str));
            return CollectionUtils.isNotEmpty(daySelections);
        }).collect(Collectors.toList());
        return daySubjectEntityList;
吴泽佳's avatar
吴泽佳 committed
202 203
    }

204 205 206 207 208 209 210 211 212 213
    public List<DaySubjectEntity> getDaySubject(String date, String orgId) {
        LambdaQueryWrapper<DaySubjectEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DaySubjectEntity::getDeletetag, BizStatus.DeleteTag.tag_init_str)
                .eq(DaySubjectEntity::getDsDate, DateUtil.parseDate(date))
                .eq(DaySubjectEntity::getDsPt, orgId)
                .eq(DaySubjectEntity::getDsStatus, "1");
        return daySubjectMapper.selectList(queryWrapper);
    }

    public String getOneMorningTitle() {
王亚雷's avatar
王亚雷 committed
214
        return morningPaperProperties.getTodayMailTitle();
215 216
    }

吴泽佳's avatar
吴泽佳 committed
217 218 219 220 221 222

    public static void main(String[] args) throws Exception {
        log.info("=========================");
    }

}