diff --git a/Debate_backend/src/main/java/com/learning/newdemo/controller/DebateHistoryController.java b/Debate_backend/src/main/java/com/learning/newdemo/controller/DebateHistoryController.java index 136aec7..fb64ad5 100644 --- a/Debate_backend/src/main/java/com/learning/newdemo/controller/DebateHistoryController.java +++ b/Debate_backend/src/main/java/com/learning/newdemo/controller/DebateHistoryController.java @@ -9,50 +9,29 @@ import org.springframework.web.bind.annotation.*; import java.util.List; -/** - * 辩论历史记录控制器 - * 处理与辩论历史记录相关的HTTP请求 - */ -@RestController // 标识为RESTful控制器 -@RequestMapping("/api/debate-history") // 基础请求路径 -@RequiredArgsConstructor // Lombok注解,自动生成构造函数注入依赖 +@RestController +@RequestMapping("/api/debate-history") +@RequiredArgsConstructor public class DebateHistoryController { - // 依赖注入 private final DebateHistoryService historyService; private final JwtUtil jwtUtil; - /** - * 保存辩论历史记录接口 - * @param history 前端传来的辩论历史数据(自动反序列化为对象) - * @param token 认证令牌(从请求头获取) - * @return 统一响应结果 - */ - @PostMapping // 处理POST请求 + @PostMapping public Result saveHistory(@RequestBody DebateHistory history, @RequestHeader("Authorization") String token) { - // 从JWT令牌中解析用户ID Integer userId = jwtUtil.getUserIdFromToken(token); - // 设置记录关联的用户ID if (userId == null) { return Result.error(401, "无效的认证令牌"); } history.setUserId(userId); - // 调用服务层保存记录 historyService.saveDebateHistory(history); - // 返回成功响应 return Result.success(); } - /** - * 获取用户辩论历史记录接口 - * @param token 认证令牌(从请求头获取) - * @return 包含历史记录列表的统一响应结果 - */ - @GetMapping // 处理GET请求 + @GetMapping public Result> getHistories(@RequestHeader("Authorization") String token) { Integer userId = jwtUtil.getUserIdFromToken(token); - // 调用服务层获取记录并包装返回 return Result.success(historyService.getHistoriesByUser(userId)); } } diff --git a/Debate_backend/src/main/java/com/learning/newdemo/controller/ReviewHistoryController.java b/Debate_backend/src/main/java/com/learning/newdemo/controller/ReviewHistoryController.java new file mode 100644 index 0000000..ec967c0 --- /dev/null +++ b/Debate_backend/src/main/java/com/learning/newdemo/controller/ReviewHistoryController.java @@ -0,0 +1,112 @@ +package com.learning.newdemo.controller; + +import com.learning.newdemo.common.Result; +import com.learning.newdemo.entity.DebateHistory; +import com.learning.newdemo.service.DebateHistoryService; +import com.learning.newdemo.service.WxReviewService; +import com.learning.newdemo.util.JwtUtil; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +import java.util.HashMap; +import java.util.Map; + +@Slf4j +@RestController +@RequiredArgsConstructor +@RequestMapping("api/review") +public class ReviewHistoryController { + + private final WxReviewService wxReviewService; + private final DebateHistoryService historyService; + private final JwtUtil jwtUtil; + + @PostMapping("/auto-save") + public Result> autoSaveReview( + @RequestBody Map params, + @RequestHeader("Authorization") String token) { + + log.info("自动保存复盘请求: {}", params); + Integer userId = parseUserId(token); + if (userId == null) return Result.error(401, "认证失败"); + + String content = params.get("content"); + String topic = params.get("topic"); + String stance = params.get("stance"); + + if (content == null || content.isEmpty()) { + return Result.error("内容不能为空"); + } + + try { + String review = wxReviewService.GetReview(content); + if (review == null) return Result.error("复盘生成失败"); + + DebateHistory history = new DebateHistory(); + history.setUserId(userId); + history.setTopic(topic); + history.setStance(stance); + history.setContent(content); + history.setReview(review); + history.setRounds(0); // 默认0轮,可根据实际情况调整 + + historyService.saveDebateHistory(history); + + Map data = new HashMap<>(); + data.put("review", review); + data.put("historyId", history.getId()); + return Result.success(data); + } catch (Exception e) { + log.error("复盘保存失败", e); + return Result.error("处理失败: " + e.getMessage()); + } + } + + @PostMapping("/save-only") + public Result> saveOnly( + @RequestBody Map params, + @RequestHeader("Authorization") String token) { + + Integer userId = parseUserId(token); + if (userId == null) return Result.error(401, "认证失败"); + + String content = params.get("content"); + String review = params.get("review"); + String topic = params.get("topic"); + String stance = params.get("stance"); + + if (content == null || review == null) { + return Result.error("内容和复盘结果不能为空"); + } + + try { + DebateHistory history = new DebateHistory(); + history.setUserId(userId); + history.setTopic(topic); + history.setStance(stance); + history.setContent(content); + history.setReview(review); + history.setRounds(0); + + historyService.saveDebateHistory(history); + + Map data = new HashMap<>(); + data.put("review", review); + data.put("historyId", history.getId()); + return Result.success(data); + } catch (Exception e) { + log.error("保存失败", e); + return Result.error("保存失败: " + e.getMessage()); + } + } + + private Integer parseUserId(String token) { + try { + return jwtUtil.getUserIdFromToken(token); + } catch (Exception e) { + log.warn("Token解析失败", e); + return null; + } + } +} diff --git a/Debate_backend/src/main/java/com/learning/newdemo/entity/DebateHistory.java b/Debate_backend/src/main/java/com/learning/newdemo/entity/DebateHistory.java index 63fd909..9357b09 100644 --- a/Debate_backend/src/main/java/com/learning/newdemo/entity/DebateHistory.java +++ b/Debate_backend/src/main/java/com/learning/newdemo/entity/DebateHistory.java @@ -1,28 +1,24 @@ package com.learning.newdemo.entity; -import com.learning.newdemo.enums.Position; import lombok.Data; - import java.util.Date; @Data public class DebateHistory { private Integer id; - private Integer userId; // 关联用户ID - - private Integer argumentId; // 关联立论记录ID - - private String topic; // 辩题 - - private Position position; // 用户持方 - - private Short totalRounds; // 总回合数 - - private String debateContent; // 所有辩论内容(JSON格式) - - private String reviewResult; // 复盘分析内容 - - private Date createTime; // 创建时间 - - private ArgumentHistory argumentHistory; //关联的立论记录 + private Integer userId; + private String topic; + private String stance; // "正方"或"反方" + private String content; // 辩论内容JSON + private String review; // AI复盘内容 + private Integer rounds; + private Date createTime; + + // 注意:setStance方法应该验证输入 + public void setStance(String stance) { + if (!"正方".equals(stance) && !"反方".equals(stance)) { + throw new IllegalArgumentException("持方必须是'正方'或'反方'"); + } + this.stance = stance; + } } diff --git a/Debate_backend/src/main/java/com/learning/newdemo/mapper/DebateHistoryMapper.java b/Debate_backend/src/main/java/com/learning/newdemo/mapper/DebateHistoryMapper.java index a569998..96af56e 100644 --- a/Debate_backend/src/main/java/com/learning/newdemo/mapper/DebateHistoryMapper.java +++ b/Debate_backend/src/main/java/com/learning/newdemo/mapper/DebateHistoryMapper.java @@ -5,90 +5,31 @@ import org.apache.ibatis.annotations.*; import java.util.List; -/** - * 辩论历史记录数据访问层接口 - * 定义所有与辩论历史记录表相关的数据库操作 - */ +@Mapper public interface DebateHistoryMapper { - /** - * 插入新的辩论记录 - * @param debateHistory 包含辩论详细信息的实体对象 - * @return 插入操作影响的行数(通常为1) - */ - @Insert("INSERT INTO debate_history(user_id, argument_id, topic, position, total_rounds, debate_content, review_result) " + - "VALUES(#{userId}, #{argumentId}, #{topic}, #{position}, #{totalRounds}, #{debateContent}, #{reviewResult})") - @Options(useGeneratedKeys = true, keyProperty = "id") // 获取自增主键 + @Insert("INSERT INTO debate_history(user_id, topic, stance, content, review, rounds) " + + "VALUES(#{userId}, #{topic}, #{stance}, #{content}, #{review}, #{rounds})") + @Options(useGeneratedKeys = true, keyProperty = "id") int insert(DebateHistory debateHistory); - /** - * 根据用户ID查询所有辩论记录(按时间倒序) - * @param userId 用户唯一标识 - * @return 该用户的所有辩论记录列表 - */ @Select("SELECT * FROM debate_history WHERE user_id = #{userId} ORDER BY create_time DESC") List selectByUserId(@Param("userId") Integer userId); - /** - * 获取用户最新的指定数量的辩论记录 - * @param userId 用户ID - * @param limit 要获取的记录数量 - * @return 最新的辩论记录列表 - */ @Select("SELECT * FROM debate_history WHERE user_id = #{userId} ORDER BY create_time DESC LIMIT #{limit}") List selectLatestByUserId(@Param("userId") Integer userId, @Param("limit") Integer limit); - /** - * 根据关联的立论记录ID查询辩论记录 - * @param argumentId 立论记录ID - * @return 相关联的辩论记录列表 - */ - @Select("SELECT * FROM debate_history WHERE argument_id = #{argumentId} ORDER BY create_time DESC") - List selectByArgumentId(@Param("argumentId") Integer argumentId); - - /** - * 根据主键ID查询单条辩论记录 - * @param id 记录主键ID - * @return 对应的辩论记录实体 - */ @Select("SELECT * FROM debate_history WHERE id = #{id}") DebateHistory selectByPrimaryKey(@Param("id") Integer id); - /** - * 更新辩论记录的复盘分析内容 - * @param id 要更新的记录ID - * @param reviewResult 新的复盘内容 - * @return 更新操作影响的行数 - */ - @Update("UPDATE debate_history SET review_result = #{reviewResult} WHERE id = #{id}") - int updateReviewResult(@Param("id") Integer id, @Param("reviewResult") String reviewResult); - - /** - * 查询辩论记录及其关联的立论内容(联合查询) - * @param id 辩论记录ID - * @return 包含完整立论内容的辩论记录实体 - */ - @Select("SELECT dh.*, ah.topic AS argument_topic, ah.argument_content " + - "FROM debate_history dh LEFT JOIN argument_history ah ON dh.argument_id = ah.id " + - "WHERE dh.id = #{id}") - DebateHistory selectWithArgument(@Param("id") Integer id); + @Update("UPDATE debate_history SET review = #{review} WHERE id = #{id}") + int updateReview(@Param("id") Integer id, @Param("review") String review); - /** - * 清理用户超出限制的历史记录(保留最新的10条) - * @param userId 用户ID - * @return 被删除的记录数量 - */ @Delete("DELETE FROM debate_history WHERE user_id = #{userId} AND id NOT IN " + "(SELECT id FROM (SELECT id FROM debate_history WHERE user_id = #{userId} " + "ORDER BY create_time DESC LIMIT 10) t)") int cleanOverflowHistories(@Param("userId") Integer userId); - /** - * 根据辩论主题关键词搜索记录(新增方法) - * @param userId 用户ID - * @param keyword 搜索关键词 - * @return 匹配的记录列表 - */ @Select("SELECT * FROM debate_history WHERE user_id = #{userId} AND topic LIKE CONCAT('%',#{keyword},'%') " + "ORDER BY create_time DESC") List searchByKeyword(@Param("userId") Integer userId, @Param("keyword") String keyword); diff --git a/Debate_backend/target/classes/com/learning/newdemo/controller/DebateHistoryController.class b/Debate_backend/target/classes/com/learning/newdemo/controller/DebateHistoryController.class new file mode 100644 index 0000000..fa04a40 Binary files /dev/null and b/Debate_backend/target/classes/com/learning/newdemo/controller/DebateHistoryController.class differ diff --git a/Debate_backend/target/classes/com/learning/newdemo/entity/ArgumentHistory.class b/Debate_backend/target/classes/com/learning/newdemo/entity/ArgumentHistory.class new file mode 100644 index 0000000..761aeac Binary files /dev/null and b/Debate_backend/target/classes/com/learning/newdemo/entity/ArgumentHistory.class differ diff --git a/Debate_backend/target/classes/com/learning/newdemo/entity/DebateHistory.class b/Debate_backend/target/classes/com/learning/newdemo/entity/DebateHistory.class new file mode 100644 index 0000000..1e14160 Binary files /dev/null and b/Debate_backend/target/classes/com/learning/newdemo/entity/DebateHistory.class differ diff --git a/Debate_backend/target/classes/com/learning/newdemo/enums/Position.class b/Debate_backend/target/classes/com/learning/newdemo/enums/Position.class new file mode 100644 index 0000000..a74b845 Binary files /dev/null and b/Debate_backend/target/classes/com/learning/newdemo/enums/Position.class differ diff --git a/Debate_backend/target/classes/com/learning/newdemo/mapper/ArgumentHistoryMapper.class b/Debate_backend/target/classes/com/learning/newdemo/mapper/ArgumentHistoryMapper.class new file mode 100644 index 0000000..1a4e64c Binary files /dev/null and b/Debate_backend/target/classes/com/learning/newdemo/mapper/ArgumentHistoryMapper.class differ diff --git a/Debate_backend/target/classes/com/learning/newdemo/mapper/DebateHistoryMapper.class b/Debate_backend/target/classes/com/learning/newdemo/mapper/DebateHistoryMapper.class new file mode 100644 index 0000000..06fa9fa Binary files /dev/null and b/Debate_backend/target/classes/com/learning/newdemo/mapper/DebateHistoryMapper.class differ diff --git a/Debate_backend/target/classes/com/learning/newdemo/service/DebateHistoryService.class b/Debate_backend/target/classes/com/learning/newdemo/service/DebateHistoryService.class new file mode 100644 index 0000000..0c3f4b0 Binary files /dev/null and b/Debate_backend/target/classes/com/learning/newdemo/service/DebateHistoryService.class differ diff --git a/Debate_backend/target/classes/com/learning/newdemo/service/impl/DebateHistoryServiceImpl.class b/Debate_backend/target/classes/com/learning/newdemo/service/impl/DebateHistoryServiceImpl.class new file mode 100644 index 0000000..b20fa19 Binary files /dev/null and b/Debate_backend/target/classes/com/learning/newdemo/service/impl/DebateHistoryServiceImpl.class differ diff --git a/Debate_backend/target/classes/mapper/ArgumentHistoryMapper.xml b/Debate_backend/target/classes/mapper/ArgumentHistoryMapper.xml new file mode 100644 index 0000000..118aca7 --- /dev/null +++ b/Debate_backend/target/classes/mapper/ArgumentHistoryMapper.xml @@ -0,0 +1,89 @@ + + + + + + + + + + + + + + + id, user_id, topic, argument_content, position, create_time + + + + + INSERT INTO argument_history ( + user_id, topic, argument_content, position + ) + VALUES ( + #{userId,jdbcType=INTEGER}, + #{topic,jdbcType=VARCHAR}, + #{argumentContent,jdbcType=LONGVARCHAR}, + #{position,jdbcType=VARCHAR, typeHandler=org.apache.ibatis.type.EnumTypeHandler} + ) + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Debate_backend/target/classes/mapper/DebateHistoryMapper.xml b/Debate_backend/target/classes/mapper/DebateHistoryMapper.xml new file mode 100644 index 0000000..98ce52c --- /dev/null +++ b/Debate_backend/target/classes/mapper/DebateHistoryMapper.xml @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + id, user_id, argument_id, topic, position, total_rounds, debate_content, review_result, create_time + + + + dh.id, dh.user_id, dh.argument_id, dh.topic, dh.position, dh.total_rounds, + dh.debate_content, dh.review_result, dh.create_time, + ah.id as arg_id, ah.user_id as arg_user_id, ah.topic as arg_topic, + ah.argument_content as arg_content, ah.position as arg_position, + ah.create_time as arg_create_time + + + + + INSERT INTO debate_history ( + user_id, argument_id, topic, position, total_rounds, debate_content + ) + VALUES ( + #{userId,jdbcType=INTEGER}, + #{argumentId,jdbcType=INTEGER}, + #{topic,jdbcType=VARCHAR}, + #{position,jdbcType=VARCHAR, typeHandler=org.apache.ibatis.type.EnumTypeHandler}, + #{totalRounds,jdbcType=SMALLINT}, + #{debateContent,jdbcType=LONGVARCHAR} + ) + + + + + + + + + + + + + + + + + UPDATE debate_history + SET review_result = #{reviewResult,jdbcType=LONGVARCHAR} + WHERE id = #{id,jdbcType=INTEGER} + + + + + \ No newline at end of file diff --git a/database/database.sql b/database/database.sql index fa536e2..871208a 100644 --- a/database/database.sql +++ b/database/database.sql @@ -19,7 +19,7 @@ CREATE TABLE IF NOT EXISTS `wx_user` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='微信用户表'; --- 在现有database.sql文件末尾添加(不要删除原有内容) + CREATE TABLE IF NOT EXISTS `debate_history` ( `id` int(11) NOT NULL AUTO_INCREMENT, `user_id` int(11) NOT NULL COMMENT '关联用户ID',