feat:生成评测docx

This commit is contained in:
lbw
2025-12-10 16:41:35 +08:00
parent 81f44376c5
commit d777437e82
20 changed files with 542 additions and 1 deletions

View File

@@ -90,6 +90,11 @@
<artifactId>log4j-core</artifactId> <artifactId>log4j-core</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@@ -0,0 +1,77 @@
package com.yinlihupo.enlish.service.controller;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.plugin.table.LoopRowTableRenderPolicy;
import com.yinlihupo.enlish.service.enums.AssessmentsType;
import com.yinlihupo.enlish.service.model.bo.Word;
import com.yinlihupo.enlish.service.model.vo.assessment.AssessmentStudentReqVO;
import com.yinlihupo.enlish.service.service.AssessmentService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RequestMapping("/assessment/")
@RestController
@Slf4j
public class AssessmentController {
@Resource
private AssessmentService assessmentService;
@PatchMapping("felt")
public void generateAssessmentDocxStudent(@RequestBody AssessmentStudentReqVO assessmentStudentReqVO, HttpServletResponse response) {
Integer studentId = assessmentStudentReqVO.getStudentId();
HashMap<Integer, Integer> unitIdAndWordCount = assessmentStudentReqVO.getUnitIdAndWordCount();
int assessmentDocxId = assessmentService.getAssessmentDocxId(studentId, AssessmentsType.ASSESSMENT_FELT);
List<Word> assessmentWords;
if (assessmentDocxId == 0) {
assessmentWords = assessmentService.genAssessmentWords(studentId, unitIdAndWordCount);
assessmentDocxId = assessmentService.getAssessmentDocxId(studentId, AssessmentsType.ASSESSMENT_FELT);
} else {
assessmentWords = assessmentService.getAssessmentWordsById(assessmentDocxId);
}
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
Configure config = Configure.builder()
.bind("words", policy)
.bind("answer", policy)
.build();
Map<String, Object> data = new HashMap<>();
data.put("assessment_id", assessmentDocxId);
data.put("words", assessmentWords);
data.put("answer", assessmentWords);
// 4. 渲染并输出
try (XWPFTemplate template = XWPFTemplate.compile("C:\\project\\java\\enlish_edu\\enlish\\enlish-service\\src\\main\\resources\\templates\\assessment.docx", config)) {
template.render(data);
String fileName = URLEncoder.encode( assessmentDocxId + "摸底测试.docx", StandardCharsets.UTF_8).replaceAll("\\+", "%20");
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
response.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + fileName);
OutputStream out = response.getOutputStream();
template.write(out);
out.flush();
out.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -0,0 +1,32 @@
package com.yinlihupo.enlish.service.domain.dataobject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class AssessmentsDO {
private Integer id;
private Integer studentId;
private LocalDateTime testDate;
private Integer testType;
private Integer level;
private Integer estimatedVocabSize;
private String contentDetailsJson;
}

View File

@@ -0,0 +1,25 @@
package com.yinlihupo.enlish.service.domain.dataobject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.math.BigDecimal;
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class WordMasteryLogDO {
private Integer id;
private Integer studentId;
private Integer wordId;
private Integer reviewCount;
private BigDecimal memoryStrength;
}

View File

@@ -0,0 +1,24 @@
package com.yinlihupo.enlish.service.domain.mapper;
import com.yinlihupo.enlish.service.domain.dataobject.AssessmentsDO;
import org.apache.ibatis.annotations.Param;
public interface AssessmentsDOMapper {
int deleteByPrimaryKey(Integer id);
int insert(AssessmentsDO record);
int insertSelective(AssessmentsDO record);
AssessmentsDO selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(AssessmentsDO record);
int updateByPrimaryKeyWithBLOBs(AssessmentsDO record);
int updateByPrimaryKey(AssessmentsDO record);
Integer selectAssessmentsIdByStudentIdAndTestType(@Param("studentId") Integer studentId, @Param("testType") Integer testType);
String selectContentDetailsJsonByAssessmentsId(@Param("assessmentsId") Integer assessmentsId);
}

View File

@@ -1,6 +1,9 @@
package com.yinlihupo.enlish.service.domain.mapper; package com.yinlihupo.enlish.service.domain.mapper;
import com.yinlihupo.enlish.service.domain.dataobject.VocabularyBankDO; import com.yinlihupo.enlish.service.domain.dataobject.VocabularyBankDO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface VocabularyBankDOMapper { public interface VocabularyBankDOMapper {
int deleteByPrimaryKey(Integer id); int deleteByPrimaryKey(Integer id);
@@ -14,4 +17,6 @@ public interface VocabularyBankDOMapper {
int updateByPrimaryKeySelective(VocabularyBankDO record); int updateByPrimaryKeySelective(VocabularyBankDO record);
int updateByPrimaryKey(VocabularyBankDO record); int updateByPrimaryKey(VocabularyBankDO record);
List<VocabularyBankDO> selectVocabularyBankDOListByUnitId(@Param("unitId") Integer unitId, @Param("wordCount") Integer wordCount);
} }

View File

@@ -0,0 +1,12 @@
package com.yinlihupo.enlish.service.domain.mapper;
import com.yinlihupo.enlish.service.domain.dataobject.WordMasteryLogDO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface WordMasteryLogDOMapper {
// 查询指定学生指定单元的词,根据记忆强度排序
List<WordMasteryLogDO> selectByStudentIdAndUnitIdOrderByMemoryStrength(@Param("studentId") Integer studentId, @Param("unitId") Integer unitId, @Param("limit") Integer limit);
}

View File

@@ -0,0 +1,15 @@
package com.yinlihupo.enlish.service.enums;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public enum AssessmentsType {
ASSESSMENT_FELT(1, "摸底测试");
private final Integer id;
private final String name;
}

View File

@@ -0,0 +1,19 @@
package com.yinlihupo.enlish.service.model.bo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class Word {
private Integer id;
private String title;
private String definition;
}

View File

@@ -0,0 +1,2 @@
// 中间层:业务对象
package com.yinlihupo.enlish.service.model.bo;

View File

@@ -0,0 +1 @@
package com.yinlihupo.enlish.service.model;

View File

@@ -0,0 +1,27 @@
package com.yinlihupo.enlish.service.model.vo.assessment;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.HashMap;
// 进行摸底测试
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class AssessmentStudentReqVO {
/**
* 学生 id
*/
private Integer studentId;
/**
* 单元 id 和词数
*/
private HashMap<Integer, Integer> unitIdAndWordCount;
}

View File

@@ -0,0 +1,17 @@
package com.yinlihupo.enlish.service.service;
import com.yinlihupo.enlish.service.enums.AssessmentsType;
import com.yinlihupo.enlish.service.model.bo.Word;
import java.util.HashMap;
import java.util.List;
public interface AssessmentService {
List<Word> genAssessmentWords(Integer studentId, HashMap<Integer, Integer> unitIdAndWordCount);
Integer getAssessmentDocxId(Integer studentId, AssessmentsType assessmentsType);
List<Word> getAssessmentWordsById(Integer id);
}

View File

@@ -0,0 +1,82 @@
package com.yinlihupo.enlish.service.service.assessment;
import com.yinlihupo.enlish.service.domain.dataobject.AssessmentsDO;
import com.yinlihupo.enlish.service.domain.dataobject.VocabularyBankDO;
import com.yinlihupo.enlish.service.domain.mapper.AssessmentsDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.VocabularyBankDOMapper;
import com.yinlihupo.enlish.service.domain.mapper.WordMasteryLogDOMapper;
import com.yinlihupo.enlish.service.enums.AssessmentsType;
import com.yinlihupo.enlish.service.model.bo.Word;
import com.yinlihupo.enlish.service.service.AssessmentService;
import com.yinlihupo.framework.common.util.JsonUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class AssessmentServiceImpl implements AssessmentService {
@Resource
private VocabularyBankDOMapper vocabularyBankDOMapper;
@Resource
private WordMasteryLogDOMapper wordMasteryLogDOMapper;
@Resource
private AssessmentsDOMapper assessmentsDOMapper;
@Override
public List<Word> genAssessmentWords(Integer studentId, HashMap<Integer, Integer> unitIdAndWordCount) {
List<Word> words = new ArrayList<>();
for (Map.Entry<Integer, Integer> entry : unitIdAndWordCount.entrySet()) {
Integer unitId = entry.getKey();
Integer wordCount = entry.getValue();
List<VocabularyBankDO> vocabularyBankDOS = vocabularyBankDOMapper.selectVocabularyBankDOListByUnitId(unitId, wordCount);
words.addAll(
vocabularyBankDOS.stream()
.map(vocabularyBankDO -> Word.builder()
.id(vocabularyBankDO.getId())
.title(vocabularyBankDO.getWord())
.definition(vocabularyBankDO.getDefinition())
.build())
.toList()
);
}
assessmentsDOMapper.insertSelective(
AssessmentsDO.builder()
.studentId(studentId)
.level(0)
.testType(AssessmentsType.ASSESSMENT_FELT.getId())
.testDate(LocalDateTime.now())
.estimatedVocabSize(0)
.contentDetailsJson(JsonUtils.toJsonString(words))
.build()
);
return words;
}
@Override
public Integer getAssessmentDocxId(Integer studentId, AssessmentsType assessmentsType) {
return assessmentsDOMapper.selectAssessmentsIdByStudentIdAndTestType(studentId, assessmentsType.getId());
}
@Override
public List<Word> getAssessmentWordsById(Integer id) {
String contentJson = assessmentsDOMapper.selectContentDetailsJsonByAssessmentsId(id);
try {
return JsonUtils.parseList(contentJson, Word.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@@ -45,7 +45,7 @@
targetProject="src/main/java"/> targetProject="src/main/java"/>
<!-- 需要生成的表-实体类 --> <!-- 需要生成的表-实体类 -->
<table tableName="unit" domainObjectName="UnitDO" <table tableName="assessments" domainObjectName="AssessmentsDO"
enableCountByExample="false" enableCountByExample="false"
enableUpdateByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableDeleteByExample="false"

View File

@@ -0,0 +1,156 @@
<?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.yinlihupo.enlish.service.domain.mapper.AssessmentsDOMapper">
<resultMap id="BaseResultMap" type="com.yinlihupo.enlish.service.domain.dataobject.AssessmentsDO">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="student_id" jdbcType="INTEGER" property="studentId" />
<result column="test_date" jdbcType="TIMESTAMP" property="testDate" />
<result column="test_type" jdbcType="INTEGER" property="testType" />
<result column="level" jdbcType="INTEGER" property="level" />
<result column="estimated_vocab_size" jdbcType="INTEGER" property="estimatedVocabSize" />
</resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="com.yinlihupo.enlish.service.domain.dataobject.AssessmentsDO">
<result column="content_details_json" jdbcType="LONGVARCHAR" property="contentDetailsJson" />
</resultMap>
<sql id="Base_Column_List">
id, student_id, test_date, test_type, level, estimated_vocab_size
</sql>
<sql id="Blob_Column_List">
content_details_json
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="ResultMapWithBLOBs">
select
<include refid="Base_Column_List" />
,
<include refid="Blob_Column_List" />
from assessments
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from assessments
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.yinlihupo.enlish.service.domain.dataobject.AssessmentsDO">
insert into assessments (id, student_id, test_date,
test_type, level, estimated_vocab_size,
content_details_json)
values (#{id,jdbcType=INTEGER}, #{studentId,jdbcType=INTEGER}, #{testDate,jdbcType=TIMESTAMP},
#{testType,jdbcType=INTEGER}, #{level,jdbcType=INTEGER}, #{estimatedVocabSize,jdbcType=INTEGER},
#{contentDetailsJson,jdbcType=LONGVARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.yinlihupo.enlish.service.domain.dataobject.AssessmentsDO">
insert into assessments
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="studentId != null">
student_id,
</if>
<if test="testDate != null">
test_date,
</if>
<if test="testType != null">
test_type,
</if>
<if test="level != null">
level,
</if>
<if test="estimatedVocabSize != null">
estimated_vocab_size,
</if>
<if test="contentDetailsJson != null">
content_details_json,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="studentId != null">
#{studentId,jdbcType=INTEGER},
</if>
<if test="testDate != null">
#{testDate,jdbcType=TIMESTAMP},
</if>
<if test="testType != null">
#{testType,jdbcType=INTEGER},
</if>
<if test="level != null">
#{level,jdbcType=INTEGER},
</if>
<if test="estimatedVocabSize != null">
#{estimatedVocabSize,jdbcType=INTEGER},
</if>
<if test="contentDetailsJson != null">
#{contentDetailsJson,jdbcType=LONGVARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.yinlihupo.enlish.service.domain.dataobject.AssessmentsDO">
update assessments
<set>
<if test="studentId != null">
student_id = #{studentId,jdbcType=INTEGER},
</if>
<if test="testDate != null">
test_date = #{testDate,jdbcType=TIMESTAMP},
</if>
<if test="testType != null">
test_type = #{testType,jdbcType=INTEGER},
</if>
<if test="level != null">
level = #{level,jdbcType=INTEGER},
</if>
<if test="estimatedVocabSize != null">
estimated_vocab_size = #{estimatedVocabSize,jdbcType=INTEGER},
</if>
<if test="contentDetailsJson != null">
content_details_json = #{contentDetailsJson,jdbcType=LONGVARCHAR},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKeyWithBLOBs" parameterType="com.yinlihupo.enlish.service.domain.dataobject.AssessmentsDO">
update assessments
set student_id = #{studentId,jdbcType=INTEGER},
test_date = #{testDate,jdbcType=TIMESTAMP},
test_type = #{testType,jdbcType=INTEGER},
level = #{level,jdbcType=INTEGER},
estimated_vocab_size = #{estimatedVocabSize,jdbcType=INTEGER},
content_details_json = #{contentDetailsJson,jdbcType=LONGVARCHAR}
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.yinlihupo.enlish.service.domain.dataobject.AssessmentsDO">
update assessments
set student_id = #{studentId,jdbcType=INTEGER},
test_date = #{testDate,jdbcType=TIMESTAMP},
test_type = #{testType,jdbcType=INTEGER},
level = #{level,jdbcType=INTEGER},
estimated_vocab_size = #{estimatedVocabSize,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
<select id="selectAssessmentsIdByStudentIdAndTestType">
select id
from assessments
where student_id = #{studentId,jdbcType=INTEGER}
and test_type = #{testType,jdbcType=INTEGER}
</select>
<select id="selectContentDetailsJsonByAssessmentsId">
select content_details_json
from assessments
where id = #{assessmentsId,jdbcType=INTEGER}
</select>
</mapper>

View File

@@ -97,4 +97,11 @@
where id = #{id,jdbcType=INTEGER} where id = #{id,jdbcType=INTEGER}
</update> </update>
<select id="selectVocabularyBankDOListByUnitId" resultMap="BaseResultMap">
select *
from vocabulary_bank
where unit_id = #{unitId}
limit #{wordCount}
</select>
</mapper> </mapper>

View File

@@ -0,0 +1,29 @@
<?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.yinlihupo.enlish.service.domain.mapper.WordMasteryLogDOMapper">
<resultMap id="BaseResultMap" type="com.yinlihupo.enlish.service.domain.dataobject.WordMasteryLogDO">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="student_id" jdbcType="INTEGER" property="studentId" />
<result column="word_id" jdbcType="INTEGER" property="wordId" />
<result column="review_count" jdbcType="INTEGER" property="reviewCount" />
<result column="memory_strength" jdbcType="DECIMAL" property="memoryStrength" />
</resultMap>
<sql id="Base_Column_List">
id, student_id, word_id, review_count, memory_strength
</sql>
<select id="selectByStudentIdAndUnitIdOrderByMemoryStrength" resultMap="BaseResultMap">
select *
from word_mastery_log
where student_id = #{studentId}
and word_id in (
select id
from vocabulary_bank
where unit_id = #{unitId}
)
order by memory_strength desc
limit #{limit}
</select>
</mapper>

View File

@@ -103,6 +103,12 @@
<version>2.20.0</version> <version>2.20.0</version>
</dependency> </dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.1</version>
</dependency>
<!-- 避免编写那些冗余的 Java 样板式代码,如 get、set 等 --> <!-- 避免编写那些冗余的 Java 样板式代码,如 get、set 等 -->
<dependency> <dependency>
<groupId>org.projectlombok</groupId> <groupId>org.projectlombok</groupId>