RyTask.java 3.8 KB
Newer Older
1 2
package com.linkwechat.quartz.task;

3 4 5 6 7
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity;
import com.linkwechat.common.core.elasticsearch.ElasticSearch;
S
sunxiwang 已提交
8
import com.linkwechat.common.core.redis.RedisCache;
9
import com.linkwechat.common.utils.StringUtils;
S
sunxiwang 已提交
10
import com.linkwechat.wecom.service.IWeChatContactMappingService;
11 12
import com.tencent.wework.FinanceUtils;
import lombok.extern.slf4j.Slf4j;
S
sunxiwang 已提交
13
import org.apache.poi.ss.formula.functions.T;
14 15 16 17 18 19 20 21 22
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
S
sunxiwang 已提交
23
import java.util.Optional;
24
import java.util.concurrent.atomic.AtomicLong;
S
sunxiwang 已提交
25
import java.util.function.Consumer;
26 27 28

/**
 * 定时任务调度测试
29
 *
30 31
 * @author ruoyi
 */
32
@Slf4j
33
@Component("ryTask")
34 35 36
public class RyTask {
    @Autowired
    private ElasticSearch elasticSearch;
S
sunxiwang 已提交
37 38 39 40
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IWeChatContactMappingService weChatContactMappingService;
41 42

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
43 44 45
        System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
    }

46
    public void ryParams(String params) {
47 48 49
        System.out.println("执行有参方法:" + params);
    }

50
    public void ryNoParams() {
51 52
        System.out.println("执行无参方法");
    }
53 54 55


    public void FinanceTask(String corpId, String secret) throws IOException {
S
sunxiwang 已提交
56 57 58
        log.info("执行有参方法: params:{},{}", corpId, secret);
        //创建索引
        elasticSearch.createIndex2(WeConstans.WECOM_FINANCE_INDEX,elasticSearch.getFinanceMapping());
S
sunxiwang 已提交
59
        //从缓存中获取消息标识
60 61 62 63

        Object seqObject = Optional.ofNullable(redisCache.getCacheObject(WeConstans.CONTACT_SEQ_KEY)).orElse(0L);
        Long seqLong = Long.valueOf(String.valueOf(seqObject));
        AtomicLong index = new AtomicLong(seqLong);
S
sunxiwang 已提交
64
        if (index.get() == 0){
65
            setRedisCacheSeqValue(index);
S
sunxiwang 已提交
66 67
        }

S
sunxiwang 已提交
68
        log.info(">>>>>>>seq:{}",index.get());
69
        FinanceUtils.initSDK(corpId, secret);
S
sunxiwang 已提交
70
        List<JSONObject> chatDataList = FinanceUtils.getChatData(index.get(),
S
sunxiwang 已提交
71
                "",
S
sunxiwang 已提交
72
                "", redisCache);
73 74
        if (CollectionUtil.isNotEmpty(chatDataList)){
            try {
S
sunxiwang 已提交
75 76
                List<ElasticSearchEntity> elasticSearchEntities = weChatContactMappingService.saveWeChatContactMapping(chatDataList);
                elasticSearch.insertBatch(WeConstans.WECOM_FINANCE_INDEX, elasticSearchEntities);
77 78 79 80 81 82
            } catch (Exception e) {
                log.error("消息处理异常:ex:{}", e);
                e.printStackTrace();
            }
        }
    }
83 84 85 86 87 88 89 90

    private void setRedisCacheSeqValue(AtomicLong index){
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        SortBuilder<?> sortBuilderPrice = SortBuilders.fieldSort(WeConstans.CONTACT_SEQ_KEY).order( SortOrder.DESC);
        searchSourceBuilder.sort(sortBuilderPrice);
        searchSourceBuilder.size(1);
        List<JSONObject> searchResultList = elasticSearch.search(WeConstans.WECOM_FINANCE_INDEX, searchSourceBuilder, JSONObject.class);
        searchResultList.stream().findFirst().ifPresent(result ->{
S
sunxiwang 已提交
91
            index.set(result.getLong(WeConstans.CONTACT_SEQ_KEY));
92 93 94
        });
        redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY,index);
    }
95
}