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
|
@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("所选手机号已绑定其他合作商,请刷新列表后重试");
|
}
|
}
|
|
remove(new LambdaQueryWrapper<TPartnerAppUser>().eq(TPartnerAppUser::getPartnerId, partnerId));
|
if (selectedIds.isEmpty()) {
|
return;
|
}
|
|
List<TPartnerAppUser> relations = new ArrayList<>(selectedIds.size());
|
for (Long appUserId : selectedIds) {
|
TPartnerAppUser relation = new TPartnerAppUser();
|
relation.setPartnerId(partnerId);
|
relation.setAppUserId(appUserId);
|
relations.add(relation);
|
}
|
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("合作商不存在");
|
}
|
}
|
}
|