Merge branch 'feature_lsl' of gitee.com:L-Shilin/go-fly-platform-services into feature_dzc

This commit is contained in:
2025-10-10 15:49:54 +08:00
6 changed files with 238 additions and 33 deletions
@@ -22,6 +22,9 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -40,6 +43,8 @@ import java.util.Map;
@RequiredArgsConstructor
public class OrderFileController {
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
@Autowired
private IOrderFileService orderFileService;
@@ -256,6 +261,7 @@ public class OrderFileController {
/**
* 检查素材数量是否达到二维码要求,如果达到则更新订单状态为UPLOADED
* 并在7分钟后若仍为UPLOADED则自动更新为COMPLETE
* @param order 订单信息
*/
private void checkAndUpdateOrderStatus(Order order) {
@@ -274,28 +280,43 @@ public class OrderFileController {
log.info("检查素材数量,订单号: {}, 二维码要求数量: {}, 当前上传数量: {}",
order.getOrderNo(), qrCode.getMaterialCount(), currentFileCount);
// 如果当前文件数量达到或超过二维码要求的数量,则更新订单状态为素材上传完成
// 如果当前文件数量达到或超过二维码要求的数量,则更新订单状态为素材上传完成(UPLOADED)
if (currentFileCount >= qrCode.getMaterialCount()) {
// 并发场景下,实时查询最新订单状态再做判断
Order latestOrder = orderService.getOrderByNumber(order.getOrderNo());
if (latestOrder == null) {
log.warn("达到素材数量阈值时订单不存在: orderNo={}", order.getOrderNo());
return;
}
Tourism tourism = tourismService.getTourismById(qrCode.getTourismId());
// 自动更新订单状态为设备执行完成
if (!OrderStatus.COMPLETE.getId().equals(order.getOrderStatus())) {
order.setOrderStatus(OrderStatus.COMPLETE.getId());
boolean updateResult = orderService.updateOrder(order);
boolean isComplete = OrderStatus.COMPLETE.getId().equals(latestOrder.getOrderStatus());
// 更新订单状态为UPLOADED(素材上传完成),但若已为COMPLETE则不更新
if (!isComplete && !OrderStatus.UPLOADED.getId().equals(latestOrder.getOrderStatus())) {
Integer oldStatus = latestOrder.getOrderStatus();
latestOrder.setOrderStatus(OrderStatus.UPLOADED.getId());
boolean updateResult = orderService.updateOrder(latestOrder);
if (updateResult) {
log.info("订单状态已自动更新为设备执行完成: orderNo={}, oldStatus={}, newStatus={}",
order.getOrderNo(), order.getOrderStatus(), OrderStatus.COMPLETE.getId());
log.info("订单状态已更新为UPLOADED: orderNo={}, oldStatus={}, newStatus={}",
latestOrder.getOrderNo(), oldStatus, OrderStatus.UPLOADED.getId());
} else {
log.error("订单状态更新失败: orderNo={}", order.getOrderNo());
log.error("订单状态更新为UPLOADED失败: orderNo={}", latestOrder.getOrderNo());
}
}
//发送用户素材上传成功通知
sendMessageForResouresUploaded(order,tourism);
sendMessageForResouresUploaded(latestOrder,tourism);
// 预热订单相关的文件到CDN
prefetchOrderFilesToCdn(order.getOrderNo());
prefetchOrderFilesToCdn(latestOrder.getOrderNo());
// 7分钟后如果状态仍为UPLOADED,则自动更新为COMPLETE;若已完成则不再安排
if (!isComplete) {
scheduleAutoCompleteIfStillUploaded(latestOrder.getOrderNo());
}
// 检查是否开启一键成片功能
if (isVideoEditEnabled(qrCode)) {
@@ -303,30 +324,30 @@ public class OrderFileController {
order.getOrderNo(), order.getCodeId());
// 检查是否存在进行中的编辑任务
EditTask existingTask = editTaskService.getTaskByOrderSn(order.getOrderNo());
EditTask existingTask = editTaskService.getTaskByOrderSn(latestOrder.getOrderNo());
if (existingTask != null && EditTaskStatus.isProcessing(existingTask.getStatus())) {
log.info("已存在进行中的编辑任务: orderNo={}, taskId={}", order.getOrderNo(), existingTask.getId());
log.info("已存在进行中的编辑任务: orderNo={}, taskId={}", latestOrder.getOrderNo(), existingTask.getId());
return ;
}
//触发一键成片
// 1. 创建新任务
EditTask task = editTaskService.createTask(order.getOrderNo(), order.getUserId());
EditTask task = editTaskService.createTask(latestOrder.getOrderNo(), latestOrder.getUserId());
// 2. 获取素材文件
List<OrderFile> orderFiles = orderFileService.getOrderFileByOrderNo(order.getOrderNo());
List<OrderFile> orderFiles = orderFileService.getOrderFileByOrderNo(latestOrder.getOrderNo());
// 3. 提交编辑任务
String jobId = videoEditService.submitEditTask(order.getOrderNo(), orderFiles);
String jobId = videoEditService.submitEditTask(latestOrder.getOrderNo(), orderFiles);
log.info("提交一键成片任务成功: orderNo={}, jobId={}, totalMediaFiles={}",
order.getOrderNo(), jobId, orderFiles.size());
latestOrder.getOrderNo(), jobId, orderFiles.size());
// 4. 更新任务信息
task.setJobId(jobId);
task.setStatus(EditTaskStatus.PROCESSING);
editTaskService.saveOrUpdateTask(task);
} else {
log.info("二维码未开启一键成片功能,跳过一键成片: orderNo={}, codeId={}, enableVideoEdit={}",
order.getOrderNo(), order.getCodeId(), qrCode.getEnableVideoEdit());
latestOrder.getOrderNo(), latestOrder.getCodeId(), qrCode.getEnableVideoEdit());
}
}
} catch (Exception e) {
@@ -334,6 +355,41 @@ public class OrderFileController {
}
}
/**
* 7分钟后检查订单状态是否仍为UPLOADED,如是则自动置为COMPLETE
*/
private void scheduleAutoCompleteIfStillUploaded(String orderNo) {
try {
scheduler.schedule(() -> {
try {
Order latest = orderService.getOrderByNumber(orderNo);
if (latest == null) {
log.warn("延迟完成检查时订单不存在: orderNo={}", orderNo);
return;
}
if (OrderStatus.UPLOADED.getId().equals(latest.getOrderStatus())) {
Integer oldStatus = latest.getOrderStatus();
latest.setOrderStatus(OrderStatus.COMPLETE.getId());
boolean updated = orderService.updateOrder(latest);
if (updated) {
log.info("订单状态在7分钟后自动由UPLOADED更新为COMPLETE: orderNo={}, oldStatus={}, newStatus={}",
orderNo, oldStatus, OrderStatus.COMPLETE.getId());
} else {
log.error("订单状态在7分钟后自动更新为COMPLETE失败: orderNo={}", orderNo);
}
} else {
log.info("订单状态在7分钟后已非UPLOADED,跳过自动完成: orderNo={}, currentStatus={}",
orderNo, latest.getOrderStatus());
}
} catch (Exception e) {
log.error("订单状态7分钟后自动完成检查异常: orderNo={}", orderNo, e);
}
}, 7, TimeUnit.MINUTES);
} catch (Exception e) {
log.error("安排订单状态自动完成任务失败: orderNo={}", orderNo, e);
}
}
/**
* 检查是否开启一键成片功能
*/
@@ -38,7 +38,7 @@ public class Order extends SuperEntity {
@Schema(description = "订单状态")
private Integer orderStatus;
@Schema(description = "支付类型(1:微信支付,2:优惠券支付)")
@Schema(description = "支付类型(1:微信支付,2:优惠券支付, 3: 第三方聚到核销)")
private Integer payType;
private Date payTime;
@@ -131,7 +131,7 @@ public class MediaServiceImpl implements IMediaService {
// 3. 获取视频文件
// List<OrderFile> videoFiles = orderFileService.getOrderVideoFiles(orderNo);
if (orderFiles == null || orderFiles.size() < 7) {
if (orderFiles == null || orderFiles.size() < qrCode.getMaterialCount()) {
throw new BusinessException("订单视频文件数量不足,至少需要2个视频文件");
}
@@ -188,16 +188,17 @@ public class MediaServiceImpl implements IMediaService {
List<OrderFile> videoFiles = taskData.getVideoFiles();
//@TODO 后期需要通过模板参数进行动态设置clips参数
clips.put("1", fileCenterService.getViewUrlByPath(videoFiles.get(4).getFilePath()));
clips.put("2", fileCenterService.getViewUrlByPath(videoFiles.get(5).getFilePath()));
clips.put("3", fileCenterService.getViewUrlByPath(videoFiles.get(6).getFilePath()));
clips.put("4", fileCenterService.getViewUrlByPath(videoFiles.get(0).getFilePath()));
clips.put("5", fileCenterService.getViewUrlByPath(videoFiles.get(1).getFilePath()));
clips.put("6", fileCenterService.getViewUrlByPath(videoFiles.get(2).getFilePath()));
clips.put("7", fileCenterService.getViewUrlByPath(videoFiles.get(3).getFilePath()));
// 保持既有模板顺序;当索引越界或无素材时返回空URL
clips.put("1", getUrlBySafeIndex(videoFiles, 4));
clips.put("2", getUrlBySafeIndex(videoFiles, 5));
clips.put("3", getUrlBySafeIndex(videoFiles, 6));
clips.put("4", getUrlBySafeIndex(videoFiles, 0));
clips.put("5", getUrlBySafeIndex(videoFiles, 1));
clips.put("6", getUrlBySafeIndex(videoFiles, 2));
clips.put("7", getUrlBySafeIndex(videoFiles, 3));
if (taskData.getVideoFiles().size() > 7) {
clips.put("8", fileCenterService.getViewUrlByPath(videoFiles.get(7).getFilePath()));
if (videoFiles != null && videoFiles.size() > 7) {
clips.put("8", getUrlBySafeIndex(videoFiles, 7));
}
return JSON.toJSONString(clips);
@@ -206,6 +207,14 @@ public class MediaServiceImpl implements IMediaService {
}
}
private String getUrlBySafeIndex(List<OrderFile> videoFiles, int index) {
int size = (videoFiles == null) ? 0 : videoFiles.size();
if (size == 0 || index < 0 || index >= size) {
return "";
}
return fileCenterService.getViewUrlByPath(videoFiles.get(index).getFilePath());
}
/**
* 构建输出配置
*/
@@ -103,7 +103,12 @@ public class OrderServiceImpl extends SuperServiceImpl<OrderMapper, Order> imple
// 创建订单,初始状态为"待支付"
@Override
public boolean createOrder(Order order) {
order.setOrderStatus(OrderStatus.UNPAID.getId());
// payType=3 视为已支付,否则为待支付
if (order.getPayType() != null && order.getPayType() == 3) {
order.setOrderStatus(OrderStatus.PAID.getId());
} else {
order.setOrderStatus(OrderStatus.UNPAID.getId());
}
String orderNo = generateOrderNumber(order.getTourismId());
order.setOrderNo(orderNo);