Commit c8dd6499 authored by wujipeng's avatar wujipeng
Browse files

优化

parent 7e7c8c28
...@@ -16,6 +16,19 @@ public interface TheoreticalPracticeStudentQuestionBankService extends IService< ...@@ -16,6 +16,19 @@ public interface TheoreticalPracticeStudentQuestionBankService extends IService<
*/ */
void ensureStudentLibrary(String level, List<String> studentIds); void ensureStudentLibrary(String level, List<String> studentIds);
/**
* 重置一次理论练习的学员数据并重新生成个人题库。
*
* <p>作答记录按练习ID整场清空,使该练习的实时统计同步归零;个人题库只清空指定
* 学员在当前等级下的数据,避免影响其其他等级的学习记录。所有操作由调用方事务统一
* 管理,重新生成失败时删除操作会一并回滚。</p>
*
* @param practiceId 当前理论练习ID
* @param level 当前理论练习等级
* @param studentIds 本次重新分配的学员ID
*/
void resetStudentLibrary(String practiceId, String level, List<String> studentIds);
List<TheoreticalPracticeStudentQuestionBankVO> onePage(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO); List<TheoreticalPracticeStudentQuestionBankVO> onePage(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO);
List<TheoreticalPracticeStudentQuestionBankVO> twoPage(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO); List<TheoreticalPracticeStudentQuestionBankVO> twoPage(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO);
......
...@@ -44,8 +44,10 @@ import java.net.URLEncoder; ...@@ -44,8 +44,10 @@ import java.net.URLEncoder;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
...@@ -140,25 +142,58 @@ public class TheoreticalPracticeServiceImpl extends ServiceImpl<TheoreticalPract ...@@ -140,25 +142,58 @@ public class TheoreticalPracticeServiceImpl extends ServiceImpl<TheoreticalPract
} else { } else {
updateById(theoreticalPracticeDTO); updateById(theoreticalPracticeDTO);
} }
List<TheoreticalPracticeStudentVO> studentList = theoreticalPracticeDTO.getStudentList(); List<TheoreticalPracticeStudentVO> studentList = normalizePracticeStudents(
if (!ObjectUtils.isEmpty(studentList)) { theoreticalPracticeDTO.getStudentList(), theoreticalPracticeDTO.getId());
List<String> studentIds = studentList.stream()
.map(TheoreticalPracticeStudentVO::getStudentId)
.collect(Collectors.toList());
/* /*
* 题库初始化以“学员+本期等级”为维度。学员换班或升级后即使保留旧等级题库, * saveOrUpdate接口采用“整场重建”语义:先清空本场所有作答记录和实时统计,再删除
* 也必须生成新等级题目,不能再用“该学员是否有任意题目”作为跳过条件。 * 当前名单、重置本次学员当前等级的个人题库,最后重新生成题库与名单。外层事务
* 保证任何一步失败都会整体回滚,避免出现只有删除没有重建的中间状态。
*/ */
theoreticalPracticeStudentQuestionBankService.ensureStudentLibrary( theoreticalPracticeStudentService.remove(
theoreticalPracticeDTO.getLev(), new LambdaQueryWrapper<TheoreticalPracticeStudentEntity>()
studentList.stream() .eq(TheoreticalPracticeStudentEntity::getPracticeId,
.map(TheoreticalPracticeStudentVO::getStudentId) theoreticalPracticeDTO.getId()));
.collect(Collectors.toList())); theoreticalPracticeStudentQuestionBankService.resetStudentLibrary(
for (TheoreticalPracticeStudentVO theoreticalPracticeStudentVO : studentList) { theoreticalPracticeDTO.getId(), theoreticalPracticeDTO.getLev(), studentIds);
theoreticalPracticeStudentVO.setPracticeId(theoreticalPracticeDTO.getId()); if (!studentList.isEmpty()) {
} theoreticalPracticeStudentService.saveBatch(
theoreticalPracticeStudentService.saveOrUpdateBatch(BeanUtil.copyToList(studentList, TheoreticalPracticeStudentEntity.class)); BeanUtil.copyToList(studentList, TheoreticalPracticeStudentEntity.class));
} }
return Result.data(Boolean.TRUE); return Result.data(Boolean.TRUE);
} }
/**
* 将前端名单规范化为可直接重建的数据。
*
* <p>同一学员只保留第一条,空ID不进入名单;同时清空前端携带的关联记录ID,防止
* saveOrUpdate误更新其他练习的名单记录。重建后的每条记录均由数据库生成新主键。</p>
*/
private List<TheoreticalPracticeStudentVO> normalizePracticeStudents(
List<TheoreticalPracticeStudentVO> students, String practiceId) {
if (ObjectUtils.isEmpty(students)) {
return Collections.emptyList();
}
Map<String, TheoreticalPracticeStudentVO> uniqueStudents = new LinkedHashMap<>();
for (TheoreticalPracticeStudentVO student : students) {
if (student == null || StrUtil.isBlank(student.getStudentId())) {
continue;
}
String studentId = student.getStudentId().trim();
if (uniqueStudents.containsKey(studentId)) {
continue;
}
student.setId(null);
student.setStudentId(studentId);
student.setPracticeId(practiceId);
uniqueStudents.put(studentId, student);
}
return new java.util.ArrayList<>(uniqueStudents.values());
}
@Override @Override
public Boolean close(TheoreticalPracticeDTO theoreticalPracticeDTO) { public Boolean close(TheoreticalPracticeDTO theoreticalPracticeDTO) {
theoreticalPracticeDTO.setIsClose(true); theoreticalPracticeDTO.setIsClose(true);
......
...@@ -115,6 +115,52 @@ public class TheoreticalPracticeStudentQuestionBankServiceImpl extends ServiceIm ...@@ -115,6 +115,52 @@ public class TheoreticalPracticeStudentQuestionBankServiceImpl extends ServiceIm
saveBatch(result); saveBatch(result);
} }
@Override
@Transactional(rollbackFor = Exception.class)
public void resetStudentLibrary(String practiceId, String level, List<String> studentIds) {
String normalizedPracticeId = practiceId == null ? null : practiceId.trim();
String normalizedLevel = level == null ? null : level.trim();
if (StringUtils.isEmpty(normalizedPracticeId) || StringUtils.isEmpty(normalizedLevel)) {
throw new BusinessException("练习ID和等级不能为空");
}
/*
* 管理端统计完全由不可变作答记录实时聚合,因此删除本场练习的全部作答记录即可
* 同步清空总体、知识点和个人统计。这里按practiceId整场删除,也能覆盖本次名单中
* 已移除的学员,避免名单替换后数据库残留不可见的历史统计。
*/
answerRecordMapper.delete(new LambdaQueryWrapper<TheoreticalPracticeAnswerRecordEntity>()
.eq(TheoreticalPracticeAnswerRecordEntity::getPracticeId, normalizedPracticeId));
Set<String> normalizedStudentIds = normalizeStudentIds(studentIds);
if (normalizedStudentIds.isEmpty()) {
return;
}
/*
* 个人题库不是按练习存储,而是按“学员+等级”复用。只删除当前等级可以完成本次
* 重置,同时保留学员在其他等级的题库;TRIM用于兼容历史scope带首尾空格的数据。
*/
remove(new LambdaQueryWrapper<TheoreticalPracticeStudentQuestionBankEntity>()
.in(TheoreticalPracticeStudentQuestionBankEntity::getStudentId, normalizedStudentIds)
.apply("TRIM(scope) = {0}", normalizedLevel));
// 删除完成后沿用统一生成入口,确保章节、题目快照和普通初始化逻辑保持一致。
ensureStudentLibrary(normalizedLevel, new ArrayList<>(normalizedStudentIds));
}
/** 去除空学员ID和重复ID,确保删除与重新生成使用完全相同的名单。 */
private Set<String> normalizeStudentIds(List<String> studentIds) {
if (studentIds == null || studentIds.isEmpty()) {
return Collections.emptySet();
}
return studentIds.stream()
.filter(Objects::nonNull)
.map(String::trim)
.filter(id -> !id.isEmpty())
.collect(Collectors.toCollection(LinkedHashSet::new));
}
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public List<TheoreticalPracticeStudentQuestionBankVO> onePage(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO) { public List<TheoreticalPracticeStudentQuestionBankVO> onePage(TheoreticalPracticeStudentQuestionBankDTO theoreticalPracticeStudentQuestionBankDTO) {
......
...@@ -163,6 +163,36 @@ class TheoreticalPracticeAnswerServiceImplTest { ...@@ -163,6 +163,36 @@ class TheoreticalPracticeAnswerServiceImplTest {
assertEquals("S4-1", generated.get(0).getCode()); assertEquals("S4-1", generated.get(0).getCode());
} }
@Test
void resetShouldClearPracticeAnswersAndCurrentLevelLibraryBeforeRegeneration() {
when(questionMapper.selectList(any())).thenReturn(Collections.emptyList());
when(theoreticalLibraryService.groupByChapter("污水四级"))
.thenReturn(Collections.emptyList());
TheoreticalLibraryEntity source = new TheoreticalLibraryEntity();
source.setScope("污水四级");
source.setCode("S4-1");
when(theoreticalLibraryService.list(any())).thenReturn(Collections.singletonList(source));
// 拦截批量入库,重点验证清空完成后会重新构造一套无作答状态的个人题库。
TheoreticalPracticeStudentQuestionBankServiceImpl spyService = spy(service);
doReturn(true).when(spyService).saveBatch(anyCollection());
spyService.resetStudentLibrary("p1", "污水四级",
java.util.Arrays.asList(" s1 ", "s1", null));
verify(answerRecordMapper).delete(any());
verify(questionMapper).delete(any());
@SuppressWarnings("unchecked")
ArgumentCaptor<Collection<TheoreticalPracticeStudentQuestionBankEntity>> captor =
ArgumentCaptor.forClass(Collection.class);
verify(spyService).saveBatch(captor.capture());
List<TheoreticalPracticeStudentQuestionBankEntity> regenerated =
(List<TheoreticalPracticeStudentQuestionBankEntity>) captor.getValue();
assertEquals(1, regenerated.size());
assertEquals("s1", regenerated.get(0).getStudentId());
assertEquals("污水四级", regenerated.get(0).getScope());
}
@Test @Test
void studentQuestionPageShouldUseServerResolvedActiveScope() { void studentQuestionPageShouldUseServerResolvedActiveScope() {
TheoreticalPracticeStudentQuestionBankDTO query = TheoreticalPracticeStudentQuestionBankDTO query =
......
...@@ -4,6 +4,7 @@ import com.platform.business.mapper.TheoreticalPracticeMapper; ...@@ -4,6 +4,7 @@ import com.platform.business.mapper.TheoreticalPracticeMapper;
import com.platform.business.model.dto.TheoreticalPracticeDTO; import com.platform.business.model.dto.TheoreticalPracticeDTO;
import com.platform.business.model.dto.TheoreticalPracticeStatisticsQueryDTO; import com.platform.business.model.dto.TheoreticalPracticeStatisticsQueryDTO;
import com.platform.business.model.entity.TheoreticalPracticeEntity; import com.platform.business.model.entity.TheoreticalPracticeEntity;
import com.platform.business.model.entity.TheoreticalPracticeStudentEntity;
import com.platform.business.model.vo.TheoreticalPracticeKnowledgeStatisticsVO; import com.platform.business.model.vo.TheoreticalPracticeKnowledgeStatisticsVO;
import com.platform.business.model.vo.TheoreticalPracticeLevelOptionVO; import com.platform.business.model.vo.TheoreticalPracticeLevelOptionVO;
import com.platform.business.model.vo.TheoreticalPracticeStatisticsOverviewVO; import com.platform.business.model.vo.TheoreticalPracticeStatisticsOverviewVO;
...@@ -21,6 +22,7 @@ import org.junit.jupiter.api.Test; ...@@ -21,6 +22,7 @@ import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.InOrder;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.util.ReflectionTestUtils;
...@@ -28,15 +30,19 @@ import org.springframework.test.util.ReflectionTestUtils; ...@@ -28,15 +30,19 @@ import org.springframework.test.util.ReflectionTestUtils;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
/** 理论练习筛选规范化、统计口径和Word报告测试。 */ /** 理论练习筛选规范化、统计口径和Word报告测试。 */
...@@ -139,22 +145,51 @@ class TheoreticalPracticeServiceImplTest { ...@@ -139,22 +145,51 @@ class TheoreticalPracticeServiceImplTest {
} }
@Test @Test
void savingPracticeShouldInitializeStudentsForThisPracticeLevel() { void savingPracticeShouldResetDataAndRebuildNormalizedStudentList() {
TheoreticalPracticeDTO request = new TheoreticalPracticeDTO(); TheoreticalPracticeDTO request = new TheoreticalPracticeDTO();
request.setId("p1"); request.setId("p1");
request.setLev(" 污水四级 "); request.setLev(" 污水四级 ");
TheoreticalPracticeStudentVO student = new TheoreticalPracticeStudentVO(); TheoreticalPracticeStudentVO student = new TheoreticalPracticeStudentVO();
student.setStudentId("s1"); student.setId("旧名单记录ID");
request.setStudentList(Collections.singletonList(student)); student.setStudentId(" s1 ");
TheoreticalPracticeStudentVO duplicate = new TheoreticalPracticeStudentVO();
duplicate.setStudentId("s1");
request.setStudentList(Arrays.asList(student, duplicate));
when(mapper.updateById(request)).thenReturn(1); when(mapper.updateById(request)).thenReturn(1);
service.saveOrUpdateData(request); service.saveOrUpdateData(request);
// 等级先规范化再传给题库服务,避免首尾空格造成scope无法匹配。 // 等级和学员ID必须先规范化,重置服务才能精确删除当前等级的数据。
assertEquals("污水四级", request.getLev()); assertEquals("污水四级", request.getLev());
verify(questionBankService).ensureStudentLibrary( InOrder rebuildOrder = inOrder(practiceStudentService, questionBankService);
"污水四级", Collections.singletonList("s1")); rebuildOrder.verify(practiceStudentService).remove(any());
rebuildOrder.verify(questionBankService).resetStudentLibrary(
"p1", "污水四级", Collections.singletonList("s1"));
@SuppressWarnings("unchecked")
ArgumentCaptor<Collection<TheoreticalPracticeStudentEntity>> studentsCaptor =
ArgumentCaptor.forClass(Collection.class);
rebuildOrder.verify(practiceStudentService).saveBatch(studentsCaptor.capture());
assertEquals(1, studentsCaptor.getValue().size());
assertEquals("p1", student.getPracticeId()); assertEquals("p1", student.getPracticeId());
assertEquals("s1", student.getStudentId());
assertNull(student.getId());
}
@Test
void savingPracticeWithEmptyListShouldClearOldRosterAndStatistics() {
TheoreticalPracticeDTO request = new TheoreticalPracticeDTO();
request.setId("p1");
request.setLev("污水四级");
request.setStudentList(Collections.emptyList());
when(mapper.updateById(request)).thenReturn(1);
service.saveOrUpdateData(request);
// 空名单仍执行整场清理,但不会生成题库或写入新的名单记录。
verify(practiceStudentService).remove(any());
verify(questionBankService).resetStudentLibrary(
"p1", "污水四级", Collections.emptyList());
verify(practiceStudentService, never()).saveBatch(any());
} }
@Test @Test
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment