溯源码分组查询去重
This commit is contained in:
@@ -8,8 +8,7 @@ import cn.iocoder.yudao.module.farm.dal.dataobject.traceabilitycode.Traceability
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
@@ -108,8 +107,34 @@ public interface TraceabilityCodeMapper extends BaseMapperX<TraceabilityCodeDO>
|
||||
return new PageResult<>(Collections.emptyList(), 0L);
|
||||
}
|
||||
|
||||
// 按CodeOutBatch去重,确保每个批次只保留一条记录(保留最新的)
|
||||
Map<String, TraceabilityCodeDO> uniqueBatchMap = latestRecords.stream()
|
||||
.filter(record -> record.getCodeOutBatch() != null) // 过滤掉批次为空的记录
|
||||
.collect(Collectors.toMap(
|
||||
TraceabilityCodeDO::getCodeOutBatch,
|
||||
record -> record,
|
||||
(existing, replacement) -> {
|
||||
// 如果存在重复批次,保留创建时间更新的记录
|
||||
if (replacement.getCreateTime() != null && existing.getCreateTime() != null) {
|
||||
return replacement.getCreateTime().isAfter(existing.getCreateTime()) ? replacement : existing;
|
||||
}
|
||||
return existing.getCreateTime() != null ? existing : replacement;
|
||||
}
|
||||
));
|
||||
|
||||
// 转换为去重后的列表
|
||||
List<TraceabilityCodeDO> uniqueRecords = new ArrayList<>(uniqueBatchMap.values());
|
||||
|
||||
// 按创建时间倒序排序
|
||||
uniqueRecords.sort((a, b) -> {
|
||||
if (a.getCreateTime() == null && b.getCreateTime() == null) return 0;
|
||||
if (a.getCreateTime() == null) return 1;
|
||||
if (b.getCreateTime() == null) return -1;
|
||||
return b.getCreateTime().compareTo(a.getCreateTime());
|
||||
});
|
||||
|
||||
// 提取ID列表
|
||||
List<String> ids = latestRecords.stream()
|
||||
List<String> ids = uniqueRecords.stream()
|
||||
.map(TraceabilityCodeDO::getId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user