package com.xiaopeng.operation.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaopeng.api.dto.order.HistoryEndPointsDto;
import com.xiaopeng.api.dto.order.HistoryStartPointsDto;
import com.xiaopeng.api.model.operation.XpPoint;
import com.xiaopeng.api.model.operation.XpSystemSetting;
import com.xiaopeng.api.vo.order.XpHistoryEndPointsVo;
import com.xiaopeng.api.vo.order.XpHistoryStartPointsVo;
import com.xiaopeng.api.vo.order.XpPointsListVo;
import com.xiaopeng.api.vo.order.XpPointsSearchVo;
import com.xiaopeng.common.core.constant.CacheConstants;
import com.xiaopeng.common.redis.service.RedisService;
import com.xiaopeng.operation.dto.NearestDto;
import com.xiaopeng.operation.mapper.XpPointMapper;
import com.xiaopeng.operation.mapper.XpSystemSettingMapper;
import com.xiaopeng.operation.query.PointByNameQuery;
import com.xiaopeng.api.dto.operation.PointMapQuery;
import com.xiaopeng.api.query.operation.PointPageListQuery;
import com.xiaopeng.operation.service.XpPointService;
import com.xiaopeng.operation.utils.map.FastDistanceCalculator;
import com.xiaopeng.operation.vo.NearestNextVo;
import com.xiaopeng.operation.vo.NearestVo;
import com.xiaopeng.operation.vo.OrderDistanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
*
* 点位管理 服务实现类
*
*
* @author liu
* @author WuGuanFengYue
* @since 2026-02-03
*/
@Service
public class XpPointServiceImpl extends ServiceImpl implements XpPointService {
@Resource
private XpSystemSettingMapper xpSystemSettingMapper;
@Resource
private RedisService redisService;
@Override
public NearestVo nearest(NearestDto dto) {
// 获取所有的站点信息
List list = list(new LambdaQueryWrapper().eq(XpPoint::getOperationStatus, 2).isNotNull(XpPoint::getElectronicAreaId));
NearestVo nearestVo = new NearestVo();
List nearestNextVos = new ArrayList<>();
// 走缓存
Object orderDistanceObj = redisService.getCacheObject(CacheConstants.NEAR_POINT_DISTANCE);
Integer orderDistance;
if (orderDistanceObj != null) {
orderDistance = Integer.valueOf(orderDistanceObj.toString());
} else {
XpSystemSetting xpSystemSetting = xpSystemSettingMapper.selectOne(null);
orderDistance = xpSystemSetting.getNearPointDistance();
}
for (XpPoint xpPoint : list) {
// 计算距离 小于的站点
double v = FastDistanceCalculator.distanceFast(dto.getLatitude(), dto.getLongitude(), xpPoint.getPointLat(), xpPoint.getPointLon());
if (v < orderDistance) {
NearestNextVo nearestNextVo = new NearestNextVo();
nearestNextVo.setId(xpPoint.getId());
nearestNextVo.setName(xpPoint.getPointName());
nearestNextVo.setLatitude(xpPoint.getPointLat());
nearestNextVo.setLongitude(xpPoint.getPointLon());
nearestNextVo.setDistance(v);
nearestNextVo.setAddress(xpPoint.getPointAddress());
nearestNextVo.setTime(xpPoint.getParkingTimeLimit());
nearestNextVo.setElectronicAreaId(xpPoint.getElectronicAreaId());
nearestNextVos.add(nearestNextVo);
}
}
if (!nearestNextVos.isEmpty()) {
nearestNextVos = nearestNextVos.stream().sorted(Comparator.comparing(NearestNextVo::getDistance)).collect(Collectors.toList());
}
nearestVo.setPoints(nearestNextVos);
nearestVo.setTotal(nearestNextVos.size());
return nearestVo;
}
@Override
public List pointMap(PointMapQuery query) {
List list = this.baseMapper.pointMap(query);
return list;
}
@Override
public List listAll(PointPageListQuery query) {
List list = this.baseMapper.listAll(query);
return list;
}
@Override
public List getHistoryStartPoints(HistoryStartPointsDto historyStartPointsDto) {
List xpPoints = this.baseMapper.selectBatchIds(historyStartPointsDto.getPointIds());
ArrayList resultList = new ArrayList<>(10);
for (XpPoint xpPoint : xpPoints) {
XpHistoryStartPointsVo xpHistoryStartPointsVo = new XpHistoryStartPointsVo();
xpHistoryStartPointsVo.setName(xpPoint.getPointName());
xpHistoryStartPointsVo.setAddress(xpPoint.getPointAddress());
xpHistoryStartPointsVo.setLatitude(xpPoint.getPointLat());
xpHistoryStartPointsVo.setLongitude(xpPoint.getPointLon());
xpHistoryStartPointsVo.setId(xpPoint.getId());
double distanced = FastDistanceCalculator.distanceFastKm(historyStartPointsDto.getLat(), historyStartPointsDto.getLon(), xpPoint.getPointLat(), xpPoint.getPointLon());
xpHistoryStartPointsVo.setDistance(distanced);
resultList.add(xpHistoryStartPointsVo);
}
return resultList;
}
@Override
public List getHistoryEndPoints(HistoryEndPointsDto historyEndPointsDto) {
List xpPoints = this.baseMapper.selectBatchIds(historyEndPointsDto.getPointIds());
ArrayList resultList = new ArrayList<>(10);
for (XpPoint xpPoint : xpPoints) {
XpHistoryEndPointsVo xpHistoryEndPointsVo = new XpHistoryEndPointsVo();
xpHistoryEndPointsVo.setName(xpPoint.getPointName());
xpHistoryEndPointsVo.setAddress(xpPoint.getPointAddress());
xpHistoryEndPointsVo.setLatitude(xpPoint.getPointLat());
xpHistoryEndPointsVo.setLongitude(xpPoint.getPointLon());
xpHistoryEndPointsVo.setId(xpPoint.getId());
double distanced = FastDistanceCalculator.distanceFastKm(historyEndPointsDto.getLat(), historyEndPointsDto.getLon(), xpPoint.getPointLat(), xpPoint.getPointLon());
xpHistoryEndPointsVo.setDistance(distanced);
resultList.add(xpHistoryEndPointsVo);
}
return resultList;
}
@Override
public XpPointsSearchVo queryPointByName(PointByNameQuery query) {
XpPointsSearchVo searchVo = new XpPointsSearchVo();
List list = this.baseMapper.queryPointByName(query);
if (list.isEmpty()) {
return searchVo;
}
// 组装数据 并排序
List resultList = list.stream().peek(vo -> vo.setDistance(FastDistanceCalculator.distanceFast(query.getLat(), query.getLon(), vo.getLatitude(), vo.getLongitude()))).collect(Collectors.toList());
// 可用点位
List availablePoints = resultList.stream().filter(e -> query.getElectronicAreaId() != null && e.getElectronicAreaId().equals(query.getElectronicAreaId())).sorted(Comparator.comparing(XpPointsListVo::getDistance)).collect(Collectors.toList());//
// 不可用点位
List unavailablePoints = resultList.stream().filter(e -> query.getElectronicAreaId() == null || !e.getElectronicAreaId().equals(query.getElectronicAreaId())).sorted(Comparator.comparing(XpPointsListVo::getDistance)).collect(Collectors.toList());
searchVo.setAvailablePoints(availablePoints);
searchVo.setUnavailablePoints(unavailablePoints);
return searchVo;
}
@Override
public OrderDistanceVo getOrderDistance() {
OrderDistanceVo orderDistanceVo = new OrderDistanceVo();
XpSystemSetting xpSystemSetting = xpSystemSettingMapper.selectOne(null);
if (xpSystemSetting != null) {
orderDistanceVo.setMaxDistance(xpSystemSetting.getOrderDistance());
orderDistanceVo.setMinDistance(xpSystemSetting.getMinOrderDistance());
orderDistanceVo.setAdsorptionDistance(xpSystemSetting.getAdsorptionDistance());
orderDistanceVo.setNearPointDistance(xpSystemSetting.getNearPointDistance());
return orderDistanceVo;
}
return orderDistanceVo;
}
}