zhongrj
2023-06-01 5d75e9dfde8350bf7ef49c3d6f07e0fd780e9e66
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
package cn.gistack.sm.notice.strategy;
 
import cn.gistack.sm.notice.strategy.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() {
        strategyMap.put("waterInfoNotHandle", (arg1, arg2) -> noticeStrategyImpl.waterInfoNotHandle(arg1, arg2));
        strategyMap.put("waterDataExcHandle", (arg1, arg2) -> noticeStrategyImpl.waterDataExcHandle(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);
    }
}