|
@@ -0,0 +1,196 @@
|
|
|
|
+package com.pig4cloud.pig.marketing.service.impl;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
+import com.pig4cloud.pig.marketing.api.dto.mongo.PageDeviceInfoDTO;
|
|
|
|
+import com.pig4cloud.pig.marketing.api.dto.mongo.SaveDeviceInfoDTO;
|
|
|
|
+import com.pig4cloud.pig.marketing.api.dto.mongo.SaveTcpMessageDTO;
|
|
|
|
+import com.pig4cloud.pig.marketing.api.entity.mongo.Device;
|
|
|
|
+import com.pig4cloud.pig.marketing.api.entity.mongo.Message;
|
|
|
|
+import com.pig4cloud.pig.marketing.repository.DeviceRepository;
|
|
|
|
+import com.pig4cloud.pig.marketing.repository.MessageRepository;
|
|
|
|
+import com.pig4cloud.pig.marketing.service.TcpDataService;
|
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
|
+import org.springframework.data.domain.Sort;
|
|
|
|
+import org.springframework.data.mongodb.core.MongoTemplate;
|
|
|
|
+import org.springframework.data.mongodb.core.query.Criteria;
|
|
|
|
+import org.springframework.data.mongodb.core.query.Query;
|
|
|
|
+import org.springframework.data.mongodb.core.query.Update;
|
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
|
+
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.time.ZoneId;
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Objects;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @author: lwh
|
|
|
|
+ * @date: 2025-08-28
|
|
|
|
+ * @description: TCP数据服务实现类
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+@Slf4j
|
|
|
|
+@Service
|
|
|
|
+@AllArgsConstructor
|
|
|
|
+public class TcpDataServiceImpl implements TcpDataService {
|
|
|
|
+
|
|
|
|
+ private final MongoTemplate mongoTemplate;
|
|
|
|
+
|
|
|
|
+ private final MessageRepository messageRepository;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 保存或更新设备信息
|
|
|
|
+ * @param reqDto
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public String saveOrUpdateDevice(SaveDeviceInfoDTO reqDto) {
|
|
|
|
+ // 根据客户端ID查询设备信息
|
|
|
|
+ Query query = new Query(Criteria.where("clientID").is(reqDto.getClientID()));
|
|
|
|
+ Device existingDevice = mongoTemplate.findOne(query, Device.class);
|
|
|
|
+
|
|
|
|
+ if (existingDevice == null) {
|
|
|
|
+ // 无数据时直接新增
|
|
|
|
+ Device device = new Device();
|
|
|
|
+ device.setClientID(reqDto.getClientID());
|
|
|
|
+ device.setDeviceInfo(reqDto.getDeviceInfo());
|
|
|
|
+ device.setCreateTime(LocalDateTime.now().withNano(0));
|
|
|
|
+ device.setUpdateTime(LocalDateTime.now().withNano(0));
|
|
|
|
+ Device insert = mongoTemplate.insert(device);
|
|
|
|
+ return insert.getId();
|
|
|
|
+ } else {
|
|
|
|
+ // 有数据时对比设备信息是否一致
|
|
|
|
+ if (!Objects.equals(existingDevice.getDeviceInfo(), reqDto.getDeviceInfo())) {
|
|
|
|
+ // 信息不一致则更新
|
|
|
|
+ Update update = new Update();
|
|
|
|
+ update.set("deviceInfo", reqDto.getDeviceInfo());
|
|
|
|
+ update.set("updateTime", LocalDateTime.now().withNano(0));
|
|
|
|
+ mongoTemplate.updateFirst(query, update, Device.class);
|
|
|
|
+ return existingDevice.getId();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return existingDevice.getId();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据客户端ID查询设备信息
|
|
|
|
+ * @param clientID 客户端ID
|
|
|
|
+ * @return 设备信息
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public Device getDeviceByClientID(String clientID) {
|
|
|
|
+ return mongoTemplate.findOne(
|
|
|
|
+ new Query(Criteria.where("clientID").is(clientID)),
|
|
|
|
+ Device.class
|
|
|
|
+ );
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 分页查询设备信息
|
|
|
|
+ * @param reqDto 入参
|
|
|
|
+ * @return 设备信息列表
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public Page<Device> pageDeviceInfo(PageDeviceInfoDTO reqDto) {
|
|
|
|
+ // 构建查询条件
|
|
|
|
+ Query query = new Query();
|
|
|
|
+
|
|
|
|
+ // 模糊匹配clientID
|
|
|
|
+ if (reqDto.getClientID() != null && !reqDto.getClientID().isEmpty()) {
|
|
|
|
+ query.addCriteria(Criteria.where("clientID").regex(reqDto.getClientID(), "i"));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 时间范围筛选
|
|
|
|
+ if (reqDto.getStartTime() != null || reqDto.getEndTime() != null) {
|
|
|
|
+ Criteria timeCriteria = Criteria.where("createTime");
|
|
|
|
+ if (reqDto.getStartTime() != null) {
|
|
|
|
+ Date startDate = Date.from(reqDto.getStartTime().atZone(ZoneId.systemDefault()).toInstant());
|
|
|
|
+ timeCriteria.gte(startDate);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (reqDto.getEndTime() != null) {
|
|
|
|
+ Date endDate = Date.from(reqDto.getEndTime().atZone(ZoneId.systemDefault()).toInstant());
|
|
|
|
+ timeCriteria.lte(endDate);
|
|
|
|
+ }
|
|
|
|
+ query.addCriteria(timeCriteria);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ // 构建分页条件
|
|
|
|
+ Pageable pageable = PageRequest.of(
|
|
|
|
+ reqDto.getCurrent() - 1,
|
|
|
|
+ reqDto.getSize(),
|
|
|
|
+ Sort.by(Sort.Direction.DESC, "createTime"));
|
|
|
|
+ query.with(pageable);
|
|
|
|
+
|
|
|
|
+ // 执行查询
|
|
|
|
+ List<Device> devices = mongoTemplate.find(query, Device.class, "devices");
|
|
|
|
+ long total = mongoTemplate.count(query.skip(0).limit(0), "devices");
|
|
|
|
+
|
|
|
|
+ Page<Device> page = new Page<>(reqDto.getCurrent(), reqDto.getSize(), total);
|
|
|
|
+ page.setRecords(devices);
|
|
|
|
+ page.setTotal( total);
|
|
|
|
+ // 封装分页结果
|
|
|
|
+ return page;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /******************************************* 消息 *******************************************
|
|
|
|
+ * 保存上报数据
|
|
|
|
+ * @param reqDto 入参
|
|
|
|
+ * @return 数据ID
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public String saveMessage(SaveTcpMessageDTO reqDto) {
|
|
|
|
+ Message message = new Message();
|
|
|
|
+ message.setClientID(reqDto.getClientID());
|
|
|
|
+ message.setMsgData(reqDto.getMsgData());
|
|
|
|
+ message.setReportTime(LocalDateTime.now());
|
|
|
|
+ Message save = messageRepository.save(message);
|
|
|
|
+ return save.getId();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据ID查询数据详情
|
|
|
|
+ * @param id 数据ID
|
|
|
|
+ * @return 数据详情
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public Message getMessagesById(String id) {
|
|
|
|
+ return messageRepository.findById(id)
|
|
|
|
+ .orElse(null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 分页查询数据列表
|
|
|
|
+ * @param reqDto 入参
|
|
|
|
+ * @return 数据列表
|
|
|
|
+ */
|
|
|
|
+ @Override
|
|
|
|
+ public Page<Message> pageMsgInfo(PageDeviceInfoDTO reqDto) {
|
|
|
|
+ // 构建查询条件
|
|
|
|
+ Query query = new Query(Criteria.where("clientID").is(reqDto.getClientID()));
|
|
|
|
+
|
|
|
|
+ // 构建分页条件
|
|
|
|
+ Pageable pageable = PageRequest.of(
|
|
|
|
+ reqDto.getCurrent() - 1,
|
|
|
|
+ reqDto.getSize(),
|
|
|
|
+ Sort.by(Sort.Direction.DESC, "reportTime"));
|
|
|
|
+ query.with(pageable);
|
|
|
|
+
|
|
|
|
+ // 执行查询
|
|
|
|
+ List<Message> messages = mongoTemplate.find(query, Message.class, "messages");
|
|
|
|
+ long total = mongoTemplate.count(query.skip(0).limit(0), "messages");
|
|
|
|
+
|
|
|
|
+ Page<Message> page = new Page<>(reqDto.getCurrent(), reqDto.getSize(), total);
|
|
|
|
+ page.setRecords(messages);
|
|
|
|
+ page.setTotal( total);
|
|
|
|
+ // 封装分页结果
|
|
|
|
+ return page;
|
|
|
|
+ }
|
|
|
|
+}
|