Преглед изворни кода

优化网关配置信息-导出接口,调整按照map结构返回数据

james пре 15 часа
родитељ
комит
fa1a662192

+ 3 - 3
service-iot/service-iot-biz/src/main/java/com/usky/iot/controller/web/DmpDeviceInfoController.java

@@ -138,13 +138,13 @@ public class DmpDeviceInfoController {
     }
 
     /**
-     * 网关配置导出
+     * 网关配置查询
      * @param gatewayDeviceInfoRequestVO
      * @return
      */
     @PostMapping("/gatewayExport")
-    public void gatewayExport(@RequestBody GatewayDeviceInfoRequestVO gatewayDeviceInfoRequestVO, HttpServletResponse response) {
-        dmpDeviceInfoService.gatewayExport(gatewayDeviceInfoRequestVO, response);
+    public ApiResult<Map<String, Map<String, GatewayDeviceExportItemVO>>> gatewayExport(@RequestBody GatewayDeviceInfoRequestVO gatewayDeviceInfoRequestVO) {
+        return ApiResult.success(dmpDeviceInfoService.gatewayExport(gatewayDeviceInfoRequestVO));
     }
 
     /**

+ 2 - 2
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/DmpDeviceInfoService.java

@@ -69,10 +69,10 @@ public interface DmpDeviceInfoService extends CrudService<DmpDeviceInfo> {
     void export(DmpDeviceInfoRequest dmpDeviceInfoRequest, HttpServletResponse response);
 
     /**
-     * 网关设备信息导出
+     * 网关设备配置查询
      * @param gatewayDeviceInfoRequestVO
      */
-    void gatewayExport(GatewayDeviceInfoRequestVO gatewayDeviceInfoRequestVO, HttpServletResponse response);
+    Map<String, Map<String, GatewayDeviceExportItemVO>> gatewayExport(GatewayDeviceInfoRequestVO gatewayDeviceInfoRequestVO);
 
     CommonPage<Object> deviceCurrentDataList(String deviceName,String installAddress,String productCode,Integer pageNum,Integer pageSize);
 

+ 22 - 60
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/impl/DmpDeviceInfoServiceImpl.java

@@ -10,7 +10,6 @@ import cn.hutool.core.date.DateTime;
 import cn.hutool.core.date.DateUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
-import com.alibaba.fastjson.serializer.SerializerFeature;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@@ -55,8 +54,6 @@ import org.springframework.web.multipart.MultipartFile;
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
-import java.net.URLEncoder;
-import java.nio.charset.StandardCharsets;
 import java.text.DecimalFormat;
 import java.time.Instant;
 import java.time.LocalDateTime;
@@ -1192,35 +1189,27 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
     }
 
     @Override
-    public void gatewayExport(GatewayDeviceInfoRequestVO gatewayDeviceInfoRequestVO, HttpServletResponse response) {
-        try {
-            if (StringUtils.isBlank(gatewayDeviceInfoRequestVO.getGatewayUuid())) {
-                throw new BusinessException("网关UUID不能为空");
-            }
-            LambdaQueryWrapper<DmpDeviceInfo> queryWrapper = Wrappers.lambdaQuery();
-            queryWrapper.eq(DmpDeviceInfo::getCategoryType, 3)
-                    .eq(DmpDeviceInfo::getDeleteFlag, 0)
-                    .eq(DmpDeviceInfo::getGatewayUuid, gatewayDeviceInfoRequestVO.getGatewayUuid())
-                    .eq(DmpDeviceInfo::getTenantId, SecurityUtils.getTenantId())
-                    .orderByDesc(DmpDeviceInfo::getId);
-            List<DmpDeviceInfo> gatewayDeviceList = this.list(queryWrapper);
-            if (CollectionUtil.isEmpty(gatewayDeviceList)) {
-                throw new BusinessException("未查询到网关子设备信息");
-            }
-            String content = buildGatewayDeviceCfgContent(gatewayDeviceList);
-            exportCfg(content, response);
-        } catch (BusinessException e) {
-            throw e;
-        } catch (Exception e) {
-            log.error("导出网关设备配置信息失败", e);
-            throw new BusinessException("导出网关设备配置信息失败" + e.getMessage());
+    public Map<String, Map<String, GatewayDeviceExportItemVO>> gatewayExport(GatewayDeviceInfoRequestVO gatewayDeviceInfoRequestVO) {
+        if (StringUtils.isBlank(gatewayDeviceInfoRequestVO.getGatewayUuid())) {
+            throw new BusinessException("网关UUID不能为空");
         }
+        LambdaQueryWrapper<DmpDeviceInfo> queryWrapper = Wrappers.lambdaQuery();
+        queryWrapper.eq(DmpDeviceInfo::getCategoryType, 3)
+                .eq(DmpDeviceInfo::getDeleteFlag, 0)
+                .eq(DmpDeviceInfo::getGatewayUuid, gatewayDeviceInfoRequestVO.getGatewayUuid())
+                .eq(DmpDeviceInfo::getTenantId, SecurityUtils.getTenantId())
+                .orderByDesc(DmpDeviceInfo::getId);
+        List<DmpDeviceInfo> gatewayDeviceList = this.list(queryWrapper);
+        if (CollectionUtil.isEmpty(gatewayDeviceList)) {
+            throw new BusinessException("未查询到网关子设备信息");
+        }
+        return buildGatewayDeviceExportMap(gatewayDeviceList);
     }
 
     /**
-     * 按设备类型简称 -> 地址码 构建网关子设备配置内容
+     * 按设备类型简称 -> 地址码 构建网关子设备配置
      */
-    private String buildGatewayDeviceCfgContent(List<DmpDeviceInfo> deviceList) {
+    private Map<String, Map<String, GatewayDeviceExportItemVO>> buildGatewayDeviceExportMap(List<DmpDeviceInfo> deviceList) {
         List<Integer> productIds = deviceList.stream()
                 .map(DmpDeviceInfo::getProductId)
                 .filter(Objects::nonNull)
@@ -1228,7 +1217,7 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
                 .collect(Collectors.toList());
         Map<Integer, String> productTypeAbbreviaMap = getProductTypeAbbreviaMap(productIds);
 
-        Map<String, Map<String, JSONObject>> grouped = new LinkedHashMap<>();
+        Map<String, Map<String, GatewayDeviceExportItemVO>> grouped = new LinkedHashMap<>();
         for (DmpDeviceInfo device : deviceList) {
             if (StringUtils.isBlank(device.getAddressCode())) {
                 continue;
@@ -1237,20 +1226,17 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
             if (StringUtils.isBlank(typeAbbrevia)) {
                 throw new BusinessException("产品ID " + device.getProductId() + " 未找到对应设备类型简称");
             }
-            JSONObject deviceInfo = new JSONObject(true);
-            deviceInfo.put("deviceid", device.getDeviceId());
-            deviceInfo.put("deviceUuid", device.getDeviceUuid());
-            deviceInfo.put("productCode", device.getProductCode());
+            GatewayDeviceExportItemVO deviceInfo = new GatewayDeviceExportItemVO();
+            deviceInfo.setDeviceid(device.getDeviceId());
+            deviceInfo.setDeviceUuid(device.getDeviceUuid());
+            deviceInfo.setProductCode(device.getProductCode());
             grouped.computeIfAbsent(typeAbbrevia, key -> new LinkedHashMap<>())
                     .put(device.getAddressCode(), deviceInfo);
         }
         if (grouped.isEmpty()) {
             throw new BusinessException("网关子设备地址码为空,无法导出配置");
         }
-
-        JSONObject root = new JSONObject(true);
-        grouped.forEach(root::put);
-        return JSON.toJSONString(root, SerializerFeature.PrettyFormat);
+        return grouped;
     }
 
     /**
@@ -1295,30 +1281,6 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
         return result;
     }
 
-    private void exportCfg(String content, HttpServletResponse response) {
-        ServletOutputStream outputStream = null;
-        try {
-            response.setContentType("application/octet-stream");
-            response.setCharacterEncoding("utf-8");
-            String fileName = URLEncoder.encode("gateway.cfg", StandardCharsets.UTF_8.name()).replace("+", "%20");
-            response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
-            outputStream = response.getOutputStream();
-            outputStream.write(content.getBytes(StandardCharsets.UTF_8));
-            outputStream.flush();
-        } catch (IOException e) {
-            log.error("导出cfg文件失败", e);
-            throw new BusinessException("导出cfg文件失败" + e.getMessage());
-        } finally {
-            if (outputStream != null) {
-                try {
-                    outputStream.close();
-                } catch (IOException e) {
-                    log.error("关闭输出流失败", e);
-                }
-            }
-        }
-    }
-
     public void exportExcel(Workbook workbook, HttpServletResponse response) {
         // 定义一个流,用作流式输出
         ServletOutputStream outputStream = null;

+ 13 - 0
service-iot/service-iot-biz/src/main/java/com/usky/iot/service/vo/GatewayDeviceExportItemVO.java

@@ -0,0 +1,13 @@
+package com.usky.iot.service.vo;
+
+import lombok.Data;
+
+@Data
+public class GatewayDeviceExportItemVO {
+
+    private String deviceid;
+
+    private String deviceUuid;
+
+    private String productCode;
+}