吉安感知网项目-后端
rain
2026-02-09 66bfbdb9251c22ffa2b5f05595a76a3547a2f5eb
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdManageDeviceServiceImpl.java
@@ -24,9 +24,12 @@
import org.sxkj.gd.utils.GdGeoAddressUtil;
import org.sxkj.gd.utils.GeomUtils;
import org.sxkj.gd.workorder.dto.GdXingtuAirportListDTO;
import org.sxkj.gd.workorder.dto.GdXingtuPayloadListDTO;
import org.sxkj.gd.workorder.dto.GdXingtuPilotListDTO;
import org.sxkj.gd.workorder.entity.GdManageDeviceEntity;
import org.sxkj.gd.workorder.entity.GdManageDevicePayloadEntity;
import org.sxkj.gd.workorder.param.GdManageDevicePageParam;
import org.sxkj.gd.workorder.service.IGdManageDevicePayloadService;
import org.sxkj.gd.workorder.vo.GdManageDeviceVO;
import org.sxkj.gd.workorder.excel.GdManageDeviceExcel;
import org.sxkj.gd.workorder.mapper.GdManageDeviceMapper;
@@ -41,9 +44,8 @@
import org.springblade.core.tool.utils.StringUtil;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 设备信息 服务实现类
@@ -57,6 +59,7 @@
public class GdManageDeviceServiceImpl extends BaseServiceImpl<GdManageDeviceMapper, GdManageDeviceEntity> implements IGdManageDeviceService {
   private final JianXingtuApiService jianXingtuApiService;
   private final IGdManageDevicePayloadService iGdManageDevicePayloadService;
   @Override
   public IPage<GdManageDeviceVO> selectGdManageDevicePage(IPage<GdManageDeviceVO> page, GdManageDevicePageParam gdManageDevice) {
@@ -131,6 +134,9 @@
            continue;
         }
         if (saveOrUpdateByAirportId(entity)) {
            // 同步负载设备
            List<GdXingtuPayloadListDTO> loadList = item.getLoadList();
            syncPayloadDevices(loadList);
            count++;
         }
      }
@@ -159,6 +165,102 @@
         }
      }
      return count;
   }
   /**
    * 同步负载设备
    *
    * @param loadList 负载设备列表
    */
   private void syncPayloadDevices(List<GdXingtuPayloadListDTO> loadList) throws Exception {
      // 1. 收集所有负载设备的飞行员序列号,用于后续查询和删除操作
      Set<String> allPilotSnCodes = new HashSet<>();
      if (loadList != null && !loadList.isEmpty()) {
         for (GdXingtuPayloadListDTO load : loadList) {
            if (load.getPilotSnCode() != null) {
               allPilotSnCodes.add(load.getPilotSnCode());
            }
         }
      }
      // 2. 获取当前数据库中这些飞行员序列号对应的所有负载设备
      List<GdManageDevicePayloadEntity> existingPayloads = new ArrayList<>();
      if (!allPilotSnCodes.isEmpty()) {
         existingPayloads = iGdManageDevicePayloadService.list(
            Wrappers.<GdManageDevicePayloadEntity>lambdaQuery().in(GdManageDevicePayloadEntity::getPilotSnCode, allPilotSnCodes)
         );
      }
      // 3. 创建现有负载设备的序列号映射,用于快速查找
      Map<String, GdManageDevicePayloadEntity> existingPayloadMap = new HashMap<>();
      for (GdManageDevicePayloadEntity payload : existingPayloads) {
         if (payload.getPilotSnCode() != null && payload.getSnCode() != null) {
            existingPayloadMap.put(payload.getPilotSnCode() + "_" + payload.getSnCode() + "_" + payload.getName(), payload);
         }
      }
      // 4. 处理负载设备列表
      if (loadList != null && !loadList.isEmpty()) {
         List<GdManageDevicePayloadEntity> toSaveOrUpdate = new ArrayList<>();
         Set<String> currentPayloadKeys = new HashSet<>();
         // 对负载设备列表进行去重处理,确保每个负载设备只被处理一次
         Map<String, GdXingtuPayloadListDTO> uniqueLoadMap = new HashMap<>();
         for (GdXingtuPayloadListDTO load : loadList) {
            if (load.getSnCode() != null && load.getPilotSnCode() != null) {
               String payloadKey = load.getPilotSnCode() + "_" + load.getSnCode() + "_" + load.getName();
               uniqueLoadMap.put(payloadKey, load);
            }
         }
         // 处理去重后的负载设备列表
         for (GdXingtuPayloadListDTO load : uniqueLoadMap.values()) {
            String snCode = load.getSnCode();
            String pilotSnCode1 = load.getPilotSnCode();
            String name = load.getName();
            String payloadKey = pilotSnCode1 + "_" + snCode + "_" + name;
            currentPayloadKeys.add(payloadKey);
            // 检查是否已存在
            GdManageDevicePayloadEntity existing = existingPayloadMap.remove(payloadKey);
            if (existing != null) {
               // 更新现有负载
               existing.setName(load.getName());
               existing.setType(load.getType());
               toSaveOrUpdate.add(existing);
            } else {
               // 新增负载
               GdManageDevicePayloadEntity newPayload = new GdManageDevicePayloadEntity();
               newPayload.setName(load.getName());
               newPayload.setType(load.getType());
               newPayload.setSnCode(snCode);
               newPayload.setPilotSnCode(pilotSnCode1);
               // 可以根据需要设置区域编码等其他字段
               toSaveOrUpdate.add(newPayload);
            }
         }
         // 4. 批量保存或更新负载设备
         if (!toSaveOrUpdate.isEmpty()) {
            iGdManageDevicePayloadService.saveOrUpdateBatch(toSaveOrUpdate);
         }
         // 5. 删除数据库中存在但负载列表中不存在的负载设备
         if (!existingPayloadMap.isEmpty()) {
            List<Long> toDeleteIds = existingPayloadMap.values().stream()
               .map(GdManageDevicePayloadEntity::getId)
               .collect(Collectors.toList());
            iGdManageDevicePayloadService.removeByIds(toDeleteIds);
         }
      } else {
         // 如果负载列表为空,删除该飞行员序列号对应的所有负载设备
         if (!existingPayloads.isEmpty()) {
            List<Long> toDeleteIds = existingPayloads.stream()
               .map(GdManageDevicePayloadEntity::getId)
               .collect(Collectors.toList());
            iGdManageDevicePayloadService.removeByIds(toDeleteIds);
         }
      }
   }
   /**
@@ -193,6 +295,7 @@
            entity.setLongitude(parent.getLongitude());
            entity.setLatitude(parent.getLatitude());
            entity.setHeight(parent.getHeight());
            entity.setModeCode(Long.valueOf(parent.getStatus()));
         }
      }
      return saveOrUpdateDevice(entity);
@@ -221,6 +324,17 @@
      entity.setInsureExpiredTime(parseInsuranceDate(item.getInsuranceInfo()));
      entity.setAreaCode(extractAreaCode(item.getRegionCode()));
      entity.setIsWithDock(item.getIsWithDock());
      entity.setDevicePayload(
         Optional.ofNullable(item.getLoadList())
            .orElse(Collections.emptyList())
            .stream()
            .filter(Objects::nonNull)
            .map(load -> load.getName())
            .filter(Objects::nonNull)
            .distinct()  // 添加去重逻辑
            .collect(Collectors.joining(","))
      );
      return entity;
   }