|
|
@@ -7,6 +7,7 @@ import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import work.baiyun.chronicdiseaseapp.mapper.UserBindingMapper;
|
|
|
+import work.baiyun.chronicdiseaseapp.mapper.UserInfoMapper;
|
|
|
import work.baiyun.chronicdiseaseapp.model.po.UserBinding;
|
|
|
import work.baiyun.chronicdiseaseapp.model.vo.BaseQueryRequest;
|
|
|
import work.baiyun.chronicdiseaseapp.model.vo.UserBindingResponse;
|
|
|
@@ -17,6 +18,9 @@ import work.baiyun.chronicdiseaseapp.service.UserBindingService;
|
|
|
import work.baiyun.chronicdiseaseapp.enums.UserBindingType;
|
|
|
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
@@ -24,6 +28,8 @@ public class UserBindingServiceImpl implements UserBindingService {
|
|
|
|
|
|
@Autowired
|
|
|
private UserBindingMapper userBindingMapper;
|
|
|
+ @Autowired
|
|
|
+ private UserInfoMapper userInfoMapper;
|
|
|
|
|
|
@Override
|
|
|
public void createUserBinding(CreateUserBindingRequest request) {
|
|
|
@@ -41,6 +47,8 @@ public class UserBindingServiceImpl implements UserBindingService {
|
|
|
// 如果不存在,则创建新的绑定关系
|
|
|
UserBinding userBinding = new UserBinding();
|
|
|
BeanUtils.copyProperties(request, userBinding);
|
|
|
+ // 手动设置绑定类型,因为类型不匹配无法自动复制
|
|
|
+ userBinding.setBindingType(UserBindingType.fromCode(request.getBindingType()));
|
|
|
userBinding.setStatus(1); // 默认状态为有效
|
|
|
userBindingMapper.insert(userBinding);
|
|
|
}
|
|
|
@@ -68,14 +76,27 @@ public class UserBindingServiceImpl implements UserBindingService {
|
|
|
.eq(UserBinding::getStatus, 1) // 只查询有效的绑定关系
|
|
|
.ge(request.getStartTime() != null, UserBinding::getCreateTime, request.getStartTime())
|
|
|
.le(request.getEndTime() != null, UserBinding::getCreateTime, request.getEndTime());
|
|
|
-
|
|
|
+
|
|
|
if (bindingType != null && !bindingType.isEmpty()) {
|
|
|
wrapper.eq(UserBinding::getBindingType, UserBindingType.fromCode(bindingType));
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
wrapper.orderByDesc(UserBinding::getCreateTime);
|
|
|
|
|
|
Page<UserBinding> result = userBindingMapper.selectPage(page, wrapper);
|
|
|
+ // 批量查询被绑定用户信息,避免 N+1
|
|
|
+ Set<Long> boundUserIds = result.getRecords().stream()
|
|
|
+ .map(UserBinding::getBoundUserId)
|
|
|
+ .filter(id -> id != null)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ Map<Long, work.baiyun.chronicdiseaseapp.model.po.UserInfo> userInfoMap;
|
|
|
+ if (boundUserIds.isEmpty()) {
|
|
|
+ userInfoMap = new HashMap<>();
|
|
|
+ } else {
|
|
|
+ List<work.baiyun.chronicdiseaseapp.model.po.UserInfo> userInfos = userInfoMapper.selectBatchIds(boundUserIds);
|
|
|
+ userInfoMap = userInfos.stream().collect(Collectors.toMap(work.baiyun.chronicdiseaseapp.model.po.UserInfo::getId, u -> u));
|
|
|
+ }
|
|
|
|
|
|
List<UserBindingResponse> responses = result.getRecords().stream().map(r -> {
|
|
|
UserBindingResponse resp = new UserBindingResponse();
|
|
|
@@ -84,6 +105,13 @@ public class UserBindingServiceImpl implements UserBindingService {
|
|
|
if (r.getBindingType() != null) {
|
|
|
resp.setBindingType(r.getBindingType().getCode());
|
|
|
}
|
|
|
+
|
|
|
+ work.baiyun.chronicdiseaseapp.model.po.UserInfo ui = userInfoMap.get(r.getBoundUserId());
|
|
|
+ if (ui != null) {
|
|
|
+ resp.setBoundUserNickname(ui.getNickname());
|
|
|
+ resp.setBoundUserPhone(ui.getPhone());
|
|
|
+ }
|
|
|
+
|
|
|
return resp;
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
@@ -112,6 +140,20 @@ public class UserBindingServiceImpl implements UserBindingService {
|
|
|
|
|
|
Page<UserBinding> result = userBindingMapper.selectPage(page, wrapper);
|
|
|
|
|
|
+ // 批量查询被绑定用户信息,避免 N+1
|
|
|
+ Set<Long> boundUserIds = result.getRecords().stream()
|
|
|
+ .map(UserBinding::getBoundUserId)
|
|
|
+ .filter(id -> id != null)
|
|
|
+ .collect(Collectors.toSet());
|
|
|
+
|
|
|
+ Map<Long, work.baiyun.chronicdiseaseapp.model.po.UserInfo> userInfoMap;
|
|
|
+ if (boundUserIds.isEmpty()) {
|
|
|
+ userInfoMap = new HashMap<>();
|
|
|
+ } else {
|
|
|
+ List<work.baiyun.chronicdiseaseapp.model.po.UserInfo> userInfos = userInfoMapper.selectBatchIds(boundUserIds);
|
|
|
+ userInfoMap = userInfos.stream().collect(Collectors.toMap(work.baiyun.chronicdiseaseapp.model.po.UserInfo::getId, u -> u));
|
|
|
+ }
|
|
|
+
|
|
|
List<UserBindingResponse> responses = result.getRecords().stream().map(r -> {
|
|
|
UserBindingResponse resp = new UserBindingResponse();
|
|
|
BeanUtils.copyProperties(r, resp);
|
|
|
@@ -119,6 +161,13 @@ public class UserBindingServiceImpl implements UserBindingService {
|
|
|
if (r.getBindingType() != null) {
|
|
|
resp.setBindingType(r.getBindingType().getCode());
|
|
|
}
|
|
|
+
|
|
|
+ work.baiyun.chronicdiseaseapp.model.po.UserInfo ui = userInfoMap.get(r.getBoundUserId());
|
|
|
+ if (ui != null) {
|
|
|
+ resp.setBoundUserNickname(ui.getNickname());
|
|
|
+ resp.setBoundUserPhone(ui.getPhone());
|
|
|
+ }
|
|
|
+
|
|
|
return resp;
|
|
|
}).collect(Collectors.toList());
|
|
|
|