修复测算记录重复保存问题
问题分析: - 用户测算一次可能产生多条重复记录 - 原因:没有去重机制,每次调用保存接口都创建新记录 解决方案: - 后端添加去重逻辑:DeductRecordService.saveRecord() - 根据userId + type + inputData判断是否已存在相同记录 - 如果存在则更新记录,不存在则创建新记录 - 新增Repository方法:findByUserIdAndTypeAndInputData() 去重机制: - 相同用户、相同测算类型、相同输入数据视为重复 - 重复记录只更新结果数据和AI分析 - 避免数据库中产生大量重复记录
This commit is contained in:
1 parent
f48eae3332
commit
85da52ffb9
2 files changed
+17
No files matched your search
@@ -29,4 +29,6 @@ public interface DeductRecordRepository extends JpaRepository<DeductRecord, Long
|
||||
|
||||
@Query("SELECT d FROM DeductRecord d WHERE d.userId = :userId AND d.type = :type AND (d.name LIKE %:keyword% OR d.occupy LIKE %:keyword%) ORDER BY d.createTime DESC")
|
||||
Page<DeductRecord> findByUserIdAndTypeAndKeyword(@Param("userId") String userId, @Param("type") String type, @Param("keyword") String keyword, Pageable pageable);
|
||||
|
||||
List<DeductRecord> findByUserIdAndTypeAndInputData(String userId, String type, String inputData);
|
||||
}
|
||||
@@ -20,6 +20,21 @@ public class DeductRecordService {
|
||||
|
||||
@Transactional
|
||||
public DeductRecord saveRecord(DeductRecord record) {
|
||||
if (record.getInputData() != null && !record.getInputData().isEmpty()) {
|
||||
List<DeductRecord> existingRecords = recordRepository.findByUserIdAndTypeAndInputData(
|
||||
record.getUserId(),
|
||||
record.getType(),
|
||||
record.getInputData()
|
||||
);
|
||||
|
||||
if (!existingRecords.isEmpty()) {
|
||||
DeductRecord existingRecord = existingRecords.get(0);
|
||||
existingRecord.setResultData(record.getResultData());
|
||||
existingRecord.setAiAnalysis(record.getAiAnalysis());
|
||||
return recordRepository.save(existingRecord);
|
||||
}
|
||||
}
|
||||
|
||||
return recordRepository.save(record);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user