liusuyi
2026-04-22 04a270ff2b5919127524913ffc45c59c6c6d9201
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package com.ard.data.region.service.impl;
 
import com.ard.common.constant.RegionConstants;
import com.ard.common.utils.DateUtils;
import com.ard.common.utils.SecurityUtils;
import com.ard.common.utils.StringUtils;
import com.ard.data.region.domain.ArdDeptRegion;
import com.ard.data.region.domain.ConflictCheckResult;
import com.ard.data.region.domain.ConflictRegion;
import com.ard.data.region.domain.Coordinate;
import com.ard.data.region.mapper.ArdDeptRegionMapper;
import com.ard.data.region.service.IArdDeptRegionService;
import com.ard.data.region.utils.RegionCoordinateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 部门区域Service业务层处理
 *
 * @author ruoyi
 */
@Slf4j
@Service
public class ArdDeptRegionServiceImpl implements IArdDeptRegionService
{
    @Autowired
    private ArdDeptRegionMapper ardDeptRegionMapper;
 
    @Override
    public ArdDeptRegion selectArdDeptRegionById(Long id)
    {
        ArdDeptRegion region = ardDeptRegionMapper.selectArdDeptRegionById(id);
        if (region != null)
        {
            region.setCoordinates(RegionCoordinateUtils.toCoordinates(region.getBoundaryWkt()));
        }
        return region;
    }
 
    @Override
    public List<ArdDeptRegion> selectArdDeptRegionList(ArdDeptRegion ardDeptRegion)
    {
        List<ArdDeptRegion> list = ardDeptRegionMapper.selectArdDeptRegionList(ardDeptRegion);
        list.forEach(region -> {
            region.setCoordinates(RegionCoordinateUtils.toCoordinates(region.getBoundaryWkt()));
        });
        return list;
    }
 
    /**
     * 校验区域名称是否唯一
     */
    @Override
    public boolean checkRegionNameUnique(ArdDeptRegion ardDeptRegion)
    {
        Long id = StringUtils.isNull(ardDeptRegion.getId()) ? -1L : ardDeptRegion.getId();
        ArdDeptRegion info = ardDeptRegionMapper.checkRegionNameUnique(ardDeptRegion.getRegionName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != id.longValue())
        {
            return RegionConstants.NOT_UNIQUE;
        }
        return RegionConstants.UNIQUE;
    }
 
    /**
     * 检查区域冲突
     */
    @Override
    public ConflictCheckResult checkConflict(ArdDeptRegion ardDeptRegion)
    {
        ConflictCheckResult result = new ConflictCheckResult();
 
        try
        {
            // 1. 验证坐标
            List<Coordinate> coordinates = ardDeptRegion.getCoordinates();
            RegionCoordinateUtils.validateCoordinates(coordinates);
 
            // 2. 根据坐标数量自动判断类型
            String regionType = RegionCoordinateUtils.getRegionTypeByCoordinates(coordinates);
            ardDeptRegion.setRegionType(regionType);
 
            // 3. 转换为WKT
            String wkt = RegionCoordinateUtils.toWkt(coordinates);
            ardDeptRegion.setBoundaryWkt(wkt);
 
            // 4. 根据类型检查冲突
            List<ConflictRegion> conflicts = null;
            Long excludeId = ardDeptRegion.getId();
 
            switch (regionType)
            {
                case "POINT":
                    conflicts = ardDeptRegionMapper.checkPointConflict(
                            ardDeptRegion.getDeptId(), wkt, excludeId);
                    break;
                case "LINE":
                    conflicts = ardDeptRegionMapper.checkLineConflict(
                            ardDeptRegion.getDeptId(), wkt, excludeId);
                    break;
                case "POLYGON":
                    conflicts = ardDeptRegionMapper.checkPolygonConflict(
                            ardDeptRegion.getDeptId(), wkt, excludeId);
                    break;
                default:
                    result.setHasConflict(true);
                    result.setMessage("不支持的几何类型");
                    return result;
            }
 
            if (!CollectionUtils.isEmpty(conflicts))
            {
                result.setHasConflict(true);
                result.setMessage(String.format("该%s与%d个已有区域存在冲突",
                        getRegionTypeName(regionType), conflicts.size()));
                result.setConflictRegions(conflicts);
            }
            else
            {
                result.setHasConflict(false);
                result.setMessage("无冲突,可以添加");
            }
        }
        catch (Exception e)
        {
            log.error("检查冲突失败", e);
            result.setHasConflict(true);
            result.setMessage("检查失败: " + e.getMessage());
        }
 
        return result;
    }
 
    @Override
    @Transactional
    public int insertArdDeptRegion(ArdDeptRegion ardDeptRegion)
    {
        // 1. 检查冲突
        ConflictCheckResult checkResult = checkConflict(ardDeptRegion);
        if (checkResult.getHasConflict())
        {
            throw new RuntimeException(checkResult.getMessage());
        }
 
        // 2. 计算中心点和面积
        List<Coordinate> coordinates = ardDeptRegion.getCoordinates();
        Coordinate center = RegionCoordinateUtils.calculateCenter(coordinates);
        ardDeptRegion.setCenterLat(center != null ? center.getLat() : null);
        ardDeptRegion.setCenterLng(center != null ? center.getLng() : null);
 
        // 只有面类型计算面积
        if ("POLYGON".equals(ardDeptRegion.getRegionType()))
        {
            ardDeptRegion.setAreaSize(RegionCoordinateUtils.calculateArea(coordinates));
        }
        else
        {
            ardDeptRegion.setAreaSize(0.0);
        }
 
        // 3. 设置默认颜色
        if (ardDeptRegion.getRegionColor() == null || ardDeptRegion.getRegionColor().isEmpty())
        {
            ardDeptRegion.setRegionColor("#3c8dbc");
        }
 
        // 4. 插入数据
        ardDeptRegion.setCreateBy(SecurityUtils.getUsername());
        ardDeptRegion.setCreateTime(DateUtils.getNowDate());
        ardDeptRegion.setStatus("0");
 
        return ardDeptRegionMapper.insertArdDeptRegion(ardDeptRegion);
    }
 
    @Override
    @Transactional
    public int updateArdDeptRegion(ArdDeptRegion ardDeptRegion)
    {
        // 1. 获取原区域
        ArdDeptRegion oldRegion = selectArdDeptRegionById(ardDeptRegion.getId());
        if (oldRegion == null)
        {
            throw new RuntimeException("区域不存在");
        }
 
        // 2. 如果坐标有变化,检查冲突
        if (ardDeptRegion.getCoordinates() != null && !ardDeptRegion.getCoordinates().isEmpty())
        {
            List<Coordinate> coordinates = ardDeptRegion.getCoordinates();
            RegionCoordinateUtils.validateCoordinates(coordinates);
 
            String regionType = RegionCoordinateUtils.getRegionTypeByCoordinates(coordinates);
            ardDeptRegion.setRegionType(regionType);
 
            String wkt = RegionCoordinateUtils.toWkt(coordinates);
            ardDeptRegion.setBoundaryWkt(wkt);
 
            // 根据类型检查冲突
            List<ConflictRegion> conflicts = null;
 
            switch (regionType)
            {
                case "POINT":
                    conflicts = ardDeptRegionMapper.checkPointConflict(
                            ardDeptRegion.getDeptId(), wkt, ardDeptRegion.getId());
                    break;
                case "LINE":
                    conflicts = ardDeptRegionMapper.checkLineConflict(
                            ardDeptRegion.getDeptId(), wkt, ardDeptRegion.getId());
                    break;
                case "POLYGON":
                    conflicts = ardDeptRegionMapper.checkPolygonConflict(
                            ardDeptRegion.getDeptId(), wkt, ardDeptRegion.getId());
                    break;
            }
 
            if (!CollectionUtils.isEmpty(conflicts))
            {
                throw new RuntimeException("更新后的区域与已有区域存在冲突");
            }
 
            // 重新计算中心点和面积
            Coordinate center = RegionCoordinateUtils.calculateCenter(coordinates);
            ardDeptRegion.setCenterLat(center != null ? center.getLat() : null);
            ardDeptRegion.setCenterLng(center != null ? center.getLng() : null);
 
            if ("POLYGON".equals(regionType))
            {
                ardDeptRegion.setAreaSize(RegionCoordinateUtils.calculateArea(coordinates));
            }
            else
            {
                ardDeptRegion.setAreaSize(0.0);
            }
        }
 
        ardDeptRegion.setUpdateBy(SecurityUtils.getUsername());
        ardDeptRegion.setUpdateTime(DateUtils.getNowDate());
 
        return ardDeptRegionMapper.updateArdDeptRegion(ardDeptRegion);
    }
 
    /**
     * 修改状态(启用/禁用)
     */
    @Override
    public int updateStatus(Long id, String status)
    {
        // 检查区域是否存在
        ArdDeptRegion region = selectArdDeptRegionById(id);
        if (region == null)
        {
            throw new RuntimeException("区域不存在");
        }
 
        // 更新状态
        return ardDeptRegionMapper.updateStatus(id, status);
    }
 
    @Override
    public ArdDeptRegion selectRegionByPoint(Double lat, Double lng)
    {
        String pointWkt = String.format("POINT(%f %f)", lat, lng);
        return ardDeptRegionMapper.selectRegionByPoint(pointWkt);
    }
 
    @Override
    public int deleteArdDeptRegionByIds(String[] ids)
    {
        return ardDeptRegionMapper.deleteArdDeptRegionByIds(ids);
    }
 
    @Override
    public int deleteArdDeptRegionById(Long id)
    {
        return ardDeptRegionMapper.deleteArdDeptRegionById(id);
    }
 
    /**
     * 获取区域类型中文名
     */
    private String getRegionTypeName(String type)
    {
        switch (type)
        {
            case "POINT": return "点";
            case "LINE": return "线";
            case "POLYGON": return "面";
            default: return type;
        }
    }
}