UcEventBusIptv2ManagementUcEngine.java
14 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package com.topdraw.mq.consumer;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.topdraw.business.module.member.service.MemberService;
import com.topdraw.business.module.member.service.dto.MemberDTO;
import com.topdraw.business.module.task.attribute.service.TaskAttrService;
import com.topdraw.business.module.task.attribute.service.dto.TaskAttrDTO;
import com.topdraw.business.module.task.domain.Task;
import com.topdraw.business.module.task.service.TaskService;
import com.topdraw.business.module.task.template.constant.TaskEventName;
import com.topdraw.business.module.task.template.constant.TaskEventType;
import com.topdraw.business.module.task.template.service.TaskTemplateService;
import com.topdraw.business.module.task.template.service.dto.TaskTemplateDTO;
import com.topdraw.business.module.user.iptv.service.UserTvService;
import com.topdraw.business.module.user.iptv.service.dto.UserTvDTO;
import com.topdraw.config.redis.RedisKeyConstant;
import com.topdraw.exception.BadRequestException;
import com.topdraw.exception.EntityNotFoundException;
import com.topdraw.mq.domain.DataSyncMsg;
import com.topdraw.resttemplate.RestTemplateClient;
import com.topdraw.util.DateUtil;
import com.topdraw.util.FileUtil;
import com.topdraw.util.JSONUtil;
import com.topdraw.utils.RedisUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ResponseStatusException;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
@Component
@Slf4j
public class UcEventBusIptv2ManagementUcEngine {
@Autowired
private TaskService taskService;
@Autowired
private UserTvService userTvService;
@Autowired
private MemberService memberService;
@Autowired
private TaskAttrService taskAttrService;
@Autowired
private TaskTemplateService taskTemplateService;
@Autowired
private RestTemplateClient restTemplateClient;
@Autowired
private RedisUtils redisUtils;
@Value("#{rabbitMqErrorLogConfig.getEventBusError()}")
private Map<String, String> error;
/**
* 事件
* @param content
* @description 基础数据同步
* @author Hongyan Wang
* @date 2021/9/7 11:26 上午
*/
@RabbitHandler
@RabbitListener(queues = "#{rabbitMqSourceConfig.getEventBusQueue()}",
containerFactory = "#{rabbitMqSourceConfig.getEventBusSource()}",
autoStartup = "#{rabbitMqSourceConfig.getEventBusStartUp()}",
ackMode = "AUTO")
public void eventBusConsumer(Channel channel, Message message, String content) throws IOException {
log.info(" receive dataSync msg , content is : {} ", content);
try {
PlayContent playContent = JSONUtil.parseMsg2Object(content, PlayContent.class);
log.info("解析后的参数 , playContent ==>> {} ", playContent);
if (Objects.nonNull(playContent)) {
this.parseContent(playContent);
}
// channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
} catch (Exception e) {
log.error("eventBus 消费异常 ==>> {}",e.getMessage());
channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
if (MapUtils.isNotEmpty(error)) {
String errorStart = this.error.get("start");
if (errorStart.equalsIgnoreCase("true")) {
String fileName = this.error.get("fileName")+"_"+LocalDate.now() +".log";
String filePath = this.error.get("filePath");
String filePath1 = filePath+fileName;
FileUtil.writeStringToFile2(filePath1, content, e.getMessage());
}
}
}
log.info("eventBusConsumer ====>>>> end");
}
/**
* 数据解析
* @param playContent
* @return
*/
private void parseContent(PlayContent playContent) throws ParseException {
PlayContent.MsgData msgData = playContent.getMsgData();
if (Objects.isNull(msgData)) {
log.error("eventBus事件消息体为空,msgData ==>> {}", msgData);
return;
}
String evt = playContent.getEvt();
switch (evt.toUpperCase()) {
case TaskEventName.PLAY:
String time = playContent.getTime();
String formatDate = DateUtil.formatDate(time);
Integer deviceType = playContent.getDeviceType();
String platformAccount = msgData.getPlatformAccount();
if (StringUtils.isBlank(platformAccount)){
log.error("参数错误,platformAccount不得为空, msgData ==>> {}", msgData);
return;
}
String mediaCode = msgData.getMediaCode();
Long mediaId = msgData.getMediaId();
String mediaName = msgData.getMediaName();
Integer playDuration = msgData.getPlayDuration();
if (Objects.isNull(playDuration) || playDuration == 0) {
return;
}
log.info("playDuration ==>> {}", playDuration);
DataSyncMsg dataSyncMsg = new DataSyncMsg();
dataSyncMsg.setEvt(evt);
dataSyncMsg.setEvent(TaskEventType.PLAY);
dataSyncMsg.setTime(LocalDateTime.now());
dataSyncMsg.setDeviceType(deviceType);
DataSyncMsg.MsgData msg = new DataSyncMsg.MsgData();
msg.setPlatformAccount(platformAccount);
msg.setMediaId(mediaId);
Integer playDurationValueTotal = 0;
if (StringUtils.isNotBlank(platformAccount)) {
Long count = this.userTvService.countByPlatformAccount(platformAccount);
log.info("通过大屏账号查询对应记录的条数,==>> {}", count);
if(count > 0L) {
String key = RedisKeyConstant.CACHE_PLATFROMACCOUNT_PLAYDURATION+platformAccount+"|"+formatDate;
Map<Object, Object> hmget =
this.redisUtils.hmget(key);
if (MapUtils.isEmpty(hmget)) {
// 初始化播放总时长<total>和第一个播放时间
playDurationValueTotal = playDuration;
Map<String, Object> map = new HashMap<>();
map.put("total", playDurationValueTotal);
// 存储时间36小时
this.redisUtils.hmset(key, map, 129600);
} else {
// 计算播放总时长 total = 播放总时长+当前播放时长
Integer total = this.getTotalPlayDurationFromRedis(key);
playDurationValueTotal = total + playDuration;
}
Map<String, Object> map = new HashMap<>();
map.put("total", playDurationValueTotal);
this.redisUtils.hmset(key, map);
JSONObject jsonObject1 = new JSONObject();
jsonObject1.put("PLAYDURATION",playDurationValueTotal);
msg.setParam(JSON.toJSONString(jsonObject1));
dataSyncMsg.setMsgData(JSON.toJSONString(msg));
log.info("调用uc-engine的接口 ==>> /uce/taskOperation/dealTask, 参数 ==>> {}",dataSyncMsg);
JSONObject response = this.restTemplateClient.dealTask(dataSyncMsg);
if (Objects.nonNull(response)) {
log.info("uc-engine任务处理接口的响应结果,/uce/taskOperation/dealTask ==>> {}",response);
} else {
log.error("uc-engine响应超时,请检查uc-engine服务");
throw new BadRequestException("uc-engine响应超时");
}
}
}
break;
}
//return null;
}
private DataSyncMsg checkTask(Integer playDurationValueTotal, String time, Integer deviceType, String mediaCode,
Long mediaId, String mediaName, DataSyncMsg dataSyncMsg,
DataSyncMsg.MsgData msgData, UserTvDTO userTvDTO) {
// 检查播放记录任务
List<TaskAttrDTO> taskAttrDTOList = new ArrayList<>();
// TODO 枚举
TaskTemplateDTO taskTemplateDTO = this.taskTemplateService.findByType(8);
if (Objects.nonNull(taskTemplateDTO.getId())) {
List<Task> taskList = this.taskService.findByTemplateId(taskTemplateDTO.getId());
if (CollectionUtils.isNotEmpty(taskList)) {
for (Task task : taskList) {
TaskAttrDTO taskAttrDTO = this.taskAttrService.findByTaskId(task.getId());
taskAttrDTOList.add(taskAttrDTO);
}
} else {
return null;
}
} else {
return null;
}
List<List<Integer>> attrList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(taskAttrDTOList)) {
for (TaskAttrDTO taskAttrDTO : taskAttrDTOList) {
String attrStr = taskAttrDTO.getAttrStr();
if (StringUtils.isNotBlank(attrStr)) {
JSONObject parse = JSONObject.parseObject(attrStr, JSONObject.class);
List<Integer> value = (List<Integer>) parse.get("value");
attrList.add(value);
}
}
} else {
return null;
}
int size = attrList.size();
DataSyncMsg dataSyncMsg1 = null;
if (size > 0) {
for (int i = size-1; i >= 0; i--) {
Integer integer = attrList.get(i).get(0);
if (playDurationValueTotal >= integer) {
dataSyncMsg1 = getDataSyncMsg(time, mediaCode, mediaId, mediaName, playDurationValueTotal, dataSyncMsg,
msgData, userTvDTO);
dataSyncMsg1.setEvt("PLAY");
dataSyncMsg1.setEvent(8);
dataSyncMsg1.setTime(LocalDateTime.now());
dataSyncMsg1.setDeviceType(deviceType);
this.taskDeal(dataSyncMsg1);
}
}
}
return dataSyncMsg1;
}
private Integer getTotalPlayDurationFromRedis(String key) {
Map<Object, Object> total = this.redisUtils.hmget("key");
if (Objects.nonNull(total)){
Object total1 = total.get("total");
if (Objects.nonNull(total1)) {
return Integer.valueOf(total1.toString());
}
}
return 0;
}
private Integer getRedisTotalKey(Map<Object, Object> hmget) {
Set<Object> objects = hmget.keySet();
return objects.size();
}
private Integer getRedisTotal(Map<Object, Object> hmget) {
Set<Object> objects = hmget.keySet();
Integer playDurationValueTotal_ = 0;
for (Object key_ : objects) {
if (key_.toString().equalsIgnoreCase("total")) {
playDurationValueTotal_ = Integer.valueOf(hmget.get(key_).toString());
return playDurationValueTotal_;
} else {
continue;
}
}
return playDurationValueTotal_;
}
private DataSyncMsg getDataSyncMsg(String time, String mediaCode, Long mediaId, String mediaName,
Integer playDuration, DataSyncMsg dataSyncMsg, DataSyncMsg.MsgData msgData1, UserTvDTO userTvDTO) {
String priorityMemberCode = userTvDTO.getPriorityMemberCode();
String memberCode = "";
if (StringUtils.isNotBlank(priorityMemberCode)) {
memberCode = priorityMemberCode;
} else {
memberCode = this.memberService.findById(userTvDTO.getMemberId()).getCode();
}
if (StringUtils.isBlank(memberCode))
throw new EntityNotFoundException(MemberDTO.class, "memberCode", "memberCode is null");
msgData1.setMemberCode(memberCode);
msgData1.setMediaId(mediaId);
JSONObject param = new JSONObject();
// 增量
param.put("playDuration", playDuration);
msgData1.setParam(JSON.toJSONString(param));
JSONObject description = new JSONObject();
description.put("mediaId", mediaId);
description.put("mediaName", mediaName);
description.put("playDuration", playDuration);
description.put("mediaCode", mediaCode);
description.put("time", time);
msgData1.setDescription(JSON.toJSONString(description));
dataSyncMsg.setMsgData(JSONObject.toJSONString(msgData1));
return dataSyncMsg;
}
/**
* 任务处理
* @param dataSyncMsg
*/
private void taskDeal(DataSyncMsg dataSyncMsg) {
this.restTemplateClient.dealTask(dataSyncMsg);
}
@Data
static class PlayContent {
private String evt;
private Integer event;
private Integer deviceType;
private String time;
private MsgData msgData;
@Data
static class MsgData {
private String platformAccount;
private Integer playDuration;
private Long mediaId;
private String mediaCode;
private String mediaName;
}
}
}