guoshilong
2024-03-13 c8b03d3da4e29fc3fb9a42ca157738256c331a00
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
package cn.gistack.alerts.notice.strategy;
 
import cn.gistack.alerts.notice.service.impl.NoticeStrategyImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiFunction;
 
/**
 * 通知方式策略
 * @author zhongrj
 * @date 2023-05-29
 *
 */
@Service
@Slf4j
public class NoticeStrategyContext {
 
    /**
     * 存放所有策略.
     */
    private Map<String, BiFunction<String, String, String>> strategyMap = new HashMap<>(18);
 
    /**
     * 具体的策略细节.
     */
    @Autowired
    private NoticeStrategyImpl noticeStrategyImpl;
 
    /**
     * 加载所有策略
     */
    @PostConstruct
    public void initStrategies() {
        //1
        strategyMap.put("waterInfoNotHandle", (arg1, arg2) -> noticeStrategyImpl.waterInfoNotHandle(arg1, arg2));
        //2
        strategyMap.put("waterDataExcHandle", (arg1, arg2) -> noticeStrategyImpl.waterDataExcHandle(arg1, arg2));
        //3
        strategyMap.put("waterLessDeadHandle", (arg1, arg2) -> noticeStrategyImpl.waterLessDeadHandle(arg1, arg2));
        //4
        strategyMap.put("waterMoreDeadLessHJHandle", (arg1, arg2) -> noticeStrategyImpl.waterMoreDeadLessHJHandle(arg1, arg2));
        //5
        strategyMap.put("waterComeOverHandle", (arg1, arg2) -> noticeStrategyImpl.waterComeOverHandle(arg1, arg2));
        //6
        strategyMap.put("waterFirstOverHandle", (arg1, arg2) -> noticeStrategyImpl.waterFirstOverHandle(arg1, arg2));
        //7
        strategyMap.put("waterSmallScaleOverHandle", (arg1, arg2) -> noticeStrategyImpl.waterSmallScaleOverHandle(arg1, arg2));
        //8
        strategyMap.put("waterBigScaleOverHandle", (arg1, arg2) -> noticeStrategyImpl.waterBigScaleOverHandle(arg1, arg2));
        //9
        strategyMap.put("waterComeDesignHandle", (arg1, arg2) -> noticeStrategyImpl.waterComeDesignHandle(arg1, arg2));
        //10
        strategyMap.put("waterMoreDesignHandle", (arg1, arg2) -> noticeStrategyImpl.waterMoreDesignHandle(arg1, arg2));
        //11
        strategyMap.put("waterMoreCheckHandle", (arg1, arg2) -> noticeStrategyImpl.waterMoreCheckHandle(arg1, arg2));
        //12
        strategyMap.put("waterComeOneHourUpHandle", (arg1, arg2) -> noticeStrategyImpl.waterComeOneHourUpHandle(arg1, arg2));
        //13
        strategyMap.put("waterComeOneHourDownHandle", (arg1, arg2) -> noticeStrategyImpl.waterComeOneHourDownHandle(arg1, arg2));
        //14
        strategyMap.put("rainstormHandle", (arg1, arg2) -> noticeStrategyImpl.rainstormHandle(arg1, arg2));
        //15
        strategyMap.put("alreadyHeavyRainHandle", (arg1, arg2) -> noticeStrategyImpl.alreadyHeavyRainHandle(arg1, arg2));
        //16
        strategyMap.put("alreadyRainstormHandle", (arg1, arg2) -> noticeStrategyImpl.alreadyRainstormHandle(arg1, arg2));
        //17
        strategyMap.put("twoHourRainHandle", (arg1, arg2) -> noticeStrategyImpl.twoHourRainHandle(arg1, arg2));
        //18
        strategyMap.put("sixHourRainHandle", (arg1, arg2) -> noticeStrategyImpl.sixHourRainHandle(arg1, arg2));
        //19
        strategyMap.put("damSafetyHandle", (arg1, arg2) -> noticeStrategyImpl.damSafetyHandle(arg1, arg2));
        //20
        strategyMap.put("damSafetySyHandle", (arg1, arg2) -> noticeStrategyImpl.damSafetySyHandle(arg1, arg2));
        //21
        strategyMap.put("damSafetySyNoneHandle", (arg1, arg2) -> noticeStrategyImpl.damSafetySyNoneHandle(arg1, arg2));
        //22
        strategyMap.put("damSafetySlNoneHandle", (arg1, arg2) -> noticeStrategyImpl.damSafetySlNoneHandle(arg1, arg2));
        //23
        strategyMap.put("damSafetyWyNoneHandle", (arg1, arg2) -> noticeStrategyImpl.damSafetyWyNoneHandle(arg1, arg2));
    }
 
    /**
     * 指派具体的策略去执行.
     *
     * @param arg1 参数1
     * @param arg2 参数2
     * @param key 根据key 来获取不同的策略
     * @return
     */
    public String doSomethingByStrategy(String arg1, String arg2, String key) {
        BiFunction<String, String, String> biFunction = strategyMap.get(key);
        if (Objects.isNull(biFunction)) {
            // 没有找到特定的策略,则采用默认实现(一般只有加了新的类型,但是没有配置对应的处理策略才会走到这里)
            log.warn("========= 没有配置该处理策略,采用默认实现,key {} ====== " + key);
            return noticeStrategyImpl.doSomethingDefault(arg1, arg2);
        }
        return biFunction.apply(arg1, arg2);
    }
}