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 implements TPartnerAppUserService { @Resource private AppUserClient appUserClient; @Resource private IPartnerService partnerService; @Override public PageInfo 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> result = appUserClient.searchPartnerBindUsers(phone, current, size); Page userPage = result == null ? null : result.getData(); if (userPage == null) { throw new ServiceException("小程序用户服务暂不可用"); } List users = userPage.getRecords(); Map relationMap = getRelationMap(users); Map partnerMap = getPartnerMap(relationMap.values()); List records = users.stream() .map(user -> toVO(user, relationMap.get(user.getId()), partnerMap, partnerId)) .collect(Collectors.toList()); PageInfo page = new PageInfo<>(userPage.getCurrent(), userPage.getSize()); page.setTotal(userPage.getTotal()); page.setRecords(records); return page; } @Override public List getBoundUsers(Integer partnerId) { assertPartnerExists(partnerId); List relations = list(new LambdaQueryWrapper() .eq(TPartnerAppUser::getPartnerId, partnerId) .orderByDesc(TPartnerAppUser::getCreateTime)); if (relations.isEmpty()) { return Collections.emptyList(); } List userIds = relations.stream() .map(TPartnerAppUser::getAppUserId) .collect(Collectors.toList()); R> userResult = appUserClient.getUserByIds(userIds); List users = userResult == null ? null : userResult.getData(); if (users == null) { throw new ServiceException("小程序用户服务暂不可用"); } Map userMap = users.stream() .collect(Collectors.toMap(TAppUser::getId, Function.identity(), (left, right) -> left)); Partner partner = partnerService.getById(partnerId); Map 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 getRelations(Integer partnerId) { assertPartnerExists(partnerId); return list(new LambdaQueryWrapper() .eq(TPartnerAppUser::getPartnerId, partnerId) .orderByAsc(TPartnerAppUser::getCreateTime)); } @Override @Transactional(rollbackFor = Exception.class) public void saveBindings(PartnerAppUserBindDTO bindDTO) { Integer partnerId = bindDTO.getPartnerId(); assertPartnerExists(partnerId); Set 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() .in(TPartnerAppUser::getAppUserId, selectedIds) .ne(TPartnerAppUser::getPartnerId, partnerId)); if (conflictCount > 0) { throw new ServiceException("所选手机号已绑定其他合作商,请刷新列表后重试"); } } List existingRelations = list(new LambdaQueryWrapper() .eq(TPartnerAppUser::getPartnerId, partnerId)); Set existingIds = existingRelations.stream() .map(TPartnerAppUser::getAppUserId) .filter(Objects::nonNull) .collect(Collectors.toSet()); Set removedIds = existingIds.stream() .filter(appUserId -> !selectedIds.contains(appUserId)) .collect(Collectors.toSet()); if (!removedIds.isEmpty()) { remove(new LambdaQueryWrapper() .eq(TPartnerAppUser::getPartnerId, partnerId) .in(TPartnerAppUser::getAppUserId, removedIds)); } List 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() .eq(TPartnerAppUser::getAppUserId, appUserId) .last("limit 1")); return relation == null ? null : partnerService.getById(relation.getPartnerId()); } private Map getRelationMap(List users) { if (users == null || users.isEmpty()) { return Collections.emptyMap(); } List userIds = users.stream().map(TAppUser::getId).collect(Collectors.toList()); return list(new LambdaQueryWrapper() .in(TPartnerAppUser::getAppUserId, userIds)).stream() .collect(Collectors.toMap(TPartnerAppUser::getAppUserId, Function.identity(), (left, right) -> left)); } private Map getPartnerMap(java.util.Collection relations) { Set 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 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("合作商不存在"); } } }