puzhibing
2026-04-15 7e51270fca4138abf3df51e08085c253ba7c0e93
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
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;
    }
}