吉安感知网项目-后端
linwei
2026-02-26 0a79e4295745bc59c0f8b8624218df7a226d14b9
drone-service/drone-gd/src/main/java/org/sxkj/gd/workorder/service/impl/GdDeviceCallServiceImpl.java
New file
@@ -0,0 +1,148 @@
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.sxkj.gd.workorder.service.impl;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.utils.StringUtil;
import org.sxkj.gd.workorder.dto.GdDeviceCallDetailExternalSaveDTO;
import org.sxkj.gd.workorder.entity.GdDeviceCallDetailEntity;
import org.sxkj.gd.workorder.entity.GdDeviceCallEntity;
import org.sxkj.gd.workorder.entity.GdManageDeviceEntity;
import org.sxkj.gd.workorder.entity.GdPatrolTaskEntity;
import org.sxkj.gd.workorder.service.IGdDeviceCallDetailService;
import org.sxkj.gd.workorder.service.IGdManageDeviceService;
import org.sxkj.gd.workorder.service.IGdPatrolTaskService;
import org.sxkj.gd.workorder.vo.GdDeviceCallVO;
import org.sxkj.gd.workorder.excel.GdDeviceCallExcel;
import org.sxkj.gd.workorder.mapper.GdDeviceCallMapper;
import org.sxkj.gd.workorder.service.IGdDeviceCallService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.sxkj.system.cache.SysCache;
import org.sxkj.system.cache.UserCache;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
/**
 * 设备调用表 服务实现类
 *
 * @author Aix
 * @since 2026-02-26
 */
@Service
@AllArgsConstructor
public class GdDeviceCallServiceImpl extends BaseServiceImpl<GdDeviceCallMapper, GdDeviceCallEntity> implements IGdDeviceCallService {
   private final IGdDeviceCallDetailService gdDeviceCallDetailService;
   private final IGdPatrolTaskService gdPatrolTaskService;
   private final IGdManageDeviceService gdManageDeviceService;
   @Override
   public IPage<GdDeviceCallVO> selectGdDeviceCallPage(IPage<GdDeviceCallVO> page, GdDeviceCallVO gdDeviceCall) {
      return page.setRecords(baseMapper.selectGdDeviceCallPage(page, gdDeviceCall));
   }
   @Override
   public boolean saveGdDeviceCallExternal(List<GdDeviceCallDetailExternalSaveDTO> dtos) {
      // 保存设备调用
      GdPatrolTaskEntity patrolTask = gdPatrolTaskService.getById(dtos.get(0).getPatrolTaskId());
      if (patrolTask == null) {
         return false;
      }
      GdDeviceCallEntity callEntity = new GdDeviceCallEntity();
      callEntity.setId(patrolTask.getId());
      callEntity.setPatrolTaskName(patrolTask.getPatrolTaskName());
      callEntity.setTaskDepartment(SysCache.getDeptName(patrolTask.getCreateDept()));
      callEntity.setTaskDepartmentId(patrolTask.getCreateDept());
      callEntity.setTaskInitiator(UserCache.getUser(patrolTask.getCreateUser()).getName());
      callEntity.setTaskInitiatorId(patrolTask.getCreateUser());
      callEntity.setPlanExecuteTime(patrolTask.getExecuteTime());
      callEntity.setActualExecuteTime(dtos.get(0).getOccurTime());// 第一条数据的发生时间
      // 计算飞行时长(毫秒转分钟)
      // 计算飞行时长(处理可能的空值情况)
      Date startTime = dtos.get(0).getOccurTime();
      Date endTime = dtos.get(dtos.size() - 1).getOccurTime();
      if (startTime == null || endTime == null) {
         log.warn("时间数据不完整,无法计算飞行时长");
         callEntity.setFlightDuration(0L);
      } else {
         long flightDurationMs = endTime.getTime() - startTime.getTime();
         // 可以选择不同的时间单位
         callEntity.setFlightDuration(flightDurationMs / 1000); // 秒
      }
      // 计算总飞行距离
      double totalDistance = dtos.stream()
         .mapToDouble(GdDeviceCallDetailExternalSaveDTO::getFlightDistance)
         .sum();
      callEntity.setFlightDistance(totalDistance);
      // 设备ID&名称
      String deviceSn = dtos.get(0).getDeviceSn();
      Long deviceId = null;
      String deviceName = null;
      if (StringUtil.hasText(deviceSn)) {
         GdManageDeviceEntity gdManageDevice = gdManageDeviceService.getOne(
            Wrappers.lambdaQuery(GdManageDeviceEntity.class)
               .eq(GdManageDeviceEntity::getDeviceSn, deviceSn),
            false
         );
         deviceId = gdManageDevice.getId();
         deviceName = gdManageDevice.getDeviceName();
      }
      callEntity.setDeviceId(deviceId);
      callEntity.setDeviceName(deviceName);
      // 保存设备调用详情
      if (baseMapper.insert(callEntity) <= 0) {
         return false;
      }
      List<GdDeviceCallDetailEntity> detailEntities = dtos.stream().map(dto -> {
         GdDeviceCallDetailEntity entity = new GdDeviceCallDetailEntity();
         entity.setCallId(dto.getPatrolTaskId());
         entity.setDeviceStatus(dto.getDeviceStatus());
         entity.setOccurTime(dto.getOccurTime());
         entity.setFlightDistance(dto.getFlightDistance());
         entity.setSort(dto.getSort());
         return entity;
      }).collect(Collectors.toList());
      return gdDeviceCallDetailService.saveBatch(detailEntities);
   }
   @Override
   public List<GdDeviceCallExcel> exportGdDeviceCall(Wrapper<GdDeviceCallEntity> queryWrapper) {
      List<GdDeviceCallExcel> gdDeviceCallList = baseMapper.exportGdDeviceCall(queryWrapper);
      //gdDeviceCallList.forEach(gdDeviceCall -> {
      //   gdDeviceCall.setTypeName(DictCache.getValue(DictEnum.YES_NO, GdDeviceCall.getType()));
      //});
      return gdDeviceCallList;
   }
}