| | |
| | | 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.springframework.beans.BeanUtils; |
| | | 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; |
| | | |
| | | |
| | |
| | | @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; |
| | |
| | | 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("点位导出失败"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 新增点位 |
| | |
| | | // @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(); |
| | | } |
| | | |
| | |
| | | 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; |
| | | } |
| | | |
| | | } |