吉安感知网项目-后端
rain
2026-01-28 3c9c3db69742fe4d3e69b5619eb52f4ed5f2cfb5
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdManageDeviceServiceImpl.java
@@ -16,21 +16,33 @@
 */
package org.sxkj.gd.workorder.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
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.GdXingtuPilotListDTO;
import org.sxkj.gd.workorder.entity.GdManageDeviceEntity;
import org.sxkj.gd.workorder.param.GdManageDevicePageParam;
import org.sxkj.gd.workorder.vo.GdManageDeviceVO;
import org.sxkj.gd.workorder.excel.GdManageDeviceExcel;
import org.sxkj.gd.workorder.mapper.GdManageDeviceMapper;
import org.sxkj.gd.workorder.service.IGdManageDeviceService;
import org.sxkj.gd.xingtu.JianXingtuApiService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.sxkj.system.cache.DictCache;
import org.sxkj.system.enums.DictEnum;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.StringUtil;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
@@ -40,7 +52,10 @@
 * @since 2026-01-14
 */
@Service
@AllArgsConstructor
public class GdManageDeviceServiceImpl extends BaseServiceImpl<GdManageDeviceMapper, GdManageDeviceEntity> implements IGdManageDeviceService {
   private final JianXingtuApiService jianXingtuApiService;
   @Override
   public IPage<GdManageDeviceVO> selectGdManageDevicePage(IPage<GdManageDeviceVO> page, GdManageDevicePageParam gdManageDevice) {
@@ -80,4 +95,199 @@
      }
      return saveOrUpdate(gdManageDeviceEntity);
   }
   /**
    * 同步星图设备(无人机、机巢)
    *
    * @return 同步数量
    */
   @Override
   @Async
   @Scheduled(cron = "0 0 0 * * ?")
   public int syncXingtuDevice() throws Exception {
      int total = 0;
      total += syncPilotDevices();
      total += syncAirportDevices();
      return total;
   }
   private int syncPilotDevices() throws Exception {
      R response = jianXingtuApiService.getDevicePilotList(null);
      List<GdXingtuPilotListDTO> items = parseList(response, GdXingtuPilotListDTO.class);
      if (items.isEmpty()) {
         return 0;
      }
      int count = 0;
      for (GdXingtuPilotListDTO item : items) {
         GdManageDeviceEntity entity = buildFromPilot(item);
         if (entity == null) {
            continue;
         }
         if (saveOrUpdateByAirportId(entity)) {
            count++;
         }
      }
      return count;
   }
   private int syncAirportDevices() throws Exception {
      R response = jianXingtuApiService.getDeviceAirportList(null);
      List<GdXingtuAirportListDTO> items = parseList(response, GdXingtuAirportListDTO.class);
      if (items.isEmpty()) {
         return 0;
      }
      int count = 0;
      for (GdXingtuAirportListDTO item : items) {
         GdManageDeviceEntity entity = buildFromAirport(item);
         if (entity == null) {
            continue;
         }
         if (saveOrUpdateByAirportId(entity)) {
            count++;
         }
      }
      return count;
   }
   private boolean saveOrUpdateByAirportId(GdManageDeviceEntity entity) throws Exception {
      if (StringUtil.isBlank(entity.getAirportId())) {
         return false;
      }
      GdManageDeviceEntity exist = lambdaQuery()
         .eq(GdManageDeviceEntity::getAirportId, entity.getAirportId())
         .one();
      if (exist != null) {
         entity.setId(exist.getId());
         if (entity.getLongitude() == null) {
            entity.setLongitude(exist.getLongitude());
         }
         if (entity.getLatitude() == null) {
            entity.setLatitude(exist.getLatitude());
         }
         if (entity.getInsureExpiredTime() == null) {
            entity.setInsureExpiredTime(exist.getInsureExpiredTime());
         }
      }
      return saveOrUpdateDevice(entity);
   }
   private GdManageDeviceEntity buildFromPilot(GdXingtuPilotListDTO item) {
      if (item == null) {
         return null;
      }
      GdManageDeviceEntity entity = new GdManageDeviceEntity();
      entity.setAirportId(item.getId());
      entity.setDeviceSn(item.getSnCode());
      entity.setDeviceName(item.getName());
      entity.setNickname(item.getName());
      entity.setDeviceType(1);
      entity.setLongitude(parseDouble(item.getLongitude()));
      entity.setLatitude(parseDouble(item.getLatitude()));
      entity.setFirmwareVersion(item.getFirmwareVersion());
      entity.setModeCode(parseModeCode(item.getStatus()));
      entity.setInsureExpiredTime(parseInsuranceDate(item.getInsuranceInfo()));
      entity.setAreaCode(extractAreaCode(item.getRegionCode()));
      return entity;
   }
   private GdManageDeviceEntity buildFromAirport(GdXingtuAirportListDTO item) {
      if (item == null) {
         return null;
      }
      GdManageDeviceEntity entity = new GdManageDeviceEntity();
      entity.setAirportId(item.getId());
      entity.setDeviceSn(item.getSnCode());
      entity.setDeviceName(item.getName());
      entity.setNickname(item.getName());
      entity.setDeviceType(0);
      entity.setLongitude(parseDouble(item.getLongitude()));
      entity.setLatitude(parseDouble(item.getLatitude()));
      entity.setFirmwareVersion(item.getFirmwareVersion());
      entity.setChildSn(item.getPilotSnCode());
      entity.setModeCode(parseModeCode(item.getStatus()));
      entity.setInsureExpiredTime(parseInsuranceDate(item.getInsuranceInfo()));
      entity.setAreaCode(extractAreaCode(item.getRegionCode()));
      return entity;
   }
   private Long parseModeCode(String status) {
      if ("在线".equals(status)) {
         return 0L;
      }
      if ("离线".equals(status)) {
         return 1L;
      }
      return null;
   }
   private Double parseDouble(String value) {
      if (StringUtil.isBlank(value)) {
         return null;
      }
      try {
         return Double.valueOf(value);
      } catch (Exception ignored) {
         return null;
      }
   }
   private String extractAreaCode(String regionCode) {
      if (StringUtil.isBlank(regionCode)) {
         return null;
      }
      String[] parts = regionCode.split(",");
      if (parts.length == 1) {
         parts = regionCode.split("-");
      }
      for (int i = parts.length - 1; i >= 0; i--) {
         if (StringUtil.isNotBlank(parts[i])) {
            return parts[i];
         }
      }
      return regionCode;
   }
   private Date parseInsuranceDate(String insuranceInfo) {
      if (StringUtil.isBlank(insuranceInfo)) {
         return null;
      }
      try {
         JSONArray array = JSON.parseArray(insuranceInfo);
         if (array == null || array.isEmpty()) {
            return null;
         }
         JSONObject last = array.getJSONObject(array.size() - 1);
         if (last == null) {
            return null;
         }
         Object dateValue = last.get("isInsuranceDate");
         String dateStr = null;
         if (dateValue instanceof JSONArray) {
            JSONArray dateArray = (JSONArray) dateValue;
            if (!dateArray.isEmpty()) {
               dateStr = dateArray.getString(dateArray.size() - 1);
            }
         } else if (dateValue != null) {
            dateStr = String.valueOf(dateValue);
         }
         if (StringUtil.isBlank(dateStr)) {
            return null;
         }
         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
         return format.parse(dateStr);
      } catch (Exception ignored) {
         return null;
      }
   }
   private <T> List<T> parseList(R response, Class<T> clazz) {
      if (response == null || !response.isSuccess() || response.getData() == null) {
         return Collections.emptyList();
      }
      Object data = response.getData();
      if (data instanceof String && StringUtil.isBlank((String) data)) {
         return Collections.emptyList();
      }
      return JSON.parseArray(JSON.toJSONString(data), clazz);
   }
}