DaySubJob.java 9.25 KB
Newer Older
吴泽佳's avatar
吴泽佳 committed
1 2 3 4 5 6 7 8 9 10 11 12 13
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.JSON;
import cn.hutool.json.JSONArray;
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
14 15
import com.tanpu.feo.feojob.dao.user.entity.*;
import com.tanpu.feo.feojob.dao.user.mapper.*;
吴泽佳's avatar
吴泽佳 committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29
import com.tanpu.feo.feojob.feign.wxcp.FeignClientForWxCp;
import com.tanpu.feo.feojob.feign.wxmp.FeignClientForWxMp;
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.boot.web.client.RestTemplateBuilder;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
吴泽佳's avatar
吴泽佳 committed
30
import java.util.Date;
吴泽佳's avatar
吴泽佳 committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
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
59 60
    @Resource
    private DaySelectionMapper daySelectionMapper;
吴泽佳's avatar
吴泽佳 committed
61 62
    @Resource
    private OrgMapper orgMapper;
吴泽佳's avatar
吴泽佳 committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99


    @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));
        int size = morningPaperProperties.getMainTitles().size();
        String mainTitle = morningPaperProperties.getMainTitles().get(RandomUtil.randomInt(0, size));
        stringListMap.keySet().forEach(orgId -> {

            // 查询 机构配置信息
            OrgExtEntity orgExtEntity = orgExtMapper.selectOne(new LambdaQueryWrapper<OrgExtEntity>()
                    .eq(OrgExtEntity::getOrgId, orgId)
                    .eq(OrgExtEntity::getDeleteTag, BizStatus.DeleteTag.tag_init_str)
                    .last("limit 1"));
            String jsonKey = ObjectUtil.isNotNull(orgExtEntity) ? orgExtEntity.getJsonWxcpKey() : null;
            if (StrUtil.isNotBlank(jsonKey)) {
                //在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)
吴泽佳's avatar
吴泽佳 committed
100
                        .eq(UserInfoEntity::getLevel, "2");
吴泽佳's avatar
吴泽佳 committed
101
                List<UserInfoEntity> userInfoEntities = userInfoMapper.selectList(queryWrapper);
吴泽佳's avatar
吴泽佳 committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
                //查询orgCode
                OrgEntity orgEntity = orgMapper.selectOne(new LambdaQueryWrapper<OrgEntity>().eq(OrgEntity::getId, orgId).last("limit 1"));
                if (ObjectUtil.isNotNull(orgEntity)) {
                    userInfoEntities.stream().forEach(userInfoEntity -> {
                        List<DaySubjectEntity> daySubjectEntities = stringListMap.get(orgId);
                        daySubjectEntities.forEach(daySubjectEntity -> {
                            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
                                StringBuilder sb = new StringBuilder();
                                sb.append(mainTitle).append("\n\n")
                                        .append(morningPaperProperties.getSubTitle()).append("\n\n")
                                        .append("<a href=\"")
                                        .append(String.format(daySubjectUrl, corpId, orgEntity.getOrgCode(), daySubjectEntity.getId())).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));
                                }
吴泽佳's avatar
吴泽佳 committed
129
                            }
吴泽佳's avatar
吴泽佳 committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
                            if (StringUtils.isNotBlank(wxOpenId)) {
                                // 2 公众号推送
                                String wxAppID = jsonKeyInfo.getStr("sendMessageAppId");
                                // ww=wxAppID&orgCode=orgCode
                                StringBuilder sb = new StringBuilder();
                                sb.append(mainTitle).append("\n\n")
                                        .append(morningPaperProperties.getSubTitle()).append("\n\n")
                                        .append("<a href=\"")
                                        .append(String.format(daySubjectUrl, wxAppID, orgEntity.getOrgCode(), daySubjectEntity.getId())).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 {
                                log.error("======用户ID:{} 没有微信或企业微信id======", userInfoEntity.getId());
吴泽佳's avatar
吴泽佳 committed
148
                            }
吴泽佳's avatar
吴泽佳 committed
149
                        });
吴泽佳's avatar
吴泽佳 committed
150
                    });
吴泽佳's avatar
吴泽佳 committed
151
                }
吴泽佳's avatar
吴泽佳 committed
152 153 154 155 156 157 158 159 160
            }
        });

    }
    private List<DaySubjectEntity> getDaySubList(String today) {
        //查询今日早报 已上架
        LambdaQueryWrapper<DaySubjectEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DaySubjectEntity::getDeletetag, BizStatus.DeleteTag.tag_init_str)
                .eq(DaySubjectEntity::getDsDate, today)
吴泽佳's avatar
吴泽佳 committed
161
                .le(DaySubjectEntity::getDsTaskuptime, new Date())
吴泽佳's avatar
吴泽佳 committed
162
                .eq(DaySubjectEntity::getDsStatus, "1");
吴泽佳's avatar
吴泽佳 committed
163 164 165 166 167 168 169 170 171
        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
172 173 174 175 176 177 178 179
    }


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

}