|  |  | 
 |  |  | import com.dy.pipIrrGlobal.pojoOp.OpeInspect; | 
 |  |  | import com.dy.pipIrrGlobal.pojoOp.OpeTrack; | 
 |  |  | import com.dy.pipIrrGlobal.voOp.VoInspect; | 
 |  |  | import com.dy.pipIrrGlobal.voOp.VoTrackPoint; | 
 |  |  | import jakarta.validation.Valid; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
 |  |  | 
 |  |  | import java.util.Date; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Objects; | 
 |  |  | import java.util.Optional; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * @author ZhuBaoMin | 
 |  |  | 
 |  |  |     private InspectSv inspectSv; | 
 |  |  |  | 
 |  |  |     private final IDLongGenerator idLongGenerator; | 
 |  |  |  | 
 |  |  |     // 地球半径,单位:千米 | 
 |  |  |     private static final double EARTH_RADIUS = 6371.0; | 
 |  |  |  | 
 |  |  |     public InspectCtrl(IDLongGenerator idLongGenerator) { | 
 |  |  |         this.idLongGenerator = idLongGenerator; | 
 |  |  | 
 |  |  |                     if(rec == null || rec == 0) { | 
 |  |  |                         return BaseResponseUtils.buildErrorMsg("巡检轨迹添加失败"); | 
 |  |  |                     } | 
 |  |  |  | 
 |  |  |                     Double distance = Optional.ofNullable(getInspectDistance(inspectId)).orElse(0.0); | 
 |  |  |                     if(distance > 0) { | 
 |  |  |                         inspectSv.updateInspectDistance(inspectId, distance); | 
 |  |  |                     } | 
 |  |  |                 }else { | 
 |  |  |                     // 该巡检已上传过记录,续传 | 
 |  |  |                     if(stopTime != null) { | 
 |  |  | 
 |  |  |                         if(rec == null || rec == 0) { | 
 |  |  |                             return BaseResponseUtils.buildErrorMsg("巡检轨迹添加失败"); | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                     Double distance = Optional.ofNullable(getInspectDistance(inspectId)).orElse(0.0); | 
 |  |  |                     if(distance > 0) { | 
 |  |  |                         inspectSv.updateInspectDistance(inspectId, distance); | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据巡检ID获取轨迹总距离 | 
 |  |  |      * @param inspectId | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     public double getInspectDistance(Long inspectId) { | 
 |  |  |         List<VoTrackPoint> points = inspectSv.getTrackPointsById(inspectId); | 
 |  |  |         if(points == null || points.size() == 0) { | 
 |  |  |             return 0.0; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         double totalDistance = calculateTotalDistance(points); | 
 |  |  |         return totalDistance; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     // 计算轨迹的总距离 | 
 |  |  |     public static double calculateTotalDistance(List<VoTrackPoint> points) { | 
 |  |  |         double totalDistance = 0.0; | 
 |  |  |  | 
 |  |  |         for (int i = 0; i < points.size() - 1; i++) { | 
 |  |  |             VoTrackPoint currentPoint = points.get(i); | 
 |  |  |             VoTrackPoint nextPoint = points.get(i + 1); | 
 |  |  |  | 
 |  |  |             double distance = haversine( currentPoint.getLat().doubleValue(), currentPoint.getLng().doubleValue(), nextPoint.getLat().doubleValue(), nextPoint.getLng().doubleValue()); | 
 |  |  |  | 
 |  |  |             totalDistance += distance; | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return totalDistance; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     // Haversine公式计算两个经纬度点之间的距离 | 
 |  |  |     public static double haversine(double lat1, double lon1, double lat2, double lon2) { | 
 |  |  |         double dLat = Math.toRadians(lat2 - lat1); | 
 |  |  |         double dLon = Math.toRadians(lon2 - lon1); | 
 |  |  |  | 
 |  |  |         double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + | 
 |  |  |                 Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * | 
 |  |  |                         Math.sin(dLon / 2) * Math.sin(dLon / 2); | 
 |  |  |  | 
 |  |  |         double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); | 
 |  |  |  | 
 |  |  |         return EARTH_RADIUS * c; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 根据巡检员ID获取巡检列表 | 
 |  |  |      * @param vo | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @GetMapping(path = "/getInspectRecords") | 
 |  |  |     @SsoAop() | 
 |  |  |     public BaseResponse<QueryResultVo<List<VoInspect>>> getInspectRecords(QoInspect vo) { | 
 |  |  |         try { | 
 |  |  |             QueryResultVo<List<VoInspect>> res = inspectSv.getInstectsByInspectorId(vo); | 
 |  |  |             return BaseResponseUtils.buildSuccess(res); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("获取迅疾爱你记录异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()); | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  | } |