MsgParamCondition.java
11 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package cn.fw.hermes.sdk.api.para;
import lombok.Data;
import org.springframework.util.CollectionUtils;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
/**
* @author 张志伟
* @date 2018-09-18 14:58
* @description
*/
@Data
public class MsgParamCondition {
/**
* 消息类型
*/
private MsgTypeEnum msgTypeEnum;
/**
* 是否使用组合消息
*/
private boolean unitMsg;
/**
* 是否使用离线消息,默认为true
*/
private boolean useOfflineMsg;
/**
* 消息离线保存时长(秒数),最长为 7 天(604800s)。若消息只发在线用户,不想保存离线,则该字段填 0。若不填,则默认保存 7 天
*/
private Integer msgLifeTime;
/**
* 目标账号id
*/
private Long userId;
/**
* 指定发送账号业务类型
*/
private BusinessType businessType;
/**
* 文本消息体
*/
private TextContent textContent;
/**
* 地理位置消息
*/
private LocationContent locationContent;
/**
* 表情消息
*/
private FaceContent faceContent;
/**
* 自定义消息
*/
private CustomContent customContent;
/**
* 组合消息的消息体,严格区分先后顺序
*/
private List<? extends BaseContent> contentList;
/**
* 离线消息推送设置
*/
private OfflinePushInfo offlinePushInfo;
/**
* 消息接收人是否为业务人员
*/
private Boolean isStaff;
/**
* 消息唯一ID
*/
private String messageId;
public MsgParamCondition() {
}
private MsgParamCondition(MsgTypeEnum msgTypeEnum, TextContent textContent, LocationContent locationContent, FaceContent faceContent,
CustomContent customContent, boolean unitMsg, List<? extends BaseContent> contentList, boolean useOfflineMsg,
OfflinePushInfo offlinePushInfo, Integer msgLifeTime, Long userId, BusinessType businessType,boolean isStaff) {
this.msgTypeEnum = msgTypeEnum;
this.textContent = textContent;
this.locationContent = locationContent;
this.faceContent = faceContent;
this.customContent = customContent;
this.unitMsg = unitMsg;
this.contentList = contentList;
this.useOfflineMsg = useOfflineMsg;
this.offlinePushInfo = offlinePushInfo;
this.msgLifeTime = msgLifeTime;
this.userId = userId;
this.businessType = businessType;
this.isStaff = isStaff;
}
/**
* 离线推送文本消息
*
* @param text 内容
* @param lifeTime 离线保留时长(秒) 最长7天
* @param title 离线推送标题
* @param desc 离线推送描述
* @param ext 离线推送扩展信息 (废弃此参数)
* @param userId 目标
* @return
*/
public static Builder getOfflineTxetPara(String text, Integer lifeTime, String title, String desc, Map<String, Object> ext
, Long userId) {
lifeTime = lifeTime == null ? 604800 : lifeTime;
if (lifeTime / 604800 > 0) {
lifeTime = 604800;
}
TextContent textContent = new TextContent();
textContent.setText(text);
OfflinePushInfo offlinePushInfo = new OfflinePushInfo();
offlinePushInfo.setDesc(desc);
offlinePushInfo.setTitle(title);
return new Builder()
.setMsgType(MsgTypeEnum.TEXT)
.setTextContent(textContent)
.setUserId(userId)
.setUseOfflineMsg(true)
.setOfflinePushInfo(offlinePushInfo)
.setMsgLifeTime(lifeTime)
.setUnitMsg(false);
}
/**
* @param text 内容
* @param lifeTime 离线保留时长(秒) 最长7天
* @param userId 目标
* @return
*/
public static Builder getTextPara(String text, Integer lifeTime, Long userId) {
TextContent textContent = new TextContent();
textContent.setText(text);
lifeTime = lifeTime == null ? 604800 : lifeTime;
if (lifeTime / 604800 > 0) {
lifeTime = 604800;
}
return new Builder()
.setMsgType(MsgTypeEnum.TEXT)
.setTextContent(textContent)
.setUserId(userId)
.setMsgLifeTime(lifeTime)
.setUseOfflineMsg(false)
.setUnitMsg(false);
}
/**
* @param text 地理位置描述
* @param lifeTime 离线保留时长(秒) 最长7天
* @param lat 纬度
* @param lng 经度
* @param userId 目标
* @return
*/
public static Builder getLocationMsg(String text, Integer lifeTime, BigDecimal lat, BigDecimal lng, Long userId) {
LocationContent locationContent = new LocationContent();
locationContent.setDesc(text);
locationContent.setLat(lat);
locationContent.setLng(lng);
lifeTime = lifeTime == null ? 604800 : lifeTime;
if (lifeTime / 604800 > 0) {
lifeTime = 604800;
}
return new Builder()
.setMsgType(MsgTypeEnum.LOCATION)
.setLocationContent(locationContent)
.setMsgLifeTime(lifeTime)
.setUserId(userId)
.setUnitMsg(false)
.setUseOfflineMsg(false);
}
/**
* 发送表情消息
*
* @param text 内容
* @param lifeTime 离线保留时长(秒) 最长7天
* @param index 表情索引
* @param userId 目标
* @return
*/
public static Builder getFaceMsg(String text, Integer lifeTime, int index, Long userId) {
FaceContent faceContent = new FaceContent();
faceContent.setData(text);
faceContent.setIndex(index);
lifeTime = lifeTime == null ? 604800 : lifeTime;
if (lifeTime / 604800 > 0) {
lifeTime = 604800;
}
return new Builder()
.setMsgType(MsgTypeEnum.FACE)
.setFaceContent(faceContent)
.setMsgLifeTime(lifeTime)
.setUserId(userId)
.setUnitMsg(false)
.setUseOfflineMsg(false);
}
/**
* 自定义消息
* <p>
* 其中业务通知时ext必须要有type字段才能解析 value参考 {@link MessageBusinessType} 可以直接使用枚举也可以使用其字符串
* 推荐直接使用枚举
*
* @param desc 内容(描述信息)
* @param title 标题(为空的时候不会在通知栏提示,静默消息)
* @param ext 扩展信息
* @param lifeTime 离线保留时长(秒) 最长7天
* @param userId 目标
* @param useOffline 是否离线推送
* @param isStaff 消息接收人是否为业务人员,必填
* @return
*/
public static Builder getCustomMsg(String desc, String title, Map<String, Object> ext, Integer lifeTime, Long userId, boolean useOffline,Boolean isStaff) {
CustomContent customContent = new CustomContent();
title = title == null ? "" : title;
customContent.setData(title);
customContent.setDesc(desc);
customContent.setExt(ext);
lifeTime = lifeTime == null ? 604800 : lifeTime;
if (lifeTime / 604800 > 0) {
lifeTime = 604800;
}
Builder builder = new Builder()
.setMsgType(MsgTypeEnum.CUSTOM)
.setCustomContent(customContent)
.setMsgLifeTime(lifeTime)
.setUserId(userId)
.setUnitMsg(false)
.setUseOfflineMsg(useOffline)
.setIsStaff(isStaff);
if (useOffline) {
OfflinePushInfo o = new OfflinePushInfo();
o.setTitle(title);
o.setDesc(desc);
o.setExt(ext);
builder.setOfflinePushInfo(o);
}
return builder;
}
public static Builder newBuilder() {
return new Builder();
}
public static class Builder {
private MsgTypeEnum msgTypeEnum;
private TextContent textContent;
private LocationContent locationContent;
private FaceContent faceContent;
private CustomContent customContent;
private boolean unitMsg;
private List<? extends BaseContent> contentList;
private boolean useOfflineMsg;
private OfflinePushInfo offlinePushInfo;
private Integer msgLifeTime;
private Long userId;
private BusinessType businessType;
private Boolean isStaff;
public Builder setMsgType(MsgTypeEnum msgType) {
this.msgTypeEnum = msgType;
return this;
}
public Builder setTextContent(TextContent textContent) {
this.textContent = textContent;
return this;
}
public Builder setLocationContent(LocationContent locationContent) {
this.locationContent = locationContent;
return this;
}
public Builder setFaceContent(FaceContent faceContent) {
this.faceContent = faceContent;
return this;
}
public Builder setCustomContent(CustomContent customContent) {
this.customContent = customContent;
return this;
}
public Builder setUnitMsg(boolean unitMsg) {
this.unitMsg = unitMsg;
return this;
}
public Builder setContentList(List<? extends BaseContent> contentList) {
this.contentList = contentList;
return this;
}
public Builder setUseOfflineMsg(boolean useOfflineMsg) {
this.useOfflineMsg = useOfflineMsg;
return this;
}
public Builder setOfflinePushInfo(OfflinePushInfo offlinePushInfo) {
this.offlinePushInfo = offlinePushInfo;
return this;
}
public Builder setMsgLifeTime(Integer msgLifeTime) {
this.msgLifeTime = msgLifeTime;
return this;
}
public Builder setUserId(Long userId) {
this.userId = userId;
return this;
}
public Builder setBusinessType(BusinessType businessType) {
this.businessType = businessType;
return this;
}
public Builder setIsStaff(Boolean isStaff) {
this.isStaff = isStaff;
return this;
}
public MsgParamCondition build() {
if (CollectionUtils.isEmpty(contentList)) {
unitMsg = false;
}
if (offlinePushInfo == null) {
useOfflineMsg = false;
}
if (userId == null) {
throw new IllegalArgumentException("目标不存在");
}
return new MsgParamCondition(msgTypeEnum, textContent, locationContent, faceContent, customContent, unitMsg, contentList,
useOfflineMsg, offlinePushInfo, msgLifeTime, userId, businessType,isStaff);
}
}
}