From c92e2a59a6abc8292ef5ea21d5561aaddfaed6c7 Mon Sep 17 00:00:00 2001 From: rqian <1206436827@qq.com> Date: Fri, 8 May 2026 13:37:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=8E=A5=E5=8F=A3system/devi?= =?UTF-8?q?cemodel/list=E5=88=86=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/system/SysDeviceModelController.java | 13 ++++++++++--- .../com/maibu/service/SysDeviceModelService.java | 10 ++++++---- .../service/impl/SysDeviceModelServiceImpl.java | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/maibu-admin/src/main/java/com/maibu/web/controller/system/SysDeviceModelController.java b/maibu-admin/src/main/java/com/maibu/web/controller/system/SysDeviceModelController.java index 7d43211..bb548bf 100644 --- a/maibu-admin/src/main/java/com/maibu/web/controller/system/SysDeviceModelController.java +++ b/maibu-admin/src/main/java/com/maibu/web/controller/system/SysDeviceModelController.java @@ -1,5 +1,7 @@ package com.maibu.web.controller.system; +import cn.hutool.core.convert.Convert; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.maibu.annotation.Log; import com.maibu.core.business.Device; import com.maibu.core.business.DeviceModel;import com.maibu.core.business.DeviceRunStatistics; @@ -11,6 +13,7 @@ import com.maibu.mapper.DeviceRunStatisticsMapper; import com.maibu.mybatis.LambdaQueryWrapperX; import com.maibu.service.IDeviceService; import com.maibu.service.SysDeviceModelService; +import com.maibu.utils.ServletUtils; import com.maibu.utils.StringUtils; import com.maibu.utils.poi.ExcelUtil; import io.swagger.annotations.Api; @@ -73,9 +76,13 @@ public class SysDeviceModelController extends BaseController { @GetMapping("/list") @ApiOperation("查询设备模型列表") public TableDataInfo list(DeviceModel deviceModel) { - startPage(); - List list = deviceModelService.selectDeviceModelList(deviceModel); - return getDataTable(list); + Integer pageNum = Convert.toInt(ServletUtils.getParameter("pageNum"), 1); + Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10); + + Page page = new Page<>(pageNum, pageSize); + Page resultPage = deviceModelService.selectDeviceModelPage(page, deviceModel); + + return getDataTable(resultPage.getRecords(), resultPage.getTotal()); } diff --git a/maibu-service/maibu-system-service/src/main/java/com/maibu/service/SysDeviceModelService.java b/maibu-service/maibu-system-service/src/main/java/com/maibu/service/SysDeviceModelService.java index 0e64110..9cb6383 100644 --- a/maibu-service/maibu-system-service/src/main/java/com/maibu/service/SysDeviceModelService.java +++ b/maibu-service/maibu-system-service/src/main/java/com/maibu/service/SysDeviceModelService.java @@ -1,5 +1,6 @@ package com.maibu.service; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.maibu.core.business.DeviceModel; import java.util.List; @@ -18,11 +19,12 @@ public interface SysDeviceModelService { int insertDeviceModel(DeviceModel deviceModel); /** - *查询设备模型列表 - * @param deviceModel - * @return + * 分页查询设备模型列表 + * @param page 分页对象 + * @param deviceModel 查询条件 + * @return 分页结果 */ - List selectDeviceModelList(DeviceModel deviceModel); + Page selectDeviceModelPage(Page page, DeviceModel deviceModel); /** * selectDeviceModelById diff --git a/maibu-service/maibu-system-service/src/main/java/com/maibu/service/impl/SysDeviceModelServiceImpl.java b/maibu-service/maibu-system-service/src/main/java/com/maibu/service/impl/SysDeviceModelServiceImpl.java index 702b572..e33107f 100644 --- a/maibu-service/maibu-system-service/src/main/java/com/maibu/service/impl/SysDeviceModelServiceImpl.java +++ b/maibu-service/maibu-system-service/src/main/java/com/maibu/service/impl/SysDeviceModelServiceImpl.java @@ -1 +1 @@ -package com.maibu.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.maibu.core.business.DeviceAction; import com.maibu.core.business.DeviceModel; import com.maibu.core.business.DeviceModelProperty; import com.maibu.exception.ServiceException; import com.maibu.mapper.SysDeviceActionMapper; import com.maibu.mapper.SysDeviceModelMapper; import com.maibu.mapper.SysDevicePropertyMapper; import com.maibu.service.SysDeviceModelService; import com.maibu.service.SysDevicePropertyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.time.LocalDateTime; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; /** * @author jsmbz * @description 针对表【iot_device_model(设备模型表)】的数据库操作Service实现 * @createDate 2026-05-07 11:36:11 */ @Service public class SysDeviceModelServiceImpl extends ServiceImpl implements SysDeviceModelService { @Autowired private SysDeviceModelMapper deviceModelMapper; @Autowired private SysDeviceActionMapper actionMapper; @Autowired private SysDevicePropertyMapper propertyMapper; @Override @Transactional(rollbackFor = Exception.class) // ⭐ 关键 public int insertDeviceModel(DeviceModel deviceModel) { // 1. 插入模型主表 deviceModel.setCreateTime(LocalDateTime.now()); deviceModel.setDelFlag(false); int rows = deviceModelMapper.insert(deviceModel); if (rows <= 0) { throw new ServiceException("模型创建失败"); } Long modelId = deviceModel.getId(); // 2. 插入属性列表 if (!CollectionUtils.isEmpty(deviceModel.getSupportProperties())) { LocalDateTime now = LocalDateTime.now(); for (DeviceModelProperty prop : deviceModel.getSupportProperties()) { prop.setModelId(modelId); prop.setCreateTime(now); prop.setDelFlag(false); propertyMapper.insert(prop); } } // 3. 插入动作列表 if (!CollectionUtils.isEmpty(deviceModel.getSupportActions())) { LocalDateTime now = LocalDateTime.now(); for (DeviceAction action : deviceModel.getSupportActions()) { action.setModelId(modelId); action.setCreateTime(now); action.setDelFlag(false); actionMapper.insert(action); } } return 1; } @Override public List selectDeviceModelList(DeviceModel deviceModel) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); // 模糊查询名称 if (deviceModel.getName() != null && !deviceModel.getName().isEmpty()) { wrapper.like(DeviceModel::getName, deviceModel.getName()); } // 精确查询连接类型 if (deviceModel.getConnectType() != null) { wrapper.eq(DeviceModel::getConnectType, deviceModel.getConnectType()); } // 精确查询设备类型 if (deviceModel.getDeviceType() != null) { wrapper.eq(DeviceModel::getDeviceType, deviceModel.getDeviceType()); } // 只查询未删除的 wrapper.eq(DeviceModel::getDelFlag, false); // 按创建时间倒序 wrapper.orderByDesc(DeviceModel::getCreateTime); // 查询列表 List list = deviceModelMapper.selectList(wrapper); // 加载每个模型的属性和动作 if (!CollectionUtils.isEmpty(list)) { list.forEach(model -> { // 加载属性列表 List properties = propertyMapper.selectList( new LambdaQueryWrapper() .eq(DeviceModelProperty::getModelId, model.getId()) .eq(DeviceModelProperty::getDelFlag, false) .orderByAsc(DeviceModelProperty::getId) ); model.setSupportProperties(properties); // 加载动作列表 List actions = actionMapper.selectList( new LambdaQueryWrapper() .eq(DeviceAction::getModelId, model.getId()) .eq(DeviceAction::getDelFlag, false) .orderByAsc(DeviceAction::getId) ); model.setSupportActions(actions); }); } return list; } @Override public DeviceModel selectDeviceModelById(Long id) { // 1. 查主表 DeviceModel model = deviceModelMapper.selectById(id); if (model == null) return null; // 2. 查属性 List properties = propertyMapper.selectList( new LambdaQueryWrapper() .eq(DeviceModelProperty::getModelId, id) .eq(DeviceModelProperty::getDelFlag, false) ); model.setSupportProperties(properties); // 3. 查动作 List actions = actionMapper.selectList( new LambdaQueryWrapper() .eq(DeviceAction::getModelId, id) .eq(DeviceAction::getDelFlag, false) ); model.setSupportActions(actions); return model; } @Override @Transactional(rollbackFor = Exception.class) public int updateDeviceModel(DeviceModel deviceModel) { // 1. 更新主表 deviceModel.setUpdateTime(LocalDateTime.now()); deviceModelMapper.updateById(deviceModel); Long modelId = deviceModel.getId(); // 2. 处理属性列表 (智能更新) if (deviceModel.getSupportProperties() != null) { // 2.1 遍历前端传来的列表:有ID则更新,无ID则新增 for (DeviceModelProperty prop : deviceModel.getSupportProperties()) { if (prop.getId() != null && prop.getId() > 0) { prop.setUpdateTime(LocalDateTime.now()); propertyMapper.updateById(prop); } else { prop.setModelId(modelId); prop.setCreateTime(LocalDateTime.now()); propertyMapper.insert(prop); } } // 2.2 删除前端没传的项 (通过 NOT IN) List receivedIds = deviceModel.getSupportProperties().stream() .map(DeviceModelProperty::getId) .filter(id -> id != null && id > 0) .collect(Collectors.toList()); if (!receivedIds.isEmpty()) { propertyMapper.delete( new LambdaQueryWrapper() .eq(DeviceModelProperty::getModelId, modelId) .notIn(DeviceModelProperty::getId, receivedIds) ); } else { // 如果传了空列表,说明要删除所有属性 propertyMapper.delete( new LambdaQueryWrapper() .eq(DeviceModelProperty::getModelId, modelId) ); } } // 3. 处理动作列表 (逻辑同上) if (deviceModel.getSupportActions() != null) { for (DeviceAction action : deviceModel.getSupportActions()) { if (action.getId() != null && action.getId() > 0) { action.setUpdateTime(LocalDateTime.now()); actionMapper.updateById(action); } else { action.setModelId(modelId); action.setCreateTime(LocalDateTime.now()); actionMapper.insert(action); } } List receivedActionIds = deviceModel.getSupportActions().stream() .map(DeviceAction::getId) .filter(id -> id != null && id > 0) .collect(Collectors.toList()); if (!receivedActionIds.isEmpty()) { actionMapper.delete( new LambdaQueryWrapper() .eq(DeviceAction::getModelId, modelId) .notIn(DeviceAction::getId, receivedActionIds) ); } else { actionMapper.delete( new LambdaQueryWrapper() .eq(DeviceAction::getModelId, modelId) ); } } return 1; } } \ No newline at end of file +package com.maibu.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.maibu.core.business.DeviceAction; import com.maibu.core.business.DeviceModel; import com.maibu.core.business.DeviceModelProperty; import com.maibu.exception.ServiceException; import com.maibu.mapper.SysDeviceActionMapper; import com.maibu.mapper.SysDeviceModelMapper; import com.maibu.mapper.SysDevicePropertyMapper; import com.maibu.service.SysDeviceModelService; import com.maibu.service.SysDevicePropertyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.time.LocalDateTime; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; /** * @author jsmbz * @description 针对表【iot_device_model(设备模型表)】的数据库操作Service实现 * @createDate 2026-05-07 11:36:11 */ @Service public class SysDeviceModelServiceImpl extends ServiceImpl implements SysDeviceModelService { @Autowired private SysDeviceModelMapper deviceModelMapper; @Autowired private SysDeviceActionMapper actionMapper; @Autowired private SysDevicePropertyMapper propertyMapper; @Override @Transactional(rollbackFor = Exception.class) // ⭐ 关键 public int insertDeviceModel(DeviceModel deviceModel) { // 1. 插入模型主表 deviceModel.setCreateTime(LocalDateTime.now()); deviceModel.setDelFlag(false); int rows = deviceModelMapper.insert(deviceModel); if (rows <= 0) { throw new ServiceException("模型创建失败"); } Long modelId = deviceModel.getId(); // 2. 插入属性列表 if (!CollectionUtils.isEmpty(deviceModel.getSupportProperties())) { LocalDateTime now = LocalDateTime.now(); for (DeviceModelProperty prop : deviceModel.getSupportProperties()) { prop.setModelId(modelId); prop.setCreateTime(now); prop.setDelFlag(false); propertyMapper.insert(prop); } } // 3. 插入动作列表 if (!CollectionUtils.isEmpty(deviceModel.getSupportActions())) { LocalDateTime now = LocalDateTime.now(); for (DeviceAction action : deviceModel.getSupportActions()) { action.setModelId(modelId); action.setCreateTime(now); action.setDelFlag(false); actionMapper.insert(action); } } return 1; } @Override public Page selectDeviceModelPage(Page page, DeviceModel deviceModel) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); if (deviceModel.getName() != null && !deviceModel.getName().isEmpty()) { wrapper.like(DeviceModel::getName, deviceModel.getName()); } if (deviceModel.getConnectType() != null) { wrapper.eq(DeviceModel::getConnectType, deviceModel.getConnectType()); } if (deviceModel.getDeviceType() != null) { wrapper.eq(DeviceModel::getDeviceType, deviceModel.getDeviceType()); } wrapper.eq(DeviceModel::getDelFlag, false); wrapper.orderByDesc(DeviceModel::getCreateTime); Page resultPage = deviceModelMapper.selectPage(page, wrapper); if (!CollectionUtils.isEmpty(resultPage.getRecords())) { resultPage.getRecords().forEach(model -> { List properties = propertyMapper.selectList( new LambdaQueryWrapper() .eq(DeviceModelProperty::getModelId, model.getId()) .eq(DeviceModelProperty::getDelFlag, false) .orderByAsc(DeviceModelProperty::getId) ); model.setSupportProperties(properties); List actions = actionMapper.selectList( new LambdaQueryWrapper() .eq(DeviceAction::getModelId, model.getId()) .eq(DeviceAction::getDelFlag, false) .orderByAsc(DeviceAction::getId) ); model.setSupportActions(actions); }); } return resultPage; } @Override public DeviceModel selectDeviceModelById(Long id) { // 1. 查主表 DeviceModel model = deviceModelMapper.selectById(id); if (model == null) return null; // 2. 查属性 List properties = propertyMapper.selectList( new LambdaQueryWrapper() .eq(DeviceModelProperty::getModelId, id) .eq(DeviceModelProperty::getDelFlag, false) ); model.setSupportProperties(properties); // 3. 查动作 List actions = actionMapper.selectList( new LambdaQueryWrapper() .eq(DeviceAction::getModelId, id) .eq(DeviceAction::getDelFlag, false) ); model.setSupportActions(actions); return model; } @Override @Transactional(rollbackFor = Exception.class) public int updateDeviceModel(DeviceModel deviceModel) { // 1. 更新主表 deviceModel.setUpdateTime(LocalDateTime.now()); deviceModelMapper.updateById(deviceModel); Long modelId = deviceModel.getId(); // 2. 处理属性列表 (智能更新) if (deviceModel.getSupportProperties() != null) { // 2.1 遍历前端传来的列表:有ID则更新,无ID则新增 for (DeviceModelProperty prop : deviceModel.getSupportProperties()) { if (prop.getId() != null && prop.getId() > 0) { prop.setUpdateTime(LocalDateTime.now()); propertyMapper.updateById(prop); } else { prop.setModelId(modelId); prop.setCreateTime(LocalDateTime.now()); propertyMapper.insert(prop); } } // 2.2 删除前端没传的项 (通过 NOT IN) List receivedIds = deviceModel.getSupportProperties().stream() .map(DeviceModelProperty::getId) .filter(id -> id != null && id > 0) .collect(Collectors.toList()); if (!receivedIds.isEmpty()) { propertyMapper.delete( new LambdaQueryWrapper() .eq(DeviceModelProperty::getModelId, modelId) .notIn(DeviceModelProperty::getId, receivedIds) ); } else { // 如果传了空列表,说明要删除所有属性 propertyMapper.delete( new LambdaQueryWrapper() .eq(DeviceModelProperty::getModelId, modelId) ); } } // 3. 处理动作列表 (逻辑同上) if (deviceModel.getSupportActions() != null) { for (DeviceAction action : deviceModel.getSupportActions()) { if (action.getId() != null && action.getId() > 0) { action.setUpdateTime(LocalDateTime.now()); actionMapper.updateById(action); } else { action.setModelId(modelId); action.setCreateTime(LocalDateTime.now()); actionMapper.insert(action); } } List receivedActionIds = deviceModel.getSupportActions().stream() .map(DeviceAction::getId) .filter(id -> id != null && id > 0) .collect(Collectors.toList()); if (!receivedActionIds.isEmpty()) { actionMapper.delete( new LambdaQueryWrapper() .eq(DeviceAction::getModelId, modelId) .notIn(DeviceAction::getId, receivedActionIds) ); } else { actionMapper.delete( new LambdaQueryWrapper() .eq(DeviceAction::getModelId, modelId) ); } } return 1; } } \ No newline at end of file