package cn.qihangerp.service.external.impl;

import cn.qihangerp.model.entity.OGoods;
import cn.qihangerp.model.request.ExternalPddSkuCanonicalSyncRequest;
import cn.qihangerp.model.vo.ExternalPddSkuCanonicalSyncResultVo;
import cn.qihangerp.module.service.OGoodsService;
import cn.qihangerp.service.external.ExternalPddSkuCanonicalSyncAppService;
import cn.qihangerp.service.external.pdd.OGoodsPddMappingPersistence;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
/**
 * @author guochengyu
 */
@Service
@RequiredArgsConstructor
public class ExternalPddSkuCanonicalSyncAppServiceImpl implements ExternalPddSkuCanonicalSyncAppService {

    private final OGoodsService goodsService;
    private final OGoodsPddMappingPersistence oGoodsPddMappingPersistence;

    @Override
    public ExternalPddSkuCanonicalSyncResultVo syncFromGoodsDetailPop(ExternalPddSkuCanonicalSyncRequest req) {
        OGoods g = goodsService.getById(req.getErpGoodsId());
        if (g == null) {
            return ExternalPddSkuCanonicalSyncResultVo.builder()
                    .updatedSkuRows(0)
                    .message("o_goods 不存在: " + req.getErpGoodsId())
                    .build();
        }
        int n = oGoodsPddMappingPersistence.mergePddSkuCanonicalFromGoodsDetailPop(
                req.getErpGoodsId(), req.getGoodsDetailPopRaw().trim());
        return ExternalPddSkuCanonicalSyncResultVo.builder()
                .updatedSkuRows(n)
                .message(n > 0 ? "已合并 pddSkuId" : "无匹配 outerErpSkuId 或未解析到 sku_list")
                .build();
    }
}
