longz
2026-07-28 cd4e26608b01cbc376760d96e2eab5d0a796f0b0
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
207
208
209
210
211
212
213
214
package com.ruoyi.chargingPile.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.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.model.TAppUser;
import com.ruoyi.chargingPile.api.model.Partner;
import com.ruoyi.chargingPile.api.model.TPartnerAppUser;
import com.ruoyi.chargingPile.dto.PartnerAppUserBindDTO;
import com.ruoyi.chargingPile.dto.PartnerAppUserVO;
import com.ruoyi.chargingPile.mapper.TPartnerAppUserMapper;
import com.ruoyi.chargingPile.service.IPartnerService;
import com.ruoyi.chargingPile.service.TPartnerAppUserService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.page.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
 
@Service
public class TPartnerAppUserServiceImpl extends ServiceImpl<TPartnerAppUserMapper, TPartnerAppUser>
        implements TPartnerAppUserService {
 
    @Resource
    private AppUserClient appUserClient;
 
    @Resource
    private IPartnerService partnerService;
 
    @Override
    public PageInfo<PartnerAppUserVO> searchUsers(Integer partnerId, String phone, Integer pageNum, Integer pageSize) {
        assertPartnerExists(partnerId);
        int current = pageNum == null || pageNum < 1 ? 1 : pageNum;
        int size = pageSize == null || pageSize < 1 ? 10 : Math.min(pageSize, 100);
        R<Page<TAppUser>> result = appUserClient.searchPartnerBindUsers(phone, current, size);
        Page<TAppUser> userPage = result == null ? null : result.getData();
        if (userPage == null) {
            throw new ServiceException("小程序用户服务暂不可用");
        }
 
        List<TAppUser> users = userPage.getRecords();
        Map<Long, TPartnerAppUser> relationMap = getRelationMap(users);
        Map<Integer, Partner> partnerMap = getPartnerMap(relationMap.values());
        List<PartnerAppUserVO> records = users.stream()
                .map(user -> toVO(user, relationMap.get(user.getId()), partnerMap, partnerId))
                .collect(Collectors.toList());
 
        PageInfo<PartnerAppUserVO> page = new PageInfo<>(userPage.getCurrent(), userPage.getSize());
        page.setTotal(userPage.getTotal());
        page.setRecords(records);
        return page;
    }
 
    @Override
    public List<PartnerAppUserVO> getBoundUsers(Integer partnerId) {
        assertPartnerExists(partnerId);
        List<TPartnerAppUser> relations = list(new LambdaQueryWrapper<TPartnerAppUser>()
                .eq(TPartnerAppUser::getPartnerId, partnerId)
                .orderByDesc(TPartnerAppUser::getCreateTime));
        if (relations.isEmpty()) {
            return Collections.emptyList();
        }
 
        List<Long> userIds = relations.stream()
                .map(TPartnerAppUser::getAppUserId)
                .collect(Collectors.toList());
        R<List<TAppUser>> userResult = appUserClient.getUserByIds(userIds);
        List<TAppUser> users = userResult == null ? null : userResult.getData();
        if (users == null) {
            throw new ServiceException("小程序用户服务暂不可用");
        }
        Map<Long, TAppUser> userMap = users.stream()
                .collect(Collectors.toMap(TAppUser::getId, Function.identity(), (left, right) -> left));
        Partner partner = partnerService.getById(partnerId);
        Map<Integer, Partner> partnerMap = Collections.singletonMap(partnerId, partner);
 
        return relations.stream()
                .map(relation -> {
                    TAppUser user = userMap.get(relation.getAppUserId());
                    return user == null ? null : toVO(user, relation, partnerMap, partnerId);
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
    }
 
    @Override
    public List<TPartnerAppUser> getRelations(Integer partnerId) {
        assertPartnerExists(partnerId);
        return list(new LambdaQueryWrapper<TPartnerAppUser>()
                .eq(TPartnerAppUser::getPartnerId, partnerId)
                .orderByAsc(TPartnerAppUser::getCreateTime));
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBindings(PartnerAppUserBindDTO bindDTO) {
        Integer partnerId = bindDTO.getPartnerId();
        assertPartnerExists(partnerId);
        Set<Long> selectedIds = bindDTO.getAppUserIds() == null
                ? Collections.emptySet()
                : bindDTO.getAppUserIds().stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toCollection(LinkedHashSet::new));
 
        if (!selectedIds.isEmpty()) {
            long conflictCount = count(new LambdaQueryWrapper<TPartnerAppUser>()
                    .in(TPartnerAppUser::getAppUserId, selectedIds)
                    .ne(TPartnerAppUser::getPartnerId, partnerId));
            if (conflictCount > 0) {
                throw new ServiceException("所选手机号已绑定其他合作商,请刷新列表后重试");
            }
        }
 
        List<TPartnerAppUser> existingRelations = list(new LambdaQueryWrapper<TPartnerAppUser>()
                .eq(TPartnerAppUser::getPartnerId, partnerId));
        Set<Long> existingIds = existingRelations.stream()
                .map(TPartnerAppUser::getAppUserId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
 
        Set<Long> removedIds = existingIds.stream()
                .filter(appUserId -> !selectedIds.contains(appUserId))
                .collect(Collectors.toSet());
        if (!removedIds.isEmpty()) {
            remove(new LambdaQueryWrapper<TPartnerAppUser>()
                    .eq(TPartnerAppUser::getPartnerId, partnerId)
                    .in(TPartnerAppUser::getAppUserId, removedIds));
        }
 
        List<TPartnerAppUser> relations = new ArrayList<>();
        for (Long appUserId : selectedIds) {
            if (existingIds.contains(appUserId)) {
                continue;
            }
            TPartnerAppUser relation = new TPartnerAppUser();
            relation.setPartnerId(partnerId);
            relation.setAppUserId(appUserId);
            relations.add(relation);
        }
        if (!relations.isEmpty()) {
            saveBatch(relations);
        }
    }
 
    @Override
    public Partner getPartnerByAppUserId(Long appUserId) {
        if (appUserId == null) {
            return null;
        }
        TPartnerAppUser relation = getOne(new LambdaQueryWrapper<TPartnerAppUser>()
                .eq(TPartnerAppUser::getAppUserId, appUserId)
                .last("limit 1"));
        return relation == null ? null : partnerService.getById(relation.getPartnerId());
    }
 
    private Map<Long, TPartnerAppUser> getRelationMap(List<TAppUser> users) {
        if (users == null || users.isEmpty()) {
            return Collections.emptyMap();
        }
        List<Long> userIds = users.stream().map(TAppUser::getId).collect(Collectors.toList());
        return list(new LambdaQueryWrapper<TPartnerAppUser>()
                .in(TPartnerAppUser::getAppUserId, userIds)).stream()
                .collect(Collectors.toMap(TPartnerAppUser::getAppUserId, Function.identity(), (left, right) -> left));
    }
 
    private Map<Integer, Partner> getPartnerMap(java.util.Collection<TPartnerAppUser> relations) {
        Set<Integer> partnerIds = relations.stream()
                .map(TPartnerAppUser::getPartnerId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        if (partnerIds.isEmpty()) {
            return Collections.emptyMap();
        }
        return partnerService.listByIds(partnerIds).stream()
                .collect(Collectors.toMap(Partner::getId, Function.identity(), (left, right) -> left));
    }
 
    private PartnerAppUserVO toVO(TAppUser user, TPartnerAppUser relation,
                                  Map<Integer, Partner> partnerMap, Integer currentPartnerId) {
        PartnerAppUserVO vo = new PartnerAppUserVO();
        vo.setAppUserId(user.getId());
        vo.setName(user.getName());
        vo.setPhone(user.getPhone());
        vo.setRemark(user.getRemark());
        vo.setCreateTime(user.getCreateTime());
        if (relation != null) {
            vo.setPartnerId(relation.getPartnerId());
            Partner partner = partnerMap.get(relation.getPartnerId());
            vo.setPartnerName(partner == null ? null : partner.getName());
        }
        boolean selected = relation != null && Objects.equals(relation.getPartnerId(), currentPartnerId);
        vo.setSelected(selected);
        vo.setDisabled(relation != null && !selected);
        return vo;
    }
 
    private void assertPartnerExists(Integer partnerId) {
        if (partnerId == null || partnerService.getById(partnerId) == null) {
            throw new ServiceException("合作商不存在");
        }
    }
}