mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-23 23:19:19 +08:00
fix: 广告费转换按内部商品汇总渠道日表金额,避免多 goods 覆盖丢金额
This commit is contained in:
+129
-49
@@ -21,11 +21,20 @@ import top.xyquan.ecomdmbbackend.modules.ec.shop.adfee.mapper.AdFeeMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.shop.adfee.model.po.AdFeePO;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 渠道日表 → 内部 ec_product_ad_fee。
|
||||
* <p>
|
||||
* 内部表按 productId+feeDate 唯一;多个渠道 goods 可绑同一内部商品,
|
||||
* 故转换时按「内部商品 × 日」对渠道日表金额求和后再 upsert,禁止逐条覆盖。
|
||||
*/
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -55,7 +64,7 @@ public class PddGoodsAdFeeConvertService {
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换任务中全部成功明细对应的日表行,返回汇总。
|
||||
* 转换任务中全部成功明细:先收集涉及的 (productId, feeDate),再按内部商品汇总渠道日表金额写入。
|
||||
*/
|
||||
public PddAdFeeConvertBatchResult convertByTaskId(Long taskId) {
|
||||
if (taskId == null) {
|
||||
@@ -66,21 +75,33 @@ public class PddGoodsAdFeeConvertService {
|
||||
.eq(PddGoodsAdFeeImportItemPO::getTaskId, taskId)
|
||||
.eq(PddGoodsAdFeeImportItemPO::getStatus, PddGoodsAdFeeImportItemPO.STATUS_SUCCESS)
|
||||
.isNotNull(PddGoodsAdFeeImportItemPO::getGoodsRowId));
|
||||
int success = 0;
|
||||
|
||||
// productId|feeDate → 任一成功明细对应的公司 / 渠道店(用于解析内部店)
|
||||
Set<String> productDateKeys = new LinkedHashSet<>();
|
||||
Map<String, ConvertTarget> targets = new HashMap<>();
|
||||
int skipped = 0;
|
||||
int failed = 0;
|
||||
|
||||
for (PddGoodsAdFeeImportItemPO item : items) {
|
||||
PddGoodsAdFeeDailyPO daily = channelAdFeeMapper.selectOne(
|
||||
new LambdaQueryWrapper<PddGoodsAdFeeDailyPO>()
|
||||
.eq(PddGoodsAdFeeDailyPO::getGoodsRowId, item.getGoodsRowId())
|
||||
.eq(PddGoodsAdFeeDailyPO::getFeeDate, item.getFeeDate())
|
||||
.last("LIMIT 1"));
|
||||
if (daily == null) {
|
||||
PddGoodsPO goods = pddGoodsMapper.selectById(item.getGoodsRowId());
|
||||
if (goods == null || goods.getProductId() == null || item.getFeeDate() == null) {
|
||||
skipped++;
|
||||
continue;
|
||||
}
|
||||
String key = productDateKey(goods.getProductId(), item.getFeeDate());
|
||||
if (productDateKeys.add(key)) {
|
||||
targets.put(key, new ConvertTarget(
|
||||
item.getCompanyId(),
|
||||
goods.getProductId(),
|
||||
item.getFeeDate(),
|
||||
goods.getShopId()));
|
||||
}
|
||||
}
|
||||
|
||||
int success = 0;
|
||||
int failed = 0;
|
||||
for (ConvertTarget target : targets.values()) {
|
||||
PddAdFeeConvertOutcome outcome = transactionTemplate.execute(
|
||||
status -> convertDaily(daily));
|
||||
status -> convertProductDate(target));
|
||||
if (outcome == PddAdFeeConvertOutcome.CONVERTED) {
|
||||
success++;
|
||||
} else if (outcome == PddAdFeeConvertOutcome.FAILED) {
|
||||
@@ -106,41 +127,89 @@ public class PddGoodsAdFeeConvertService {
|
||||
log.warn("渠道广告费不存在,跳过转换 id={}", channelAdFeeId);
|
||||
return;
|
||||
}
|
||||
convertDaily(channelFee);
|
||||
}
|
||||
|
||||
private PddAdFeeConvertOutcome convertDaily(PddGoodsAdFeeDailyPO channelFee) {
|
||||
if (channelFee.getFeeAmount() == null
|
||||
|| channelFee.getFeeAmount().compareTo(BigDecimal.ZERO) <= 0) {
|
||||
log.warn("渠道广告费金额无效,跳过转换 id={} amount={}",
|
||||
channelFee.getId(), channelFee.getFeeAmount());
|
||||
return PddAdFeeConvertOutcome.SKIPPED;
|
||||
}
|
||||
if (channelFee.getGoodsRowId() == null) {
|
||||
log.warn("渠道广告费缺少 goods_row_id,跳过转换 id={}", channelFee.getId());
|
||||
return PddAdFeeConvertOutcome.SKIPPED;
|
||||
if (channelFee.getGoodsRowId() == null || channelFee.getFeeDate() == null) {
|
||||
return;
|
||||
}
|
||||
PddGoodsPO goods = pddGoodsMapper.selectById(channelFee.getGoodsRowId());
|
||||
if (goods == null || goods.getProductId() == null) {
|
||||
log.warn("渠道商品未绑定内部商品,跳过转换 dailyId={} goodsRowId={}",
|
||||
channelFee.getId(), channelFee.getGoodsRowId());
|
||||
return PddAdFeeConvertOutcome.SKIPPED;
|
||||
}
|
||||
Long internalShopId = resolveInternalShopId(channelFee.getCompanyId(), goods.getShopId());
|
||||
if (internalShopId == null) {
|
||||
log.warn("未找到绑定该渠道店的内部店铺,跳过转换 dailyId={} channelShopId={}",
|
||||
channelFee.getId(), goods.getShopId());
|
||||
return PddAdFeeConvertOutcome.SKIPPED;
|
||||
return;
|
||||
}
|
||||
convertProductDate(new ConvertTarget(
|
||||
channelFee.getCompanyId(),
|
||||
goods.getProductId(),
|
||||
channelFee.getFeeDate(),
|
||||
goods.getShopId()));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将「内部商品 × 自然日」下全部已绑定渠道日表金额求和,写入 ec_product_ad_fee。
|
||||
*/
|
||||
private PddAdFeeConvertOutcome convertProductDate(ConvertTarget target) {
|
||||
try {
|
||||
boolean changed = upsertInternalAdFee(channelFee, goods, goods.getProductId(), internalShopId);
|
||||
Long internalShopId = resolveInternalShopId(target.companyId(), target.channelShopId());
|
||||
if (internalShopId == null) {
|
||||
log.warn("未找到绑定该渠道店的内部店铺,跳过转换 productId={} feeDate={} channelShopId={}",
|
||||
target.productId(), target.feeDate(), target.channelShopId());
|
||||
return PddAdFeeConvertOutcome.SKIPPED;
|
||||
}
|
||||
|
||||
BigDecimal total = sumChannelFeesForProductDate(
|
||||
target.companyId(), target.productId(), target.feeDate());
|
||||
if (total.compareTo(BigDecimal.ZERO) <= 0) {
|
||||
log.warn("内部商品当日渠道广告费合计无效,跳过 productId={} feeDate={} total={}",
|
||||
target.productId(), target.feeDate(), total);
|
||||
return PddAdFeeConvertOutcome.SKIPPED;
|
||||
}
|
||||
|
||||
boolean changed = upsertInternalAdFee(
|
||||
target.companyId(),
|
||||
internalShopId,
|
||||
target.productId(),
|
||||
target.feeDate(),
|
||||
total);
|
||||
return changed ? PddAdFeeConvertOutcome.CONVERTED : PddAdFeeConvertOutcome.SKIPPED;
|
||||
} catch (Exception e) {
|
||||
log.error("转换内部广告费失败 dailyId={}", channelFee.getId(), e);
|
||||
log.error("转换内部广告费失败 productId={} feeDate={}",
|
||||
target.productId(), target.feeDate(), e);
|
||||
return PddAdFeeConvertOutcome.FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 汇总:所有 product_id 指向该内部商品、且 fee_date 相同的渠道日表金额。
|
||||
*/
|
||||
private BigDecimal sumChannelFeesForProductDate(Long companyId, Long productId, LocalDate feeDate) {
|
||||
List<PddGoodsPO> goodsList = pddGoodsMapper.selectList(new LambdaQueryWrapper<PddGoodsPO>()
|
||||
.eq(PddGoodsPO::getCompanyId, companyId)
|
||||
.eq(PddGoodsPO::getProductId, productId));
|
||||
if (goodsList.isEmpty()) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
Set<Long> goodsRowIds = new HashSet<>();
|
||||
for (PddGoodsPO g : goodsList) {
|
||||
if (g.getId() != null) {
|
||||
goodsRowIds.add(g.getId());
|
||||
}
|
||||
}
|
||||
if (goodsRowIds.isEmpty()) {
|
||||
return BigDecimal.ZERO;
|
||||
}
|
||||
List<PddGoodsAdFeeDailyPO> dailies = channelAdFeeMapper.selectList(
|
||||
new LambdaQueryWrapper<PddGoodsAdFeeDailyPO>()
|
||||
.eq(PddGoodsAdFeeDailyPO::getCompanyId, companyId)
|
||||
.eq(PddGoodsAdFeeDailyPO::getFeeDate, feeDate)
|
||||
.in(PddGoodsAdFeeDailyPO::getGoodsRowId, goodsRowIds));
|
||||
BigDecimal sum = BigDecimal.ZERO;
|
||||
for (PddGoodsAdFeeDailyPO d : dailies) {
|
||||
if (d.getFeeAmount() != null && d.getFeeAmount().compareTo(BigDecimal.ZERO) > 0) {
|
||||
sum = sum.add(d.getFeeAmount());
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
private Long resolveInternalShopId(Long companyId, Long channelShopId) {
|
||||
PddShopPO pddShop = pddShopMapper.selectOne(new LambdaQueryWrapper<PddShopPO>()
|
||||
.eq(PddShopPO::getId, channelShopId)
|
||||
@@ -150,49 +219,60 @@ public class PddGoodsAdFeeConvertService {
|
||||
}
|
||||
|
||||
/** @return true 表示写入或更新 */
|
||||
private boolean upsertInternalAdFee(PddGoodsAdFeeDailyPO channelFee,
|
||||
PddGoodsPO goods,
|
||||
private boolean upsertInternalAdFee(Long companyId,
|
||||
Long internalShopId,
|
||||
Long productId,
|
||||
Long internalShopId) {
|
||||
LocalDate feeDate,
|
||||
BigDecimal feeAmount) {
|
||||
AdFeePO existing = adFeeMapper.selectOne(new LambdaQueryWrapper<AdFeePO>()
|
||||
.eq(AdFeePO::getProductId, productId)
|
||||
.eq(AdFeePO::getFeeDate, channelFee.getFeeDate())
|
||||
.eq(AdFeePO::getFeeDate, feeDate)
|
||||
.last("LIMIT 1"));
|
||||
|
||||
String goodsId = goods.getGoodsId();
|
||||
if (existing == null) {
|
||||
AdFeePO po = new AdFeePO();
|
||||
po.setCompanyId(channelFee.getCompanyId());
|
||||
po.setCompanyId(companyId);
|
||||
po.setShopId(internalShopId);
|
||||
po.setProductId(productId);
|
||||
po.setCampaignName(CAMPAIGN_NAME);
|
||||
po.setFeeAmount(channelFee.getFeeAmount());
|
||||
po.setFeeDate(channelFee.getFeeDate());
|
||||
po.setRemark("channel_pdd_goods_ad_fee_daily#" + channelFee.getId()
|
||||
+ " goods_id=" + goodsId);
|
||||
po.setFeeAmount(feeAmount);
|
||||
po.setFeeDate(feeDate);
|
||||
po.setRemark("channel_pdd_goods_ad_fee_daily 汇总 product_id=" + productId);
|
||||
adFeeMapper.insert(po);
|
||||
log.info("已转换新增内部广告费 productId={} feeDate={} amount={} channelId={}",
|
||||
productId, channelFee.getFeeDate(), channelFee.getFeeAmount(), channelFee.getId());
|
||||
log.info("已转换新增内部广告费 productId={} feeDate={} amount={}",
|
||||
productId, feeDate, feeAmount);
|
||||
return true;
|
||||
}
|
||||
|
||||
boolean amountChanged = existing.getFeeAmount() == null
|
||||
|| existing.getFeeAmount().compareTo(channelFee.getFeeAmount()) != 0;
|
||||
|| existing.getFeeAmount().compareTo(feeAmount) != 0;
|
||||
boolean shopChanged = !Objects.equals(existing.getShopId(), internalShopId);
|
||||
if (!amountChanged && !shopChanged) {
|
||||
log.debug("内部广告费无变化,跳过更新 productId={} feeDate={}",
|
||||
productId, channelFee.getFeeDate());
|
||||
log.debug("内部广告费无变化,跳过更新 productId={} feeDate={} amount={}",
|
||||
productId, feeDate, feeAmount);
|
||||
return false;
|
||||
}
|
||||
|
||||
existing.setShopId(internalShopId);
|
||||
existing.setFeeAmount(channelFee.getFeeAmount());
|
||||
existing.setFeeAmount(feeAmount);
|
||||
if (!StringUtils.hasText(existing.getCampaignName())) {
|
||||
existing.setCampaignName(CAMPAIGN_NAME);
|
||||
}
|
||||
existing.setRemark("channel_pdd_goods_ad_fee_daily 汇总 product_id=" + productId);
|
||||
adFeeMapper.updateById(existing);
|
||||
log.info("已转换更新内部广告费 id={} productId={} feeDate={} amount={}",
|
||||
existing.getId(), productId, channelFee.getFeeDate(), channelFee.getFeeAmount());
|
||||
existing.getId(), productId, feeDate, feeAmount);
|
||||
return true;
|
||||
}
|
||||
|
||||
private static String productDateKey(Long productId, LocalDate feeDate) {
|
||||
return productId + "|" + feeDate;
|
||||
}
|
||||
|
||||
private record ConvertTarget(
|
||||
Long companyId,
|
||||
Long productId,
|
||||
LocalDate feeDate,
|
||||
Long channelShopId) {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user