RAgentEventServiceImpl.java
2.31 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
package vion.service.impl.monitor;
import com.github.yulichang.base.MPJBaseServiceImpl;
import lombok.RequiredArgsConstructor;
import net.dreamlu.iot.mqtt.codec.MqttQoS;
import net.dreamlu.iot.mqtt.spring.client.MqttClientTemplate;
import org.dromara.hutool.core.collection.CollUtil;
import org.dromara.hutool.core.text.StrUtil;
import org.springframework.stereotype.Service;
import vion.mapper.monitor.RAgentEventMapper;
import vion.model.monitor.RAgentEvent;
import vion.service.monitor.IRAgentEventService;
import vion.utils.JsonUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* @author vion
* @date 2024/10/31
*/
@Service
@RequiredArgsConstructor
public class RAgentEventServiceImpl extends MPJBaseServiceImpl<RAgentEventMapper, RAgentEvent> implements IRAgentEventService {
private final MqttClientTemplate client;
@Override
public String bind(String agentUid, String mallUid, List<RAgentEvent> dtoList) {
dtoList.forEach(ae -> ae.setAgentUid(agentUid));
var agentEventList = this.lambdaQuery().eq(RAgentEvent::getMallUid, mallUid).list();
var eventUid2AEMap = agentEventList.stream().collect(Collectors.toMap(RAgentEvent::getEventUid, Function.identity()));
List<RAgentEvent> updList = new ArrayList<>();
List<RAgentEvent> insList = new ArrayList<>();
dtoList.forEach(dto -> {
if (eventUid2AEMap.containsKey(dto.getEventUid())) {
var ae = eventUid2AEMap.get(dto.getEventUid());
dto.setId(ae.getId());
updList.add(dto);
} else {
insList.add(dto);
}
});
if (CollUtil.isNotEmpty(updList)) {
this.updateBatchById(updList);
}
if (CollUtil.isNotEmpty(insList)) {
this.saveBatch(insList);
}
dtoList.forEach(dto -> client.publish(StrUtil.format("/SA/{}/event/push", agentUid), JsonUtil.toJsonByte(dto), MqttQoS.QOS2));
return "绑定成功";
}
@Override
public String unbind(List<Long> idList) {
// todo
// client.publish(StrUtil.format("/SA/{}/event/push", agentUid), JsonUtil.toJsonByte(agentEvent), MqttQoS.QOS2);
return this.removeBatchByIds(idList) ? "解绑成功" : "解绑失败";
}
}