|
|
@@ -8,7 +8,6 @@ import cn.afterturn.easypoi.excel.entity.ImportParams;
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
-import cn.hutool.core.io.FileUtil;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
@@ -38,6 +37,7 @@ import com.usky.iot.domain.*;
|
|
|
import com.usky.common.security.utils.SecurityUtils;
|
|
|
import com.usky.iot.mapper.DmpDeviceInfoMapper;
|
|
|
import com.usky.iot.mapper.DmpProductAttributeMapper;
|
|
|
+import com.usky.iot.mapper.DmpProductInfoMapper;
|
|
|
import com.usky.iot.service.*;
|
|
|
import com.usky.iot.service.config.DeviceOperate;
|
|
|
import com.usky.iot.service.enums.TopicInfo;
|
|
|
@@ -53,10 +53,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.servlet.ServletOutputStream;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
-import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
-import java.io.OutputStream;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
import java.text.DecimalFormat;
|
|
|
import java.time.Instant;
|
|
|
import java.time.LocalDateTime;
|
|
|
@@ -106,6 +103,12 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
|
|
|
@Autowired
|
|
|
private DeviceOperate deviceOperate;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private DmpProductInfoMapper dmpProductInfoMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DmpDeviceTypeService dmpDeviceTypeService;
|
|
|
+
|
|
|
private static final String ALARM_HTTP_URL = "/service-alarm/baseAlarm/alarmInfo";
|
|
|
|
|
|
|
|
|
@@ -654,6 +657,9 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
|
|
|
spaceGatewayLambdaQueryWrapper.eq(BaseSpaceGateway::getSpaceId, diRequest.getSpaceId());
|
|
|
gatewayUuidList = baseSpaceGatewayService.list(spaceGatewayLambdaQueryWrapper).stream().map(BaseSpaceGateway::getGatewayUuid).collect(Collectors.toList());
|
|
|
}
|
|
|
+ if(StringUtils.isNotBlank(diRequest.getGatewayUuid())){
|
|
|
+ gatewayUuidList.add(diRequest.getGatewayUuid());
|
|
|
+ }
|
|
|
|
|
|
if (diRequest.getDeviceStatus() != null) {
|
|
|
if (StringUtils.isNotBlank(diRequest.getDeviceId()) || StringUtils.isNotBlank(diRequest.getDeviceUuid()) || StringUtils.isNotBlank(diRequest.getDeviceName()) || diRequest.getProductId() != null || StringUtils.isNotBlank(diRequest.getProductCode()) || diRequest.getServiceStatus() != null || diRequest.getCategoryType() != null) {
|
|
|
@@ -860,6 +866,9 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
|
|
|
spaceGatewayLambdaQueryWrapper.eq(BaseSpaceGateway::getSpaceId, diRequest.getSpaceId());
|
|
|
gatewayUuidList = baseSpaceGatewayService.list(spaceGatewayLambdaQueryWrapper).stream().map(BaseSpaceGateway::getGatewayUuid).collect(Collectors.toList());
|
|
|
}
|
|
|
+ if(StringUtils.isNotBlank(diRequest.getGatewayUuid())){
|
|
|
+ gatewayUuidList.add(diRequest.getGatewayUuid());
|
|
|
+ }
|
|
|
|
|
|
if (diRequest.getDeviceStatus() != null) {
|
|
|
if (StringUtils.isNotBlank(diRequest.getDeviceId()) || StringUtils.isNotBlank(diRequest.getDeviceName()) || diRequest.getProductId() != null || StringUtils.isNotBlank(diRequest.getProductCode()) || diRequest.getServiceStatus() != null || diRequest.getCategoryType() != null) {
|
|
|
@@ -1180,58 +1189,96 @@ public class DmpDeviceInfoServiceImpl extends AbstractCrudService<DmpDeviceInfoM
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void gatewayExport(GatewayDeviceInfoRequestVO gatewayDeviceInfoRequestVO){
|
|
|
- try {
|
|
|
- // 1. 查询数据库中的设备信息
|
|
|
- LambdaQueryWrapper<DmpDeviceInfo> queryWrapper = Wrappers.lambdaQuery();
|
|
|
- queryWrapper.eq(DmpDeviceInfo::getCategoryType,3)
|
|
|
- .eq(DmpDeviceInfo::getDeleteFlag, 0)
|
|
|
- .eq(StringUtils.isNotBlank(gatewayDeviceInfoRequestVO.getGatewayUuid()), DmpDeviceInfo::getGatewayUuid, gatewayDeviceInfoRequestVO.getGatewayUuid())
|
|
|
- .eq(StringUtils.isNotBlank(gatewayDeviceInfoRequestVO.getProductCode()), DmpDeviceInfo::getProductCode, gatewayDeviceInfoRequestVO.getProductCode())
|
|
|
- .eq(DmpDeviceInfo::getTenantId, SecurityUtils.getTenantId())
|
|
|
- .orderByDesc(DmpDeviceInfo::getId);
|
|
|
- List<DmpDeviceInfo> gatewayDeviceList = this.list(queryWrapper);
|
|
|
-
|
|
|
- // 2. 定义服务器本地保存路径(建议配置在application.yml中,这里简化写死)
|
|
|
- // 注意:要确保该目录存在,否则会报错;Windows路径用\\,Linux/Mac用/
|
|
|
- String localPath = gatewayDeviceInfoRequestVO.getFilePath()+ "/" + gatewayDeviceInfoRequestVO.getProductCode() + ".cfg";
|
|
|
-
|
|
|
- // 3. 调用exportToLocalFile导出到本地
|
|
|
- File file = FileUtil.file(localPath);
|
|
|
- exportDeviceToTxt(gatewayDeviceList, FileUtil.getOutputStream(file));
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException("导出网关设备配置信息失败", e);
|
|
|
+ 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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 导出设备信息到TXT文件
|
|
|
- * @param deviceList 设备列表
|
|
|
- * @param outputStream 输出流(用于下载)
|
|
|
+ * 按设备类型简称 -> 地址码 构建网关子设备配置
|
|
|
*/
|
|
|
- public static void exportDeviceToTxt(List<DmpDeviceInfo> deviceList, OutputStream outputStream) {
|
|
|
- // 1. 构建TXT内容(自定义格式,按行写入)
|
|
|
- StringBuilder content = new StringBuilder();
|
|
|
- // 写入表头
|
|
|
- content.append("{");
|
|
|
- // 写入数据行
|
|
|
+ private Map<String, Map<String, GatewayDeviceExportItemVO>> buildGatewayDeviceExportMap(List<DmpDeviceInfo> deviceList) {
|
|
|
+ List<Integer> productIds = deviceList.stream()
|
|
|
+ .map(DmpDeviceInfo::getProductId)
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .distinct()
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ Map<Integer, String> productTypeAbbreviaMap = getProductTypeAbbreviaMap(productIds);
|
|
|
+
|
|
|
+ Map<String, Map<String, GatewayDeviceExportItemVO>> grouped = new LinkedHashMap<>();
|
|
|
for (DmpDeviceInfo device : deviceList) {
|
|
|
- content.append("\n\t\"").append(device.getImsiCode()).append("\":")
|
|
|
- .append("{").append("\"").append("deviceid").append("\"").append(":").append("\"").append(device.getDeviceId()).append("\"").append(",")
|
|
|
- .append("\"").append("deviceUuid").append("\"").append(":").append("\"").append(device.getDeviceUuid()).append("\"").append(",")
|
|
|
- .append("\"").append("productCode").append("\"").append(":").append("\"").append(device.getProductCode()).append("\"")
|
|
|
- .append("}").append(",");
|
|
|
+ if (StringUtils.isBlank(device.getAddressCode())) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ String typeAbbrevia = productTypeAbbreviaMap.get(device.getProductId());
|
|
|
+ if (StringUtils.isBlank(typeAbbrevia)) {
|
|
|
+ throw new BusinessException("产品ID " + device.getProductId() + " 未找到对应设备类型简称");
|
|
|
+ }
|
|
|
+ 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);
|
|
|
}
|
|
|
- content.delete(content.length()-1, content.length());
|
|
|
- content.append("\n}");
|
|
|
+ if (grouped.isEmpty()) {
|
|
|
+ throw new BusinessException("网关子设备地址码为空,无法导出配置");
|
|
|
+ }
|
|
|
+ return grouped;
|
|
|
+ }
|
|
|
|
|
|
- // 2. 将内容写入输出流(UTF-8编码避免乱码)
|
|
|
- try {
|
|
|
- outputStream.write(content.toString().getBytes(StandardCharsets.UTF_8));
|
|
|
- outputStream.flush();
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException("导出TXT文件失败", e);
|
|
|
+ /**
|
|
|
+ * 根据产品ID批量查询设备类型简称
|
|
|
+ */
|
|
|
+ private Map<Integer, String> getProductTypeAbbreviaMap(List<Integer> productIds) {
|
|
|
+ if (CollectionUtil.isEmpty(productIds)) {
|
|
|
+ return Collections.emptyMap();
|
|
|
}
|
|
|
+ List<Map<String, Object>> products = dmpProductInfoMapper.selectDeviceTypeByProductIds(productIds);
|
|
|
+ if (CollectionUtil.isEmpty(products)) {
|
|
|
+ return Collections.emptyMap();
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<Integer, Integer> productDeviceTypeMap = products.stream()
|
|
|
+ .collect(Collectors.toMap(
|
|
|
+ map -> Integer.valueOf(map.get("id").toString()),
|
|
|
+ map -> Integer.valueOf(map.get("deviceType").toString()),
|
|
|
+ (a, b) -> a));
|
|
|
+
|
|
|
+ Set<String> typeCodes = productDeviceTypeMap.values().stream()
|
|
|
+ .filter(Objects::nonNull)
|
|
|
+ .map(String::valueOf)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+ if (CollectionUtil.isEmpty(typeCodes)) {
|
|
|
+ return Collections.emptyMap();
|
|
|
+ }
|
|
|
+
|
|
|
+ LambdaQueryWrapper<DmpDeviceType> typeQuery = Wrappers.lambdaQuery();
|
|
|
+ typeQuery.select(DmpDeviceType::getTypeCode, DmpDeviceType::getTypeAbbrevia)
|
|
|
+ .in(DmpDeviceType::getTypeCode, typeCodes);
|
|
|
+ Map<String, String> typeCodeAbbreviaMap = dmpDeviceTypeService.list(typeQuery).stream()
|
|
|
+ .collect(Collectors.toMap(DmpDeviceType::getTypeCode, DmpDeviceType::getTypeAbbrevia, (a, b) -> a));
|
|
|
+
|
|
|
+ Map<Integer, String> result = new HashMap<>();
|
|
|
+ productDeviceTypeMap.forEach((productId, deviceType) -> {
|
|
|
+ String typeAbbrevia = typeCodeAbbreviaMap.get(String.valueOf(deviceType));
|
|
|
+ if (StringUtils.isNotBlank(typeAbbrevia)) {
|
|
|
+ result.put(productId, typeAbbrevia);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
public void exportExcel(Workbook workbook, HttpServletResponse response) {
|