From 1dd3f8fdbbfc8a41f35a32f153201724a8faa2b3 Mon Sep 17 00:00:00 2001
From: xiangpei <xiangpei@timesnew.cn>
Date: 星期二, 14 五月 2024 18:12:27 +0800
Subject: [PATCH] 保存顺序练习记录

---
 src/main/java/com/mindskip/xzs/service/PracticeQuestionConditionService.java               |   25 ++++++
 src/main/resources/mapper/PracticeQuestionConditionMapper.xml                              |   22 +++++
 src/main/java/com/mindskip/xzs/service/impl/PracticeQuestionConditionServiceImpl.java      |   46 +++++++++++
 src/main/java/com/mindskip/xzs/repository/PracticeQuestionConditionMapper.java             |   38 +++++++++
 src/main/java/com/mindskip/xzs/controller/student/PracticeQuestionConditionController.java |   30 +++++++
 src/main/java/com/mindskip/xzs/domain/PracticeQuestionCondition.java                       |   53 +++++++++++++
 6 files changed, 214 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/mindskip/xzs/controller/student/PracticeQuestionConditionController.java b/src/main/java/com/mindskip/xzs/controller/student/PracticeQuestionConditionController.java
new file mode 100644
index 0000000..30e883c
--- /dev/null
+++ b/src/main/java/com/mindskip/xzs/controller/student/PracticeQuestionConditionController.java
@@ -0,0 +1,30 @@
+package com.mindskip.xzs.controller.student;
+
+import com.mindskip.xzs.base.RestResponse;
+import com.mindskip.xzs.domain.PracticeQuestionCondition;
+import com.mindskip.xzs.service.PracticeQuestionConditionService;
+import lombok.RequiredArgsConstructor;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author锛歺p
+ * @date锛�2024/5/14 17:44
+ */
+@RestController
+@RequiredArgsConstructor
+@RequestMapping("/api/student/practice/condition")
+public class PracticeQuestionConditionController {
+
+    private final PracticeQuestionConditionService service;
+
+    @PostMapping("/save")
+    public RestResponse save(@RequestBody @Validated PracticeQuestionCondition vo) {
+        service.save(vo);
+        return RestResponse.ok();
+    }
+
+}
diff --git a/src/main/java/com/mindskip/xzs/domain/PracticeQuestionCondition.java b/src/main/java/com/mindskip/xzs/domain/PracticeQuestionCondition.java
new file mode 100644
index 0000000..98e76ea
--- /dev/null
+++ b/src/main/java/com/mindskip/xzs/domain/PracticeQuestionCondition.java
@@ -0,0 +1,53 @@
+package com.mindskip.xzs.domain;
+
+import io.swagger.models.auth.In;
+import lombok.Data;
+
+import javax.validation.constraints.NotEmpty;
+import javax.validation.constraints.NotNull;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * 缁冧範璁板綍
+ *
+ * @author锛歺p
+ * @date锛�2024/5/14 17:13
+ */
+@Data
+public class PracticeQuestionCondition {
+
+    private Integer id;
+
+    @NotNull(message = "缁冧範ID涓嶈兘涓虹┖")
+    private Integer practiceId;
+
+    private Integer userId;
+
+    /** json瀛樺偍棰樼洰鏁版嵁锛歀ist<QuestionFinishCondition> */
+    private String content;
+
+    /** 鏈�杩戜竴娆′繚瀛樻椂闂� */
+    private Date latestTime;
+
+
+    /** 鍋氶璇︽儏 */
+    @NotEmpty(message = "鏈仛棰樹笉鑳戒繚瀛�")
+    private List<QuestionFinishCondition> questionConditions;
+
+    @Data
+    public static class QuestionFinishCondition {
+
+        /** 棰樼洰 */
+        private Integer questionId;
+
+        /** 鏄惁宸茬粡鍋氫簡 */
+        private Boolean hasDo;
+
+        /** 鍋氬娌℃湁 */
+        private Boolean right;
+
+        /** 绛旀 */
+        private String answer;
+    }
+}
diff --git a/src/main/java/com/mindskip/xzs/repository/PracticeQuestionConditionMapper.java b/src/main/java/com/mindskip/xzs/repository/PracticeQuestionConditionMapper.java
new file mode 100644
index 0000000..5c79620
--- /dev/null
+++ b/src/main/java/com/mindskip/xzs/repository/PracticeQuestionConditionMapper.java
@@ -0,0 +1,38 @@
+package com.mindskip.xzs.repository;
+
+import com.mindskip.xzs.domain.PracticeQuestionCondition;
+import org.apache.ibatis.annotations.Mapper;
+import org.apache.ibatis.annotations.Param;
+
+import java.util.Date;
+
+/**
+ * @author锛歺p
+ * @date锛�2024/5/14 17:23
+ */
+@Mapper
+public interface PracticeQuestionConditionMapper {
+
+    /**
+     * 鏍规嵁缁冧範ID鏌ユ壘
+     *
+     * @param practiceId
+     * @return
+     */
+    PracticeQuestionCondition selectByPracticeId(Integer practiceId);
+
+    /**
+     * 娣诲姞
+     *
+     * @param old
+     */
+    void save(@Param("entity") PracticeQuestionCondition old);
+
+    /**
+     * 鏇存柊鍐呭
+     *
+     * @param content
+     * @param id
+     */
+    void updateContentById(@Param("content") String content, @Param("id") Integer id, @Param("latestTime") Date latestTime);
+}
diff --git a/src/main/java/com/mindskip/xzs/service/PracticeQuestionConditionService.java b/src/main/java/com/mindskip/xzs/service/PracticeQuestionConditionService.java
new file mode 100644
index 0000000..f773a65
--- /dev/null
+++ b/src/main/java/com/mindskip/xzs/service/PracticeQuestionConditionService.java
@@ -0,0 +1,25 @@
+package com.mindskip.xzs.service;
+
+import com.mindskip.xzs.domain.PracticeQuestionCondition;
+
+/**
+ * @author锛歺p
+ * @date锛�2024/5/14 17:24
+ */
+public interface PracticeQuestionConditionService {
+
+    /**
+     * 淇濆瓨鏁版嵁
+     *
+     * @param entity
+     */
+    void save(PracticeQuestionCondition entity);
+
+    /**
+     * 閫氳繃缁冧範ID鏌ユ壘
+     *
+     * @param practiceId
+     */
+    PracticeQuestionCondition selectByPracticeId(Integer practiceId);
+
+}
diff --git a/src/main/java/com/mindskip/xzs/service/impl/PracticeQuestionConditionServiceImpl.java b/src/main/java/com/mindskip/xzs/service/impl/PracticeQuestionConditionServiceImpl.java
new file mode 100644
index 0000000..31f74d9
--- /dev/null
+++ b/src/main/java/com/mindskip/xzs/service/impl/PracticeQuestionConditionServiceImpl.java
@@ -0,0 +1,46 @@
+package com.mindskip.xzs.service.impl;
+
+import com.alibaba.fastjson.JSON;
+import com.mindskip.xzs.context.WebContext;
+import com.mindskip.xzs.domain.PracticeQuestionCondition;
+import com.mindskip.xzs.repository.PracticeQuestionConditionMapper;
+import com.mindskip.xzs.service.PracticeQuestionConditionService;
+import lombok.RequiredArgsConstructor;
+import org.springframework.stereotype.Service;
+
+import java.util.Date;
+import java.util.Objects;
+
+/**
+ * @author锛歺p
+ * @date锛�2024/5/14 17:26
+ */
+@Service
+@RequiredArgsConstructor
+public class PracticeQuestionConditionServiceImpl implements PracticeQuestionConditionService {
+
+    private final PracticeQuestionConditionMapper mapper;
+    private final WebContext webContext;
+
+    @Override
+    public void save(PracticeQuestionCondition entity) {
+        PracticeQuestionCondition old = mapper.selectByPracticeId(entity.getPracticeId());
+        if (Objects.isNull(old)) {
+            old = new PracticeQuestionCondition();
+            old.setUserId(webContext.getCurrentUser().getId());
+            old.setPracticeId(entity.getPracticeId());
+            old.setContent(JSON.toJSONString(entity.getQuestionConditions()));
+            old.setLatestTime(new Date());
+            mapper.save(old);
+        } else {
+            // 鏇存柊鍐呭
+            mapper.updateContentById(JSON.toJSONString(entity.getQuestionConditions()), old.getId(), new Date());
+        }
+    }
+
+    @Override
+    public PracticeQuestionCondition selectByPracticeId(Integer practiceId) {
+        PracticeQuestionCondition old = mapper.selectByPracticeId(practiceId);
+        return old;
+    }
+}
diff --git a/src/main/resources/mapper/PracticeQuestionConditionMapper.xml b/src/main/resources/mapper/PracticeQuestionConditionMapper.xml
new file mode 100644
index 0000000..305a8f6
--- /dev/null
+++ b/src/main/resources/mapper/PracticeQuestionConditionMapper.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="com.mindskip.xzs.repository.PracticeQuestionConditionMapper">
+
+    <select id="selectByPracticeId" resultType="com.mindskip.xzs.domain.PracticeQuestionCondition">
+        SELECT
+            *
+        FROM
+            t_practice_question_condition
+        WHERE
+            practice_id = #{practiceId}
+    </select>
+
+    <insert id="save" parameterType="com.mindskip.xzs.domain.PracticeQuestionCondition">
+        INSERT INTO t_practice_question_condition(user_id, practice_id, content, latest_time)
+            VALUE (#{entity.userId}, #{entity.practiceId}, #{entity.content}, #{entity.latestTime})
+    </insert>
+
+    <update id="updateContentById">
+        UPDATE t_practice_question_condition SET content = #{content}, latest_time = #{latestTime} WHERE id = #{id}
+    </update>
+</mapper>

--
Gitblit v1.8.0