mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
feat: 增加自动建品类
This commit is contained in:
@@ -18,6 +18,7 @@ CREATE TABLE `ec_category` (
|
||||
`create_by` bigint UNSIGNED NULL DEFAULT NULL COMMENT '创建人ID',
|
||||
`update_by` bigint UNSIGNED NULL DEFAULT NULL COMMENT '更新人ID',
|
||||
PRIMARY KEY (`id`) USING BTREE,
|
||||
UNIQUE INDEX `uk_ec_category_company_name`(`company_id` ASC, `name` ASC) USING BTREE COMMENT '同公司类目名称唯一',
|
||||
INDEX `idx_company_id`(`company_id` ASC) USING BTREE,
|
||||
INDEX `idx_parent_id`(`parent_id` ASC) USING BTREE,
|
||||
INDEX `idx_sort_order`(`sort_order` ASC) USING BTREE,
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
-- ec_category:同公司类目名称唯一(自动建品按叶子类目名复用)
|
||||
-- 若已有同公司重名类目,需先手工去重后再执行本脚本。
|
||||
|
||||
DELIMITER $$
|
||||
|
||||
DROP PROCEDURE IF EXISTS `tmp_migrate_ec_category_company_name_uk`$$
|
||||
CREATE PROCEDURE `tmp_migrate_ec_category_company_name_uk`()
|
||||
BEGIN
|
||||
DECLARE db_name VARCHAR(64) DEFAULT DATABASE();
|
||||
DECLARE dup_cnt INT DEFAULT 0;
|
||||
|
||||
SELECT COUNT(*) INTO dup_cnt
|
||||
FROM (
|
||||
SELECT `company_id`, `name`
|
||||
FROM `ec_category`
|
||||
GROUP BY `company_id`, `name`
|
||||
HAVING COUNT(*) > 1
|
||||
) d;
|
||||
|
||||
IF dup_cnt > 0 THEN
|
||||
SIGNAL SQLSTATE '45000'
|
||||
SET MESSAGE_TEXT = 'ec_category 存在同公司重名类目,请先去重后再加 uk_ec_category_company_name';
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.statistics
|
||||
WHERE table_schema = db_name AND table_name = 'ec_category'
|
||||
AND index_name = 'uk_ec_category_company_name'
|
||||
) THEN
|
||||
ALTER TABLE `ec_category`
|
||||
ADD UNIQUE INDEX `uk_ec_category_company_name` (`company_id`, `name`)
|
||||
COMMENT '同公司类目名称唯一';
|
||||
END IF;
|
||||
END$$
|
||||
|
||||
DELIMITER ;
|
||||
|
||||
CALL `tmp_migrate_ec_category_company_name_uk`();
|
||||
DROP PROCEDURE IF EXISTS `tmp_migrate_ec_category_company_name_uk`;
|
||||
+99
-6
@@ -8,9 +8,13 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.convert.messaging.PddProductAutoCreateMessage;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.mapper.PddOrderItemMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.model.po.PddOrderItemPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.sync.model.PddOrderSyncShopContext;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.order.sync.service.PddOrderSyncContextResolver;
|
||||
import top.xyquan.ecomdmbbackend.modules.channel.pdd.pop.goods.PddGoodsPopApiClient;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.category.model.po.CategoryPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.category.service.CategoryService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductSkuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductService;
|
||||
@@ -18,6 +22,7 @@ import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuService;
|
||||
|
||||
/**
|
||||
* 按平台 goodsId 拉详情,自动创建内部商品 + 全部 SKU(已存在则跳过),并回绑 orphan 明细。
|
||||
* 类目:PDD 一级/叶子 → 内部一级/二级;按公司内叶子类目名复用,不重复创建。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -26,6 +31,8 @@ public class PddProductAutoCreateService {
|
||||
|
||||
private final PddOrderSyncContextResolver contextResolver;
|
||||
private final PddGoodsPopApiClient goodsPopApiClient;
|
||||
private final PddOrderItemMapper pddOrderItemMapper;
|
||||
private final CategoryService categoryService;
|
||||
private final ProductService productService;
|
||||
private final ProductSkuService productSkuService;
|
||||
|
||||
@@ -53,7 +60,9 @@ public class PddProductAutoCreateService {
|
||||
? detail.goodsName().trim()
|
||||
: productCode;
|
||||
|
||||
ProductPO product = ensureProduct(message.getCompanyId(), productCode, productName, detail.imageUrl());
|
||||
String imageUrl = resolveProductImageUrl(detail, message.getCompanyId(), message.getGoodsId());
|
||||
Long categoryId = ensureCategoryId(message.getCompanyId(), detail.goodsCategory(), detail.lastCategory());
|
||||
ProductPO product = ensureProduct(message.getCompanyId(), productCode, productName, imageUrl, categoryId);
|
||||
if (product == null || product.getId() == null) {
|
||||
return;
|
||||
}
|
||||
@@ -79,22 +88,106 @@ public class PddProductAutoCreateService {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
if (createSku(message.getCompanyId(), product.getId(), skuCode, skuDetail, detail.imageUrl())) {
|
||||
if (createSku(message.getCompanyId(), product.getId(), skuCode, skuDetail, imageUrl)) {
|
||||
created++;
|
||||
} else {
|
||||
skipped++;
|
||||
}
|
||||
}
|
||||
log.info("自动建品完成 companyId={} goodsId={} productId={} createdSku={} skipped={}",
|
||||
message.getCompanyId(), detail.goodsId(), product.getId(), created, skipped);
|
||||
log.info("自动建品完成 companyId={} goodsId={} productId={} categoryId={} createdSku={} skipped={} hasImage={}",
|
||||
message.getCompanyId(), detail.goodsId(), product.getId(), categoryId, created, skipped,
|
||||
StringUtils.hasText(imageUrl));
|
||||
}
|
||||
|
||||
private ProductPO ensureProduct(Long companyId, String productCode, String productName, String imageUrl) {
|
||||
/**
|
||||
* 主图优先级:详情 image_url → 首个 sku_img → 订单明细 goods_img。
|
||||
*/
|
||||
private String resolveProductImageUrl(PddGoodsPopApiClient.GoodsDetail detail,
|
||||
Long companyId, Long goodsId) {
|
||||
String fromDetail = detail.resolveImageUrl();
|
||||
if (StringUtils.hasText(fromDetail)) {
|
||||
return fromDetail;
|
||||
}
|
||||
if (companyId == null || goodsId == null) {
|
||||
return null;
|
||||
}
|
||||
PddOrderItemPO sample = pddOrderItemMapper.selectOne(new LambdaQueryWrapper<PddOrderItemPO>()
|
||||
.eq(PddOrderItemPO::getCompanyId, companyId)
|
||||
.eq(PddOrderItemPO::getGoodsId, String.valueOf(goodsId))
|
||||
.isNotNull(PddOrderItemPO::getGoodsImg)
|
||||
.ne(PddOrderItemPO::getGoodsImg, "")
|
||||
.last("LIMIT 1"));
|
||||
if (sample != null && StringUtils.hasText(sample.getGoodsImg())) {
|
||||
return sample.getGoodsImg().trim();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按叶子类目名判断是否已有内部类目;有则直接用。
|
||||
* 无则:一级类目(goods_category) → parent_id=null;叶子(last_category) → 挂在一级下。
|
||||
* 商品绑定叶子类目 id(仅有一级时绑一级)。
|
||||
*/
|
||||
private Long ensureCategoryId(Long companyId, String goodsCategory, String lastCategory) {
|
||||
String leaf = StringUtils.hasText(lastCategory) ? lastCategory.trim() : null;
|
||||
String level1 = StringUtils.hasText(goodsCategory) ? goodsCategory.trim() : null;
|
||||
if (leaf == null && level1 == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (leaf != null) {
|
||||
CategoryPO existingLeaf = categoryService.getByCompanyIdAndName(companyId, leaf);
|
||||
if (existingLeaf != null) {
|
||||
return existingLeaf.getId();
|
||||
}
|
||||
Long parentId = null;
|
||||
if (level1 != null && !level1.equals(leaf)) {
|
||||
parentId = ensureNamedCategory(companyId, null, level1);
|
||||
}
|
||||
return ensureNamedCategory(companyId, parentId, leaf);
|
||||
}
|
||||
|
||||
return ensureNamedCategory(companyId, null, level1);
|
||||
}
|
||||
|
||||
/** 按公司+名称查找或创建类目(公司内名称唯一)。 */
|
||||
private Long ensureNamedCategory(Long companyId, Long parentId, String name) {
|
||||
CategoryPO existing = categoryService.getByCompanyIdAndName(companyId, name);
|
||||
if (existing != null) {
|
||||
return existing.getId();
|
||||
}
|
||||
CategoryPO category = new CategoryPO();
|
||||
category.setCompanyId(companyId);
|
||||
category.setParentId(parentId);
|
||||
category.setName(name);
|
||||
category.setSortOrder(0);
|
||||
try {
|
||||
categoryService.save(category);
|
||||
return category.getId();
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
CategoryPO again = categoryService.getByCompanyIdAndName(companyId, name);
|
||||
if (again != null) {
|
||||
return again.getId();
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
private ProductPO ensureProduct(Long companyId, String productCode, String productName,
|
||||
String imageUrl, Long categoryId) {
|
||||
ProductPO existing = productService.getByCompanyIdAndProductCode(companyId, productCode);
|
||||
if (existing != null) {
|
||||
boolean dirty = false;
|
||||
if (!StringUtils.hasText(existing.getMainImageUrl()) && StringUtils.hasText(imageUrl)
|
||||
&& existing.getMainImageId() == null) {
|
||||
existing.setMainImageUrl(imageUrl.trim());
|
||||
dirty = true;
|
||||
}
|
||||
if (existing.getCategoryId() == null && categoryId != null) {
|
||||
existing.setCategoryId(categoryId);
|
||||
dirty = true;
|
||||
}
|
||||
if (dirty) {
|
||||
productService.updateById(existing);
|
||||
}
|
||||
return existing;
|
||||
@@ -103,6 +196,7 @@ public class PddProductAutoCreateService {
|
||||
product.setCompanyId(companyId);
|
||||
product.setProductCode(productCode);
|
||||
product.setName(productName);
|
||||
product.setCategoryId(categoryId);
|
||||
product.setMainImageUrl(StringUtils.hasText(imageUrl) ? imageUrl.trim() : null);
|
||||
try {
|
||||
productService.save(product);
|
||||
@@ -130,7 +224,6 @@ public class PddProductAutoCreateService {
|
||||
sku.setSortOrder(0);
|
||||
sku.setRemark("channel_auto:pdd");
|
||||
try {
|
||||
// save 会触发 orphan 回绑
|
||||
return productSkuService.save(sku);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
log.info("自动建品 SKU 并发冲突,跳过 skuCode={}", skuCode);
|
||||
|
||||
+26
-1
@@ -60,10 +60,18 @@ public class PddGoodsPopApiClient {
|
||||
info.getGoodsId(),
|
||||
info.getGoodsName(),
|
||||
info.getImageUrl(),
|
||||
info.getGoodsCategory(),
|
||||
info.getLastCategory(),
|
||||
Collections.unmodifiableList(skus));
|
||||
}
|
||||
|
||||
public record GoodsDetail(Long goodsId, String goodsName, String imageUrl, List<SkuDetail> skuList) {
|
||||
public record GoodsDetail(
|
||||
Long goodsId,
|
||||
String goodsName,
|
||||
String imageUrl,
|
||||
String goodsCategory,
|
||||
String lastCategory,
|
||||
List<SkuDetail> skuList) {
|
||||
public String resolveProductCode() {
|
||||
if (skuList != null) {
|
||||
for (SkuDetail sku : skuList) {
|
||||
@@ -74,6 +82,23 @@ public class PddGoodsPopApiClient {
|
||||
}
|
||||
return goodsId != null ? "PDD:" + goodsId : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主图:优先 goods.image_url;接口常返回空串,则回退首个非空 sku_img。
|
||||
*/
|
||||
public String resolveImageUrl() {
|
||||
if (StringUtils.hasText(imageUrl)) {
|
||||
return imageUrl.trim();
|
||||
}
|
||||
if (skuList != null) {
|
||||
for (SkuDetail sku : skuList) {
|
||||
if (sku != null && StringUtils.hasText(sku.skuImg())) {
|
||||
return sku.skuImg().trim();
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public record SkuDetail(Long skuId, String spec, String outerId, String outerGoodsId, String skuImg) {
|
||||
|
||||
+3
@@ -61,4 +61,7 @@ public interface CategoryService extends IService<CategoryPO> {
|
||||
* @return 扁平类目列表
|
||||
*/
|
||||
List<CategoryVO> getCategoryList();
|
||||
|
||||
/** 按公司 + 名称精确查找(公司内名称唯一)。 */
|
||||
CategoryPO getByCompanyIdAndName(Long companyId, String name);
|
||||
}
|
||||
|
||||
+15
-6
@@ -49,13 +49,12 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryPO>
|
||||
}
|
||||
}
|
||||
|
||||
// 验证类目名称在同级下不重复
|
||||
// 验证类目名称在公司内不重复
|
||||
LambdaQueryWrapper<CategoryPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CategoryPO::getCompanyId, companyId)
|
||||
.eq(CategoryPO::getParentId, createCategoryDTO.getParentId())
|
||||
.eq(CategoryPO::getName, createCategoryDTO.getName());
|
||||
if (this.count(wrapper) > 0) {
|
||||
throw new BusinessException("同级类目名称已存在");
|
||||
throw new BusinessException("类目名称已存在");
|
||||
}
|
||||
|
||||
CategoryPO category = new CategoryPO();
|
||||
@@ -96,14 +95,13 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryPO>
|
||||
}
|
||||
}
|
||||
|
||||
// 验证同级下名称不重复(排除自己)
|
||||
// 验证公司内名称不重复(排除自己)
|
||||
LambdaQueryWrapper<CategoryPO> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(CategoryPO::getCompanyId, companyId)
|
||||
.eq(CategoryPO::getParentId, updateCategoryDTO.getParentId())
|
||||
.eq(CategoryPO::getName, updateCategoryDTO.getName())
|
||||
.ne(CategoryPO::getId, id);
|
||||
if (this.count(wrapper) > 0) {
|
||||
throw new BusinessException("同级类目名称已存在");
|
||||
throw new BusinessException("类目名称已存在");
|
||||
}
|
||||
|
||||
existing.setParentId(updateCategoryDTO.getParentId());
|
||||
@@ -221,6 +219,17 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryPO>
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public CategoryPO getByCompanyIdAndName(Long companyId, String name) {
|
||||
if (companyId == null || name == null || name.isBlank()) {
|
||||
return null;
|
||||
}
|
||||
return this.getOne(new LambdaQueryWrapper<CategoryPO>()
|
||||
.eq(CategoryPO::getCompanyId, companyId)
|
||||
.eq(CategoryPO::getName, name.trim())
|
||||
.last("LIMIT 1"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 targetId 是否是 ancestorId 的后代
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user