Files
happy-life-star/backend-single/src/main/java/com/emotion/service/impl/UserProfileServiceImpl.java
T

219 lines
8.2 KiB
Java

package com.emotion.service.impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.emotion.common.PageResult;
import com.emotion.dto.request.userprofile.UserProfileCreateRequest;
import com.emotion.dto.request.userprofile.UserProfilePageRequest;
import com.emotion.dto.request.userprofile.UserProfileUpdateRequest;
import com.emotion.dto.response.userprofile.UserProfileResponse;
import com.emotion.entity.User;
import com.emotion.entity.UserProfile;
import com.emotion.mapper.UserProfileMapper;
import com.emotion.service.UserProfileService;
import com.emotion.service.UserService;
import com.emotion.util.UserContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.List;
import java.util.stream.Collectors;
/**
* 用户档案服务实现类
*
* @author huazhongmin
* @date 2025-12-21
*/
@Slf4j
@Service
public class UserProfileServiceImpl extends ServiceImpl<UserProfileMapper, UserProfile> implements UserProfileService {
@Autowired
private UserService userService;
@Override
public UserProfileResponse createProfile(UserProfileCreateRequest request) {
log.info("Creating user profile: {}", request);
// 获取当前登录用户ID
String currentUserId = UserContextHolder.getCurrentUserId();
if (!StringUtils.hasText(currentUserId)) {
throw new RuntimeException("用户未登录");
}
// 检查是否已存在档案
LambdaQueryWrapper<UserProfile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserProfile::getUserId, currentUserId);
if (count(queryWrapper) > 0) {
throw new RuntimeException("当前用户已存在档案信息");
}
UserProfile userProfile = new UserProfile();
BeanUtils.copyProperties(request, userProfile);
userProfile.setUserId(currentUserId);
userProfile.setStatus(1); // 默认正常
save(userProfile);
log.info("User profile created with ID: {}", userProfile.getId());
return convertToResponse(userProfile);
}
@Override
public UserProfileResponse updateProfile(UserProfileUpdateRequest request) {
log.info("Updating user profile: {}", request);
// 获取当前登录用户ID
String currentUserId = UserContextHolder.getCurrentUserId();
if (!StringUtils.hasText(currentUserId)) {
throw new RuntimeException("用户未登录");
}
UserProfile userProfile = null;
// 先根据请求ID查询
if (StringUtils.hasText(request.getId())) {
userProfile = getById(request.getId());
}
// 如果没有查到,根据当前用户ID查询
if (userProfile == null) {
LambdaQueryWrapper<UserProfile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserProfile::getUserId, currentUserId);
userProfile = getOne(queryWrapper);
}
// 如果还是没有,则创建新档案
if (userProfile == null) {
log.info("User profile not found, creating new profile for user: {}", currentUserId);
UserProfileCreateRequest createRequest = new UserProfileCreateRequest();
BeanUtils.copyProperties(request, createRequest);
return createProfile(createRequest);
}
// 权限校验:只能修改自己的档案
if (!userProfile.getUserId().equals(currentUserId)) {
throw new RuntimeException("无权修改他人档案");
}
// 使用Hutool的BeanUtil进行部分更新,忽略null值
BeanUtil.copyProperties(request, userProfile, CopyOptions.create().setIgnoreNullValue(true));
updateById(userProfile);
log.info("User profile updated: {}", userProfile.getId());
return convertToResponse(userProfile);
}
@Override
public UserProfileResponse getCurrentUserProfile() {
String currentUserId = UserContextHolder.getCurrentUserId();
if (!StringUtils.hasText(currentUserId)) {
throw new RuntimeException("用户未登录");
}
LambdaQueryWrapper<UserProfile> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(UserProfile::getUserId, currentUserId);
// 获取最新的一条(理论上应该只有一条)
List<UserProfile> list = list(queryWrapper);
if (list.isEmpty()) {
return null;
}
return convertToResponse(list.get(0));
}
@Override
public UserProfileResponse getProfileById(String id) {
UserProfile userProfile = getById(id);
if (userProfile == null) {
return null;
}
return convertToResponse(userProfile);
}
@Override
public PageResult<UserProfileResponse> getProfilePage(UserProfilePageRequest request) {
Page<UserProfile> page = new Page<>(request.getCurrent(), request.getSize());
LambdaQueryWrapper<UserProfile> queryWrapper = new LambdaQueryWrapper<>();
// 构建查询条件
if (StringUtils.hasText(request.getUserId())) {
queryWrapper.eq(UserProfile::getUserId, request.getUserId());
}
if (StringUtils.hasText(request.getNickname())) {
queryWrapper.like(UserProfile::getNickname, request.getNickname());
}
if (StringUtils.hasText(request.getMbti())) {
queryWrapper.eq(UserProfile::getMbti, request.getMbti());
}
if (request.getStatus() != null) {
queryWrapper.eq(UserProfile::getStatus, request.getStatus());
}
queryWrapper.orderByDesc(UserProfile::getCreateTime);
IPage<UserProfile> userProfilePage = page(page, queryWrapper);
List<UserProfileResponse> list = userProfilePage.getRecords().stream()
.map(this::convertToResponse)
.collect(Collectors.toList());
PageResult<UserProfileResponse> result = new PageResult<>();
result.setCurrent(userProfilePage.getCurrent());
result.setSize(userProfilePage.getSize());
result.setTotal(userProfilePage.getTotal());
result.setPages(userProfilePage.getPages());
result.setRecords(list);
return result;
}
@Override
public List<UserProfileResponse> getProfileList(UserProfilePageRequest request) {
LambdaQueryWrapper<UserProfile> queryWrapper = new LambdaQueryWrapper<>();
// 构建查询条件
if (StringUtils.hasText(request.getUserId())) {
queryWrapper.eq(UserProfile::getUserId, request.getUserId());
}
if (StringUtils.hasText(request.getNickname())) {
queryWrapper.like(UserProfile::getNickname, request.getNickname());
}
if (StringUtils.hasText(request.getMbti())) {
queryWrapper.eq(UserProfile::getMbti, request.getMbti());
}
if (request.getStatus() != null) {
queryWrapper.eq(UserProfile::getStatus, request.getStatus());
}
queryWrapper.orderByDesc(UserProfile::getCreateTime);
List<UserProfile> list = list(queryWrapper);
return list.stream()
.map(this::convertToResponse)
.collect(Collectors.toList());
}
@Override
public boolean deleteProfile(String id) {
log.info("Deleting user profile: {}", id);
return removeById(id);
}
private UserProfileResponse convertToResponse(UserProfile userProfile) {
if (userProfile == null) {
return null;
}
UserProfileResponse response = new UserProfileResponse();
BeanUtils.copyProperties(userProfile, response);
return response;
}
}