允许重复
All checks were successful
ZhenTu-BE/pipeline/head This commit looks good

This commit is contained in:
2025-09-24 05:03:47 +08:00
parent dc3a46362b
commit becbe5f6ab

View File

@@ -132,6 +132,7 @@ public class TemplateBiz {
log.info("filterTaskParams: templateId:{} has no placeholders", templateId); log.info("filterTaskParams: templateId:{} has no placeholders", templateId);
return Map.of(); return Map.of();
} }
TemplateConfigEntity templateConfig = templateRepository.getTemplateConfig(templateId);
// 统计每个 placeholder 在模板中出现的次数 // 统计每个 placeholder 在模板中出现的次数
Map<String, Long> placeholderCounts = templatePlaceholders.stream() Map<String, Long> placeholderCounts = templatePlaceholders.stream()
@@ -142,6 +143,9 @@ public class TemplateBiz {
Map<String, List<SourceEntity>> filteredParams = new HashMap<>(); Map<String, List<SourceEntity>> filteredParams = new HashMap<>();
// 判断是否允许片段重复
boolean allowDuplicate = templateConfig != null && Integer.valueOf(1).equals(templateConfig.getDuplicateEnable());
for (Map.Entry<String, Long> entry : placeholderCounts.entrySet()) { for (Map.Entry<String, Long> entry : placeholderCounts.entrySet()) {
String placeholder = entry.getKey(); String placeholder = entry.getKey();
Long requiredCount = entry.getValue(); Long requiredCount = entry.getValue();
@@ -151,28 +155,53 @@ public class TemplateBiz {
String imageKey = placeholder; String imageKey = placeholder;
if (allTaskParams.containsKey(imageKey)) { if (allTaskParams.containsKey(imageKey)) {
List<SourceEntity> allSources = allTaskParams.get(imageKey); List<SourceEntity> allSources = allTaskParams.get(imageKey);
int actualCount = Math.min(requiredCount.intValue(), allSources.size()); List<SourceEntity> selectedSources = selectSources(allSources, requiredCount.intValue(), allowDuplicate);
List<SourceEntity> selectedSources = allSources.subList(0, actualCount); if (!selectedSources.isEmpty()) {
filteredParams.put(imageKey, new ArrayList<>(selectedSources)); filteredParams.put(imageKey, selectedSources);
}
} }
} else { } else {
// 视频源:占位符直接对应设备ID // 视频源:占位符直接对应设备ID
String videoKey = placeholder; String videoKey = placeholder;
if (allTaskParams.containsKey(videoKey)) { if (allTaskParams.containsKey(videoKey)) {
List<SourceEntity> allSources = allTaskParams.get(videoKey); List<SourceEntity> allSources = allTaskParams.get(videoKey);
int actualCount = Math.min(requiredCount.intValue(), allSources.size()); List<SourceEntity> selectedSources = selectSources(allSources, requiredCount.intValue(), allowDuplicate);
List<SourceEntity> selectedSources = allSources.subList(0, actualCount); if (!selectedSources.isEmpty()) {
filteredParams.put(videoKey, new ArrayList<>(selectedSources)); filteredParams.put(videoKey, selectedSources);
}
} }
} }
} }
log.debug("filterTaskParams: templateId:{}, original keys:{}, filtered keys:{}, placeholder counts:{}", log.debug("filterTaskParams: templateId:{}, original keys:{}, filtered keys:{}, placeholder counts:{}, allowDuplicate:{}",
templateId, allTaskParams.keySet().size(), filteredParams.keySet().size(), placeholderCounts); templateId, allTaskParams.keySet().size(), filteredParams.keySet().size(), placeholderCounts, allowDuplicate);
return filteredParams; return filteredParams;
} }
private List<SourceEntity> selectSources(List<SourceEntity> allSources, int requiredCount, boolean allowDuplicate) {
if (allSources == null || allSources.isEmpty()) {
return new ArrayList<>();
}
if (!allowDuplicate) {
// 不允许重复,使用原有逻辑
int actualCount = Math.min(requiredCount, allSources.size());
return new ArrayList<>(allSources.subList(0, actualCount));
}
// 允许重复,循环填充到所需数量
List<SourceEntity> selectedSources = new ArrayList<>();
int sourceIndex = 0;
for (int i = 0; i < requiredCount; i++) {
selectedSources.add(allSources.get(sourceIndex));
sourceIndex = (sourceIndex + 1) % allSources.size();
}
return selectedSources;
}
public Long findFirstAvailableTemplate(List<Long> templateIds, Long faceId, boolean scanSource) { public Long findFirstAvailableTemplate(List<Long> templateIds, Long faceId, boolean scanSource) {
if (templateIds == null || templateIds.isEmpty() || faceId == null) { if (templateIds == null || templateIds.isEmpty() || faceId == null) {
return null; return null;