feat(视频更新): 添加视频片段更新检查功能
All checks were successful
ZhenTu-BE/pipeline/head This commit looks good

- 新增TaskUpdateResult类存储任务更新检查结果
- 在VideoTaskRepository中实现checkTaskUpdate方法检查任务更新状态
- 重构GoodsServiceImpl中的视频更新检查逻辑,使用VideoTaskRepository的统一实现
- 在ContentPageVO中添加newSegmentCount字段显示新增片段数
This commit is contained in:
2025-09-18 15:05:25 +08:00
parent fde4deb370
commit 079c5dc540
5 changed files with 180 additions and 120 deletions

View File

@@ -27,8 +27,8 @@ public class ContentPageVO {
private Long contentId;
// 模版id
private Long templateId;
// 模版封面图片 contentType为0或1时才有值
private String templateCoverUrl;
private Integer newSegmentCount;
// 是否购买:0未购买,1已购买
private Integer isBuy;
private BigDecimal duration;

View File

@@ -0,0 +1,30 @@
package com.ycwl.basic.model.repository;
import lombok.Data;
/**
* 任务更新检查结果
* @author Claude
*/
@Data
public class TaskUpdateResult {
/**
* 是否可以更新
*/
private boolean canUpdate;
/**
* 新增片段数量
*/
private int newSegmentCount;
/**
* 当前总片段数量
*/
private int totalSegmentCount;
/**
* 原始片段数量
*/
private int originalSegmentCount;
}

View File

@@ -1,23 +1,25 @@
package com.ycwl.basic.repository;
import cn.hutool.core.date.DateUtil;
import com.ycwl.basic.utils.JacksonUtil;
import com.ycwl.basic.biz.TemplateBiz;
import com.ycwl.basic.config.VideoUpdateConfig;
import com.ycwl.basic.mapper.TaskMapper;
import com.ycwl.basic.model.pc.source.entity.SourceEntity;
import com.ycwl.basic.model.pc.task.entity.TaskEntity;
import com.ycwl.basic.model.pc.task.resp.TaskRespVO;
import com.ycwl.basic.model.repository.TaskUpdateResult;
import com.ycwl.basic.utils.JacksonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@Slf4j
@Component
public class VideoTaskRepository {
@Autowired
@@ -28,6 +30,12 @@ public class VideoTaskRepository {
public static final String TASK_CACHE_KEY = "task:byId:%s";
@Autowired
private TemplateRepository templateRepository;
@Autowired
private SourceRepository sourceRepository;
@Autowired
private TemplateBiz templateBiz;
@Autowired
private VideoUpdateConfig videoUpdateConfig;
public TaskEntity getTaskById(Long taskId) {
if (redisTemplate.hasKey(String.format(TASK_CACHE_KEY, taskId))) {
@@ -122,4 +130,122 @@ public class VideoTaskRepository {
});
return deviceCount.get();
}
/**
* 检查任务是否可以更新
* @param taskId 任务ID
* @return 任务更新检查结果
*/
public TaskUpdateResult checkTaskUpdate(Long taskId) {
TaskUpdateResult result = new TaskUpdateResult();
TaskEntity task = getTaskById(taskId);
if (task == null) {
log.error("任务不存在: taskId={}", taskId);
result.setCanUpdate(false);
return result;
}
try {
Map<String, Object> originalTaskParams = JacksonUtil.parseObject(task.getTaskParams(), Map.class);
if (originalTaskParams == null) {
log.error("原始任务参数解析失败: taskId={}", taskId);
result.setCanUpdate(false);
return result;
}
int originalSegmentCount = calculateSegmentCount(originalTaskParams);
result.setOriginalSegmentCount(originalSegmentCount);
Map<String, List<SourceEntity>> currentTaskParams = sourceRepository.getTaskParams(task.getFaceId(), task.getTemplateId());
if (currentTaskParams.isEmpty()) {
log.info("当前没有可用的任务参数: faceId={}, templateId={}", task.getFaceId(), task.getTemplateId());
result.setCanUpdate(false);
result.setTotalSegmentCount(0);
result.setNewSegmentCount(0);
return result;
}
Map<String, List<SourceEntity>> filteredTaskParams = templateBiz.filterTaskParams(task.getTemplateId(), currentTaskParams);
int currentSegmentCount = calculateSegmentCount(filteredTaskParams);
result.setTotalSegmentCount(currentSegmentCount);
boolean hasNewSegments = videoUpdateConfig.isDetectChangesAsNew()
? hasNewSegments(originalTaskParams, filteredTaskParams)
: currentSegmentCount > originalSegmentCount;
int newSegmentCount = Math.max(0, currentSegmentCount - originalSegmentCount);
boolean canUpdate = hasNewSegments && newSegmentCount >= videoUpdateConfig.getMinNewSegmentCount();
result.setCanUpdate(canUpdate);
result.setNewSegmentCount(newSegmentCount);
log.info("任务更新检查完成: taskId={}, canUpdate={}, newSegmentCount={}, originalCount={}, currentCount={}",
taskId, result.isCanUpdate(), newSegmentCount, originalSegmentCount, currentSegmentCount);
} catch (Exception e) {
log.error("检查任务更新失败: taskId={}", taskId, e);
result.setCanUpdate(false);
}
return result;
}
/**
* 计算片段数量
*/
private int calculateSegmentCount(Map<String, ?> taskParams) {
if (taskParams == null || taskParams.isEmpty()) {
return 0;
}
return taskParams.entrySet().stream()
.filter(entry -> StringUtils.isNumeric(entry.getKey()))
.mapToInt(entry -> {
Object value = entry.getValue();
if (value instanceof List) {
return ((List<?>) value).size();
}
return 0;
})
.sum();
}
/**
* 检查是否有新片段
*/
private boolean hasNewSegments(Map<String, ?> originalParams, Map<String, List<SourceEntity>> currentParams) {
Set<String> originalFileSet = extractFileSet(originalParams);
Set<String> currentFileSet = extractFileSetFromEntities(currentParams);
return !currentFileSet.equals(originalFileSet);
}
private Set<String> extractFileSet(Map<String, ?> params) {
Set<String> fileSet = new HashSet<>();
params.entrySet().stream()
.filter(entry -> StringUtils.isNumeric(entry.getKey()))
.forEach(entry -> {
Object value = entry.getValue();
if (value instanceof List) {
((List<?>) value).forEach(item -> {
if (item instanceof Map) {
Map<?, ?> itemMap = (Map<?, ?>) item;
Object fileName = itemMap.get("fileName");
if (fileName != null) {
fileSet.add(fileName.toString());
}
}
});
}
});
return fileSet;
}
private Set<String> extractFileSetFromEntities(Map<String, List<SourceEntity>> params) {
Set<String> fileSet = new HashSet<>();
params.values().forEach(entities ->
entities.forEach(entity -> fileSet.add(entity.getVideoUrl()))
);
return fileSet;
}
}

View File

@@ -45,6 +45,7 @@ import com.ycwl.basic.repository.TemplateRepository;
import com.ycwl.basic.repository.SourceRepository;
import com.ycwl.basic.biz.TemplateBiz;
import com.ycwl.basic.config.VideoUpdateConfig;
import com.ycwl.basic.model.repository.TaskUpdateResult;
import com.ycwl.basic.service.task.TaskService;
import com.ycwl.basic.storage.StorageFactory;
import com.ycwl.basic.storage.adapters.IStorageAdapter;
@@ -792,20 +793,20 @@ public class GoodsServiceImpl implements GoodsService {
public VideoUpdateCheckVO checkVideoUpdate(Long videoId) {
VideoUpdateCheckVO result = new VideoUpdateCheckVO();
result.setVideoId(videoId);
if (!videoUpdateConfig.isEnabled()) {
log.info("视频更新检查功能已禁用");
result.setCanUpdate(false);
return result;
}
VideoEntity video = videoRepository.getVideo(videoId);
if (video == null) {
log.error("视频不存在: videoId={}", videoId);
result.setCanUpdate(false);
return result;
}
Long taskId = video.getTaskId();
TaskEntity task = videoTaskRepository.getTaskById(taskId);
if (task == null) {
@@ -813,121 +814,21 @@ public class GoodsServiceImpl implements GoodsService {
result.setCanUpdate(false);
return result;
}
result.setTaskId(taskId);
result.setFaceId(task.getFaceId());
result.setTemplateId(task.getTemplateId());
try {
Map<String, Object> originalTaskParams = JacksonUtil.parseObject(task.getTaskParams(), Map.class);
if (originalTaskParams == null) {
log.error("原始任务参数解析失败: taskId={}", taskId);
result.setCanUpdate(false);
return result;
}
int originalSegmentCount = calculateSegmentCount(originalTaskParams);
result.setOriginalSegmentCount(originalSegmentCount);
Map<String, List<SourceEntity>> currentTaskParams = sourceRepository.getTaskParams(task.getFaceId(), task.getTemplateId());
if (currentTaskParams.isEmpty()) {
log.info("当前没有可用的任务参数: faceId={}, templateId={}", task.getFaceId(), task.getTemplateId());
result.setCanUpdate(false);
result.setTotalSegmentCount(0);
result.setNewSegmentCount(0);
return result;
}
Map<String, List<SourceEntity>> filteredTaskParams = templateBiz.filterTaskParams(task.getTemplateId(), currentTaskParams);
int currentSegmentCount = calculateSegmentCount(filteredTaskParams);
result.setTotalSegmentCount(currentSegmentCount);
boolean hasNewSegments = videoUpdateConfig.isDetectChangesAsNew()
? hasNewSegments(originalTaskParams, filteredTaskParams)
: currentSegmentCount > originalSegmentCount;
int newSegmentCount = Math.max(0, currentSegmentCount - originalSegmentCount);
boolean canUpdate = hasNewSegments && newSegmentCount >= videoUpdateConfig.getMinNewSegmentCount();
result.setCanUpdate(canUpdate);
result.setNewSegmentCount(newSegmentCount);
log.info("视频更新检查完成: videoId={}, taskId={}, canUpdate={}, newSegmentCount={}, originalCount={}, currentCount={}",
videoId, taskId, result.isCanUpdate(), newSegmentCount, originalSegmentCount, currentSegmentCount);
} catch (Exception e) {
log.error("检查视频更新失败: videoId={}, taskId={}", videoId, taskId, e);
result.setCanUpdate(false);
}
TaskUpdateResult taskResult = videoTaskRepository.checkTaskUpdate(taskId);
result.setCanUpdate(taskResult.isCanUpdate());
result.setNewSegmentCount(taskResult.getNewSegmentCount());
result.setTotalSegmentCount(taskResult.getTotalSegmentCount());
result.setOriginalSegmentCount(taskResult.getOriginalSegmentCount());
log.info("视频更新检查完成: videoId={}, taskId={}, canUpdate={}, newSegmentCount={}",
videoId, taskId, result.isCanUpdate(), result.getNewSegmentCount());
return result;
}
private int calculateSegmentCount(Map<String, ?> taskParams) {
if (taskParams == null || taskParams.isEmpty()) {
return 0;
}
int totalCount = 0;
for (Map.Entry<String, ?> entry : taskParams.entrySet()) {
Object value = entry.getValue();
if (value instanceof List) {
totalCount += ((List<?>) value).size();
} else if (value instanceof String && StringUtils.isNumeric(entry.getKey())) {
try {
List<?> jsonArray = JacksonUtil.parseArray((String) value, Object.class);
if (jsonArray != null) {
totalCount += jsonArray.size();
}
} catch (Exception e) {
log.warn("解析任务参数失败: key={}, value={}", entry.getKey(), value);
}
}
}
return totalCount;
}
private boolean hasNewSegments(Map<String, ?> originalParams, Map<String, List<SourceEntity>> currentParams) {
if (currentParams == null || currentParams.isEmpty()) {
return false;
}
if (originalParams == null || originalParams.isEmpty()) {
return !currentParams.isEmpty();
}
for (Map.Entry<String, List<SourceEntity>> entry : currentParams.entrySet()) {
String deviceKey = entry.getKey();
List<SourceEntity> currentSources = entry.getValue();
if (!originalParams.containsKey(deviceKey)) {
if (currentSources != null && !currentSources.isEmpty()) {
return true;
}
} else {
Object originalValue = originalParams.get(deviceKey);
int originalCount = 0;
if (originalValue instanceof List) {
originalCount = ((List<?>) originalValue).size();
} else if (originalValue instanceof String) {
try {
List<?> jsonArray = JacksonUtil.parseArray((String) originalValue, Object.class);
if (jsonArray != null) {
originalCount = jsonArray.size();
}
} catch (Exception e) {
log.warn("解析原始参数失败: key={}, value={}", deviceKey, originalValue);
}
}
int currentCount = currentSources != null ? currentSources.size() : 0;
if (currentCount > originalCount) {
return true;
}
}
}
return false;
}
}

View File

@@ -42,6 +42,7 @@ import com.ycwl.basic.model.pc.order.entity.OrderEntity;
import com.ycwl.basic.model.pc.task.entity.TaskEntity;
import com.ycwl.basic.model.pc.video.entity.MemberVideoEntity;
import com.ycwl.basic.model.pc.video.entity.VideoEntity;
import com.ycwl.basic.model.repository.TaskUpdateResult;
import com.ycwl.basic.model.task.resp.SearchFaceRespVo;
import com.ycwl.basic.repository.DeviceRepository;
import com.ycwl.basic.repository.FaceRepository;
@@ -669,6 +670,8 @@ public class FaceServiceImpl implements FaceService {
if (video != null) {
contentPageVO.setDuration(video.getDuration());
contentPageVO.setLockType(-1);
TaskUpdateResult updResult = videoTaskRepository.checkTaskUpdate(video.getTaskId());
contentPageVO.setNewSegmentCount(updResult.getNewSegmentCount());
} else {
TaskEntity taskById = videoTaskRepository.getTaskById(memberVideoEntityList.getFirst().getTaskId());
if (taskById == null) {