This commit is contained in:
2025-10-30 18:24:05 +08:00
parent 0971c4a3a7
commit 471c8df097
4 changed files with 50 additions and 1 deletions

View File

@@ -67,6 +67,9 @@ public class BatchPublishItem extends BaseEntity
@Excel(name = "失败原因")
private String errorMessage;
/** 执行日志 */
private String execLog;
/** 上架时间 */
@Excel(name = "上架时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
private Date publishTime;
@@ -178,6 +181,14 @@ public class BatchPublishItem extends BaseEntity
this.errorMessage = errorMessage;
}
public String getExecLog() {
return execLog;
}
public void setExecLog(String execLog) {
this.execLog = execLog;
}
public Date getPublishTime() {
return publishTime;
}
@@ -210,6 +221,7 @@ public class BatchPublishItem extends BaseEntity
.append("outerId", getOuterId())
.append("publishPrice", getPublishPrice())
.append("errorMessage", getErrorMessage())
.append("execLog", getExecLog())
.append("publishTime", getPublishTime())
.append("delaySeconds", getDelaySeconds())
.append("createTime", getCreateTime())

View File

@@ -512,6 +512,8 @@ public class BatchPublishServiceImpl implements IBatchPublishService
if (!items.isEmpty()) {
itemMapper.batchInsertBatchPublishItem(items);
// 批量插入无法回填ID重新查询一次拿到带ID的明细列表
items = itemMapper.selectBatchPublishItemByTaskId(taskId);
}
// 异步执行发品任务(通过代理调用,确保@Async生效
@@ -553,23 +555,28 @@ public class BatchPublishServiceImpl implements IBatchPublishService
try {
// 更新明细状态为发布中
item.setStatus(1);
appendItemLogSafe(item.getId(), "【开始发布】设置状态=发布中");
itemMapper.updateBatchPublishItem(item);
// 调用发品接口
appendItemLogSafe(item.getId(), "调用发品接口...");
boolean success = publishProduct(item, request);
if (success) {
successCount++;
appendItemLogSafe(item.getId(), "发品成功,调度上架(" + item.getDelaySeconds() + "s)");
// 发品成功,调度延迟上架
schedulePublish(item.getId(), item.getDelaySeconds());
} else {
failCount++;
appendItemLogSafe(item.getId(), "发品失败");
}
} catch (Exception e) {
log.error("发品失败,商品: {}, 账号: {}", item.getProductName(), item.getAccountRemark(), e);
item.setStatus(3); // 发布失败
item.setErrorMessage("发品异常: " + e.getMessage());
itemMapper.updateBatchPublishItem(item);
appendItemLogSafe(item.getId(), "发品异常: " + e.getMessage());
failCount++;
}
}
@@ -819,6 +826,7 @@ public class BatchPublishServiceImpl implements IBatchPublishService
// 更新状态为上架中
item.setStatus(4);
itemMapper.updateBatchPublishItem(item);
appendItemLogSafe(item.getId(), "开始上架,设置状态=上架中");
// 检查商品ID是否存在
if (item.getProductId() == null) {
@@ -859,6 +867,7 @@ public class BatchPublishServiceImpl implements IBatchPublishService
item.setPublishTime(new Date());
item.setErrorMessage(null);
itemMapper.updateBatchPublishItem(item);
appendItemLogSafe(item.getId(), "上架成功");
log.info("上架成功: itemId={}, productId={}", itemId, item.getProductId());
} else {
@@ -872,9 +881,31 @@ public class BatchPublishServiceImpl implements IBatchPublishService
item.setStatus(6); // 上架失败
item.setErrorMessage("上架异常: " + e.getMessage());
itemMapper.updateBatchPublishItem(item);
appendItemLogSafe(item.getId(), "上架异常: " + e.getMessage());
}
}
private void appendItemLogSafe(Long itemId, String line) {
try {
if (itemId == null) return;
BatchPublishItem current = itemMapper.selectBatchPublishItemById(itemId);
if (current == null) return;
StringBuilder sb = new StringBuilder();
if (current.getExecLog() != null) {
sb.append(current.getExecLog());
if (!current.getExecLog().endsWith("\n")) sb.append('\n');
}
sb.append('[').append(new java.text.SimpleDateFormat("HH:mm:ss").format(new java.util.Date())).append("] ").append(line);
String newLog = sb.toString();
// 限制日志长度避免过长例如保留最近5000字符
if (newLog.length() > 5000) {
newLog = newLog.substring(newLog.length() - 5000);
}
current.setExecLog(newLog);
itemMapper.updateBatchPublishItem(current);
} catch (Exception ignore) {}
}
/**
* 查询批量发品任务
*/

View File

@@ -18,6 +18,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="outerId" column="outer_id" />
<result property="publishPrice" column="publish_price" />
<result property="errorMessage" column="error_message" />
<result property="execLog" column="exec_log" />
<result property="publishTime" column="publish_time" />
<result property="delaySeconds" column="delay_seconds" />
<result property="createTime" column="create_time" />
@@ -25,7 +26,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<sql id="selectBatchPublishItemVo">
select id, task_id, skuid, product_name, target_account, account_remark, sub_account, status,
product_id, product_status, outer_id, publish_price, error_message,
product_id, product_status, outer_id, publish_price, error_message, exec_log,
publish_time, delay_seconds, create_time
from batch_publish_item
</sql>
@@ -116,6 +117,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="productStatus != null">product_status = #{productStatus},</if>
<if test="outerId != null">outer_id = #{outerId},</if>
<if test="errorMessage != null">error_message = #{errorMessage},</if>
<if test="execLog != null">exec_log = #{execLog},</if>
<if test="publishTime != null">publish_time = #{publishTime},</if>
</trim>
where id = #{id}

View File

@@ -0,0 +1,4 @@
-- 为批量发品明细表添加执行日志字段
ALTER TABLE batch_publish_item ADD COLUMN exec_log TEXT COMMENT '执行日志' AFTER error_message;