1
This commit is contained in:
@@ -63,5 +63,11 @@ public interface IBatchPublishService
|
||||
* @param delaySeconds 延迟秒数
|
||||
*/
|
||||
void schedulePublish(Long itemId, Integer delaySeconds);
|
||||
|
||||
/**
|
||||
* 重新调度执行某个任务的未完成明细
|
||||
* @param taskId 任务ID
|
||||
*/
|
||||
void retryTask(Long taskId);
|
||||
}
|
||||
|
||||
|
||||
@@ -25,6 +25,7 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -59,6 +60,11 @@ public class BatchPublishServiceImpl implements IBatchPublishService
|
||||
@Autowired
|
||||
private IOuterIdGeneratorService outerIdGeneratorService;
|
||||
|
||||
// 为了使 @Async 生效,需通过代理调用自身异步方法
|
||||
@Autowired
|
||||
@Lazy
|
||||
private BatchPublishServiceImpl self;
|
||||
|
||||
/**
|
||||
* 清理文本中的所有URL链接(保留换行符)
|
||||
*
|
||||
@@ -488,8 +494,8 @@ public class BatchPublishServiceImpl implements IBatchPublishService
|
||||
itemMapper.batchInsertBatchPublishItem(items);
|
||||
}
|
||||
|
||||
// 异步执行发品任务
|
||||
asyncBatchPublish(taskId, items, request);
|
||||
// 异步执行发品任务(通过代理调用,确保@Async生效)
|
||||
self.asyncBatchPublish(taskId, items, request);
|
||||
|
||||
return taskId;
|
||||
}
|
||||
@@ -571,11 +577,14 @@ public class BatchPublishServiceImpl implements IBatchPublishService
|
||||
throw new RuntimeException("未找到ERP账号: " + item.getTargetAccount());
|
||||
}
|
||||
|
||||
// 【修改】获取对应的商品配置(包含自定义价格和文案)
|
||||
BatchPublishRequest.ProductItem productConfig = request.getProducts().stream()
|
||||
.filter(p -> p.getSkuid().equals(item.getSkuid()))
|
||||
.findFirst()
|
||||
.orElseThrow(() -> new RuntimeException("未找到商品配置: " + item.getSkuid()));
|
||||
// 获取对应的商品配置(包含自定义价格和文案);如果不存在,使用回退策略
|
||||
BatchPublishRequest.ProductItem productConfig = null;
|
||||
if (request != null && request.getProducts() != null) {
|
||||
productConfig = request.getProducts().stream()
|
||||
.filter(p -> item.getSkuid().equals(p.getSkuid()))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
// 获取通用参数
|
||||
BatchPublishRequest.CommonParams commonParams = request.getCommonParams();
|
||||
@@ -587,9 +596,14 @@ public class BatchPublishServiceImpl implements IBatchPublishService
|
||||
erpShop.setSpBizType(commonParams.getSpBizType());
|
||||
|
||||
// 【修改】使用自定义的发布价格
|
||||
Double publishPrice = productConfig.getPublishPrice() != null ?
|
||||
productConfig.getPublishPrice() :
|
||||
(productConfig.getPrice() != null ? productConfig.getPrice() : 0.0);
|
||||
Double publishPrice = null;
|
||||
if (productConfig != null) {
|
||||
publishPrice = productConfig.getPublishPrice() != null ? productConfig.getPublishPrice() : productConfig.getPrice();
|
||||
}
|
||||
if (publishPrice == null) {
|
||||
// 回退到明细中的价格(分转元)
|
||||
publishPrice = item.getPublishPrice() != null ? item.getPublishPrice() / 100.0 : 0.0;
|
||||
}
|
||||
erpShop.setPrice(Math.round(publishPrice * 100)); // 价格转分
|
||||
|
||||
erpShop.setExpressFee(Math.round(commonParams.getExpressFee() * 100)); // 邮费转分
|
||||
@@ -615,21 +629,21 @@ public class BatchPublishServiceImpl implements IBatchPublishService
|
||||
shop.setTitle(truncateByCodePoints(title, 60));
|
||||
|
||||
// 【修改】使用用户选择的文案
|
||||
String content = getSelectedWenanContent(productConfig);
|
||||
String content = productConfig != null ? getSelectedWenanContent(productConfig) : null;
|
||||
if (StringUtils.isEmpty(content)) {
|
||||
// 如果没有选择文案,使用默认描述
|
||||
content = "【正品保障】" + item.getProductName() + "\n\n" +
|
||||
"SKUID: " + item.getSkuid() + "\n" +
|
||||
"店铺信息: " + (productConfig.getShopName() != null ? productConfig.getShopName() : "京东商城");
|
||||
"店铺信息: " + ((productConfig != null && productConfig.getShopName() != null) ? productConfig.getShopName() : "京东商城");
|
||||
}
|
||||
shop.setContent(content);
|
||||
|
||||
// 【修改】使用商品配置中的图片数组
|
||||
List<String> images = productConfig.getImages();
|
||||
List<String> images = productConfig != null ? productConfig.getImages() : null;
|
||||
if (images == null || images.isEmpty()) {
|
||||
// 如果没有图片,使用主图
|
||||
images = new ArrayList<>();
|
||||
if (productConfig.getProductImage() != null) {
|
||||
if (productConfig != null && productConfig.getProductImage() != null) {
|
||||
images.add(productConfig.getProductImage());
|
||||
}
|
||||
}
|
||||
@@ -861,5 +875,40 @@ public class BatchPublishServiceImpl implements IBatchPublishService
|
||||
public List<BatchPublishTask> selectTaskList(BatchPublishTask task) {
|
||||
return taskMapper.selectBatchPublishTaskList(task);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void retryTask(Long taskId) {
|
||||
BatchPublishTask task = taskMapper.selectBatchPublishTaskById(taskId);
|
||||
if (task == null) {
|
||||
throw new RuntimeException("任务不存在: " + taskId);
|
||||
}
|
||||
|
||||
// 仅重试 待发布(0)/发布失败(3) 的明细
|
||||
List<BatchPublishItem> allItems = itemMapper.selectBatchPublishItemByTaskId(taskId);
|
||||
List<BatchPublishItem> itemsToRetry = new ArrayList<>();
|
||||
for (BatchPublishItem it : allItems) {
|
||||
if (it.getStatus() != null && (it.getStatus() == 0 || it.getStatus() == 3)) {
|
||||
itemsToRetry.add(it);
|
||||
}
|
||||
}
|
||||
|
||||
if (itemsToRetry.isEmpty()) {
|
||||
log.info("任务{} 无需重试,未发现待发布/失败的明细", taskId);
|
||||
return;
|
||||
}
|
||||
|
||||
// 构造最小请求对象,仅提供通用参数用于发布
|
||||
BatchPublishRequest req = new BatchPublishRequest();
|
||||
try {
|
||||
BatchPublishRequest.CommonParams commonParams = JSON.parseObject(
|
||||
task.getCommonParams(), BatchPublishRequest.CommonParams.class);
|
||||
req.setCommonParams(commonParams);
|
||||
} catch (Exception e) {
|
||||
log.warn("解析任务通用参数失败,将使用默认参数: {}", task.getCommonParams(), e);
|
||||
}
|
||||
|
||||
log.info("开始重试任务{} 的 {} 条明细", taskId, itemsToRetry.size());
|
||||
self.asyncBatchPublish(taskId, itemsToRetry, req);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user