package com.xiaopeng.account.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.xiaopeng.account.mapper.XpAppUserCollectAddrMapper;
|
import com.xiaopeng.account.model.dto.CollectAddrDTO;
|
import com.xiaopeng.account.model.query.XpUserCollectAddrListQuery;
|
import com.xiaopeng.account.model.vo.CollectAddrTargetVO;
|
import com.xiaopeng.account.model.vo.XpAppUserCollectAddrVO;
|
import com.xiaopeng.account.service.IXpAppUserCollectAddrService;
|
import com.xiaopeng.api.dto.map.XpPointExportExcelQuery;
|
import com.xiaopeng.api.feignClient.map.XpStationClient;
|
import com.xiaopeng.api.feignClient.operation.XpPointClient;
|
import com.xiaopeng.api.model.account.XpAppUserCollectAddr;
|
import com.xiaopeng.api.model.operation.XpPoint;
|
import com.xiaopeng.api.model.operation.XpStation;
|
import com.xiaopeng.common.core.exception.ServiceException;
|
import com.xiaopeng.common.core.web.page.PageInfo;
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import java.util.Arrays;
|
import java.util.List;
|
import java.util.Objects;
|
import java.util.stream.Collectors;
|
|
/**
|
* <p>
|
* 用户收藏地址 服务实现类
|
* </p>
|
*
|
* @author ck
|
* @since 2026-04-10
|
*/
|
@Service
|
public class XpAppUserCollectAddrServiceImpl extends ServiceImpl<XpAppUserCollectAddrMapper, XpAppUserCollectAddr> implements IXpAppUserCollectAddrService {
|
|
private static final int COLLECT_TYPE_ADD = 1;
|
private static final int COLLECT_TYPE_CANCEL = 2;
|
private static final int ITEM_TYPE_POINT = 1;
|
private static final int ITEM_TYPE_STATION = 2;
|
private static final int COLLECT_LIMIT = 50;
|
|
|
@Resource
|
private XpPointClient xpPointClient;
|
|
@Resource
|
private XpStationClient xpStationClient;
|
|
|
|
|
/**
|
* 收藏地址。
|
* 收藏表显式保存 item_type / station_id:
|
* 1. 站点收藏:item_type=1,point_id 保存站点自身。
|
* 2. 场站收藏:item_type=2,station_id 保存场站id,point_id 保存代表点位id。
|
* 这样取消收藏、列表回显和后续扩展都不需要再做隐式推断。
|
*/
|
@Override
|
public Boolean collectAddr(CollectAddrDTO dto, String uid) {
|
Long userId = Long.parseLong(uid);
|
if(dto.getItemType() == 1){
|
XpPointExportExcelQuery xpPointExportExcelQuery = new XpPointExportExcelQuery();
|
xpPointExportExcelQuery.setPointIds(Arrays.asList(dto.getPointId()));
|
List<XpPoint> data = xpPointClient.getByIds(xpPointExportExcelQuery).getData();
|
if(null == data || data.size() == 0){
|
throw new ServiceException("站点不存在");
|
}
|
}
|
if(dto.getItemType() == 2){
|
XpStation data = xpStationClient.getXpStationById(dto.getStationId()).getData();
|
if(null == data){
|
throw new ServiceException("场站不存在");
|
}
|
}
|
|
if(1 == dto.getType()){
|
XpAppUserCollectAddr collectAddr = new XpAppUserCollectAddr();
|
collectAddr.setAppUserId(userId);
|
collectAddr.setItemType(dto.getItemType());
|
collectAddr.setPointId(dto.getPointId());
|
collectAddr.setStationId(dto.getStationId());
|
collectAddr.setLongitude(dto.getLongitude());
|
collectAddr.setLatitude(dto.getLatitude());
|
return this.save(collectAddr);
|
}else{
|
if(dto.getItemType() == 1){
|
return this.remove(new LambdaQueryWrapper<XpAppUserCollectAddr>().eq(XpAppUserCollectAddr::getAppUserId, userId)
|
.eq(XpAppUserCollectAddr::getPointId, dto.getPointId()));
|
}
|
if(dto.getItemType() == 2){
|
return this.remove(new LambdaQueryWrapper<XpAppUserCollectAddr>().eq(XpAppUserCollectAddr::getAppUserId, userId)
|
.eq(XpAppUserCollectAddr::getStationId, dto.getStationId()));
|
}
|
|
}
|
return false;
|
}
|
|
/**
|
* 收藏地址列表。
|
* 列表查询直接联表回显,场站/站点通过 item_type 直接区分。
|
*/
|
@Override
|
public PageInfo<XpAppUserCollectAddrVO> userCollectAddrList(XpUserCollectAddrListQuery query, String uid) {
|
Page<XpAppUserCollectAddrVO> page = new Page<>(query.getPageNum(), query.getPageSize());
|
Page<XpAppUserCollectAddrVO> pageData = this.baseMapper.userCollectAddrPageList(page, query, Long.parseLong(uid));
|
pageData.getRecords().forEach(xpAppUserCollectAddrVO -> {
|
if(xpAppUserCollectAddrVO.getItemType() == 1){
|
XpPointExportExcelQuery xpPointExportExcelQuery = new XpPointExportExcelQuery();
|
xpPointExportExcelQuery.setPointIds(Arrays.asList(xpAppUserCollectAddrVO.getPointId()));
|
List<XpPoint> data = xpPointClient.getByIds(xpPointExportExcelQuery).getData();
|
if(null != data && data.size() > 0){
|
xpAppUserCollectAddrVO.setPointName(data.get(0).getPointName());
|
xpAppUserCollectAddrVO.setPointAddress(data.get(0).getPointAddress());
|
}
|
}
|
if(xpAppUserCollectAddrVO.getItemType() == 2){
|
XpStation data = xpStationClient.getXpStationById(xpAppUserCollectAddrVO.getStationId()).getData();
|
if(null != data){
|
xpAppUserCollectAddrVO.setPointName(data.getStationName());
|
}
|
}
|
});
|
|
|
PageInfo<XpAppUserCollectAddrVO> resultPage = new PageInfo<>(query.getPageNum(), query.getPageSize());
|
resultPage.setTotal(pageData.getTotal());
|
resultPage.setRecords(pageData.getRecords());
|
return resultPage;
|
}
|
|
@Override
|
public List<Long> getPointIdsByUserId(Long uid) {
|
return this.list(buildUserCollectQuery(uid)
|
.eq(XpAppUserCollectAddr::getItemType, ITEM_TYPE_POINT))
|
.stream()
|
.map(XpAppUserCollectAddr::getPointId)
|
.filter(Objects::nonNull)
|
.distinct()
|
.collect(Collectors.toList());
|
}
|
|
@Override
|
public List<Long> getStationIdsByUserId(Long uid) {
|
return this.list(buildUserCollectQuery(uid)
|
.eq(XpAppUserCollectAddr::getItemType, ITEM_TYPE_STATION))
|
.stream()
|
.map(XpAppUserCollectAddr::getStationId)
|
.filter(Objects::nonNull)
|
.distinct()
|
.collect(Collectors.toList());
|
}
|
|
/**
|
* 将前端传入的站点/场站目标解析成统一收藏目标。
|
* 优先按 itemType 判断,未传时兼容旧逻辑按 pointId 处理。
|
*/
|
private CollectAddrTargetVO resolveCollectTarget(CollectAddrDTO dto) {
|
if (isStationItem(dto)) {
|
if (dto.getStationId() == null) {
|
throw new ServiceException("场站id不能为空");
|
}
|
return this.baseMapper.selectStationCollectTarget(dto.getStationId());
|
}
|
if (dto.getPointId() == null && dto.getStationId() != null) {
|
return this.baseMapper.selectStationCollectTarget(dto.getStationId());
|
}
|
if (dto.getPointId() == null) {
|
throw new ServiceException("站点id不能为空");
|
}
|
return this.baseMapper.selectPointCollectTarget(dto.getPointId());
|
}
|
|
/**
|
* 收藏唯一键规则:
|
* 1. 站点收藏按 user_id + item_type + point_id 去重。
|
* 2. 场站收藏按 user_id + item_type + station_id 去重。
|
* point_id 在场站收藏里仍保留,方便联动现有“已收藏点位”能力。
|
*/
|
private LambdaQueryWrapper<XpAppUserCollectAddr> buildSameTargetWrapper(Long userId, CollectAddrTargetVO target) {
|
LambdaQueryWrapper<XpAppUserCollectAddr> wrapper = new LambdaQueryWrapper<XpAppUserCollectAddr>()
|
.eq(XpAppUserCollectAddr::getAppUserId, userId)
|
.eq(XpAppUserCollectAddr::getDeleted, 0)
|
.eq(XpAppUserCollectAddr::getItemType, target.getItemType());
|
if (target.getItemType() == ITEM_TYPE_STATION) {
|
wrapper.eq(XpAppUserCollectAddr::getStationId, target.getStationId());
|
} else {
|
wrapper.eq(XpAppUserCollectAddr::getPointId, target.getPointId());
|
}
|
return wrapper;
|
}
|
|
private LambdaQueryWrapper<XpAppUserCollectAddr> buildUserCollectQuery(Long uid) {
|
return new LambdaQueryWrapper<XpAppUserCollectAddr>()
|
.eq(XpAppUserCollectAddr::getAppUserId, uid)
|
.eq(XpAppUserCollectAddr::getDeleted, 0);
|
}
|
|
private boolean isStationItem(CollectAddrDTO dto) {
|
return dto.getItemType() != null && dto.getItemType() == ITEM_TYPE_STATION;
|
}
|
}
|