From 802fc74478ae3f2c4e8ab7cd417f97469c84f157 Mon Sep 17 00:00:00 2001
From: zhubaomin <zhubaomin>
Date: 星期二, 25 三月 2025 21:31:03 +0800
Subject: [PATCH] 轮灌功能

---
 pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectCtrl.java |  211 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 209 insertions(+), 2 deletions(-)

diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectCtrl.java
index d71764c..6e7c743 100644
--- a/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectCtrl.java
+++ b/pipIrr-platform/pipIrr-web/pipIrr-web-app/src/main/java/com/dy/pipIrrApp/inspect/InspectCtrl.java
@@ -1,9 +1,30 @@
 package com.dy.pipIrrApp.inspect;
 
+import com.alibaba.fastjson2.JSONArray;
+import com.alibaba.fastjson2.JSONObject;
+import com.dy.common.aop.SsoAop;
+import com.dy.common.util.IDLongGenerator;
+import com.dy.common.webUtil.BaseResponse;
+import com.dy.common.webUtil.BaseResponseUtils;
+import com.dy.common.webUtil.QueryResultVo;
+import com.dy.pipIrrApp.inspect.dto.Inspects;
+import com.dy.pipIrrApp.inspect.qo.QoInspect;
+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 org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.http.MediaType;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.validation.BindingResult;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Date;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
 
 /**
  * @author ZhuBaoMin
@@ -19,4 +40,190 @@
     @Autowired
     private InspectSv inspectSv;
 
+    private final IDLongGenerator idLongGenerator;
+
+    // 鍦扮悆鍗婂緞锛屽崟浣嶏細鍗冪背
+    private static final double EARTH_RADIUS = 6371.0;
+
+    public InspectCtrl(IDLongGenerator idLongGenerator) {
+        this.idLongGenerator = idLongGenerator;
+    }
+
+    /**
+     * 娣诲姞宸℃杞ㄨ抗
+     * @param list_Inspects 宸℃瀵硅薄鏁扮粍
+     * @param bindingResult
+     * @return
+     */
+    @PostMapping(path = "save", consumes = MediaType.APPLICATION_JSON_VALUE)
+    @Transactional(rollbackFor = Exception.class)
+    @SsoAop()
+    public BaseResponse<Boolean> save(@RequestBody @Valid List<Inspects> list_Inspects, BindingResult bindingResult){
+        if(bindingResult != null && bindingResult.hasErrors()){
+            return BaseResponseUtils.buildErrorMsg(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
+        }
+
+        JSONArray result_array = new JSONArray();
+        if(list_Inspects != null && list_Inspects.size() > 0) {
+            for (int i = 0; i < list_Inspects.size(); i++) {
+                Inspects inspects = list_Inspects.get(i);
+
+                Long inspectorId = inspects.getInspectorId();
+                Long inspectId = inspects.getInspectId();
+                Date startTime = inspects.getStartTime();
+                Date stopTime = inspects.getStopTime();
+                List<OpeTrack> tracks = inspects.getTracks();
+
+                if(tracks == null || tracks.size() == 0) {
+                    return BaseResponseUtils.buildErrorMsg("鏃犲贰妫�杞ㄨ抗");
+                }
+
+                if(inspectId == null || inspectId.equals(0)) {
+                    // 璇ュ贰妫�鏈笂浼犺繃璁板綍
+
+                    // 娣诲姞宸℃璁板綍
+                    OpeInspect opeInspect = new OpeInspect();
+                    opeInspect.setInspectorId(inspectorId);
+                    if(startTime != null) {
+                        opeInspect.setStartTime(startTime);
+                    }
+                    if(stopTime != null) {
+                        opeInspect.setStopTime(stopTime);
+                    }
+                    inspectId = inspectSv.addInspect(opeInspect);
+                    if(inspectId == null) {
+                        return BaseResponseUtils.buildErrorMsg("宸℃璁板綍娣诲姞澶辫触");
+                    }
+
+                    // 鏋勯�犺繑鍥炲��
+                    JSONObject inspect_job = new JSONObject();
+                    inspect_job.put("inspectorId", inspectorId.toString());
+                    inspect_job.put("inspectId", inspectId.toString());
+                    result_array.add(inspect_job);
+
+                    for (int j = 0; j < tracks.size(); j++) {
+                        tracks.get(j).setId(idLongGenerator.generate());
+                        tracks.get(j).setInspectId(inspectId);
+                    }
+                    Integer rec = inspectSv.insertTracks(tracks);
+                    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) {
+                        OpeInspect inspect = new OpeInspect();
+                        inspect.setId(inspectId);
+                        inspect.setStopTime(stopTime);
+                        inspectSv.updateInspect(inspect);
+                    }
+
+                    if(tracks != null && tracks.size() > 0) {
+                        for (int j = 0; j < tracks.size(); j++) {
+                            tracks.get(j).setId(idLongGenerator.generate());
+                            tracks.get(j).setInspectId(inspectId);
+                        }
+                        Integer rec = inspectSv.insertTracks(tracks);
+                        if(rec == null || rec == 0) {
+                            return BaseResponseUtils.buildErrorMsg("宸℃杞ㄨ抗娣诲姞澶辫触");
+                        }
+                    }
+                    Double distance = Optional.ofNullable(getInspectDistance(inspectId)).orElse(0.0);
+                    if(distance > 0) {
+                        inspectSv.updateInspectDistance(inspectId, distance);
+                    }
+                }
+            }
+            if(result_array != null && result_array.size() > 0) {
+                return BaseResponseUtils.buildSuccess(result_array) ;
+            }
+            return BaseResponseUtils.buildSuccess() ;
+        }
+        return BaseResponseUtils.buildErrorMsg("鎮ㄦ彁浜ょ殑宸℃杞ㄨ抗涓虹┖");
+    }
+
+    /**
+     * 鏌ヨ宸℃杞ㄨ抗
+     * @param qo
+     * @return
+     */
+    @GetMapping(path = "/getInspects")
+    @SsoAop()
+    public BaseResponse<QueryResultVo<List<VoInspect>>> getInspects(QoInspect qo){
+
+        try{
+            QueryResultVo<List<VoInspect>> res = inspectSv.getInspects(qo);
+            return BaseResponseUtils.buildSuccess(res);
+        }catch (Exception e){
+            log.error("鑾峰彇宸℃鍒楄〃寮傚父", e);
+            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;
+    }
+
+    /**
+     * 鏍规嵁宸℃鍛業D鑾峰彇宸℃鍒楄〃
+     * @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());
+        }
+    }
 }

--
Gitblit v1.8.0