DaySubJob.java 6.84 KB
Newer Older
吴泽佳's avatar
吴泽佳 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 54 55 56 57 58 59 60 61 62 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 100 101 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
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;
import com.tanpu.feo.feojob.dao.user.entity.DaySubjectEntity;
import com.tanpu.feo.feojob.dao.user.entity.OrgExtEntity;
import com.tanpu.feo.feojob.dao.user.entity.UserInfoEntity;
import com.tanpu.feo.feojob.dao.user.mapper.DaySubjectMapper;
import com.tanpu.feo.feojob.dao.user.mapper.OrgExtMapper;
import com.tanpu.feo.feojob.dao.user.mapper.UserInfoMapper;
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;
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;


    @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);
//                String wxOpenId = jsonKeyInfo.getStr("wxOpenId");
//                String wxcpUId = jsonKeyInfo.getStr("wxcpUId");
                // 查询该机构下 用户
                LambdaQueryWrapper<UserInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(UserInfoEntity::getDeletetag, BizStatus.DeleteTag.tag_init_str)
                        .eq(UserInfoEntity::getOrgId, orgId)
                        .eq(UserInfoEntity::getLevel, "2")
                        .isNotNull(UserInfoEntity::getUiOpenid);
                List<UserInfoEntity> userInfoEntities = userInfoMapper.selectList(queryWrapper);
                userInfoEntities.stream().forEach(userInfoEntity -> {
                    List<DaySubjectEntity> daySubjectEntities = stringListMap.get(orgId);
                    daySubjectEntities.forEach(daySubjectEntity -> {
                        HashMap<String, String> parmMap = new HashMap<>();
                        parmMap.put("type", "text");
                        StringBuilder sb = new StringBuilder();
                        sb.append(mainTitle).append("\n\n")
                                .append(morningPaperProperties.getSubTitle()).append("\n\n")
                                .append("<a href=\"").append(daySubjectUrl).append(daySubjectEntity.getId()).append("\">").append("快去转发获客吧").append("</a>\n\n");
                        parmMap.put("content", sb.toString());
                        String wxOpenId = userInfoEntity.getUiOpenid();
                        String wxcpUId = userInfoEntity.getWxcpUid();
                        if (StringUtils.isNotBlank(wxcpUId)) {
                            // 1 企业微信 推送
                            String agentId = jsonKeyInfo.getStr("agentId");
                            String corpId = jsonKeyInfo.getStr("corpId");
                            feignClientForWxCp.sendMessage(agentId, corpId, wxcpUId, JSONUtil.toJsonStr(parmMap));
                        }
                        if (StringUtils.isNotBlank(wxOpenId)) {
                            // 2 公众号推送
                            String wxAppID = jsonKeyInfo.getStr("sendMessageAppId");
                            feignClientForWxMp.sendMessage(wxAppID, wxOpenId, JSONUtil.toJsonStr(parmMap));
                        } else {
                            log.error("======用户ID:{} 没有微信或企业微信id======", userInfoEntity.getId());
                        }

                    });
                });
            }
        });

    }
    private List<DaySubjectEntity> getDaySubList(String today) {
        //查询今日早报 已上架
        LambdaQueryWrapper<DaySubjectEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DaySubjectEntity::getDeletetag, BizStatus.DeleteTag.tag_init_str)
                .eq(DaySubjectEntity::getDsDate, today)
                .eq(DaySubjectEntity::getDsStatus, "1");
        return daySubjectMapper.selectList(queryWrapper);
    }


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

}