新增人生轨迹模块代码
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
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);
|
||||
|
||||
UserProfile userProfile = getById(request.getId());
|
||||
if (userProfile == null) {
|
||||
throw new RuntimeException("档案不存在");
|
||||
}
|
||||
|
||||
// 权限校验:只能修改自己的档案
|
||||
String currentUserId = UserContextHolder.getCurrentUserId();
|
||||
if (!StringUtils.hasText(currentUserId)) {
|
||||
throw new RuntimeException("用户未登录");
|
||||
}
|
||||
if (!userProfile.getUserId().equals(currentUserId)) {
|
||||
// 管理员可以修改任意档案 (此处假设没有管理员逻辑,严格按需求: 只能修改自己的)
|
||||
// 如果需要管理员权限,需配合 SecurityUtils 判断角色
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user