puzhibing
2026-03-12 e031959940a80792a60e2d8b83499cd168fa83d6
xp-robotaxi-map-boot/src/main/java/com/xiaopeng/map/controller/XpPointManagementController.java
@@ -10,23 +10,43 @@
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.api.dto.map.XpPointExportExcelQuery;
import com.xiaopeng.map.exportExcel.XpPointExportExcel;
import com.xiaopeng.map.service.XpElectronicFenceAreaService;
import com.xiaopeng.map.service.XpElectronicFenceService;
import com.xiaopeng.map.service.XpPointService;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
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.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
@@ -35,6 +55,8 @@
@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;
@@ -79,6 +101,49 @@
        List<XpPoint> list = xpPointService.lambdaQuery().in(XpPoint::getId, query.getPointIds()).list();
        return R.ok(list);
    }
    // "2029885249115987970" "2029883649798512642"
    @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("点位导出失败");
        }
    }
    /**
     * 新增点位
@@ -86,8 +151,6 @@
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "新增点位")
//    @Transactional
//    @ShardingSphereTransactionType(TransactionType.BASE)
    public R<XpPoint> add(@Validated @RequestBody PointAddDto pointAddDto) {
        return R.ok(xpPointService.addPointWithGeneratedNumber(pointAddDto));
    }
@@ -98,8 +161,6 @@
    @GetMapping("/deletePoint")
    @ResponseBody
    @ApiOperation(value = "删除点位")
//    @Transactional
//    @ShardingSphereTransactionType(TransactionType.BASE)
    public R deletePoint(@RequestParam("ids") String ids) {
        xpPointService.removeBatchByIds(Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList()));
        return R.ok();
@@ -110,15 +171,20 @@
     */
    @PostMapping("/edit")
    @ApiOperation(value = "编辑点位")
//    @Transactional
//    @ShardingSphereTransactionType(TransactionType.BASE)
    @Transactional
    @ShardingSphereTransactionType(TransactionType.BASE)
    public R edit(@Validated @RequestBody PointUpdateDto pointAddDto) {
        XpPoint xpPoint = xpPointService.getById(pointAddDto.getId());
        //先删除原有的数据,因为会存在点位的从属区域变化的情况,所以先删除原有的数据后添加新的数据重新分片到新的库中
        xpPointService.removeById(pointAddDto.getId());
        BeanUtils.copyProperties(pointAddDto, xpPoint);
        xpPoint.setId(null);
        xpPointService.save(xpPoint);
        if (!xpPoint.getElectronicAreaId().equals(pointAddDto.getElectronicAreaId())) {
            BeanUtils.copyProperties(pointAddDto, xpPoint);
            xpPointService.updateById(xpPoint);
        } else {
            //先删除原有的数据,因为会存在点位的从属区域变化的情况,所以先删除原有的数据后添加新的数据重新分片到新的库中
            BeanUtils.copyProperties(pointAddDto, xpPoint);
            xpPointService.removeById(pointAddDto.getId());
            xpPoint.setId(null);
            xpPointService.save(xpPoint);
        }
        return R.ok();
    }
@@ -136,8 +202,6 @@
     */
    @PostMapping("/enableDisable")
    @ApiOperation(value = "启用/禁用")
//    @Transactional
//    @ShardingSphereTransactionType(TransactionType.BASE)
    public R enableDisable(@RequestBody XpPointEnableDisableDto dto) {
        XpPoint xpPoint = xpPointService.getById(dto.getId());
        xpPoint.setOperationStatus(dto.getOperationStatus());
@@ -147,5 +211,56 @@
        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;
    }
}