新增人生轨迹模块代码

This commit is contained in:
2025-12-21 17:44:59 +08:00
parent f3c06ce6af
commit cfd12f01db
14 changed files with 1156 additions and 72 deletions
@@ -0,0 +1,100 @@
package com.emotion.controller;
import com.emotion.common.PageResult;
import com.emotion.common.Result;
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.service.UserProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
/**
* 用户档案控制器
*
* @author huazhongmin
* @date 2025-12-21
*/
@RestController
@RequestMapping("/user-profile")
public class UserProfileController {
@Autowired
private UserProfileService userProfileService;
/**
* 新增档案
*/
@PostMapping("/create")
public Result<UserProfileResponse> create(@Valid @RequestBody UserProfileCreateRequest request) {
UserProfileResponse response = userProfileService.createProfile(request);
return Result.success(response);
}
/**
* 删除档案
*/
@DeleteMapping("/delete")
public Result<Void> delete(@RequestParam String id) {
boolean success = userProfileService.deleteProfile(id);
if (success) {
return Result.success();
} else {
return Result.error("删除失败");
}
}
/**
* 修改档案
*/
@PutMapping("/update")
public Result<UserProfileResponse> update(@Valid @RequestBody UserProfileUpdateRequest request) {
UserProfileResponse response = userProfileService.updateProfile(request);
return Result.success(response);
}
/**
* 根据ID查询详情
*/
@GetMapping("/detail")
public Result<UserProfileResponse> getById(@RequestParam String id) {
UserProfileResponse response = userProfileService.getProfileById(id);
if (response == null) {
return Result.notFound("档案不存在");
}
return Result.success(response);
}
/**
* 获取当前登录用户的档案
*/
@GetMapping("/me")
public Result<UserProfileResponse> getCurrentProfile() {
UserProfileResponse response = userProfileService.getCurrentUserProfile();
// 如果不存在,返回null data,不报错
return Result.success(response);
}
/**
* 分页查询
*/
@GetMapping("/page")
public Result<PageResult<UserProfileResponse>> getPage(@Validated UserProfilePageRequest request) {
PageResult<UserProfileResponse> pageResult = userProfileService.getProfilePage(request);
return Result.success(pageResult);
}
/**
* 列表查询
*/
@GetMapping("/list")
public Result<List<UserProfileResponse>> getList(@Validated UserProfilePageRequest request) {
List<UserProfileResponse> list = userProfileService.getProfileList(request);
return Result.success(list);
}
}
@@ -0,0 +1,87 @@
package com.emotion.dto.request.userprofile;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.time.LocalDate;
/**
* 用户档案创建请求对象
*
* @author huazhongmin
* @date 2025-12-21
*/
@Data
public class UserProfileCreateRequest {
/**
* 昵称
*/
@NotBlank(message = "昵称不能为空")
private String nickname;
/**
* 性别
*/
private String gender;
/**
* 星座
*/
private String zodiac;
/**
* MBTI人格类型
*/
@NotBlank(message = "MBTI人格类型不能为空")
private String mbti;
/**
* 兴趣爱好 (JSON字符串)
*/
private String hobbies;
/**
* 童年经历日期
*/
private LocalDate childhoodDate;
/**
* 童年经历描述
*/
private String childhoodContent;
/**
* 高光时刻日期
*/
private LocalDate peakDate;
/**
* 高光时刻描述
*/
private String peakContent;
/**
* 低谷时期日期
*/
private LocalDate valleyDate;
/**
* 低谷时期描述
*/
private String valleyContent;
/**
* 未来期许
*/
private String futureVision;
/**
* 生成的剧本列表 (JSON字符串)
*/
private String scripts;
/**
* 选择的路径列表 (JSON字符串)
*/
private String paths;
}
@@ -0,0 +1,36 @@
package com.emotion.dto.request.userprofile;
import com.emotion.dto.request.PageRequest;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* 用户档案分页查询请求对象
*
* @author huazhongmin
* @date 2025-12-21
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class UserProfilePageRequest extends PageRequest {
/**
* 用户ID
*/
private String userId;
/**
* 昵称
*/
private String nickname;
/**
* MBTI人格类型
*/
private String mbti;
/**
* 状态
*/
private Integer status;
}
@@ -0,0 +1,96 @@
package com.emotion.dto.request.userprofile;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.time.LocalDate;
/**
* 用户档案更新请求对象
*
* @author huazhongmin
* @date 2025-12-21
*/
@Data
public class UserProfileUpdateRequest {
/**
* ID
*/
@NotBlank(message = "ID不能为空")
private String id;
/**
* 昵称
*/
private String nickname;
/**
* 性别
*/
private String gender;
/**
* 星座
*/
private String zodiac;
/**
* MBTI人格类型
*/
private String mbti;
/**
* 兴趣爱好 (JSON字符串)
*/
private String hobbies;
/**
* 童年经历日期
*/
private LocalDate childhoodDate;
/**
* 童年经历描述
*/
private String childhoodContent;
/**
* 高光时刻日期
*/
private LocalDate peakDate;
/**
* 高光时刻描述
*/
private String peakContent;
/**
* 低谷时期日期
*/
private LocalDate valleyDate;
/**
* 低谷时期描述
*/
private String valleyContent;
/**
* 未来期许
*/
private String futureVision;
/**
* 生成的剧本列表 (JSON字符串)
*/
private String scripts;
/**
* 选择的路径列表 (JSON字符串)
*/
private String paths;
/**
* 状态: 0-禁用, 1-正常
*/
private Integer status;
}
@@ -0,0 +1,123 @@
package com.emotion.dto.response.userprofile;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
/**
* 用户档案响应对象
*
* @author huazhongmin
* @date 2025-12-21
*/
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserProfileResponse {
/**
* ID
*/
private String id;
/**
* 用户ID
*/
private String userId;
/**
* 昵称
*/
private String nickname;
/**
* 性别
*/
private String gender;
/**
* 星座
*/
private String zodiac;
/**
* MBTI人格类型
*/
private String mbti;
/**
* 兴趣爱好 (JSON字符串)
*/
private String hobbies;
/**
* 童年经历日期
*/
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate childhoodDate;
/**
* 童年经历描述
*/
private String childhoodContent;
/**
* 高光时刻日期
*/
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate peakDate;
/**
* 高光时刻描述
*/
private String peakContent;
/**
* 低谷时期日期
*/
@JsonFormat(pattern = "yyyy-MM-dd")
private LocalDate valleyDate;
/**
* 低谷时期描述
*/
private String valleyContent;
/**
* 未来期许
*/
private String futureVision;
/**
* 生成的剧本列表 (JSON字符串)
*/
private String scripts;
/**
* 选择的路径列表 (JSON字符串)
*/
private String paths;
/**
* 状态: 0-禁用, 1-正常
*/
private Integer status;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
/**
* 更新时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updateTime;
}
@@ -0,0 +1,123 @@
package com.emotion.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.emotion.common.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import java.time.LocalDate;
/**
* 用户档案实体类
*
* @author huazhongmin
* @date 2025-12-21
*/
@Data
@EqualsAndHashCode(callSuper = true)
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
@TableName("t_user_profile")
public class UserProfile extends BaseEntity {
/**
* 用户ID (关联t_user.id)
*/
@TableField("user_id")
private String userId;
/**
* 昵称
*/
@TableField("nickname")
private String nickname;
/**
* 性别: male, female, secret
*/
@TableField("gender")
private String gender;
/**
* 星座
*/
@TableField("zodiac")
private String zodiac;
/**
* MBTI人格类型
*/
@TableField("mbti")
private String mbti;
/**
* 兴趣爱好 (JSON字符串)
*/
@TableField("hobbies")
private String hobbies;
/**
* 童年经历日期
*/
@TableField("childhood_date")
private LocalDate childhoodDate;
/**
* 童年经历描述
*/
@TableField("childhood_content")
private String childhoodContent;
/**
* 高光时刻日期
*/
@TableField("peak_date")
private LocalDate peakDate;
/**
* 高光时刻描述
*/
@TableField("peak_content")
private String peakContent;
/**
* 低谷时期日期
*/
@TableField("valley_date")
private LocalDate valleyDate;
/**
* 低谷时期描述
*/
@TableField("valley_content")
private String valleyContent;
/**
* 未来期许
*/
@TableField("future_vision")
private String futureVision;
/**
* 生成的剧本列表 (JSON字符串)
*/
@TableField("scripts")
private String scripts;
/**
* 选择的路径列表 (JSON字符串)
*/
@TableField("paths")
private String paths;
/**
* 状态: 0-禁用, 1-正常
*/
@TableField("status")
private Integer status;
}
@@ -0,0 +1,15 @@
package com.emotion.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.emotion.entity.UserProfile;
import org.apache.ibatis.annotations.Mapper;
/**
* 用户档案 Mapper 接口
*
* @author huazhongmin
* @date 2025-12-21
*/
@Mapper
public interface UserProfileMapper extends BaseMapper<UserProfile> {
}
@@ -0,0 +1,75 @@
package com.emotion.service;
import com.baomidou.mybatisplus.extension.service.IService;
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.UserProfile;
import java.util.List;
/**
* 用户档案服务接口
*
* @author huazhongmin
* @date 2025-12-21
*/
public interface UserProfileService extends IService<UserProfile> {
/**
* 创建用户档案
*
* @param request 创建请求
* @return 用户档案响应对象
*/
UserProfileResponse createProfile(UserProfileCreateRequest request);
/**
* 更新用户档案
*
* @param request 更新请求
* @return 用户档案响应对象
*/
UserProfileResponse updateProfile(UserProfileUpdateRequest request);
/**
* 获取用户档案详情
*
* @param id 档案ID
* @return 用户档案响应对象
*/
UserProfileResponse getProfileById(String id);
/**
* 获取当前用户档案详情
*
* @return 用户档案响应对象
*/
UserProfileResponse getCurrentUserProfile();
/**
* 分页查询用户档案
*
* @param request 分页请求
* @return 分页结果
*/
PageResult<UserProfileResponse> getProfilePage(UserProfilePageRequest request);
/**
* 列表查询用户档案
*
* @param request 查询请求
* @return 列表结果
*/
List<UserProfileResponse> getProfileList(UserProfilePageRequest request);
/**
* 删除用户档案
*
* @param id 档案ID
* @return 是否成功
*/
boolean deleteProfile(String id);
}
@@ -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;
}
}