refactor(iot): 重构物联网模块代码

- 移除 DeviceController 中未使用的 RecordDataRepository- 修改 DeviceController 中 recordData 方法的 URL 路径
- 在 DeviceMapper.xml 中添加 selectList 方法查询设备信息
- 更新 DeviceRepository 接口,增加 selectList 方法
- 重构 DeviceServiceImpl 中的 query 方法,使用新增的 selectList 方法
- 更新 RecordData 实体类,将 content 字段的类型处理器改为 JacksonTypeHandler
- 修改 RecordDataMapper.xml,更新 recordData 方法的 SQL 查询
-
This commit is contained in:
zhuangtianxiang 2025-03-06 16:52:42 +08:00
parent 38f868cfd3
commit 741eb73137
10 changed files with 23 additions and 61 deletions

View File

@ -35,7 +35,6 @@ import java.util.List;
@RequestMapping("api/rest/device") @RequestMapping("api/rest/device")
public class DeviceController { public class DeviceController {
private final RecordDataRepository recordDataRepository;
DeviceService service; DeviceService service;
RecordDataService recordService; RecordDataService recordService;
@ -116,7 +115,7 @@ public class DeviceController {
/** /**
* 查询设备上报记录 * 查询设备上报记录
*/ */
@GetMapping("record/data/list") @GetMapping("record/data")
@PreAuthorize("hasAuthority('iot:device:query')") @PreAuthorize("hasAuthority('iot:device:query')")
public IPage<RecordData> recordData(Page<RecordData> page, String clientId) { public IPage<RecordData> recordData(Page<RecordData> page, String clientId) {
return recordService.query(page, clientId); return recordService.query(page, clientId);

View File

@ -1,7 +1,12 @@
package com.zsc.edu.gateway.modules.iot.device.repo; package com.zsc.edu.gateway.modules.iot.device.repo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.modules.iot.device.entity.Device; import com.zsc.edu.gateway.modules.iot.device.entity.Device;
import com.zsc.edu.gateway.modules.iot.device.query.DeviceQuery;
import com.zsc.edu.gateway.modules.iot.device.vo.DeviceVo; import com.zsc.edu.gateway.modules.iot.device.vo.DeviceVo;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
@ -20,4 +25,5 @@ public interface DeviceRepository extends BaseMapper<Device> {
Device findByClientIdAndStateAndOnline(@Param("clientId") String clientId, Device findByClientIdAndStateAndOnline(@Param("clientId") String clientId,
@Param("status") Device.Status status, @Param("status") Device.Status status,
@Param("online") Boolean online); @Param("online") Boolean online);
} }

View File

@ -81,15 +81,6 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
@Override @Override
@Transactional @Transactional
public List<Device> batchCreate(BatchDeviceDto dto) { public List<Device> batchCreate(BatchDeviceDto dto) {
if (dto.getNum() == null || dto.getNum() <= 0) {
throw new ConstraintException("设备数量必须大于0");
}
if (dto.getPrefix() == null) {
throw new ConstraintException("前缀不能为空!");
}
if (dto.getProductId() == null) {
throw new ConstraintException("产品ID不能为空");
}
// 获取产品信息 // 获取产品信息
Product product = productRepo.selectById(dto.getProductId()); Product product = productRepo.selectById(dto.getProductId());
if (product == null) { if (product == null) {
@ -183,23 +174,7 @@ public class DeviceServiceImpl extends ServiceImpl<DeviceRepository, Device> imp
*/ */
@Override @Override
public IPage<Device> query(Page<Device> page, DeviceQuery query) { public IPage<Device> query(Page<Device> page, DeviceQuery query) {
IPage<Device> devicePage = this.page(page, query.wrapper()); return deviceRepo.selectPage(page, query.wrapper());
List<Long> productIds = devicePage.getRecords().stream()
.map(Device::getProductId)
.filter(Objects::nonNull)
.distinct()
.collect(Collectors.toList());
LambdaQueryWrapper<Product> productQueryWrapper = new LambdaQueryWrapper<>();
productQueryWrapper.in(Product::getId, productIds);
List<Product> products = productRepo.selectList(productQueryWrapper);
Map<Long, String> productMap = products.stream()
.collect(Collectors.toMap(Product::getId, Product::getName));
devicePage.getRecords().forEach(device -> {
if (device.getProductId() != null) {
device.setProductName(productMap.get(device.getProductId()));
}
});
return devicePage;
} }
@Override @Override

View File

@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
//import com.zsc.edu.gateway.framework.json.MapJsonTypeHandler; //import com.zsc.edu.gateway.framework.json.MapJsonTypeHandler;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import com.zsc.edu.gateway.framework.json.JsonbTypeHandler; import com.zsc.edu.gateway.framework.json.JsonbTypeHandler;
import lombok.*; import lombok.*;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
@ -26,7 +27,7 @@ public class RecordData {
private String clientId; private String clientId;
@TableField(typeHandler = JsonbTypeHandler.class) @TableField(typeHandler = JacksonTypeHandler.class)
private Map<String, Object> content; private Map<String, Object> content;
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")

View File

@ -62,10 +62,8 @@ public class RecordDataServiceImpl extends ServiceImpl<RecordDataRepository, Rec
@Override @Override
public IPage<RecordData> query(Page<RecordData> page, String clientId) { public IPage<RecordData> query(Page<RecordData> page, String clientId) {
List<RecordData> recordDataList = recordDataRepository.selectByClientId(clientId); LambdaQueryWrapper<RecordData> queryWrapper = new LambdaQueryWrapper<>();
page.setRecords(recordDataList); queryWrapper.eq(RecordData::getClientId, clientId);
page.setTotal(recordDataList.size()); return recordDataRepository.selectPage(page, queryWrapper);
return page;
} }
} }

View File

@ -85,9 +85,4 @@ public class ServeController {
return serveService.detail(id); return serveService.detail(id);
} }
@GetMapping("/params/{id}")
@PreAuthorize("hasAnyAuthority('iot:serve:query')")
public JSONObject getServeParam(@PathVariable("id") Long id) {
return serveService.getServeParam(id);
}
} }

View File

@ -20,5 +20,4 @@ public interface ServeService extends IService<Serve> {
Boolean delete(Long id); Boolean delete(Long id);
JSONObject getServeParam(Long id);
} }

View File

@ -93,20 +93,4 @@ public class ServeServiceImpl extends ServiceImpl<ServeRepository, Serve> implem
} }
/**
* 获取服务的输入和输出参数并转换为JSON格式
*
* @param id 服务ID
* @return 包含输入和输出参数的JSON对象
*/
@Override
public JSONObject getServeParam(Long id) {
Serve serve = detail(id);
List<Param> inputs = serve.getInputs();
List<Param> outputs = serve.getOutputs();
JSONObject result = new JSONObject();
result.put("inputs", inputs);
result.put("outputs", outputs);
return result;
}
} }

View File

@ -63,4 +63,9 @@
where d.id = #{id} where d.id = #{id}
</select> </select>
<select id="selectList" resultType="com.zsc.edu.gateway.modules.iot.device.entity.Device">
SELECT d.*, p.name AS productName
FROM iot_device d
LEFT JOIN iot_product p ON d.product_id = p.id
</select>
</mapper> </mapper>

View File

@ -6,13 +6,13 @@
<resultMap id="BaseResultMap" type="com.zsc.edu.gateway.modules.iot.record.entity.RecordData"> <resultMap id="BaseResultMap" type="com.zsc.edu.gateway.modules.iot.record.entity.RecordData">
<id column="id" property="id"/> <id column="id" property="id"/>
<result column="client_id" property="clientId"/> <result column="client_id" property="clientId"/>
<result column="content" property="content" typeHandler="com.zsc.edu.gateway.framework.json.JsonbTypeHandler"/> <result column="content" property="content"
typeHandler="com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler"/>
<result column="record_time" property="recordTime"/> <result column="record_time" property="recordTime"/>
<result column="dept_id" property="deptId"/> <result column="dept_id" property="deptId"/>
</resultMap> </resultMap>
<select id="selectByClientId" resultMap="BaseResultMap"> <select id="selectList" resultMap="BaseResultMap">
select * select ira.*
from iot_record_data from iot_record_data ira
where client_id = #{clientId}
</select> </select>
</mapper> </mapper>