Commit b02e396aff2885e402f598d300ad923d559396fd
1 parent
34506581
【功能新增】IoT:设备管理界面增加批量删除功能
Showing
3 changed files
with
45 additions
and
2 deletions
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/controller/admin/device/IotDeviceController.java
| ... | ... | @@ -23,6 +23,7 @@ import org.springframework.validation.annotation.Validated; |
| 23 | 23 | import org.springframework.web.bind.annotation.*; |
| 24 | 24 | |
| 25 | 25 | import java.io.IOException; |
| 26 | +import java.util.Collection; | |
| 26 | 27 | import java.util.List; |
| 27 | 28 | |
| 28 | 29 | import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT; |
| ... | ... | @@ -62,7 +63,7 @@ public class IotDeviceController { |
| 62 | 63 | } |
| 63 | 64 | |
| 64 | 65 | @DeleteMapping("/delete") |
| 65 | - @Operation(summary = "删除设备") | |
| 66 | + @Operation(summary = "删除单个设备") | |
| 66 | 67 | @Parameter(name = "id", description = "编号", required = true) |
| 67 | 68 | @PreAuthorize("@ss.hasPermission('iot:device:delete')") |
| 68 | 69 | public CommonResult<Boolean> deleteDevice(@RequestParam("id") Long id) { |
| ... | ... | @@ -70,6 +71,15 @@ public class IotDeviceController { |
| 70 | 71 | return success(true); |
| 71 | 72 | } |
| 72 | 73 | |
| 74 | + @DeleteMapping("/delete-list") | |
| 75 | + @Operation(summary = "删除多个设备") | |
| 76 | + @Parameter(name = "ids", description = "编号数组", required = true) | |
| 77 | + @PreAuthorize("@ss.hasPermission('iot:device:delete')") | |
| 78 | + public CommonResult<Boolean> deleteDeviceList(@RequestParam("ids") Collection<Long> ids) { | |
| 79 | + deviceService.deleteDeviceList(ids); | |
| 80 | + return success(true); | |
| 81 | + } | |
| 82 | + | |
| 73 | 83 | @GetMapping("/get") |
| 74 | 84 | @Operation(summary = "获得设备") |
| 75 | 85 | @Parameter(name = "id", description = "编号", required = true, example = "1024") | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceService.java
| ... | ... | @@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.iot.dal.dataobject.device.IotDeviceDO; |
| 8 | 8 | import jakarta.validation.Valid; |
| 9 | 9 | |
| 10 | 10 | import javax.annotation.Nullable; |
| 11 | +import java.util.Collection; | |
| 11 | 12 | import java.util.List; |
| 12 | 13 | |
| 13 | 14 | /** |
| ... | ... | @@ -33,13 +34,20 @@ public interface IotDeviceService { |
| 33 | 34 | void updateDevice(@Valid IotDeviceSaveReqVO updateReqVO); |
| 34 | 35 | |
| 35 | 36 | /** |
| 36 | - * 删除设备 | |
| 37 | + * 删除单个设备 | |
| 37 | 38 | * |
| 38 | 39 | * @param id 编号 |
| 39 | 40 | */ |
| 40 | 41 | void deleteDevice(Long id); |
| 41 | 42 | |
| 42 | 43 | /** |
| 44 | + * 删除多个设备 | |
| 45 | + * | |
| 46 | + * @param ids 编号数组 | |
| 47 | + */ | |
| 48 | + void deleteDeviceList(Collection<Long> ids); | |
| 49 | + | |
| 50 | + /** | |
| 43 | 51 | * 获得设备 |
| 44 | 52 | * |
| 45 | 53 | * @param id 编号 | ... | ... |
yudao-module-iot/yudao-module-iot-biz/src/main/java/cn/iocoder/yudao/module/iot/service/device/IotDeviceServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.iot.service.device; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.collection.CollUtil; | |
| 3 | 4 | import cn.hutool.core.util.IdUtil; |
| 4 | 5 | import cn.hutool.core.util.RandomUtil; |
| 5 | 6 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| ... | ... | @@ -18,10 +19,12 @@ import jakarta.annotation.Resource; |
| 18 | 19 | import lombok.extern.slf4j.Slf4j; |
| 19 | 20 | import org.springframework.context.annotation.Lazy; |
| 20 | 21 | import org.springframework.stereotype.Service; |
| 22 | +import org.springframework.transaction.annotation.Transactional; | |
| 21 | 23 | import org.springframework.validation.annotation.Validated; |
| 22 | 24 | |
| 23 | 25 | import javax.annotation.Nullable; |
| 24 | 26 | import java.time.LocalDateTime; |
| 27 | +import java.util.Collection; | |
| 25 | 28 | import java.util.List; |
| 26 | 29 | import java.util.Objects; |
| 27 | 30 | |
| ... | ... | @@ -117,6 +120,28 @@ public class IotDeviceServiceImpl implements IotDeviceService { |
| 117 | 120 | deviceMapper.deleteById(id); |
| 118 | 121 | } |
| 119 | 122 | |
| 123 | + @Override | |
| 124 | + @Transactional(rollbackFor = Exception.class) | |
| 125 | + public void deleteDeviceList(Collection<Long> ids) { | |
| 126 | + // 1.1 校验存在 | |
| 127 | + if (CollUtil.isEmpty(ids)) { | |
| 128 | + return; | |
| 129 | + } | |
| 130 | + List<IotDeviceDO> devices = deviceMapper.selectBatchIds(ids); | |
| 131 | + if (CollUtil.isEmpty(devices)) { | |
| 132 | + return; | |
| 133 | + } | |
| 134 | + // 1.2 校验网关设备是否存在 | |
| 135 | + for (IotDeviceDO device : devices) { | |
| 136 | + if (device.getGatewayId() != null && deviceMapper.selectCountByGatewayId(device.getId()) > 0) { | |
| 137 | + throw exception(DEVICE_HAS_CHILDREN); | |
| 138 | + } | |
| 139 | + } | |
| 140 | + | |
| 141 | + // 2. 删除设备 | |
| 142 | + deviceMapper.deleteByIds(ids); | |
| 143 | + } | |
| 144 | + | |
| 120 | 145 | /** |
| 121 | 146 | * 校验设备是否存在 |
| 122 | 147 | * | ... | ... |