jinpengyong
2021-11-25 6050927edf6c00f2f06b0b0775502d38eb5b7705
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
package com.moral.task;
 
import com.moral.strategy.RepairDataContext;
import com.moral.strategy.RepairDataStrategy;
import com.moral.strategy.RepairStrategyFactory;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.ObjectUtils;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.text.ParseException;
import java.util.*;
 
@Component
public class RepairDataTask {
    @Resource
    RedisTemplate redisTemplate;
    @Resource
    RepairStrategyFactory factory;
 
    private Map<String,Integer> sortMap = new HashMap<>();
 
    @PostConstruct
    public void init(){
        this.sortMap.put("repairHourlyData",3);
        this.sortMap.put("repairFiveMinutelyData",2);
        this.sortMap.put("repairMinutelyData",1);
    }
 
    @XxlJob("repairData")
    public ReturnT repairData(String param) throws ParseException {
        //从缓存中读取插入失败的数据
        List<String> records = redisTemplate.opsForList().range("unrepair_data", 0, -1);
        if(ObjectUtils.isEmpty(records))
            return new ReturnT(200,"无数据需要修补");
        //对错误信息进行排序
        sorRecords(records);
        redisTemplate.delete("unrepair_data");
        MultiValueMap<String, String> results = new LinkedMultiValueMap<>();
        RepairDataContext context = new RepairDataContext();
        for (String record : records) {
            RepairDataStrategy instance = factory.getInstance(record);
            context.setRepairDataStrategy(instance);
            context.executeStrategy(record.split("_")[1], results);
        }
        return new ReturnT(200,results.toString());
    }
 
 
    public void sorRecords(List<String> records){
        Collections.sort(records, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                Integer integer = sortMap.get(o1.split("_")[0]);
                Integer integer1 = sortMap.get(o2.split("_")[0]);
                return integer>integer1?1:-1;
            }
        });
    }
 
}