peng
2026-03-20 aeb465194251550c9d3748c34910e435d7f760cb
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
package com.tievd.jyz.cache;
 
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.baomidou.mybatisplus.extension.service.IService;
import com.tievd.jyz.constants.SystemConstant;
import com.tievd.jyz.entity.*;
import com.tievd.jyz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * Author: wqy
 * Date: 2023/3/9 9:37
 */
 
@Component
public class AlgorithmCache {
 
    @Autowired
    private ISysAlgorithmItemService sysAlgorithmItemService;
    
    public static final String OIL_ALG_CODE = "C10001,C10003";
 
    private static Map<String, SysAlgorithmItem> algorithmMap = new HashMap<>();
    /** 所属阶段 1卸前 2卸中 3卸后 */
    private static Map<Byte, List<SysAlgorithmItem>> phraseAlgorithmMap = new HashMap<>();
    
    public static Map<String, SysAlgorithmItem> algorithmMap() {
        return algorithmMap;
    }
    
    /**
     * 获取卸油算法map
     * @return
     */
    public static Map<String, SysAlgorithmItem> oiloutAlgMap() {
        Map<String, SysAlgorithmItem> oiloutMap = new HashMap<>();
        for (Map.Entry<String, SysAlgorithmItem> entry : algorithmMap.entrySet()) {
            if (entry.getValue().getRegion() == SystemConstant.INSTALL_ADDRESS_OIL_OUT.byteValue()) {
                oiloutMap.put(entry.getKey(), entry.getValue());
            }
        }
        return oiloutMap;
    }
 
    /**
     *获取阶段算法
     * @param phrase
     * @return
     */
    public static List<SysAlgorithmItem> getPhraseAlgorithmMap(Byte phrase) {
        return phraseAlgorithmMap.get(phrase);
    }
 
    /**
     * 初始化
     */
    @PostConstruct
    private void init(){
        algorithmMap.clear();
        phraseAlgorithmMap.clear();
        List<SysAlgorithmItem> algorithmItems = sysAlgorithmItemService.list();
        for(SysAlgorithmItem algorithmItem:algorithmItems){
            algorithmMap.put(algorithmItem.getAlgorithmCode(),algorithmItem);
            List<SysAlgorithmItem> sysAlgorithmItems = phraseAlgorithmMap.get(algorithmItem.getPhrase());
            if(sysAlgorithmItems == null){
                sysAlgorithmItems = new ArrayList<>();
                phraseAlgorithmMap.put(algorithmItem.getPhrase(),sysAlgorithmItems);
            }
            sysAlgorithmItems.add(algorithmItem);
        }
    }
 
    /**
     * 获取算法行为
     * @param algCode
     * @return
     */
    public static SysAlgorithmItem getAlgorithmMap(String algCode) {
        return algorithmMap.get(algCode);
    }
    
    
    public static Model getEntity(String algCode) {
        int type = algorithmMap.get(algCode).getAlgorithmType();
        switch(type) {
            case 1 : return new OilEvent();
            case 2 : return new OiloutEvent();
            case 3 : return new PatrolEvent();
            case 4 : return new SpotcheckEvent();
            default: return new OilEvent();
        }
    }
    
    public static Class<? extends IService> getService(String algCode) {
        int type = algorithmMap.get(algCode).getAlgorithmType();
        switch(type) {
            case 1 : return IOilEventService.class;
            case 2 : return IOiloutEventService.class;
            case 3 : return IPatrolEventService.class;
            case 4 : return ISpotcheckEventService.class;
            default: return IOilEventService.class;
        }
    }
    
}