feat: 发布工作台草稿解绑平台ID,按店累计推送/发布成功次数

This commit is contained in:
2026-10-14 11:41:57 +08:00
parent f272122918
commit 5dee928321
21 changed files with 452 additions and 264 deletions
@@ -20,7 +20,7 @@ public class PddGoodsPublishDTO {
private Long pddShopId;
/**
* 可选:关联的系统草稿 ID。发布成功后回写 goods_commit_id / goods_id,供审核驳回消息匹配。
* 可选:关联的系统草稿 ID。正式发布成功后将该草稿标为 READY,并累加对应店铺的发布次数。
*/
private Long systemDraftId;
@@ -19,7 +19,7 @@ public interface PddGoodsPublishDraftMapper extends BaseMapper<PddGoodsPublishDr
SELECT pdd_shop_id AS pddShopId, COUNT(*) AS draftCount
FROM channel_pdd_goods_publish_draft
WHERE company_id = #{companyId}
AND status IN ('EDITING', 'NEED_CHECK', 'READY', 'FAILED', 'PUBLISHING', 'REJECTED')
AND status IN ('EDITING', 'NEED_CHECK', 'READY', 'FAILED', 'PUBLISHING')
GROUP BY pdd_shop_id
""")
List<PddPublishDeskShopCountVO> countByShop(@Param("companyId") Long companyId);
@@ -0,0 +1,44 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.po.PddGoodsPublishDraftShopStatPO;
/**
* 草稿×店铺成功次数 Mapper。
*/
@Mapper
public interface PddGoodsPublishDraftShopStatMapper extends BaseMapper<PddGoodsPublishDraftShopStatPO> {
/** 推平台草稿成功 +1 */
@Insert("""
INSERT INTO channel_pdd_goods_publish_draft_shop_stat
(company_id, draft_id, pdd_shop_id, push_success_count, publish_success_count, last_push_at)
VALUES
(#{companyId}, #{draftId}, #{pddShopId}, 1, 0, NOW())
ON DUPLICATE KEY UPDATE
push_success_count = push_success_count + 1,
last_push_at = NOW()
""")
int incrPushSuccess(
@Param("companyId") Long companyId,
@Param("draftId") Long draftId,
@Param("pddShopId") Long pddShopId);
/** 正式发布成功 +1 */
@Insert("""
INSERT INTO channel_pdd_goods_publish_draft_shop_stat
(company_id, draft_id, pdd_shop_id, push_success_count, publish_success_count, last_publish_at)
VALUES
(#{companyId}, #{draftId}, #{pddShopId}, 0, 1, NOW())
ON DUPLICATE KEY UPDATE
publish_success_count = publish_success_count + 1,
last_publish_at = NOW()
""")
int incrPublishSuccess(
@Param("companyId") Long companyId,
@Param("draftId") Long draftId,
@Param("pddShopId") Long pddShopId);
}
@@ -9,7 +9,7 @@ import lombok.Data;
import java.time.LocalDateTime;
/**
* 拼多多商品发布草稿。
* 拼多多商品发布系统草稿(编辑工作区,不绑平台 commit/goods)。
*/
@Data
@TableName("channel_pdd_goods_publish_draft")
@@ -20,9 +20,6 @@ public class PddGoodsPublishDraftPO {
public static final String STATUS_READY = "READY";
public static final String STATUS_PUBLISHING = "PUBLISHING";
public static final String STATUS_FAILED = "FAILED";
public static final String STATUS_PUBLISHED = "PUBLISHED";
/** 拼多多审核驳回(仅系统草稿有 commit/goods 关联时写入) */
public static final String STATUS_REJECTED = "REJECTED";
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@@ -54,25 +51,6 @@ public class PddGoodsPublishDraftPO {
@TableField("last_error")
private String lastError;
/** 最近一次推送到拼多多草稿箱的 commitId */
@TableField("pdd_goods_commit_id")
private Long pddGoodsCommitId;
/** 平台可能返回的 goodsId;草稿阶段常为空 */
@TableField("pdd_goods_id")
private Long pddGoodsId;
@TableField("pdd_draft_pushed_at")
private LocalDateTime pddDraftPushedAt;
/** 拼多多审核驳回原因 */
@TableField("reject_comment")
private String rejectComment;
/** 拼多多审核驳回时间 */
@TableField("reject_time")
private LocalDateTime rejectTime;
@TableField("created_by")
private Long createdBy;
@@ -0,0 +1,47 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.po;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 系统草稿在各拼多多店铺的成功投递次数。
*/
@Data
@TableName("channel_pdd_goods_publish_draft_shop_stat")
public class PddGoodsPublishDraftShopStatPO {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@TableField("company_id")
private Long companyId;
@TableField("draft_id")
private Long draftId;
@TableField("pdd_shop_id")
private Long pddShopId;
@TableField("push_success_count")
private Integer pushSuccessCount;
@TableField("publish_success_count")
private Integer publishSuccessCount;
@TableField("last_push_at")
private LocalDateTime lastPushAt;
@TableField("last_publish_at")
private LocalDateTime lastPublishAt;
@TableField("create_time")
private LocalDateTime createTime;
@TableField("update_time")
private LocalDateTime updateTime;
}
@@ -21,12 +21,19 @@ public class PddGoodsPublishJobPO {
public static final String STATUS_PARTIAL = "PARTIAL";
public static final String STATUS_FAILED = "FAILED";
public static final String ACTION_PUSH_DRAFT = "PUSH_DRAFT";
public static final String ACTION_PUBLISH = "PUBLISH";
@TableId(value = "id", type = IdType.AUTO)
private Long id;
@TableField("company_id")
private Long companyId;
/** PUSH_DRAFT / PUBLISH */
@TableField("action")
private String action;
@TableField("status")
private String status;
@@ -20,11 +20,14 @@ public class PddGoodsPublishDraftVO {
private String payloadJson;
private Long sourceDraftId;
private String lastError;
private Long pddGoodsCommitId;
private Long pddGoodsId;
private LocalDateTime pddDraftPushedAt;
private String rejectComment;
private LocalDateTime rejectTime;
/** 成功推平台草稿累计次数(各店合计) */
private Integer pushSuccessCount;
/** 成功正式发布累计次数(各店合计) */
private Integer publishSuccessCount;
/** 成功推过草稿的店铺数 */
private Integer pushShopCount;
/** 成功发布过的店铺数 */
private Integer publishShopCount;
private Long createdBy;
private LocalDateTime createTime;
private LocalDateTime updateTime;
@@ -10,11 +10,10 @@ import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.vo.
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.vo.PddPublishDeskDraftPageVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.vo.PddPublishDeskShopCountVO;
import java.time.LocalDateTime;
import java.util.List;
/**
* 拼多多发布工作台:草稿 CRUD、一键克隆、批量发布任务。
* 拼多多发布工作台:草稿 CRUD、克隆、推平台草稿、批量发布。
*/
public interface PddGoodsPublishDeskService {
@@ -40,19 +39,6 @@ public interface PddGoodsPublishDeskService {
/** 重试任务中失败的明细 */
PddGoodsPublishJobVO retryFailedItems(Long jobId);
/** 将系统草稿推送到拼多多草稿箱并回写 commitId */
/** 将系统草稿推送到拼多多草稿箱(新建平台草稿),并累加店铺成功次数 */
PddGoodsPublishDraftVO pushPlatformDraft(Long id);
/**
* 审核驳回消息回写:按 commitId / goodsId 匹配系统草稿;无匹配则忽略(纯平台草稿)。
*
* @return 是否更新到系统草稿
*/
boolean applyGoodsCheckReject(
Long companyId,
Long pddShopId,
Long goodsCommitId,
Long goodsId,
String rejectComment,
LocalDateTime rejectTime);
}
@@ -0,0 +1,13 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.service;
/**
* 系统草稿投递成功次数累加。
*/
public interface PddGoodsPublishDraftShopStatService {
/** 推平台草稿成功:该草稿在该店 push_success_count +1 */
void incrPushSuccess(Long companyId, Long draftId, Long pddShopId);
/** 正式发布成功:该草稿在该店 publish_success_count +1 */
void incrPublishSuccess(Long companyId, Long draftId, Long pddShopId);
}
@@ -14,9 +14,8 @@ import org.springframework.util.StringUtils;
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.dto.publish.PddGoodsPublishDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.vo.publish.PddGoodsPublishResultVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.service.PddGoodsPublishService;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.mapper.PddGoodsPublishDraftMapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.mapper.PddGoodsPublishDraftShopStatMapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.mapper.PddGoodsPublishJobItemMapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.mapper.PddGoodsPublishJobMapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.dto.PddPublishDeskCloneDTO;
@@ -24,6 +23,7 @@ import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.dto
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.dto.PddPublishDeskDraftSaveDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.dto.PddPublishDeskJobCreateDTO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.po.PddGoodsPublishDraftPO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.po.PddGoodsPublishDraftShopStatPO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.po.PddGoodsPublishJobItemPO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.po.PddGoodsPublishJobPO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.vo.PddGoodsPublishDraftVO;
@@ -32,12 +32,13 @@ import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.vo.
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.vo.PddPublishDeskDraftPageVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.vo.PddPublishDeskShopCountVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.service.PddGoodsPublishDeskService;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.service.PddGoodsPublishDraftShopStatService;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.service.PddGoodsPublishJobRunner;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.service.PddGoodsPublishService;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.mapper.PddShopMapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.shop.model.po.PddShopPO;
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -58,11 +59,12 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
PddGoodsPublishDraftPO.STATUS_NEED_CHECK,
PddGoodsPublishDraftPO.STATUS_READY,
PddGoodsPublishDraftPO.STATUS_FAILED,
PddGoodsPublishDraftPO.STATUS_PUBLISHING,
PddGoodsPublishDraftPO.STATUS_REJECTED
PddGoodsPublishDraftPO.STATUS_PUBLISHING
);
private final PddGoodsPublishDraftMapper draftMapper;
private final PddGoodsPublishDraftShopStatMapper shopStatMapper;
private final PddGoodsPublishDraftShopStatService shopStatService;
private final PddGoodsPublishJobMapper jobMapper;
private final PddGoodsPublishJobItemMapper jobItemMapper;
private final PddShopMapper pddShopMapper;
@@ -102,13 +104,16 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
.map(PddGoodsPublishDraftPO::getPddShopId)
.filter(Objects::nonNull)
.collect(Collectors.toSet()));
Map<Long, int[]> statsByDraft = loadStatSums(companyId, page.getRecords().stream()
.map(PddGoodsPublishDraftPO::getId)
.collect(Collectors.toSet()));
PddPublishDeskDraftPageVO vo = new PddPublishDeskDraftPageVO();
vo.setTotal(page.getTotal());
vo.setPage((long) query.getPage());
vo.setPageSize((long) query.getPageSize());
vo.setRecords(page.getRecords().stream()
.map(po -> toDraftVo(po, shopNames))
.map(po -> toDraftVo(po, shopNames, statsByDraft.get(po.getId())))
.collect(Collectors.toList()));
List<PddPublishDeskShopCountVO> counts = draftMapper.countByShop(companyId);
@@ -137,7 +142,8 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
@Override
public PddGoodsPublishDraftVO getDraft(Long id) {
PddGoodsPublishDraftPO po = requireDraft(id);
return toDraftVo(po, loadShopNamesForOne(po));
Map<Long, int[]> stats = loadStatSums(po.getCompanyId(), Set.of(po.getId()));
return toDraftVo(po, loadShopNamesForOne(po), stats.get(po.getId()));
}
@Override
@@ -157,7 +163,7 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
po.setPayloadJson(dto.getPayloadJson());
po.setCreatedBy(currentUserContext.getCurrentUserId());
draftMapper.insert(po);
return toDraftVo(po, loadShopNamesForOne(po));
return toDraftVo(po, loadShopNamesForOne(po), null);
}
@Override
@@ -165,9 +171,8 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
public PddGoodsPublishDraftVO updateDraft(Long id, PddPublishDeskDraftSaveDTO dto) {
Long companyId = currentUserContext.getCurrentCompanyId();
PddGoodsPublishDraftPO po = requireDraft(id);
if (PddGoodsPublishDraftPO.STATUS_PUBLISHING.equals(po.getStatus())
|| PddGoodsPublishDraftPO.STATUS_PUBLISHED.equals(po.getStatus())) {
throw new BusinessException("发布中或已发布的草稿不可编辑");
if (PddGoodsPublishDraftPO.STATUS_PUBLISHING.equals(po.getStatus())) {
throw new BusinessException("发布中的草稿不可编辑");
}
if (dto.getPddShopId() != null) {
requireOwnedShop(dto.getPddShopId(), companyId);
@@ -180,8 +185,6 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
po.setStatus(normalizeStatus(dto.getStatus(), po.getStatus()));
}
po.setPayloadJson(dto.getPayloadJson());
po.setLastError(null);
// 用户再次编辑时清掉驳回信息(null 需 UpdateWrapper)
draftMapper.update(null, new LambdaUpdateWrapper<PddGoodsPublishDraftPO>()
.eq(PddGoodsPublishDraftPO::getId, po.getId())
.set(PddGoodsPublishDraftPO::getPddShopId, po.getPddShopId())
@@ -190,11 +193,10 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
.set(PddGoodsPublishDraftPO::getSkuCount, po.getSkuCount())
.set(PddGoodsPublishDraftPO::getStatus, po.getStatus())
.set(PddGoodsPublishDraftPO::getPayloadJson, po.getPayloadJson())
.set(PddGoodsPublishDraftPO::getLastError, null)
.set(PddGoodsPublishDraftPO::getRejectComment, null)
.set(PddGoodsPublishDraftPO::getRejectTime, null));
.set(PddGoodsPublishDraftPO::getLastError, null));
PddGoodsPublishDraftPO refreshed = draftMapper.selectById(po.getId());
return toDraftVo(refreshed != null ? refreshed : po, loadShopNamesForOne(po));
Map<Long, int[]> stats = loadStatSums(companyId, Set.of(po.getId()));
return toDraftVo(refreshed != null ? refreshed : po, loadShopNamesForOne(po), stats.get(po.getId()));
}
@Override
@@ -235,9 +237,11 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
? source.getStatus()
: PddGoodsPublishDraftPO.STATUS_EDITING)
: PddGoodsPublishDraftPO.STATUS_NEED_CHECK;
if (PddGoodsPublishDraftPO.STATUS_PUBLISHED.equals(status)
|| PddGoodsPublishDraftPO.STATUS_PUBLISHING.equals(status)
|| PddGoodsPublishDraftPO.STATUS_REJECTED.equals(status)) {
if (PddGoodsPublishDraftPO.STATUS_PUBLISHING.equals(status)) {
status = PddGoodsPublishDraftPO.STATUS_EDITING;
}
// 历史脏状态
if ("PUBLISHED".equals(status) || "REJECTED".equals(status)) {
status = PddGoodsPublishDraftPO.STATUS_EDITING;
}
po.setStatus(status);
@@ -245,7 +249,7 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
po.setSourceDraftId(source.getId());
po.setCreatedBy(userId);
draftMapper.insert(po);
created.add(toDraftVo(po, loadShopNamesForOne(po)));
created.add(toDraftVo(po, loadShopNamesForOne(po), null));
}
}
return created;
@@ -271,9 +275,8 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
if (PddGoodsPublishDraftPO.STATUS_NEED_CHECK.equals(d.getStatus())) {
throw new BusinessException("草稿「" + d.getTitle() + "」待校验(跨店克隆后请核对类目/运费)");
}
if (PddGoodsPublishDraftPO.STATUS_PUBLISHING.equals(d.getStatus())
|| PddGoodsPublishDraftPO.STATUS_PUBLISHED.equals(d.getStatus())) {
throw new BusinessException("草稿「" + d.getTitle() + "」正在发布或已发布");
if (PddGoodsPublishDraftPO.STATUS_PUBLISHING.equals(d.getStatus())) {
throw new BusinessException("草稿「" + d.getTitle() + "」正在发布中");
}
if (!StringUtils.hasText(d.getPayloadJson()) || !d.getPayloadJson().contains("publishPayload")) {
throw new BusinessException("草稿「" + d.getTitle() + "」缺少可发布数据,请先在编辑器保存");
@@ -282,6 +285,7 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
PddGoodsPublishJobPO job = new PddGoodsPublishJobPO();
job.setCompanyId(companyId);
job.setAction(PddGoodsPublishJobPO.ACTION_PUBLISH);
job.setStatus(PddGoodsPublishJobPO.STATUS_PENDING);
job.setTotalCount(drafts.size());
job.setSuccessCount(0);
@@ -346,6 +350,7 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
}
@Override
@Transactional
public PddGoodsPublishDraftVO pushPlatformDraft(Long id) {
PddGoodsPublishDraftPO po = requireDraft(id);
if (po.getPddShopId() == null) {
@@ -367,14 +372,13 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
}
payload.setPddShopId(po.getPddShopId());
try {
PddGoodsPublishResultVO result = publishService.pushPlatformDraft(
payload, po.getPddGoodsCommitId(), po.getPddGoodsId());
po.setPddGoodsCommitId(result.getGoodsCommitId());
po.setPddGoodsId(result.getGoodsId());
po.setPddDraftPushedAt(LocalDateTime.now());
po.setLastError(null);
draftMapper.updateById(po);
return toDraftVo(po, loadShopNamesForOne(po));
// 一律新建平台草稿,不依赖本地历史 commitId
publishService.pushPlatformDraft(payload);
shopStatService.incrPushSuccess(po.getCompanyId(), po.getId(), po.getPddShopId());
draftMapper.update(null, new LambdaUpdateWrapper<PddGoodsPublishDraftPO>()
.eq(PddGoodsPublishDraftPO::getId, po.getId())
.set(PddGoodsPublishDraftPO::getLastError, null));
return getDraft(po.getId());
} catch (BusinessException e) {
po.setLastError(trimError(e.getMessage()));
draftMapper.updateById(po);
@@ -386,51 +390,6 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
}
}
@Override
@Transactional
public boolean applyGoodsCheckReject(
Long companyId,
Long pddShopId,
Long goodsCommitId,
Long goodsId,
String rejectComment,
LocalDateTime rejectTime) {
if (companyId == null || pddShopId == null) {
return false;
}
PddGoodsPublishDraftPO draft = null;
if (goodsCommitId != null) {
draft = draftMapper.selectOne(new LambdaQueryWrapper<PddGoodsPublishDraftPO>()
.eq(PddGoodsPublishDraftPO::getCompanyId, companyId)
.eq(PddGoodsPublishDraftPO::getPddShopId, pddShopId)
.eq(PddGoodsPublishDraftPO::getPddGoodsCommitId, goodsCommitId)
.orderByDesc(PddGoodsPublishDraftPO::getUpdateTime)
.last("LIMIT 1"));
}
if (draft == null && goodsId != null) {
draft = draftMapper.selectOne(new LambdaQueryWrapper<PddGoodsPublishDraftPO>()
.eq(PddGoodsPublishDraftPO::getCompanyId, companyId)
.eq(PddGoodsPublishDraftPO::getPddShopId, pddShopId)
.eq(PddGoodsPublishDraftPO::getPddGoodsId, goodsId)
.orderByDesc(PddGoodsPublishDraftPO::getUpdateTime)
.last("LIMIT 1"));
}
if (draft == null) {
return false;
}
String comment = rejectComment;
if (StringUtils.hasText(comment) && comment.length() > 1000) {
comment = comment.substring(0, 1000);
}
draftMapper.update(null, new LambdaUpdateWrapper<PddGoodsPublishDraftPO>()
.eq(PddGoodsPublishDraftPO::getId, draft.getId())
.set(PddGoodsPublishDraftPO::getStatus, PddGoodsPublishDraftPO.STATUS_REJECTED)
.set(PddGoodsPublishDraftPO::getRejectComment, comment)
.set(PddGoodsPublishDraftPO::getRejectTime, rejectTime)
.set(PddGoodsPublishDraftPO::getLastError, comment));
return true;
}
private String trimError(String msg) {
if (!StringUtils.hasText(msg)) {
return "推送到拼多多草稿箱失败";
@@ -530,7 +489,36 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
return map;
}
private PddGoodsPublishDraftVO toDraftVo(PddGoodsPublishDraftPO po, Map<Long, String> shopNames) {
/**
* @return draftId → [pushSum, publishSum, pushShopCount, publishShopCount]
*/
private Map<Long, int[]> loadStatSums(Long companyId, Set<Long> draftIds) {
Map<Long, int[]> map = new HashMap<>();
if (draftIds == null || draftIds.isEmpty()) {
return map;
}
List<PddGoodsPublishDraftShopStatPO> rows = shopStatMapper.selectList(
new LambdaQueryWrapper<PddGoodsPublishDraftShopStatPO>()
.eq(PddGoodsPublishDraftShopStatPO::getCompanyId, companyId)
.in(PddGoodsPublishDraftShopStatPO::getDraftId, draftIds));
for (PddGoodsPublishDraftShopStatPO row : rows) {
int[] agg = map.computeIfAbsent(row.getDraftId(), k -> new int[4]);
int push = row.getPushSuccessCount() == null ? 0 : row.getPushSuccessCount();
int pub = row.getPublishSuccessCount() == null ? 0 : row.getPublishSuccessCount();
agg[0] += push;
agg[1] += pub;
if (push > 0) {
agg[2]++;
}
if (pub > 0) {
agg[3]++;
}
}
return map;
}
private PddGoodsPublishDraftVO toDraftVo(
PddGoodsPublishDraftPO po, Map<Long, String> shopNames, int[] stat) {
PddGoodsPublishDraftVO vo = new PddGoodsPublishDraftVO();
vo.setId(po.getId());
vo.setCompanyId(po.getCompanyId());
@@ -545,11 +533,17 @@ public class PddGoodsPublishDeskServiceImpl implements PddGoodsPublishDeskServic
vo.setPayloadJson(po.getPayloadJson());
vo.setSourceDraftId(po.getSourceDraftId());
vo.setLastError(po.getLastError());
vo.setPddGoodsCommitId(po.getPddGoodsCommitId());
vo.setPddGoodsId(po.getPddGoodsId());
vo.setPddDraftPushedAt(po.getPddDraftPushedAt());
vo.setRejectComment(po.getRejectComment());
vo.setRejectTime(po.getRejectTime());
if (stat != null) {
vo.setPushSuccessCount(stat[0]);
vo.setPublishSuccessCount(stat[1]);
vo.setPushShopCount(stat[2]);
vo.setPublishShopCount(stat[3]);
} else {
vo.setPushSuccessCount(0);
vo.setPublishSuccessCount(0);
vo.setPushShopCount(0);
vo.setPublishShopCount(0);
}
vo.setCreatedBy(po.getCreatedBy());
vo.setCreateTime(po.getCreateTime());
vo.setUpdateTime(po.getUpdateTime());
@@ -0,0 +1,35 @@
package top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.service.impl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.mapper.PddGoodsPublishDraftShopStatMapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.service.PddGoodsPublishDraftShopStatService;
/**
* 按草稿×店铺 UPSERT 累加成功次数。
*/
@Service
@RequiredArgsConstructor
public class PddGoodsPublishDraftShopStatServiceImpl implements PddGoodsPublishDraftShopStatService {
private final PddGoodsPublishDraftShopStatMapper shopStatMapper;
@Override
@Transactional
public void incrPushSuccess(Long companyId, Long draftId, Long pddShopId) {
if (companyId == null || draftId == null || pddShopId == null) {
return;
}
shopStatMapper.incrPushSuccess(companyId, draftId, pddShopId);
}
@Override
@Transactional
public void incrPublishSuccess(Long companyId, Long draftId, Long pddShopId) {
if (companyId == null || draftId == null || pddShopId == null) {
return;
}
shopStatMapper.incrPublishSuccess(companyId, draftId, pddShopId);
}
}
@@ -16,6 +16,7 @@ import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.mapper.Pd
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.po.PddGoodsPublishDraftPO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.po.PddGoodsPublishJobItemPO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.po.PddGoodsPublishJobPO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.service.PddGoodsPublishDraftShopStatService;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.service.PddGoodsPublishJobRunner;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.service.PddGoodsPublishService;
@@ -23,7 +24,7 @@ import java.time.LocalDateTime;
import java.util.List;
/**
* 批量发布执行器:逐条调用现有单店 publish。
* 批量投递执行器:按 job.action 逐条推平台草稿或正式发布。
*/
@Slf4j
@Service
@@ -34,6 +35,7 @@ public class PddGoodsPublishJobRunnerImpl implements PddGoodsPublishJobRunner {
private final PddGoodsPublishJobItemMapper jobItemMapper;
private final PddGoodsPublishDraftMapper draftMapper;
private final PddGoodsPublishService publishService;
private final PddGoodsPublishDraftShopStatService shopStatService;
private final ObjectMapper objectMapper;
@Override
@@ -69,6 +71,7 @@ public class PddGoodsPublishJobRunnerImpl implements PddGoodsPublishJobRunner {
PddGoodsPublishJobItemPO.STATUS_FAILED);
}
List<PddGoodsPublishJobItemPO> items = jobItemMapper.selectList(qw);
boolean pushOnly = PddGoodsPublishJobPO.ACTION_PUSH_DRAFT.equals(job.getAction());
for (PddGoodsPublishJobItemPO item : items) {
item.setStatus(PddGoodsPublishJobItemPO.STATUS_RUNNING);
@@ -80,9 +83,11 @@ public class PddGoodsPublishJobRunnerImpl implements PddGoodsPublishJobRunner {
markItemFailed(item, "草稿不存在");
continue;
}
draft.setStatus(PddGoodsPublishDraftPO.STATUS_PUBLISHING);
draft.setLastError(null);
draftMapper.updateById(draft);
if (!pushOnly) {
draft.setStatus(PddGoodsPublishDraftPO.STATUS_PUBLISHING);
draft.setLastError(null);
draftMapper.updateById(draft);
}
try {
PddGoodsPublishDTO dto = extractPublishPayload(draft);
@@ -90,22 +95,40 @@ public class PddGoodsPublishJobRunnerImpl implements PddGoodsPublishJobRunner {
dto.setPddShopId(draft.getPddShopId());
}
dto.setSystemDraftId(draft.getId());
PddGoodsPublishResultVO result = publishService.publish(dto);
item.setStatus(PddGoodsPublishJobItemPO.STATUS_SUCCESS);
item.setGoodsId(result.getGoodsId() == null ? null : String.valueOf(result.getGoodsId()));
item.setErrorMessage(null);
jobItemMapper.updateById(item);
// status / commitId / goodsId 由 publish(systemDraftId) 回写
// 任务明细目标店优先(支持一草稿多店)
if (item.getPddShopId() != null) {
dto.setPddShopId(item.getPddShopId());
}
Long targetShopId = dto.getPddShopId();
if (pushOnly) {
publishService.pushPlatformDraft(dto);
shopStatService.incrPushSuccess(companyId, draft.getId(), targetShopId);
item.setStatus(PddGoodsPublishJobItemPO.STATUS_SUCCESS);
item.setErrorMessage(null);
jobItemMapper.updateById(item);
draft.setLastError(null);
draftMapper.updateById(draft);
} else {
// publish 内会 READY + 累加发布次数
PddGoodsPublishResultVO result = publishService.publish(dto);
item.setStatus(PddGoodsPublishJobItemPO.STATUS_SUCCESS);
item.setGoodsId(result.getGoodsId() == null ? null : String.valueOf(result.getGoodsId()));
item.setErrorMessage(null);
jobItemMapper.updateById(item);
}
} catch (Exception e) {
String msg = e.getMessage() == null ? "发布失败" : e.getMessage();
String msg = e.getMessage() == null ? (pushOnly ? "推草稿失败" : "发布失败") : e.getMessage();
if (msg.length() > 900) {
msg = msg.substring(0, 900);
}
markItemFailed(item, msg);
draft.setStatus(PddGoodsPublishDraftPO.STATUS_FAILED);
if (!pushOnly) {
draft.setStatus(PddGoodsPublishDraftPO.STATUS_FAILED);
}
draft.setLastError(msg);
draftMapper.updateById(draft);
log.warn("publish desk job item failed jobId={} draftId={}", jobId, item.getDraftId(), e);
log.warn("publish desk job item failed jobId={} draftId={} action={}",
jobId, item.getDraftId(), job.getAction(), e);
}
}
@@ -119,9 +142,10 @@ public class PddGoodsPublishJobRunnerImpl implements PddGoodsPublishJobRunner {
job.setSuccessCount((int) ok);
job.setFailCount((int) bad);
job.setFinishedAt(LocalDateTime.now());
String verb = pushOnly ? "推草稿" : "发布";
if (bad == 0) {
job.setStatus(PddGoodsPublishJobPO.STATUS_SUCCESS);
job.setMessage("全部发布成功");
job.setMessage("全部" + verb + "成功");
} else if (ok == 0) {
job.setStatus(PddGoodsPublishJobPO.STATUS_FAILED);
job.setMessage("全部失败");
@@ -45,11 +45,7 @@ public interface PddGoodsPublishService {
PddGoodsPublishResultVO publish(PddGoodsPublishDTO dto);
/**
* 推送到拼多多草稿箱:图转传 + commit,不入库渠道商品。
*
* @param existingCommitId 系统草稿上已挂的平台 commitId,可空
* @param existingGoodsId 已挂的平台 goodsId,可空;与 commitId 同时有效时走覆盖保存
* 推送到拼多多草稿箱:图转传 + 新建平台草稿,不入库渠道商品、不覆盖历史 commit。
*/
PddGoodsPublishResultVO pushPlatformDraft(
PddGoodsPublishDTO dto, Long existingCommitId, Long existingGoodsId);
PddGoodsPublishResultVO pushPlatformDraft(PddGoodsPublishDTO dto);
}
@@ -38,6 +38,7 @@ import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.vo.publish.PddP
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.model.vo.publish.PddPublishSpecIdVO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.mapper.PddGoodsPublishDraftMapper;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.model.po.PddGoodsPublishDraftPO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.service.PddGoodsPublishDraftShopStatService;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.service.PddGoodsPublishService;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.service.PddGoodsService;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.support.PddGoodsImageTransfer;
@@ -74,6 +75,7 @@ public class PddGoodsPublishServiceImpl implements PddGoodsPublishService {
private final PddGoodsService pddGoodsService;
private final PddGoodsMapper pddGoodsMapper;
private final PddGoodsPublishDraftMapper publishDraftMapper;
private final PddGoodsPublishDraftShopStatService draftShopStatService;
public PddGoodsPublishServiceImpl(CurrentUserContext currentUserContext,
PddShopMapper pddShopMapper,
@@ -84,7 +86,8 @@ public class PddGoodsPublishServiceImpl implements PddGoodsPublishService {
ProductSkuService productSkuService,
PddGoodsService pddGoodsService,
PddGoodsMapper pddGoodsMapper,
PddGoodsPublishDraftMapper publishDraftMapper) {
PddGoodsPublishDraftMapper publishDraftMapper,
PddGoodsPublishDraftShopStatService draftShopStatService) {
this.currentUserContext = currentUserContext;
this.pddShopMapper = pddShopMapper;
this.authTokenService = authTokenService;
@@ -95,6 +98,7 @@ public class PddGoodsPublishServiceImpl implements PddGoodsPublishService {
this.pddGoodsService = pddGoodsService;
this.pddGoodsMapper = pddGoodsMapper;
this.publishDraftMapper = publishDraftMapper;
this.draftShopStatService = draftShopStatService;
}
@Override
@@ -300,8 +304,7 @@ public class PddGoodsPublishServiceImpl implements PddGoodsPublishService {
result.setGoodsCommitId(addResult.goodsCommitId());
result.setMatchedSpuId(addResult.matchedSpuId());
result.setGoods(goodsVo);
bindSystemDraftAfterPublish(companyId, dto.getSystemDraftId(),
addResult.goodsCommitId(), addResult.goodsId());
markSystemDraftAfterPublish(companyId, dto.getSystemDraftId(), ctx.shop().getId());
log.info("拼多多商品发布成功 companyId={} shopId={} goodsId={}",
companyId, ctx.shop().getId(), addResult.goodsId());
return result;
@@ -313,8 +316,7 @@ public class PddGoodsPublishServiceImpl implements PddGoodsPublishService {
}
@Override
public PddGoodsPublishResultVO pushPlatformDraft(
PddGoodsPublishDTO dto, Long existingCommitId, Long existingGoodsId) {
public PddGoodsPublishResultVO pushPlatformDraft(PddGoodsPublishDTO dto) {
if (dto == null || dto.getCatId() == null) {
throw new BusinessException("叶子类目不能为空");
}
@@ -323,13 +325,8 @@ public class PddGoodsPublishServiceImpl implements PddGoodsPublishService {
try {
PddGoodsPublishPopApiClient.GoodsAddCommand command =
assembleAddCommand(dto, companyId, ctx, false);
PddGoodsPublishPopApiClient.GoodsAddResult popResult;
if (existingCommitId != null && existingGoodsId != null && existingGoodsId > 0) {
popResult = publishPopApiClient.submitGoodsCommitSave(
ctx.mallId(), ctx.accessToken(), command, existingCommitId, existingGoodsId);
} else {
popResult = publishPopApiClient.editGoodsCommit(ctx.mallId(), ctx.accessToken(), command);
}
PddGoodsPublishPopApiClient.GoodsAddResult popResult =
publishPopApiClient.editGoodsCommit(ctx.mallId(), ctx.accessToken(), command);
PddGoodsPublishResultVO result = new PddGoodsPublishResultVO();
result.setGoodsCommitId(popResult.goodsCommitId());
Long goodsId = popResult.goodsId();
@@ -746,9 +743,8 @@ public class PddGoodsPublishServiceImpl implements PddGoodsPublishService {
return new ShopAuth(shop, shop.getMallId(), auth.getAccessToken());
}
/** 发布成功后把平台 commitId/goodsId 写回系统草稿,供审核驳回消息匹配 */
private void bindSystemDraftAfterPublish(
Long companyId, Long systemDraftId, Long goodsCommitId, Long goodsId) {
/** 发布成功:系统草稿回到 READY,并累加该店发布成功次数(不绑平台 ID) */
private void markSystemDraftAfterPublish(Long companyId, Long systemDraftId, Long pddShopId) {
if (systemDraftId == null) {
return;
}
@@ -759,12 +755,10 @@ public class PddGoodsPublishServiceImpl implements PddGoodsPublishService {
}
publishDraftMapper.update(null, new LambdaUpdateWrapper<PddGoodsPublishDraftPO>()
.eq(PddGoodsPublishDraftPO::getId, draft.getId())
.set(PddGoodsPublishDraftPO::getStatus, PddGoodsPublishDraftPO.STATUS_PUBLISHED)
.set(PddGoodsPublishDraftPO::getLastError, null)
.set(PddGoodsPublishDraftPO::getPddGoodsCommitId, goodsCommitId)
.set(PddGoodsPublishDraftPO::getPddGoodsId, goodsId)
.set(PddGoodsPublishDraftPO::getRejectComment, null)
.set(PddGoodsPublishDraftPO::getRejectTime, null));
.set(PddGoodsPublishDraftPO::getStatus, PddGoodsPublishDraftPO.STATUS_READY)
.set(PddGoodsPublishDraftPO::getLastError, null));
Long shopId = pddShopId != null ? pddShopId : draft.getPddShopId();
draftShopStatService.incrPublishSuccess(companyId, draft.getId(), shopId);
}
private BusinessException wrapPop(String prefix, Exception e) {
@@ -5,17 +5,12 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.goods.publishdesk.service.PddGoodsPublishDeskService;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.dispatch.PddMsgHandler;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.inbox.model.po.PddMsgInboxPO;
import top.xyquan.ecomdmbbackend.modules.channel.pdd.msg.support.PddMsgContentSupport;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
/**
* 商品审核驳回:仅当系统草稿挂有对应 commitId/goodsId 时回写驳回状态;纯平台草稿忽略。
* 商品审核驳回:系统草稿已不再绑定平台 commit/goods,本处理器仅记日志(驳回看消息中心/渠道商品)。
*/
@Slf4j
@Component
@@ -24,7 +19,6 @@ public class PddGoodsCheckRejectMsgHandler implements PddMsgHandler {
public static final String TYPE = "pdd_goods_GoodsCheckReject";
private final PddGoodsPublishDeskService deskService;
private final PddMsgContentSupport contentSupport;
@Override
@@ -34,9 +28,6 @@ public class PddGoodsCheckRejectMsgHandler implements PddMsgHandler {
@Override
public void handle(PddMsgInboxPO inbox) {
if (inbox.getCompanyId() == null || inbox.getChannelShopId() == null) {
throw new IllegalStateException("驳回消息未绑定店铺 inboxId=" + inbox.getId());
}
JsonNode content = contentSupport.parseContent(inbox.getContentJson());
Long commitId = readLong(content, "goods_commit_id");
Long goodsId = readLong(content, "goods_id");
@@ -47,29 +38,13 @@ public class PddGoodsCheckRejectMsgHandler implements PddMsgHandler {
// ignore
}
}
// 文档类型写成 LONG,示例为文案;按字符串兼容
String rejectComment = readText(content, "reject_comment");
Long rejectTimeMs = readLong(content, "reject_time");
LocalDateTime rejectTime = null;
if (rejectTimeMs != null && rejectTimeMs > 0) {
rejectTime = LocalDateTime.ofInstant(
Instant.ofEpochMilli(rejectTimeMs), ZoneId.systemDefault());
}
boolean updated = deskService.applyGoodsCheckReject(
inbox.getCompanyId(),
log.info(
"拼多多商品审核驳回(不回写系统草稿) shopId={} commitId={} goodsId={} comment={}",
inbox.getChannelShopId(),
commitId,
goodsId,
rejectComment,
rejectTime);
if (updated) {
log.info("系统草稿已标记审核驳回 shopId={} commitId={} goodsId={}",
inbox.getChannelShopId(), commitId, goodsId);
} else {
log.info("驳回消息无匹配系统草稿,忽略 shopId={} commitId={} goodsId={}",
inbox.getChannelShopId(), commitId, goodsId);
}
rejectComment);
}
private static Long readLong(JsonNode content, String field) {
@@ -105,7 +80,6 @@ public class PddGoodsCheckRejectMsgHandler implements PddMsgHandler {
if (node.isTextual()) {
return node.asText();
}
// 兼容错误标注为数字的驳回原因
return node.asText(null);
}
}