zhubaomin
2025-02-18 3bb15fc20653b796df0d83770bcb37ba3578b26d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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.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
 * @date 2024-09-24 10:21
 * @LastEditTime 2024-09-24 10:21
 * @Description
 */
 
@Slf4j
@RestController
@RequestMapping(path="inspect")
public class InspectCtrl {
    @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;
    }
 
    /**
     * 根据巡检员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());
        }
    }
}