feat: SKU 成本支持删除段与历史编辑模态,调价/删除均进入待同步影响面

This commit is contained in:
2026-08-31 23:48:32 +08:00
parent d29e23a4f2
commit a3ac5b1e8d
6 changed files with 403 additions and 113 deletions
@@ -77,6 +77,28 @@ public class ProductSkuCostController {
return ApiResponse.success(changed ? "调价成功,已记入待同步影响面" : "成本未变化", vo);
}
/**
* 删除成本历史段,并记入待同步影响面(与调价同一条回刷链路)。
*/
@DeleteMapping("/record/{id}")
@RequirePermission(value = "ec:product:update", description = "删除SKU成本段", allowSuper = true)
public ApiResponse<ProductSkuVO> deleteCostRecord(@PathVariable Long id) {
Long companyId = currentUserContext.getCurrentCompanyId();
Long userId = currentUserContext.getCurrentUserId();
var impactSnap = productSkuCostService.deleteCostSegment(id, companyId);
impactService.recordAfterAdjust(
companyId,
impactSnap.getSkuId(),
impactSnap.getEffectiveFrom(),
impactSnap.getProductCost() != null ? impactSnap.getProductCost() : BigDecimal.ZERO,
impactSnap.getShippingCost() != null ? impactSnap.getShippingCost() : BigDecimal.ZERO,
impactSnap.getOtherCost() != null ? impactSnap.getOtherCost() : BigDecimal.ZERO,
userId);
ProductSkuVO vo = productSkuService.toVo(productSkuService.getById(impactSnap.getSkuId()));
vo.setCostChanged(true);
return ApiResponse.success("删除成功,已记入待同步影响面", vo);
}
/** @deprecated 请使用 POST /api/ec/cost-sync/apply-pending */
@PostMapping("/sync-to-orders")
@RequirePermission(value = "ec:product:update", description = "同步成本到订单", allowSuper = true)
@@ -50,4 +50,11 @@ public interface ProductSkuCostService {
/** 业务日成本:effective_from ≤ onDate 的最新一条 */
ProductSkuCostPO getCostAsOf(Long skuId, LocalDate onDate);
/**
* 删除一条成本历史段;删后该生效日起回落到前一段(或无成本)。
*
* @return 用于记 PENDING 的快照(effectiveFrom=被删段起始日,金额=回落后成本)
*/
ProductSkuCostPO deleteCostSegment(Long costId, Long companyId);
}
@@ -3,6 +3,7 @@ package top.xyquan.ecomdmbbackend.modules.ec.product.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
import top.xyquan.ecomdmbbackend.modules.ec.product.mapper.ProductSkuCostMapper;
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductSkuCostPO;
@@ -157,6 +158,36 @@ public class ProductSkuCostServiceImpl extends ServiceImpl<ProductSkuCostMapper,
return getBaselineCost(skuId, onDate);
}
@Override
@Transactional(rollbackFor = Exception.class)
public ProductSkuCostPO deleteCostSegment(Long costId, Long companyId) {
if (costId == null || companyId == null) {
throw new BusinessException("成本记录/公司不能为空");
}
ProductSkuCostPO row = getById(costId);
if (row == null || !companyId.equals(row.getCompanyId())) {
throw new BusinessException("成本记录不存在");
}
Long skuId = row.getSkuId();
LocalDate effectiveFrom = row.getEffectiveFrom();
if (skuId == null || effectiveFrom == null) {
throw new BusinessException("成本记录数据不完整");
}
removeById(costId);
// 回落后的成本(供 PENDING 账本与一键回刷使用)
ProductSkuCostPO fallback = getBaselineCost(skuId, effectiveFrom);
ProductSkuCostPO impactSnap = new ProductSkuCostPO();
impactSnap.setCompanyId(companyId);
impactSnap.setSkuId(skuId);
impactSnap.setEffectiveFrom(effectiveFrom);
impactSnap.setProductCost(fallback != null ? nz(fallback.getProductCost()) : BigDecimal.ZERO);
impactSnap.setShippingCost(fallback != null ? nz(fallback.getShippingCost()) : BigDecimal.ZERO);
impactSnap.setOtherCost(fallback != null ? nz(fallback.getOtherCost()) : BigDecimal.ZERO);
return impactSnap;
}
/** 取 effective_from <= 指定日 的最新一条(含当日) */
private ProductSkuCostPO getBaselineCost(Long skuId, LocalDate onOrBefore) {
LambdaQueryWrapper<ProductSkuCostPO> w = new LambdaQueryWrapper<>();