|
@@ -3,11 +3,11 @@ 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.PageMessageDTO;
|
|
|
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;
|
|
@@ -22,9 +22,7 @@ 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;
|
|
|
|
|
@@ -43,6 +41,8 @@ public class TcpDataServiceImpl implements TcpDataService {
|
|
|
|
|
|
private final MessageRepository messageRepository;
|
|
|
|
|
|
+ // 定义时间格式器
|
|
|
+ private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
/**
|
|
|
* 保存或更新设备信息
|
|
@@ -60,8 +60,10 @@ public class TcpDataServiceImpl implements TcpDataService {
|
|
|
Device device = new Device();
|
|
|
device.setClientID(reqDto.getClientID());
|
|
|
device.setDeviceInfo(reqDto.getDeviceInfo());
|
|
|
- device.setCreateTime(LocalDateTime.now().withNano(0));
|
|
|
- device.setUpdateTime(LocalDateTime.now().withNano(0));
|
|
|
+ // 设置创建时间和更新时间为当前时间的字符串格式
|
|
|
+ String currentTime = LocalDateTime.now().format(DATE_TIME_FORMATTER);
|
|
|
+ device.setCreateTime(currentTime);
|
|
|
+ device.setUpdateTime(currentTime);
|
|
|
Device insert = mongoTemplate.insert(device);
|
|
|
return insert.getId();
|
|
|
} else {
|
|
@@ -70,7 +72,10 @@ public class TcpDataServiceImpl implements TcpDataService {
|
|
|
// 信息不一致则更新
|
|
|
Update update = new Update();
|
|
|
update.set("deviceInfo", reqDto.getDeviceInfo());
|
|
|
- update.set("updateTime", LocalDateTime.now().withNano(0));
|
|
|
+
|
|
|
+ // 设置更新时间为当前时间的字符串格式
|
|
|
+ update.set("updateTime", LocalDateTime.now().format(DATE_TIME_FORMATTER));
|
|
|
+
|
|
|
mongoTemplate.updateFirst(query, update, Device.class);
|
|
|
return existingDevice.getId();
|
|
|
}
|
|
@@ -98,45 +103,56 @@ public class TcpDataServiceImpl implements TcpDataService {
|
|
|
*/
|
|
|
@Override
|
|
|
public Page<Device> pageDeviceInfo(PageDeviceInfoDTO reqDto) {
|
|
|
- // 构建查询条件
|
|
|
- Query query = new Query();
|
|
|
+ // 创建查询条件
|
|
|
+ Criteria criteria = new Criteria();
|
|
|
|
|
|
- // 模糊匹配clientID
|
|
|
- if (reqDto.getClientID() != null && !reqDto.getClientID().isEmpty()) {
|
|
|
- query.addCriteria(Criteria.where("clientID").regex(reqDto.getClientID(), "i"));
|
|
|
+ // 添加clientID条件
|
|
|
+ if (reqDto.getClientID() != null && !reqDto.getClientID().trim().isEmpty()) {
|
|
|
+ criteria.and("clientID").regex(reqDto.getClientID());
|
|
|
}
|
|
|
|
|
|
- // 时间范围筛选
|
|
|
- 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);
|
|
|
- }
|
|
|
+ // 添加时间范围条件
|
|
|
+ LocalDateTime startTime = reqDto.getStartTime();
|
|
|
+ LocalDateTime endTime = reqDto.getEndTime();
|
|
|
|
|
|
- if (reqDto.getEndTime() != null) {
|
|
|
- Date endDate = Date.from(reqDto.getEndTime().atZone(ZoneId.systemDefault()).toInstant());
|
|
|
- timeCriteria.lte(endDate);
|
|
|
- }
|
|
|
- query.addCriteria(timeCriteria);
|
|
|
+ // 创建时间字段的条件对象
|
|
|
+ Criteria timeCriteria = new Criteria("createTime");
|
|
|
+
|
|
|
+ if (startTime != null) {
|
|
|
+ String startStr = startTime.format(DATE_TIME_FORMATTER);
|
|
|
+ timeCriteria.gte(startStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (endTime != null) {
|
|
|
+ String endStr = endTime.format(DATE_TIME_FORMATTER);
|
|
|
+ timeCriteria.lte(endStr);
|
|
|
}
|
|
|
|
|
|
+ // 只有当存在时间条件时,才将时间条件添加到主条件中
|
|
|
+ if (startTime != null || endTime != null) {
|
|
|
+ criteria.andOperator(timeCriteria);
|
|
|
+ }
|
|
|
|
|
|
- // 构建分页条件
|
|
|
+ // 创建查询对象
|
|
|
+ Query query = new Query(criteria);
|
|
|
+
|
|
|
+ // 计算总记录数
|
|
|
+ long total = mongoTemplate.count(query, Device.class);
|
|
|
+
|
|
|
+ // 设置分页和排序
|
|
|
Pageable pageable = PageRequest.of(
|
|
|
- reqDto.getCurrent() - 1,
|
|
|
+ reqDto.getCurrent() - 1, // MongoDB页码从0开始
|
|
|
reqDto.getSize(),
|
|
|
- Sort.by(Sort.Direction.DESC, "createTime"));
|
|
|
+ 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");
|
|
|
+ List<Device> devices = mongoTemplate.find(query, Device.class);
|
|
|
|
|
|
+ // 封装分页结果
|
|
|
Page<Device> page = new Page<>(reqDto.getCurrent(), reqDto.getSize(), total);
|
|
|
page.setRecords(devices);
|
|
|
- page.setTotal( total);
|
|
|
- // 封装分页结果
|
|
|
return page;
|
|
|
}
|
|
|
|
|
@@ -150,7 +166,7 @@ public class TcpDataServiceImpl implements TcpDataService {
|
|
|
Message message = new Message();
|
|
|
message.setClientID(reqDto.getClientID());
|
|
|
message.setMsgData(reqDto.getMsgData());
|
|
|
- message.setReportTime(LocalDateTime.now());
|
|
|
+ message.setReportTime(LocalDateTime.now().format(DATE_TIME_FORMATTER));
|
|
|
Message save = messageRepository.save(message);
|
|
|
return save.getId();
|
|
|
}
|
|
@@ -172,25 +188,59 @@ public class TcpDataServiceImpl implements TcpDataService {
|
|
|
* @return 数据列表
|
|
|
*/
|
|
|
@Override
|
|
|
- public Page<Message> pageMsgInfo(PageDeviceInfoDTO reqDto) {
|
|
|
- // 构建查询条件
|
|
|
- Query query = new Query(Criteria.where("clientID").is(reqDto.getClientID()));
|
|
|
+ public Page<Message> pageMsgInfo(PageMessageDTO reqDto) {
|
|
|
+ // 创建查询条件
|
|
|
+ Criteria criteria = new Criteria();
|
|
|
+
|
|
|
+ // 添加clientID条件
|
|
|
+ criteria.and("clientID").is(reqDto.getClientID());
|
|
|
|
|
|
- // 构建分页条件
|
|
|
+ if (reqDto.getKeyWord() != null && !reqDto.getKeyWord().isEmpty()){
|
|
|
+ criteria.and("msgData").regex(reqDto.getKeyWord());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加时间范围条件
|
|
|
+ LocalDateTime startTime = reqDto.getStartTime();
|
|
|
+ LocalDateTime endTime = reqDto.getEndTime();
|
|
|
+
|
|
|
+ // 创建时间字段的条件对象
|
|
|
+ Criteria timeCriteria = new Criteria("reportTime");
|
|
|
+
|
|
|
+ if (startTime != null) {
|
|
|
+ String startStr = startTime.format(DATE_TIME_FORMATTER);
|
|
|
+ timeCriteria.gte(startStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (endTime != null) {
|
|
|
+ String endStr = endTime.format(DATE_TIME_FORMATTER);
|
|
|
+ timeCriteria.lte(endStr);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 只有当存在时间条件时,才将时间条件添加到主条件中
|
|
|
+ if (startTime != null || endTime != null) {
|
|
|
+ criteria.andOperator(timeCriteria);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 创建查询对象
|
|
|
+ Query query = new Query(criteria);
|
|
|
+
|
|
|
+ // 计算总记录数
|
|
|
+ long total = mongoTemplate.count(query, Message.class);
|
|
|
+
|
|
|
+ // 设置分页和排序
|
|
|
Pageable pageable = PageRequest.of(
|
|
|
- reqDto.getCurrent() - 1,
|
|
|
+ reqDto.getCurrent() - 1, // MongoDB页码从0开始
|
|
|
reqDto.getSize(),
|
|
|
- Sort.by(Sort.Direction.DESC, "reportTime"));
|
|
|
+ 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");
|
|
|
+ List<Message> messages = mongoTemplate.find(query, Message.class);
|
|
|
|
|
|
+ // 封装分页结果
|
|
|
Page<Message> page = new Page<>(reqDto.getCurrent(), reqDto.getSize(), total);
|
|
|
page.setRecords(messages);
|
|
|
- page.setTotal( total);
|
|
|
- // 封装分页结果
|
|
|
return page;
|
|
|
}
|
|
|
}
|