xuhy
2026-04-17 c903a3606fa825e5ecb9eaefa4127f6f9cd1fc07
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package com.xiaopeng.map.controller;
 
 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.xiaopeng.api.dto.map.XpPointExportExcelQuery;
import com.xiaopeng.api.dto.operation.PointAddDto;
import com.xiaopeng.api.dto.operation.PointUpdateDto;
import com.xiaopeng.api.dto.operation.XpPointEnableDisableDto;
import com.xiaopeng.api.feignClient.operation.XpCruisePlanClient;
import com.xiaopeng.api.model.operation.XpElectronicFence;
import com.xiaopeng.api.model.operation.XpElectronicFenceArea;
import com.xiaopeng.api.model.operation.XpPoint;
import com.xiaopeng.api.query.operation.PointListQuery;
import com.xiaopeng.api.query.operation.PointPageListListQuery;
import com.xiaopeng.common.core.domain.R;
import com.xiaopeng.common.core.exception.ServiceException;
import com.xiaopeng.common.core.utils.WebUtils;
import com.xiaopeng.common.core.web.controller.BaseController;
import com.xiaopeng.common.core.web.page.PageInfo;
import com.xiaopeng.map.constant.I18nKeyConstant;
import com.xiaopeng.map.exportExcel.XpPointExportExcel;
import com.xiaopeng.map.mangodb.service.XpGeoFencingPolygonService;
import com.xiaopeng.map.service.XpElectronicFenceAreaService;
import com.xiaopeng.map.service.XpElectronicFenceService;
import com.xiaopeng.map.service.XpPointService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shardingsphere.transaction.annotation.ShardingSphereTransactionType;
import org.apache.shardingsphere.transaction.core.TransactionType;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
 
 
@Slf4j
@RestController
@RequestMapping("/api/v1/point")
@Api(tags = "点位管理")
public class XpPointManagementController extends BaseController {
 
    private static final DateTimeFormatter EXPORT_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
 
    @Resource
    private XpPointService xpPointService;
    @Resource
    private XpElectronicFenceAreaService xpElectronicFenceAreaService;
    @Resource
    private XpElectronicFenceService xpElectronicFenceService;
    @Resource
    private XpGeoFencingPolygonService xpGeoFencingPolygonService;
    @Resource
    private XpCruisePlanClient xpCruisePlanClient;
 
 
    @ApiOperation(value = "获取所有运营中的区域", response = XpElectronicFenceArea.class)
    @PostMapping("/listElectronicFenceArea")
    public R<List<XpElectronicFenceArea>> listElectronicFenceArea() {
        // 获取所有启用的电子围栏
        List<XpElectronicFence> electronicFences = xpElectronicFenceService.lambdaQuery()
                .eq(XpElectronicFence::getStatus, 2).list();
        List<Long> electronicFenceIds = electronicFences.stream().map(XpElectronicFence::getId).collect(Collectors.toList());
        if (electronicFenceIds.isEmpty()) {
            return R.ok(new ArrayList<XpElectronicFenceArea>());
        }
        // 获取电子围栏下启用的运营区域
        List<XpElectronicFenceArea> electronicFenceAreaList = xpElectronicFenceAreaService.lambdaQuery().in(XpElectronicFenceArea::getElectronicId, electronicFenceIds)
                .eq(XpElectronicFenceArea::getAreaStatus, 1).list();
        return R.ok(electronicFenceAreaList);
    }
 
    @ApiOperation(value = "点位管理列表", response = XpPoint.class)
    @PostMapping("/list")
    public R<List<XpPoint>> list(@RequestBody PointListQuery query) {
        List<XpPoint> pageList = xpPointService.listAll(query);
        return R.ok(pageList);
    }
 
    @ApiOperation(value = "点位管理分页列表", response = XpPoint.class)
    @PostMapping("/pageList")
    public R<PageInfo<XpPoint>> pageList(@RequestBody PointPageListListQuery query) {
        PageInfo<XpPoint> pageList = xpPointService.pageList(query);
        return R.ok(pageList);
    }
 
    @PostMapping("/getByIds")
    public R<List<XpPoint>> getByIds(@RequestBody XpPointExportExcelQuery query) {
        List<XpPoint> list = xpPointService.lambdaQuery().in(XpPoint::getId, query.getPointIds()).list();
        return R.ok(list);
    }
 
    @ApiOperation(value = "点位导出")
    @PostMapping("/exportExcel")
    public void exportExcel(@RequestBody XpPointExportExcelQuery query) {
        List<Long> pointIds = query == null ? Collections.emptyList() : query.getPointIds();
        List<Long> distinctIds = pointIds == null
                ? Collections.emptyList()
                : pointIds.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList());
 
        List<XpPoint> points = distinctIds.isEmpty()
                ? Collections.emptyList()
                : xpPointService.lambdaQuery().in(XpPoint::getId, distinctIds).list();
        Map<Long, XpPoint> pointById = points.stream()
                .filter(p -> p.getId() != null)
                .collect(Collectors.toMap(XpPoint::getId, Function.identity(), (a, b) -> a));
 
        List<XpPointExportExcel> exportList = new ArrayList<>(points.size());
        for (Long id : distinctIds) {
            XpPoint xpPoint = pointById.get(id);
            if (xpPoint == null) {
                continue;
            }
            exportList.add(toExportRow(xpPoint));
        }
 
        HttpServletResponse response = WebUtils.response();
        if (response == null) {
            throw new ServiceException("导出失败:未获取到响应对象");
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding(StandardCharsets.UTF_8.name());
        try {
            String fileName = URLEncoder.encode("点位列表.xls", StandardCharsets.UTF_8.name());
            response.setHeader("Content-disposition", "attachment;filename=" + fileName);
            try (Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), XpPointExportExcel.class, exportList);
                 ServletOutputStream outputStream = response.getOutputStream()) {
                workbook.write(outputStream);
            }
        } catch (IOException e) {
            log.error("点位导出失败 pointIds={}", distinctIds, e);
            throw new ServiceException("点位导出失败");
        }
    }
 
    /**
     * 新增点位
     */
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "新增点位")
    public R<?> add(@Validated @RequestBody PointAddDto pointAddDto) {
        if (xpGeoFencingPolygonService.isInFence(pointAddDto.getPointLon(), pointAddDto.getPointLat(), pointAddDto.getFenceId(), "prohibited_area")) {
            return R.ok("in_prohibited_area");
        }
        return R.ok(xpPointService.addPointWithGeneratedNumber(pointAddDto));
    }
 
    /**
     * 批量删除点位
     */
    @GetMapping("/deletePoint")
    @ResponseBody
    @ApiOperation(value = "删除点位")
    public R deletePoint(@RequestParam("ids") String ids) {
        List<Long> pointIds = Arrays.stream(ids.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .map(Long::parseLong)
                .distinct()
                .collect(Collectors.toList());
        if (pointIds.isEmpty()) {
            return R.ok();
        }
        R<Boolean> existsResult = xpCruisePlanClient.existsCruisePlanPointByPointIds(pointIds);
        if (existsResult == null || existsResult.getData() == null) {
            throw new ServiceException("系统异常", I18nKeyConstant.SYSTEM_EXCEPTION);
        }
        if (Boolean.TRUE.equals(existsResult.getData())) {
            throw new ServiceException("操作失败,该点位有关联的巡游计划点位", I18nKeyConstant.POINT_DELETE_HAS_CRUISE_PLAN_POINT);
        }
        xpPointService.removeBatchByIds(pointIds);
        return R.ok();
    }
 
    /**
     * 编辑点位
     */
    @PostMapping("/edit")
    @ApiOperation(value = "编辑点位")
    @Transactional
    @ShardingSphereTransactionType(TransactionType.BASE)
    public R edit(@Validated @RequestBody PointUpdateDto pointAddDto) {
        XpPoint xpPoint = xpPointService.getById(pointAddDto.getId());
        if (xpPoint == null) {
            throw new ServiceException("系统异常", I18nKeyConstant.SYSTEM_EXCEPTION);
        }
        BeanUtils.copyProperties(pointAddDto, xpPoint);
        if (Integer.valueOf(2).equals(pointAddDto.getOperationStatus()) && Integer.valueOf(5).equals(pointAddDto.getVerificationStatus())) {
            Long electronicAreaId = xpPoint.getElectronicAreaId();
            if (electronicAreaId == null) {
                throw new ServiceException("当前点位不在围栏运营区域内,不能启用。", I18nKeyConstant.POINT_ENABLE_NOT_IN_OPERATING_AREA);
            }
            XpElectronicFenceArea electronicFenceArea = xpElectronicFenceAreaService.getById(electronicAreaId);
            if (electronicFenceArea == null || electronicFenceArea.getElectronicId() == null) {
                throw new ServiceException("运营区域不存在", I18nKeyConstant.POINT_ELECTRONIC_AREA_NOT_EXIST);
            }
            if (xpPoint.getPointLon() == null || xpPoint.getPointLat() == null) {
                throw new ServiceException("系统异常", I18nKeyConstant.SYSTEM_EXCEPTION);
            }
            String fenceId = String.valueOf(electronicFenceArea.getElectronicId());
            boolean inOperatingArea = xpGeoFencingPolygonService.isInFence(xpPoint.getPointLon(), xpPoint.getPointLat(), fenceId, "operating_area");
            if (!inOperatingArea) {
                throw new ServiceException("当前点位不在围栏运营区域内,不能启用。", I18nKeyConstant.POINT_ENABLE_NOT_IN_OPERATING_AREA);
            }
            boolean inProhibitedArea = xpGeoFencingPolygonService.isInFence(xpPoint.getPointLon(), xpPoint.getPointLat(), fenceId, "prohibited_area");
            if (inProhibitedArea) {
                throw new ServiceException("当前点位在禁行区内,不能启用。", I18nKeyConstant.POINT_ENABLE_IN_PROHIBITED_AREA);
            }
        }
        xpPoint.setFpElectronicAreaId(null);
        // 使用 lambdaUpdate 确保 pointPictures 为 null 时也能将数据库字段清空
        // (updateById 默认策略 NOT_NULL 会跳过 null 值,导致删除图片后数据库仍保留旧图片)
        xpPointService.lambdaUpdate()
                .eq(XpPoint::getId, xpPoint.getId())
                .set(XpPoint::getPointName, xpPoint.getPointName())
                .set(XpPoint::getPointPoi, xpPoint.getPointPoi())
                .set(XpPoint::getPointNumber, xpPoint.getPointNumber())
                .set(XpPoint::getOperationStatus, xpPoint.getOperationStatus())
                .set(XpPoint::getPointType, xpPoint.getPointType())
                .set(XpPoint::getVerificationStatus, xpPoint.getVerificationStatus())
                .set(XpPoint::getParkingTimeLimit, xpPoint.getParkingTimeLimit())
                .set(XpPoint::getOperationType, xpPoint.getOperationType())
                .set(XpPoint::getEffectivePeriod, xpPoint.getEffectivePeriod())
                .set(XpPoint::getEffectiveTimePeriod, xpPoint.getEffectiveTimePeriod())
                .set(XpPoint::getPointAddress, xpPoint.getPointAddress())
                .set(XpPoint::getRemarks, xpPoint.getRemarks())
                .set(XpPoint::getPointPictures, xpPoint.getPointPictures())
                .set(XpPoint::getPointLon, xpPoint.getPointLon())
                .set(XpPoint::getPointLat, xpPoint.getPointLat())
                .set(XpPoint::getElectronicAreaId, xpPoint.getElectronicAreaId())
                .set(XpPoint::getChargePoint, xpPoint.getChargePoint())
                .set(XpPoint::getParkingPoint, xpPoint.getParkingPoint())
                .set(XpPoint::getProvinceName, xpPoint.getProvinceName())
                .set(XpPoint::getProvinceCode, xpPoint.getProvinceCode())
                .set(XpPoint::getCityCode, xpPoint.getCityCode())
                .set(XpPoint::getCityName, xpPoint.getCityName())
                .set(XpPoint::getDistrictName, xpPoint.getDistrictName())
                .set(XpPoint::getDistrictCode, xpPoint.getDistrictCode())
                .set(XpPoint::getStationId, xpPoint.getStationId())
                .set(XpPoint::getStationSortNo, xpPoint.getStationSortNo())
                .update();
        return R.ok();
    }
 
    /**
     * 点位详情
     */
    @GetMapping("/api/v1/point/queryById")
    @ApiOperation(value = "点位详情")
    public R<XpPoint> queryById(@RequestParam Long id) {
        return R.ok(xpPointService.getById(id));
    }
 
    /**
     * 启用/禁用
     */
    @PostMapping("/enableDisable")
    @ApiOperation(value = "启用/禁用")
    public R enableDisable(@RequestBody XpPointEnableDisableDto dto) {
        XpPoint xpPoint = xpPointService.getById(dto.getId());
        xpPoint.setOperationStatus(dto.getOperationStatus());
        xpPoint.setVerificationStatus(dto.getVerificationStatus());
        xpPoint.setFpElectronicAreaId(null);
        xpPointService.updateById(xpPoint);
        return R.ok();
    }
 
    private static XpPointExportExcel toExportRow(XpPoint xpPoint) {
        XpPointExportExcel exportExcel = new XpPointExportExcel();
        exportExcel.setPointNumber(xpPoint.getPointNumber());
        exportExcel.setPointName(xpPoint.getPointName());
        exportExcel.setStatus(calcExportStatus(xpPoint.getOperationStatus(), xpPoint.getVerificationStatus()));
        exportExcel.setParkingTimeLimit(xpPoint.getParkingTimeLimit() == null ? null : String.valueOf(xpPoint.getParkingTimeLimit()));
        exportExcel.setPointLon(xpPoint.getPointLon());
        exportExcel.setPointLat(xpPoint.getPointLat());
        exportExcel.setCreateBy(xpPoint.getCreateBy());
        exportExcel.setCreateTime(formatExportTime(xpPoint.getCreateTime()));
        return exportExcel;
    }
 
    private static String formatExportTime(LocalDateTime time) {
        return time == null ? null : EXPORT_TIME_FORMATTER.format(time);
    }
 
    private static Integer calcExportStatus(Integer operationStatus, Integer verificationStatus) {
        if (operationStatus == null) {
            return null;
        }
        if (verificationStatus == 1) {
            return 1;
        }
        if (verificationStatus == 2) {
            return 2;
        }
        if (verificationStatus == 4) {
            return 3;
        }
        if (verificationStatus == 3 && operationStatus == 1) {
            return 4;
        }
        if (verificationStatus == 5 && operationStatus == 1) {
            return 5;
        }
        if (operationStatus == 2) {
            return 6;
        }
        if (operationStatus == 3) {
            return 7;
        }
        if (operationStatus == 4) {
            return 8;
        }
        if (operationStatus != 1 || verificationStatus == null) {
            return null;
        }
 
        return null;
    }
 
}