puzhibing
2026-08-10 7c0d083f31217f84ecf4cb8009891a088a099624
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
package com.stylefeng.guns.modular.crossCity.server.impl;
 
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stylefeng.guns.modular.crossCity.dao.LocationMapper;
import com.stylefeng.guns.modular.crossCity.dao.SiteMapper;
import com.stylefeng.guns.modular.crossCity.model.Site;
import com.stylefeng.guns.modular.crossCity.server.ISiteService;
import com.stylefeng.guns.modular.crossCity.warpper.SiteWarpper;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.*;
 
 
@Service
public class SiteServiceImpl extends ServiceImpl<SiteMapper, Site> implements ISiteService {
 
    @Resource
    private SiteMapper siteMapper;
 
    @Resource
    private LocationMapper locationMapper;
 
    private static final double COORDINATE_EPSILON = 1E-10;
 
 
    /**
     * 获取站点
     * @return
     * @throws Exception
     */
    @Override
    public List<SiteWarpper> querySite(Integer startSiteId) throws Exception {
        List<Map<String, Object>> sites = null;
        if(null == startSiteId){
            sites = siteMapper.querySite(null);
        }else{
            sites = siteMapper.querySite(startSiteId);
        }
        Set<String> set = new HashSet<>();
        for(Map<String, Object> site : sites) {
            set.add(site.get("cityCode").toString());
        }
 
        List<SiteWarpper> list = new ArrayList<>();
        for(String c : set){
            SiteWarpper siteWarpper = new SiteWarpper();
            List<Object> data = new ArrayList<>();
            String name = "";
            String code = "";
            for(Map<String, Object> s : sites){
                if(c.equals(s.get("cityCode").toString())){
                    Map<String, Object> map = new HashMap<>();
                    map.put("id", s.get("id"));
                    map.put("name", s.get("name").toString());
                    name = s.get("cityName").toString();
                    code = s.get("cityCode").toString();
                    data.add(map);
                }
            }
            siteWarpper.setName(name);
            siteWarpper.setCode(code);
            siteWarpper.setSites(data);
            list.add(siteWarpper);
        }
        return list;
    }
 
 
    /**
     * 判断点是都在区域范围内
     * @param siteId
     * @param code
     * @param lonLat
     * @return
     * @throws Exception
     */
    @Override
    public boolean areaMonitoring(Integer siteId, String code, String lonLat) throws Exception {
        List<Map<String, Object>> list = this.queryLocation(siteId);
        for(Map<String, Object> map : list){
            if(Integer.valueOf(map.get("type").toString()) == 1){//行政区域
                if(null != map.get("districtCode")){
                    if(code.equals(map.get("districtCode").toString())){
                        return true;
                    }
                    continue;
                }
                if(null != map.get("cityCode")){
                    if(code.equals(map.get("cityCode").toString())){
                        return true;
                    }
                    code = code.substring(0, 4) + "00";
                    if(code.equals(map.get("cityCode").toString())){
                        return true;
                    }
                    continue;
                }
                if(null != map.get("provinceCode")){
                    code = code.substring(0, 2) + "0000";
                    if(code.equals(map.get("provinceCode").toString())){
                        return true;
                    }
                    continue;
                }
            }
            if(Integer.valueOf(map.get("type").toString()) == 2){//电子围栏
                Object coordinate = map.get("coordinate");
                if(coordinate != null && isPointInElectronicFence(lonLat, coordinate.toString())){
                    return true;
                }
                continue;
            }
        }
        return false;
    }
 
    static boolean isPointInElectronicFence(String lonLat, String coordinate) {
        double[] point = parseCoordinate(lonLat);
        if (point == null || coordinate == null || coordinate.trim().isEmpty()) {
            return false;
        }
        String[] fences = coordinate.split("_");
        for (String fence : fences) {
            String[] coordinatePoints = fence.split(";");
            if (coordinatePoints.length < 3) {
                continue;
            }
            List<double[]> polygon = new ArrayList<>();
            boolean validPolygon = true;
            for (String coordinatePoint : coordinatePoints) {
                double[] vertex = parseCoordinate(coordinatePoint);
                if (vertex == null) {
                    validPolygon = false;
                    break;
                }
                polygon.add(vertex);
            }
            if (validPolygon && isPointInPolygon(point, polygon)) {
                return true;
            }
        }
        return false;
    }
 
    private static double[] parseCoordinate(String coordinate) {
        if (coordinate == null || coordinate.trim().isEmpty()) {
            return null;
        }
        String[] values = coordinate.trim().split(",");
        if (values.length != 2) {
            return null;
        }
        try {
            double longitude = Double.parseDouble(values[0].trim());
            double latitude = Double.parseDouble(values[1].trim());
            if (Double.isNaN(longitude) || Double.isInfinite(longitude)
                    || Double.isNaN(latitude) || Double.isInfinite(latitude)) {
                return null;
            }
            return new double[]{longitude, latitude};
        } catch (NumberFormatException e) {
            return null;
        }
    }
 
    private static boolean isPointInPolygon(double[] point, List<double[]> polygon) {
        boolean inside = false;
        for (int i = 0, j = polygon.size() - 1; i < polygon.size(); j = i++) {
            double[] start = polygon.get(j);
            double[] end = polygon.get(i);
            if (isPointOnSegment(point, start, end)) {
                return true;
            }
            boolean intersects = (start[1] > point[1]) != (end[1] > point[1])
                    && point[0] < (end[0] - start[0]) * (point[1] - start[1])
                    / (end[1] - start[1]) + start[0];
            if (intersects) {
                inside = !inside;
            }
        }
        return inside;
    }
 
    private static boolean isPointOnSegment(double[] point, double[] start, double[] end) {
        double crossProduct = (point[0] - start[0]) * (end[1] - start[1])
                - (point[1] - start[1]) * (end[0] - start[0]);
        if (Math.abs(crossProduct) > COORDINATE_EPSILON) {
            return false;
        }
        return point[0] >= Math.min(start[0], end[0]) - COORDINATE_EPSILON
                && point[0] <= Math.max(start[0], end[0]) + COORDINATE_EPSILON
                && point[1] >= Math.min(start[1], end[1]) - COORDINATE_EPSILON
                && point[1] <= Math.max(start[1], end[1]) + COORDINATE_EPSILON;
    }
 
    /**
     * 根据站点id获取地点区域
     * @param siteId
     * @return
     * @throws Exception
     */
    @Override
    public List<Map<String, Object>> queryLocation(Integer siteId) throws Exception {
        return locationMapper.queryLocation(siteId);
    }
}