From b2524e67b4ea987c2a13424d46c2f363bed95f78 Mon Sep 17 00:00:00 2001 From: gaoyoulong <774597026@qq.com> Date: Fri, 8 Mar 2024 11:20:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=AE=BE=E5=A4=87=E5=88=86=E7=BB=84?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../manager/controller/DeviceController.java | 30 +++++- .../vo/devicegroup/DeviceGroupImportVo.java | 27 +++++ .../listener/DeviceGroupImportListener.java | 98 +++++++++++++++++++ .../service/IDeviceManagerService.java | 5 + .../impl/DeviceManagerServiceImpl.java | 22 +++++ 5 files changed, 178 insertions(+), 4 deletions(-) create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/devicegroup/DeviceGroupImportVo.java create mode 100644 iot-module/iot-manager/src/main/java/cc/iotkit/manager/listener/DeviceGroupImportListener.java diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/DeviceController.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/DeviceController.java index 43546c06..d4498589 100644 --- a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/DeviceController.java +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/controller/DeviceController.java @@ -12,6 +12,7 @@ package cc.iotkit.manager.controller; import cc.iotkit.common.api.PageRequest; import cc.iotkit.common.api.Paging; import cc.iotkit.common.api.Request; +import cc.iotkit.common.excel.utils.ExcelUtil; import cc.iotkit.common.thing.ThingModelMessage; import cc.iotkit.manager.dto.bo.device.*; import cc.iotkit.manager.dto.bo.deviceconfig.DeviceConfigAddBo; @@ -19,6 +20,7 @@ import cc.iotkit.manager.dto.bo.devicegroup.DeviceAddGroupBo; import cc.iotkit.manager.dto.bo.devicegroup.DeviceGroupBo; import cc.iotkit.manager.dto.bo.thingmodel.ThingModelMessageBo; import cc.iotkit.manager.dto.vo.deviceconfig.DeviceConfigVo; +import cc.iotkit.manager.dto.vo.devicegroup.DeviceGroupImportVo; import cc.iotkit.manager.dto.vo.devicegroup.DeviceGroupVo; import cc.iotkit.manager.dto.vo.deviceinfo.DeviceInfoVo; import cc.iotkit.manager.dto.vo.deviceinfo.ParentDeviceVo; @@ -37,12 +39,12 @@ import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.springframework.web.context.request.async.DeferredResult; +import org.springframework.web.multipart.MultipartFile; +import javax.servlet.http.HttpServletResponse; +import java.util.ArrayList; import java.util.List; @Api(tags = {"设备"}) @@ -232,6 +234,26 @@ public class DeviceController { return deviceServiceImpl.addGroup(group.getData().to(DeviceGroup.class)); } + + /** + * 导入设备分组-批量添加设备分组 + */ + @ApiOperation(value = "导入设备分组") + @SaCheckPermission("iot:deviceGroup:add") + @PostMapping("/group/importData") + public String importGroup(@RequestPart("file") MultipartFile file, @RequestParam("requestId") String requestId) { + return deviceServiceImpl.importGroup(file); + } + + /** + * 获取导入模板 + */ + @ApiOperation("下载设备分组模板") + @PostMapping("/group/exportData") + public void exportGroupTemplate(HttpServletResponse response) { + ExcelUtil.exportExcel(new ArrayList<>(), "设备分组", DeviceGroupImportVo.class, response); + } + /** * 修改设备分组 */ diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/devicegroup/DeviceGroupImportVo.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/devicegroup/DeviceGroupImportVo.java new file mode 100644 index 00000000..447c639e --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/dto/vo/devicegroup/DeviceGroupImportVo.java @@ -0,0 +1,27 @@ +package cc.iotkit.manager.dto.vo.devicegroup; + +import cc.iotkit.model.device.DeviceGroup; +import com.alibaba.excel.annotation.ExcelProperty; +import io.github.linpeilie.annotations.AutoMapper; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.io.Serializable; + + +@Data +@NoArgsConstructor +@AutoMapper(target = DeviceGroup.class, reverseConvertGenerate = false) +public class DeviceGroupImportVo implements Serializable { + + private static final long serialVersionUID = -1L; + + @ExcelProperty(value = "设备组id") + private String id; + + @ExcelProperty(value = "设备组名称") + private String name; + + @ExcelProperty(value = "分组说明") + private String remark; +} diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/listener/DeviceGroupImportListener.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/listener/DeviceGroupImportListener.java new file mode 100644 index 00000000..c514c34c --- /dev/null +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/listener/DeviceGroupImportListener.java @@ -0,0 +1,98 @@ +package cc.iotkit.manager.listener; + +import cc.iotkit.common.excel.core.ExcelListener; +import cc.iotkit.common.excel.core.ExcelResult; +import cc.iotkit.common.exception.BizException; +import cc.iotkit.common.satoken.utils.LoginHelper; +import cc.iotkit.common.utils.SpringUtils; +import cc.iotkit.manager.dto.bo.devicegroup.DeviceGroupBo; +import cc.iotkit.manager.dto.vo.devicegroup.DeviceGroupImportVo; +import cc.iotkit.manager.dto.vo.devicegroup.DeviceGroupVo; +import cc.iotkit.manager.service.IDeviceManagerService; +import cc.iotkit.model.device.DeviceGroup; +import cn.hutool.core.bean.BeanUtil; +import cn.hutool.core.util.ObjectUtil; +import com.alibaba.excel.context.AnalysisContext; +import com.alibaba.excel.event.AnalysisEventListener; +import lombok.extern.slf4j.Slf4j; + +import java.util.Collections; +import java.util.List; + +@Slf4j +public class DeviceGroupImportListener extends AnalysisEventListener implements ExcelListener { + + private final Boolean isUpdateSupport; + + private int successNum = 0; + private int failureNum = 0; + + private final String userId; + + private IDeviceManagerService deviceManagerService; + + private final StringBuilder successMsg = new StringBuilder(); + private final StringBuilder failureMsg = new StringBuilder(); + + public DeviceGroupImportListener(Boolean isUpdateSupport) { + this.isUpdateSupport = isUpdateSupport; + this.userId = String.valueOf(LoginHelper.getUserId()); + this.deviceManagerService = SpringUtils.getBean(IDeviceManagerService.class); + } + + @Override + public ExcelResult getExcelResult() { + + return new ExcelResult<>() { + @Override + public String getAnalysis() { + if (failureNum > 0) { + failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:"); + throw new BizException(failureMsg.toString()); + } else { + successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条"); + } + return successMsg.toString(); + } + + @Override + public List getList() { + return Collections.emptyList(); + } + + @Override + public List getErrorList() { + return Collections.emptyList(); + } + }; + } + + @Override + public void invoke(DeviceGroupImportVo deviceGroupImportVo, AnalysisContext analysisContext) { + DeviceGroupVo deviceGroupVo = this.deviceManagerService.getDeviceGroup(deviceGroupImportVo.getId()); + try { + if (ObjectUtil.isNull(deviceGroupVo)) { + // 新增 + DeviceGroup deviceGroup = BeanUtil.toBean(deviceGroupImportVo, DeviceGroup.class); + deviceGroup.setUid(this.userId); + this.deviceManagerService.addGroup(deviceGroup); + successNum++; + } else if (Boolean.TRUE.equals(isUpdateSupport)) { + // 修改 + DeviceGroupBo deviceGroupBo = BeanUtil.toBean(deviceGroupImportVo, DeviceGroupBo.class); + deviceGroupBo.setUid(this.userId); + this.deviceManagerService.updateGroup(deviceGroupBo); + successNum++; + } + } catch (Exception e) { + failureNum++; + String msg = "
第" + failureNum + "条分组: " + deviceGroupImportVo.getName() + " 导入失败:"; + failureMsg.append(msg).append(e.getMessage()); + } + } + + @Override + public void doAfterAllAnalysed(AnalysisContext analysisContext) { + + } +} diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/IDeviceManagerService.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/IDeviceManagerService.java index cbbd4325..100e7799 100644 --- a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/IDeviceManagerService.java +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/IDeviceManagerService.java @@ -18,6 +18,7 @@ import cc.iotkit.model.device.DeviceGroup; import cc.iotkit.model.device.DeviceInfo; import cc.iotkit.model.device.message.DeviceProperty; import org.springframework.web.context.request.async.DeferredResult; +import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -75,4 +76,8 @@ public interface IDeviceManagerService { DeviceConfigVo getConfig(String deviceId); boolean saveDevice(DeviceInfoBo data); + + String importGroup(MultipartFile file); + + DeviceGroupVo getDeviceGroup(String id); } diff --git a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/DeviceManagerServiceImpl.java b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/DeviceManagerServiceImpl.java index 4df5bdca..12f2c7be 100644 --- a/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/DeviceManagerServiceImpl.java +++ b/iot-module/iot-manager/src/main/java/cc/iotkit/manager/service/impl/DeviceManagerServiceImpl.java @@ -4,6 +4,8 @@ import cc.iotkit.common.api.PageRequest; import cc.iotkit.common.api.Paging; import cc.iotkit.common.constant.Constants; import cc.iotkit.common.enums.ErrCode; +import cc.iotkit.common.excel.core.ExcelResult; +import cc.iotkit.common.excel.utils.ExcelUtil; import cc.iotkit.common.exception.BizException; import cc.iotkit.common.satoken.utils.AuthUtil; import cc.iotkit.common.thing.ThingModelMessage; @@ -19,9 +21,11 @@ import cc.iotkit.manager.dto.bo.device.DeviceTagAddBo; import cc.iotkit.manager.dto.bo.devicegroup.DeviceAddGroupBo; import cc.iotkit.manager.dto.bo.devicegroup.DeviceGroupBo; import cc.iotkit.manager.dto.vo.deviceconfig.DeviceConfigVo; +import cc.iotkit.manager.dto.vo.devicegroup.DeviceGroupImportVo; import cc.iotkit.manager.dto.vo.devicegroup.DeviceGroupVo; import cc.iotkit.manager.dto.vo.deviceinfo.DeviceInfoVo; import cc.iotkit.manager.dto.vo.deviceinfo.ParentDeviceVo; +import cc.iotkit.manager.listener.DeviceGroupImportListener; import cc.iotkit.manager.service.DataOwnerService; import cc.iotkit.manager.service.DeferredDataConsumer; import cc.iotkit.manager.service.DeviceCtrlService; @@ -34,13 +38,16 @@ import cc.iotkit.model.product.Product; import cc.iotkit.mq.MqProducer; import cc.iotkit.temporal.IDevicePropertyData; import cc.iotkit.temporal.IThingModelMessageData; +import lombok.SneakyThrows; import org.apache.commons.lang3.RandomStringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; import org.springframework.web.context.request.async.DeferredResult; +import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; import java.util.List; import java.util.stream.Collectors; @@ -423,4 +430,19 @@ public class DeviceManagerServiceImpl implements IDeviceManagerService { return deviceInfoData.save(di) != null; } + + @SneakyThrows + @Override + public String importGroup(MultipartFile file) { + ExcelResult result = ExcelUtil.importExcel(file.getInputStream(), DeviceGroupImportVo.class, new DeviceGroupImportListener(true)); + return result.getAnalysis(); + } + + @Override + public DeviceGroupVo getDeviceGroup(String id) { + DeviceGroup deviceGroup = deviceGroupData.findById(id); + return MapstructUtils.convert(deviceGroup, DeviceGroupVo.class); + } + + }