fix(emr): 修复全表删除错误

- 使用JdbcTemplate执行TRUNCATE替代MyBatis-Plus的remove
- 添加备用方案:查询所有ID后批量删除
This commit is contained in:
2026-06-21 14:14:08 +08:00
parent fde29104ab
commit dc352ace4a

View File

@@ -12,6 +12,7 @@ import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@@ -29,6 +30,7 @@ public class EmrSyncController {
private final IEmrService emrService;
private final IEmrRevisionService emrRevisionService;
private final IEmrSearchIndexService emrSearchIndexService;
private final JdbcTemplate jdbcTemplate;
/**
* 同步EMR数据
@@ -39,10 +41,25 @@ public class EmrSyncController {
public R<?> syncEmrData() {
log.info("开始同步EMR数据...");
// 1. 清空假数据
emrRevisionService.remove(new LambdaQueryWrapper<>());
emrSearchIndexService.remove(new LambdaQueryWrapper<>());
log.info("已清空emr_revision和emr_search_index");
// 1. 清空假数据使用原生SQL避免全表删除限制
try {
jdbcTemplate.execute("TRUNCATE TABLE emr_revision CASCADE");
jdbcTemplate.execute("TRUNCATE TABLE emr_search_index CASCADE");
log.info("已清空emr_revision和emr_search_index表");
} catch (Exception e) {
log.warn("TRUNCATE失败尝试使用DELETE: {}", e.getMessage());
// 备用方案查询所有ID后删除
List<Long> revisionIds = emrRevisionService.list(new LambdaQueryWrapper<EmrRevision>().select(EmrRevision::getId))
.stream().map(EmrRevision::getId).toList();
if (!revisionIds.isEmpty()) {
emrRevisionService.removeByIds(revisionIds);
}
List<Long> searchIndexIds = emrSearchIndexService.list(new LambdaQueryWrapper<EmrSearchIndex>().select(EmrSearchIndex::getId))
.stream().map(EmrSearchIndex::getId).toList();
if (!searchIndexIds.isEmpty()) {
emrSearchIndexService.removeByIds(searchIndexIds);
}
}
// 2. 从doc_emr获取所有病历
List<Emr> emrList = emrService.list(new LambdaQueryWrapper<Emr>()