shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.walker.push;
 
import com.walker.infrastructure.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import java.util.List;
 
public abstract class AbstractPushObject<T> implements Pushable<T>{
 
    protected transient final Logger logger = LoggerFactory.getLogger(getClass());
 
    @Override
    public String getId() {
        return this.id;
    }
 
    @Override
    public String getName() {
        return this.name;
    }
 
    @Override
    public PushResult push(Notification notification) throws PushException {
        if(notification == null){
            throw new IllegalArgumentException("推送消息未提供");
        }
        if(StringUtils.isEmpty(notification.getContent())
                || StringUtils.isEmptyList(notification.getChannelList())){
            throw new IllegalArgumentException("推送消息缺少必要参数");
        }
        if(!notification.getBroadcast() && StringUtils.isEmptyList(notification.getReceiverList())){
            throw new IllegalArgumentException("非广播推送必须指定接收者");
        }
 
        boolean hasChannel = false;
        for(NotificationChannel channel : notification.getChannelList()){
            if(channel == this.getNotificationChannel()){
                hasChannel = true;
                break;
            }
        }
        if(!hasChannel){
            throw new PushException(notification.getId(), "当前消息不支持该推送者:" + this.name, null);
        }
 
        // 转换成具体发送的业务特定对象
        List<T> list = this.translateToTarget(notification);
        if(StringUtils.isEmptyList(list)){
//            throw new PushException(notification.getId(), "转换业务通知失败: list = null", null);
            logger.warn("translateToTarget():转换返回空对象");
        }
 
        // 2023-04-24 异步操作处理
        if(this.supportAsync){
            if(this.pushStatusListener == null){
                throw new IllegalStateException("异步推送,需要先添加:pushStatusListener");
            }
        }
 
        // 调用发送请求
        PushResult pushResult = this.doPushContent(notification, list);
//        if(!StringUtils.isEmptyList(pushResult.getFailedList())){
//            this.logger.error("推送消息失败,id={}, failed={}", notification.getId(), pushResult.getFailedList());
//            if(!notification.getBroadcast()){
//                // 非广播消息,需要检查是否全部失败,成功的需要保存
//                List<String> successList = new ArrayList<>(8);
//                for(String one : notification.getReceiverList()){
//                    if(!pushResult.getFailedList().contains(one)){
//                        successList.add(one);
//                    }
//                }
//                this.persistent(notification, successList);
//                logger.debug("保存了(部分)成功推送集合");
//            }
//        } else {
//            // 全部成功,保存
//            this.persistent(notification, notification.getReceiverList());
//            logger.debug("保存了(全部)成功推送集合");
//        }
        return pushResult;
    }
 
    /**
     * 执行具体推送的方法,由子类完成。
     * @param notification
     * @param data 转换成业务通知集合
     * @return 只有同步类型时,返回的才是正确调用结果,异步需要看监听器
     * @throws PushException
     * @date 2023-04-22
     */
    protected abstract PushResult doPushContent(Notification notification, List<T> data) throws PushException;
 
//    /**
//     * 持久化保存推送数据(到数据库)
//     * @param notification
//     * @param successReceiverList
//     */
//    protected abstract void persistent(Notification notification, List<String> successReceiverList);
 
//    protected abstract void onException(Notification notification, String error);
//    protected abstract void onSuccess(Notification notification, Object option);
 
    @Override
    public int hashCode(){
        return this.id.hashCode();
    }
 
    @Override
    public boolean equals(Object obj){
        if(obj == null){return false;}
        if(obj instanceof Pushable){
            Pushable push = (Pushable) obj;
            if(push.getId().equals(this.id)){
                return true;
            }
        }
        return false;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    @Override
    public boolean supportAsync() {
        return supportAsync;
    }
 
    public void setSupportAsync(boolean supportAsync) {
        this.supportAsync = supportAsync;
    }
 
    @Override
    public PushStatusListener getPushStatusListener() {
        return pushStatusListener;
    }
 
    @Override
    public void setPushStatusListener(PushStatusListener pushStatusListener) {
        this.pushStatusListener = pushStatusListener;
    }
 
    private PushStatusListener pushStatusListener;
    private boolean supportAsync = false;
    private String id;
    private String name;
}