You've already forked FrameTour-BE
refactor(task): 重构任务参数处理逻辑
- 新增 filterTaskParams 方法,用于过滤模板所需的源数据 - 新增 getTaskParams 方法,用于获取任务参数并进行预处理 - 优化了视频源和图片源的处理逻辑,提高了代码可读性和可维护性 - 重构了任务回调中的源数据处理流程,使用新方法替代原有逻辑
This commit is contained in:
@@ -15,7 +15,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -121,4 +123,39 @@ public class TemplateBiz {
|
||||
return count >= minimalPlaceholderFill;
|
||||
}
|
||||
|
||||
public Map<String, List<SourceEntity>> filterTaskParams(Long templateId, Map<String, List<SourceEntity>> allTaskParams) {
|
||||
if (allTaskParams == null || allTaskParams.isEmpty()) {
|
||||
return Map.of();
|
||||
}
|
||||
|
||||
List<String> templatePlaceholders = templateRepository.getTemplatePlaceholder(templateId);
|
||||
if (templatePlaceholders == null || templatePlaceholders.isEmpty()) {
|
||||
log.info("filterTaskParams: templateId:{} has no placeholders", templateId);
|
||||
return Map.of();
|
||||
}
|
||||
|
||||
Map<String, List<SourceEntity>> filteredParams = new HashMap<>();
|
||||
|
||||
for (String placeholder : templatePlaceholders) {
|
||||
if (placeholder.startsWith("P")) {
|
||||
// 图片源:占位符格式为 "P{deviceId}"
|
||||
String imageKey = placeholder;
|
||||
if (allTaskParams.containsKey(imageKey)) {
|
||||
filteredParams.put(imageKey, allTaskParams.get(imageKey));
|
||||
}
|
||||
} else {
|
||||
// 视频源:占位符直接对应设备ID
|
||||
String videoKey = placeholder;
|
||||
if (allTaskParams.containsKey(videoKey)) {
|
||||
filteredParams.put(videoKey, allTaskParams.get(videoKey));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.info("filterTaskParams: templateId:{}, original keys:{}, filtered keys:{}",
|
||||
templateId, allTaskParams.keySet().size(), filteredParams.keySet().size());
|
||||
|
||||
return filteredParams;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -4,6 +4,7 @@ import com.ycwl.basic.mapper.SourceMapper;
|
||||
import com.ycwl.basic.model.pc.face.entity.FaceEntity;
|
||||
import com.ycwl.basic.model.pc.source.entity.MemberSourceEntity;
|
||||
import com.ycwl.basic.model.pc.source.entity.SourceEntity;
|
||||
import com.ycwl.basic.model.pc.device.entity.DeviceEntity;
|
||||
import com.ycwl.basic.pricing.dto.VoucherInfo;
|
||||
import com.ycwl.basic.pricing.enums.VoucherDiscountType;
|
||||
import com.ycwl.basic.pricing.service.IVoucherService;
|
||||
@@ -13,7 +14,9 @@ import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -26,6 +29,10 @@ public class SourceRepository {
|
||||
private IVoucherService iVoucherService;
|
||||
@Autowired
|
||||
private FaceRepository faceRepository;
|
||||
@Autowired
|
||||
private TemplateRepository templateRepository;
|
||||
@Autowired
|
||||
private DeviceRepository deviceRepository;
|
||||
|
||||
public void addSource(SourceEntity source) {
|
||||
sourceMapper.add(source);
|
||||
@@ -86,4 +93,65 @@ public class SourceRepository {
|
||||
public SourceEntity getSource(Long id) {
|
||||
return sourceMapper.getEntity(id);
|
||||
}
|
||||
|
||||
public Map<String, List<SourceEntity>> getTaskParams(Long faceId, Long templateId) {
|
||||
FaceEntity face = faceRepository.getFace(faceId);
|
||||
if (face == null) {
|
||||
log.info("getTaskParams: faceId:{} is not exist", faceId);
|
||||
return Map.of();
|
||||
}
|
||||
|
||||
List<SourceEntity> videoSourceList = sourceMapper.listVideoByScenicFaceRelation(face.getScenicId(), faceId);
|
||||
Map<String, List<SourceEntity>> sourcesMap = videoSourceList.stream()
|
||||
.peek(item -> item.setUrl(item.getVideoUrl()))
|
||||
.filter(item -> item.getDeviceId() != null)
|
||||
.filter(item -> {
|
||||
DeviceEntity device = deviceRepository.getDevice(item.getDeviceId());
|
||||
if (device == null) {
|
||||
log.info("getTaskParams: deviceId:{} is not exist", item.getDeviceId());
|
||||
return false;
|
||||
}
|
||||
return Integer.valueOf(1).equals(device.getStatus());
|
||||
})
|
||||
.collect(Collectors.groupingBy(item -> item.getDeviceId().toString()));
|
||||
|
||||
if (sourcesMap.isEmpty()) {
|
||||
log.info("getTaskParams: 没有视频源,templateId: {}", templateId);
|
||||
return Map.of();
|
||||
}
|
||||
|
||||
List<String> templatePlaceholder = templateRepository.getTemplatePlaceholder(templateId);
|
||||
if (templatePlaceholder.stream().distinct().count() == templatePlaceholder.size()) {
|
||||
sourcesMap.forEach((key, value) -> {
|
||||
value.removeIf(item -> !value.getFirst().equals(item));
|
||||
});
|
||||
}
|
||||
|
||||
boolean hasPPlaceholder = templatePlaceholder.stream().anyMatch(placeholder -> placeholder.startsWith("P"));
|
||||
if (hasPPlaceholder) {
|
||||
List<SourceEntity> imageSourceList = sourceMapper.listImageSourcesByFaceId(faceId);
|
||||
if (!imageSourceList.isEmpty()) {
|
||||
Map<String, List<SourceEntity>> imageSourceMap = imageSourceList.stream()
|
||||
.peek(item -> item.setUrl(item.getUrl()))
|
||||
.filter(item -> item.getDeviceId() != null)
|
||||
.filter(item -> {
|
||||
DeviceEntity device = deviceRepository.getDevice(item.getDeviceId());
|
||||
if (device == null) {
|
||||
return false;
|
||||
}
|
||||
return Integer.valueOf(1).equals(device.getStatus());
|
||||
})
|
||||
.collect(Collectors.groupingBy(item -> "P" + item.getDeviceId().toString()));
|
||||
|
||||
imageSourceMap.forEach((key, value) -> {
|
||||
sourcesMap.merge(key, value, (existing, replacement) -> {
|
||||
existing.addAll(replacement);
|
||||
return existing;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return sourcesMap;
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,7 @@ import cn.hutool.crypto.digest.MD5;
|
||||
import com.ycwl.basic.integration.common.manager.DeviceConfigManager;
|
||||
import com.ycwl.basic.integration.common.manager.RenderWorkerConfigManager;
|
||||
import com.ycwl.basic.integration.common.manager.ScenicConfigManager;
|
||||
import com.ycwl.basic.repository.SourceRepository;
|
||||
import com.ycwl.basic.utils.JacksonUtil;
|
||||
import com.ycwl.basic.biz.OrderBiz;
|
||||
import com.ycwl.basic.biz.TaskStatusBiz;
|
||||
@@ -126,6 +127,8 @@ public class TaskTaskServiceImpl implements TaskService {
|
||||
private VideoReUploader videoReUploader;
|
||||
@Autowired
|
||||
private RedisTemplate<String, String> redisTemplate;
|
||||
@Autowired
|
||||
private SourceRepository sourceRepository;
|
||||
|
||||
private RenderWorkerEntity getWorker(@NonNull WorkerAuthReqVo req) {
|
||||
String accessKey = req.getAccessKey();
|
||||
@@ -348,58 +351,15 @@ public class TaskTaskServiceImpl implements TaskService {
|
||||
}
|
||||
}
|
||||
|
||||
List<SourceEntity> videoSourceList = sourceMapper.listVideoByScenicFaceRelation(face.getScenicId(), faceId);
|
||||
Map<String, List<SourceEntity>> sourcesMap = videoSourceList.stream()
|
||||
.peek(item -> item.setUrl(item.getVideoUrl()))
|
||||
.filter(item -> item.getDeviceId() != null) // 添加对 deviceId 为 null 的检查
|
||||
.filter(item -> {
|
||||
DeviceEntity device = deviceRepository.getDevice(item.getDeviceId());
|
||||
if (device == null) {
|
||||
log.info("task callback: deviceId:{} is not exist", item.getDeviceId());
|
||||
return false;
|
||||
}
|
||||
return Integer.valueOf(1).equals(device.getStatus());
|
||||
})
|
||||
.collect(Collectors.groupingBy(item -> item.getDeviceId().toString()));
|
||||
if (sourcesMap.isEmpty()) {
|
||||
// 主动禁止没有视频源视频生成
|
||||
log.info("task callback: 没有视频源,templateId: {}", templateId);
|
||||
Map<String, List<SourceEntity>> allTaskParams = sourceRepository.getTaskParams(faceId, templateId);
|
||||
if (allTaskParams.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
List<String> templatePlaceholder = templateRepository.getTemplatePlaceholder(templateId);
|
||||
if (templatePlaceholder.stream().distinct().count() == templatePlaceholder.size()) {
|
||||
sourcesMap.forEach((key, value) -> {
|
||||
// 每个value只保留第一个
|
||||
value.removeIf(item -> !value.getFirst().equals(item));
|
||||
});
|
||||
}
|
||||
|
||||
// 处理以P开头的templatePlaceHolder,添加type=2的source
|
||||
boolean hasPPlaceholder = templatePlaceholder.stream().anyMatch(placeholder -> placeholder.startsWith("P"));
|
||||
if (hasPPlaceholder) {
|
||||
List<SourceEntity> imageSourceList = sourceMapper.listImageSourcesByFaceId(faceId);
|
||||
if (!imageSourceList.isEmpty()) {
|
||||
// 将图片source按设备ID分组并添加到sourcesMap中
|
||||
Map<String, List<SourceEntity>> imageSourceMap = imageSourceList.stream()
|
||||
.peek(item -> item.setUrl(item.getUrl())) // 图片使用url字段
|
||||
.filter(item -> item.getDeviceId() != null)
|
||||
.filter(item -> {
|
||||
DeviceEntity device = deviceRepository.getDevice(item.getDeviceId());
|
||||
if (device == null) {
|
||||
return false;
|
||||
}
|
||||
return Integer.valueOf(1).equals(device.getStatus());
|
||||
})
|
||||
.collect(Collectors.groupingBy(item -> Strings.concat("P", item.getDeviceId().toString())));
|
||||
|
||||
// 合并到现有的sourcesMap中
|
||||
imageSourceMap.forEach((key, value) -> {
|
||||
sourcesMap.merge(key, value, (existing, replacement) -> {
|
||||
existing.addAll(replacement);
|
||||
return existing;
|
||||
});
|
||||
});
|
||||
}
|
||||
Map<String, List<SourceEntity>> sourcesMap = templateBiz.filterTaskParams(templateId, allTaskParams);
|
||||
if (sourcesMap.isEmpty()) {
|
||||
log.info("task callback: 筛选后无有效源数据,templateId: {}", templateId);
|
||||
return;
|
||||
}
|
||||
TaskReqQuery taskReqQuery = new TaskReqQuery();
|
||||
taskReqQuery.setFaceId(faceId);
|
||||
|
Reference in New Issue
Block a user