吉安感知网项目-后端
linwei
2026-07-16 04f727bd9e91d80de93efeeaf1addcb62abbea10
drone-service/drone-fw/src/main/java/org/sxkj/fw/cockpit/service/impl/CockpitServiceImpl.java
@@ -1,10 +1,25 @@
package org.sxkj.fw.cockpit.service.impl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.stereotype.Service;
import org.sxkj.common.utils.HeaderUtils;
import org.sxkj.fw.area.entity.FwDefenseSceneEntity;
import org.sxkj.fw.area.param.FwAreaDivideListParam;
import org.sxkj.fw.area.service.IFwAreaDivideService;
import org.sxkj.fw.area.service.IFwDefenseSceneService;
import org.sxkj.fw.area.service.IFwDefenseSceneManageService;
import org.sxkj.fw.area.vo.FwAreaDivideVO;
import org.sxkj.fw.area.vo.FwDefenseSceneManageVO;
import org.sxkj.fw.cockpit.service.ICockpitService;
import org.sxkj.fw.cockpit.vo.AlarmStatisticsVO;
import org.sxkj.fw.cockpit.vo.DeviceStatisticsVO;
import org.sxkj.fw.cockpit.vo.SceneManageStatisticsVO;
import org.sxkj.fw.common.FwBaseInfo;
import org.sxkj.fw.detection.entity.FwEffectEvalEntity;
import org.sxkj.fw.detection.param.FwEffectEvalParam;
import org.sxkj.fw.detection.service.IFwEffectEvalService;
@@ -16,22 +31,42 @@
import org.sxkj.fw.record.entity.FwDroneAlarmRecordEntity;
import org.sxkj.fw.record.service.IFwDroneAlarmRecordService;
import org.sxkj.fw.record.vo.FwDroneAlarmRecordVO;
import org.sxkj.system.cache.RegionCache;
import org.sxkj.system.cache.SysCache;
import org.sxkj.system.entity.Dept;
import org.sxkj.system.entity.Region;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@Service
@Slf4j
public class CockpitServiceImpl implements ICockpitService {
   @Resource
   private  IFwDroneAlarmRecordService fwDroneAlarmRecordService;
   private IFwDroneAlarmRecordService fwDroneAlarmRecordService;
   @Resource
   private  IFwDeviceService fwDeviceService;
   private IFwDeviceService fwDeviceService;
   @Resource
   private  IFwEffectEvalService fwEffectEvalService;
   private IFwEffectEvalService fwEffectEvalService;
   @Resource
   private IFwAreaDivideService iFwAreaDivideService;
   @Resource
   private IFwDefenseSceneManageService iFwDefenseSceneManageService;
   @Resource
   private IFwDefenseSceneService iFwDefenseSceneService;
   @Override
   public IPage<FwDroneAlarmRecordVO> selectFwDroneAlarmRecordPage(IPage<FwDroneAlarmRecordVO> page, FwDroneAlarmRecordDTO fwDroneAlarmRecord) {
@@ -40,19 +75,32 @@
   @Override
   public IPage<CockpitFwDeviceVO> selectCockpitDevicePage(IPage<CockpitFwDeviceVO> page, FwDeviceDTO fwDevice) {
      return fwDeviceService.selectCockpitDevicePage(page, fwDevice);
         Dept dept = SysCache.getDept(Long.valueOf(AuthUtil.getDeptId()));
         Region byCode = RegionCache.getByCode(dept.getAreaCode());
         fwDevice.setRegionCode(HeaderUtils.formatAreaCode(byCode.getCode()));
         fwDevice.setCurrentDeptId(AuthUtil.getDeptId());
         fwDevice.setFlyTime(new Date());
      IPage<CockpitFwDeviceVO> cockpitFwDeviceVOIPage = fwDeviceService.selectCockpitDevicePage(page, fwDevice);
      return cockpitFwDeviceVOIPage;
   }
   @Override
   public List<DeviceStatisticsVO> statisticalDeviceType() {
      return fwDeviceService.statisticalDeviceType();
      Dept dept = SysCache.getDept(Long.valueOf(AuthUtil.getDeptId()));
      Region byCode = RegionCache.getByCode(dept.getAreaCode());
      return fwDeviceService.statisticalDeviceType(HeaderUtils.formatAreaCode(byCode.getCode()), AuthUtil.getDeptId());
   }
   @Override
   public boolean interferenceAndExpulsion(FwEffectEvalParam fwEffectEvalParam) {
      // 告警记录
      FwDroneAlarmRecordEntity alarmRecordEntity = fwDroneAlarmRecordService.getById(fwEffectEvalParam.getAlarmRecordId());
      // 设备信息
      FwDeviceEntity device = fwDeviceService.getById(alarmRecordEntity.getDeviceId());
      FwEffectEvalEntity fwEffectEvalEntity = new FwEffectEvalEntity();
      // 反制效果
      FwEffectEvalEntity one = fwEffectEvalService.getOne(Wrappers.<FwEffectEvalEntity>lambdaQuery().eq(FwEffectEvalEntity::getAlarmRecordId, fwEffectEvalParam.getAlarmRecordId()));
      FwEffectEvalEntity fwEffectEvalEntity = Optional.ofNullable(one)
         .orElse(new FwEffectEvalEntity());
      // 告警记录相关字段
      fwEffectEvalEntity.setAlarmRecordId(fwEffectEvalParam.getAlarmRecordId());
@@ -63,33 +111,179 @@
      // 从参数获取反制方式,否则使用告警记录中的值
      fwEffectEvalEntity.setCounterWay(fwEffectEvalParam.getCounterWay());
      alarmRecordEntity.setCounterWay(fwEffectEvalParam.getCounterWay());
      // 设备相关字段
      fwEffectEvalEntity.setDeviceId(alarmRecordEntity.getDeviceId());
      fwEffectEvalEntity.setDeviceSn(device.getDeviceSn());
      fwEffectEvalEntity.setDeviceName(device.getDeviceName());
      fwEffectEvalEntity.setDeviceModel(device.getDeviceModel());
      fwEffectEvalEntity.setDeviceType(device.getDeviceType());
      fwEffectEvalEntity.setDeployLongitude(device.getLongitude() != null ? Double.parseDouble(device.getLongitude()) : null);
      fwEffectEvalEntity.setDeployLatitude(device.getLatitude() != null ? Double.parseDouble(device.getLatitude()) : null);
      fwEffectEvalEntity.setDeployLongitude(StringUtil.isNotBlank(device.getLongitude()) ? Double.parseDouble(device.getLongitude()) : null);
      fwEffectEvalEntity.setDeployLatitude(StringUtil.isNotBlank(device.getLatitude()) ? Double.parseDouble(device.getLatitude()) : null);
      fwEffectEvalEntity.setAreaCode(device.getAreaCode());
      // 反制效果及工作模式(从参数获取,否则使用默认值)
      fwEffectEvalEntity.setCounterEffect( "1"); // 1.success/2.fail
      fwEffectEvalEntity.setCounterEffect("1"); // 1.success/2.fail
      fwEffectEvalEntity.setCoverRadiusM(2500); // 覆盖范围(米)
      fwEffectEvalEntity.setWorkMode( "1"); // 1.机动/2.固定
      fwEffectEvalEntity.setWorkMode("1"); // 1.机动/2.固定
      fwDroneAlarmRecordService.updateById(alarmRecordEntity);
      return fwEffectEvalService.saveOrUpdate(fwEffectEvalEntity);
   }
   @Override
   public AlarmStatisticsVO statisticsAlarmsAndDeviceRecords() {
      Dept dept = SysCache.getDept(Long.valueOf(AuthUtil.getDeptId()));
      Region byCode = RegionCache.getByCode(dept.getAreaCode());
      String regionCode = HeaderUtils.formatAreaCode(byCode.getCode());
      // 设备统计
      AlarmStatisticsVO alarmStatisticsVO = fwDeviceService.statisticalDeviceAtt();
      AlarmStatisticsVO alarmStatisticsVO = fwDeviceService.statisticalDeviceAtt(regionCode, AuthUtil.getDeptId());
      // 告警统计
      AlarmStatisticsVO alarmStatistics =  fwDroneAlarmRecordService.alarmTypeStatistics();
      AlarmStatisticsVO alarmStatistics = fwDroneAlarmRecordService.alarmTypeStatistics(regionCode, AuthUtil.getDeptId());
      alarmStatisticsVO.setHistoryAlarmCount(alarmStatistics.getHistoryAlarmCount());
      alarmStatisticsVO.setRealTimeAlarmCount(alarmStatistics.getRealTimeAlarmCount());
      return alarmStatisticsVO;
   }
   /**
    * @param effectiveRangeKmIsNotNull
    * @return
    */
   @Override
   public List<DeviceStatisticsVO> getDeviceOutStatistics(String effectiveRangeKmIsNotNull) {
      FwDeviceDTO fwDeviceDTO = new FwDeviceDTO();
      // 打印用户信息
      log.info("用户信息:{}", AuthUtil.getUser());
      // 获取部门信息
      Dept dept = SysCache.getDept(Long.valueOf(AuthUtil.getDeptId()));
      log.info("部门信息:{}", dept);
      Region byCode = RegionCache.getByCode(dept.getAreaCode());
      log.info("区域信息:{}", byCode);
      fwDeviceDTO.setCurrentDeptId(AuthUtil.getDeptId());
      fwDeviceDTO.setRegionCode(HeaderUtils.formatAreaCode(byCode.getCode()));
      fwDeviceDTO.setFlyTime(new Date());
      fwDeviceDTO.setIsEnabled(1);
      return fwDeviceService.getDeviceStatistics(fwDeviceDTO);
   }
   /**
    *
    * @param filterTime
    * @return
    */
   @Override
   public List<FwDefenseSceneManageVO> selectFwDefenseSceneManageList(Date filterTime) {
      return iFwDefenseSceneManageService.selectFwDefenseSceneManageList(filterTime);
   }
   /**
    * 统计场景管理与区域数量
    * <p>
    * 该方法统计指定时间下的场景管理数量和关联的区域总数。
    * 统计逻辑:
    * 1. 查询符合条件的场景管理列表
    * 2. 去重统计场景管理数量
    * 3. 获取场景管理关联的防控场景ID集合
    * 4. 查询防控场景并统计每个场景关联的区域划分数量
    * 5. 累加所有场景的区域数量得到总数
    * </p>
    *
    * @param filterTime 过滤时间,用于筛选有效的场景管理数据
    * @return 场景管理统计结果,包含场景管理数量和区域总数
    */
   @Override
   public SceneManageStatisticsVO statisticsSceneManageAndArea(Date filterTime) {
      List<FwDefenseSceneManageVO> sceneManageList = iFwDefenseSceneManageService.selectFwDefenseSceneManageCockpitList(buildFwBaseInfo(), filterTime);
      log.info("场景管理列表数据:{}", sceneManageList);
      SceneManageStatisticsVO statisticsVO = new SceneManageStatisticsVO();
      if (sceneManageList == null || sceneManageList.isEmpty()) {
         statisticsVO.setSceneManageCount(0);
         statisticsVO.setAreaCount(0);
         return statisticsVO;
      }
      // 场景管理数量:按 id 去重(JOIN 可能产生重复行)
      long sceneManageCount = sceneManageList.stream()
         .filter(vo -> vo != null && vo.getId() != null)
         .map(FwDefenseSceneManageVO::getId)
         .distinct()
         .count();
      statisticsVO.setSceneManageCount((int) sceneManageCount);
      // 区域数量:按防控场景ID去重后,统计每个防控场景关联的区域数量
      // 同一个防控场景可能被多个场景管理引用,只计一次
      Map<Long, String> defenseSceneAreaMap = new HashMap<>();
      for (FwDefenseSceneManageVO vo : sceneManageList) {
         if (vo != null && vo.getDefenseSceneId() != null) {
            defenseSceneAreaMap.putIfAbsent(vo.getDefenseSceneId(), vo.getDefenseSceneName());
         }
      }
      if (defenseSceneAreaMap.isEmpty()) {
         statisticsVO.setAreaCount(0);
         return statisticsVO;
      }
      // 批量查询防控场景,统计区域数量(同一防控场景只计一次)
      List<FwDefenseSceneEntity> defenseScenes = iFwDefenseSceneService.listByIds(defenseSceneAreaMap.keySet());
      Set<Long> countedAreaIds = new HashSet<>();
      int totalAreaCount = 0;
      if (defenseScenes != null) {
         for (FwDefenseSceneEntity scene : defenseScenes) {
            if (scene == null || scene.getId() == null) {
               continue;
            }
            List<Long> areaIds = Func.toLongList(scene.getAreaDivideIds());
            for (Long areaId : areaIds) {
               // 全局去重:同一区域ID被多个防控场景引用只计一次
               if (countedAreaIds.add(areaId)) {
                  totalAreaCount++;
               }
            }
         }
      }
      statisticsVO.setAreaCount(totalAreaCount);
      return statisticsVO;
   }
   /**
    *
    * @param filterSelected
    * @param sceneId
    * @param areaTypeKeys
    * @param isSetSceneManage
    * @param flyTime
    * @return
    */
   @Override
   public List<FwAreaDivideVO> selectFwAreaDivideList(Integer filterSelected, Long sceneId, String areaTypeKeys, Integer isSetSceneManage, LocalDateTime flyTime) {
      FwAreaDivideListParam fwAreaDivideListParam = new FwAreaDivideListParam();
      fwAreaDivideListParam.setFilterSelected(filterSelected);
      fwAreaDivideListParam.setSceneId(sceneId);
      fwAreaDivideListParam.setAreaTypeKeys(areaTypeKeys);
      fwAreaDivideListParam.setIsSetSceneManage(isSetSceneManage);
      fwAreaDivideListParam.setFlyTime(flyTime);
      // 解析区域类型键值列表
      fwAreaDivideListParam.setAreaTypeKeyList(Func.toStrList(areaTypeKeys));
      Dept dept = SysCache.getDept(Long.valueOf(AuthUtil.getDeptId()));
      Region byCode = RegionCache.getByCode(dept.getAreaCode());
      fwAreaDivideListParam.setCurrentDeptId(AuthUtil.getDeptId());
      fwAreaDivideListParam.setRegionCode(HeaderUtils.formatAreaCode(byCode.getCode()));
      return iFwAreaDivideService.selectFwAreaDivideList(fwAreaDivideListParam);
   }
   /**
    * 构建当前部门与区域信息
    *
    * @return 基础信息
    */
   private FwBaseInfo buildFwBaseInfo() {
      FwBaseInfo fwBaseInfo = new FwBaseInfo();
      Dept dept = SysCache.getDept(Long.valueOf(AuthUtil.getDeptId()));
      Region byCode = RegionCache.getByCode(dept.getAreaCode());
      fwBaseInfo.setCurrentDeptId(AuthUtil.getDeptId());
      fwBaseInfo.setRegionCode(HeaderUtils.formatAreaCode(byCode.getCode()));
      return fwBaseInfo;
   }
}