mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:19:19 +08:00
feat: 完善sku成本
This commit is contained in:
-81
@@ -8,21 +8,16 @@ import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.dto.BatchSyncCostDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.dto.CreateProductSkuDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.dto.ProductSkuFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.dto.SyncCostDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.dto.UpdateProductSkuDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductSkuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.vo.ProductSkuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.order.service.OrderService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.vo.ProductVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuCostService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuService.ProductSkuQueryDTO;
|
||||
|
||||
@@ -35,17 +30,11 @@ import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuService.Pr
|
||||
public class ProductSkuController {
|
||||
|
||||
private final ProductSkuService productSkuService;
|
||||
private final ProductSkuCostService productSkuCostService;
|
||||
private final OrderService orderService;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public ProductSkuController(ProductSkuService productSkuService,
|
||||
ProductSkuCostService productSkuCostService,
|
||||
OrderService orderService,
|
||||
CurrentUserContext currentUserContext) {
|
||||
this.productSkuService = productSkuService;
|
||||
this.productSkuCostService = productSkuCostService;
|
||||
this.orderService = orderService;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@@ -113,74 +102,4 @@ public class ProductSkuController {
|
||||
List<ProductVO> productVOs = productSkuService.getProductSkusByProductAndSkus(queries);
|
||||
return ApiResponse.success("批量查询SKU成功", productVOs);
|
||||
}
|
||||
|
||||
@Operation(summary = "同步SKU成本到历史订单")
|
||||
@PostMapping("/sync-costs-to-orders")
|
||||
@RequirePermission(value = "ecom:product:update", description = "同步成本到订单", allowSuper = true)
|
||||
public ApiResponse<Integer> syncCostsToOrders(@Valid @RequestBody SyncCostDTO syncCostDTO) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
ProductSkuPO sku = productSkuService.getById(syncCostDTO.getSkuId());
|
||||
if (sku == null || !sku.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("SKU不存在");
|
||||
}
|
||||
int updatedCount = orderService.syncCostsToOrders(
|
||||
syncCostDTO.getSkuId(),
|
||||
syncCostDTO.getProductCost(),
|
||||
syncCostDTO.getShippingCost(),
|
||||
syncCostDTO.getOtherCost(),
|
||||
syncCostDTO.getStartTime(),
|
||||
syncCostDTO.getEndTime());
|
||||
return ApiResponse.success("成本同步成功,共更新" + updatedCount + "条订单", updatedCount);
|
||||
}
|
||||
|
||||
@Operation(summary = "统计成本同步将影响的订单数量")
|
||||
@PostMapping("/count-orders-for-cost-sync")
|
||||
@RequirePermission(value = "ecom:product:read", description = "统计成本同步订单数", allowSuper = true)
|
||||
public ApiResponse<Long> countOrdersForCostSync(@Valid @RequestBody BatchSyncCostDTO batchSyncCostDTO) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
List<ProductSkuPO> skus = productSkuService.getByIds(batchSyncCostDTO.getSkuIds());
|
||||
if (skus.isEmpty()) {
|
||||
return ApiResponse.success("统计成功", 0L);
|
||||
}
|
||||
for (ProductSkuPO sku : skus) {
|
||||
if (!sku.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("SKU不存在,SKU ID: " + sku.getId());
|
||||
}
|
||||
}
|
||||
long count = orderService.countOrdersForCostSync(
|
||||
companyId,
|
||||
batchSyncCostDTO.getSkuIds(),
|
||||
batchSyncCostDTO.getStartTime(),
|
||||
batchSyncCostDTO.getEndTime());
|
||||
return ApiResponse.success("统计成功", count);
|
||||
}
|
||||
|
||||
@Operation(summary = "批量同步SKU成本到历史订单")
|
||||
@PostMapping("/batch-sync-costs-to-orders")
|
||||
@RequirePermission(value = "ecom:product:update", description = "批量同步成本到订单", allowSuper = true)
|
||||
public ApiResponse<Integer> batchSyncCostsToOrders(@Valid @RequestBody BatchSyncCostDTO batchSyncCostDTO) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
List<ProductSkuPO> skus = productSkuService.getByIds(batchSyncCostDTO.getSkuIds());
|
||||
if (skus.isEmpty()) {
|
||||
return ApiResponse.success("没有找到需要同步的SKU", 0);
|
||||
}
|
||||
for (ProductSkuPO sku : skus) {
|
||||
if (!sku.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("SKU不存在,SKU ID: " + sku.getId());
|
||||
}
|
||||
}
|
||||
int totalUpdated = 0;
|
||||
for (ProductSkuPO sku : skus) {
|
||||
var cost = productSkuCostService.getCurrentCost(sku.getId());
|
||||
int updated = orderService.syncCostsToOrders(
|
||||
sku.getId(),
|
||||
cost != null ? cost.getProductCost() : null,
|
||||
cost != null ? cost.getShippingCost() : null,
|
||||
cost != null ? cost.getOtherCost() : null,
|
||||
batchSyncCostDTO.getStartTime(),
|
||||
batchSyncCostDTO.getEndTime());
|
||||
totalUpdated += updated;
|
||||
}
|
||||
return ApiResponse.success("批量成本同步成功,共更新" + totalUpdated + "条订单", totalUpdated);
|
||||
}
|
||||
}
|
||||
|
||||
+148
@@ -0,0 +1,148 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.ec.product.controller;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.order.service.OrderService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.dto.AdjustSkuCostDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.dto.BatchSyncCostDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.dto.SyncCostDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductSkuPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.vo.ProductSkuCostHistoryVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.vo.ProductSkuVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuCostService;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuService;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 商品 SKU 成本:历史、调价、同步到订单
|
||||
*/
|
||||
@Tag(name = "电商-商品SKU成本", description = "SKU成本历史、按生效日调价、同步到订单")
|
||||
@RestController
|
||||
@RequestMapping("/api/ec/product/sku/cost")
|
||||
public class ProductSkuCostController {
|
||||
|
||||
private final ProductSkuService productSkuService;
|
||||
private final ProductSkuCostService productSkuCostService;
|
||||
private final OrderService orderService;
|
||||
private final CurrentUserContext currentUserContext;
|
||||
|
||||
public ProductSkuCostController(ProductSkuService productSkuService,
|
||||
ProductSkuCostService productSkuCostService,
|
||||
OrderService orderService,
|
||||
CurrentUserContext currentUserContext) {
|
||||
this.productSkuService = productSkuService;
|
||||
this.productSkuCostService = productSkuCostService;
|
||||
this.orderService = orderService;
|
||||
this.currentUserContext = currentUserContext;
|
||||
}
|
||||
|
||||
@Operation(summary = "查询SKU成本历史")
|
||||
@GetMapping("/{skuId}")
|
||||
@RequirePermission(value = "ecom:product:read", description = "查询SKU成本历史", allowSuper = true)
|
||||
public ApiResponse<List<ProductSkuCostHistoryVO>> listSkuCosts(
|
||||
@Parameter(description = "SKU ID") @PathVariable Long skuId) {
|
||||
ProductSkuPO sku = requireCompanySku(skuId);
|
||||
return ApiResponse.success("查询成本历史成功", productSkuCostService.listCostHistory(sku.getId()));
|
||||
}
|
||||
|
||||
@Operation(summary = "SKU调价(按生效日追加/同日纠错)")
|
||||
@PostMapping("/{skuId}")
|
||||
@RequirePermission(value = "ecom:product:update", description = "SKU调价", allowSuper = true)
|
||||
public ApiResponse<ProductSkuVO> adjustSkuCost(
|
||||
@Parameter(description = "SKU ID") @PathVariable Long skuId,
|
||||
@Valid @RequestBody AdjustSkuCostDTO dto) {
|
||||
ProductSkuPO sku = requireCompanySku(skuId);
|
||||
Long userId = currentUserContext.getCurrentUserId();
|
||||
boolean changed = productSkuCostService.adjustCost(
|
||||
sku.getId(), sku.getCompanyId(), sku.getProductId(),
|
||||
dto.getEffectiveFrom(),
|
||||
dto.getProductCost(), dto.getShippingCost(), dto.getOtherCost(),
|
||||
dto.getRemark(), userId);
|
||||
ProductSkuVO vo = productSkuService.toVo(productSkuService.getById(sku.getId()));
|
||||
vo.setCostChanged(changed);
|
||||
return ApiResponse.success(changed ? "调价成功" : "成本未变化", vo);
|
||||
}
|
||||
|
||||
@Operation(summary = "同步SKU成本到历史订单")
|
||||
@PostMapping("/sync-to-orders")
|
||||
@RequirePermission(value = "ecom:product:update", description = "同步成本到订单", allowSuper = true)
|
||||
public ApiResponse<Integer> syncCostsToOrders(@Valid @RequestBody SyncCostDTO syncCostDTO) {
|
||||
requireCompanySku(syncCostDTO.getSkuId());
|
||||
int updatedCount = orderService.syncCostsToOrders(
|
||||
syncCostDTO.getSkuId(),
|
||||
syncCostDTO.getProductCost(),
|
||||
syncCostDTO.getShippingCost(),
|
||||
syncCostDTO.getOtherCost(),
|
||||
syncCostDTO.getStartTime(),
|
||||
syncCostDTO.getEndTime());
|
||||
return ApiResponse.success("成本同步成功,共更新" + updatedCount + "条订单", updatedCount);
|
||||
}
|
||||
|
||||
@Operation(summary = "统计成本同步将影响的订单数量")
|
||||
@PostMapping("/count-orders-for-sync")
|
||||
@RequirePermission(value = "ecom:product:read", description = "统计成本同步订单数", allowSuper = true)
|
||||
public ApiResponse<Long> countOrdersForCostSync(@Valid @RequestBody BatchSyncCostDTO batchSyncCostDTO) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
List<ProductSkuPO> skus = productSkuService.getByIds(batchSyncCostDTO.getSkuIds());
|
||||
if (skus.isEmpty()) {
|
||||
return ApiResponse.success("统计成功", 0L);
|
||||
}
|
||||
for (ProductSkuPO sku : skus) {
|
||||
if (!sku.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("SKU不存在,SKU ID: " + sku.getId());
|
||||
}
|
||||
}
|
||||
long count = orderService.countOrdersForCostSync(
|
||||
companyId,
|
||||
batchSyncCostDTO.getSkuIds(),
|
||||
batchSyncCostDTO.getStartTime(),
|
||||
batchSyncCostDTO.getEndTime());
|
||||
return ApiResponse.success("统计成功", count);
|
||||
}
|
||||
|
||||
@Operation(summary = "批量同步SKU当前生效成本到历史订单")
|
||||
@PostMapping("/batch-sync-to-orders")
|
||||
@RequirePermission(value = "ecom:product:update", description = "批量同步成本到订单", allowSuper = true)
|
||||
public ApiResponse<Integer> batchSyncCostsToOrders(@Valid @RequestBody BatchSyncCostDTO batchSyncCostDTO) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
List<ProductSkuPO> skus = productSkuService.getByIds(batchSyncCostDTO.getSkuIds());
|
||||
if (skus.isEmpty()) {
|
||||
return ApiResponse.success("没有找到需要同步的SKU", 0);
|
||||
}
|
||||
for (ProductSkuPO sku : skus) {
|
||||
if (!sku.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("SKU不存在,SKU ID: " + sku.getId());
|
||||
}
|
||||
}
|
||||
int totalUpdated = 0;
|
||||
for (ProductSkuPO sku : skus) {
|
||||
var cost = productSkuCostService.getCurrentCost(sku.getId());
|
||||
int updated = orderService.syncCostsToOrders(
|
||||
sku.getId(),
|
||||
cost != null ? cost.getProductCost() : null,
|
||||
cost != null ? cost.getShippingCost() : null,
|
||||
cost != null ? cost.getOtherCost() : null,
|
||||
batchSyncCostDTO.getStartTime(),
|
||||
batchSyncCostDTO.getEndTime());
|
||||
totalUpdated += updated;
|
||||
}
|
||||
return ApiResponse.success("批量成本同步成功,共更新" + totalUpdated + "条订单", totalUpdated);
|
||||
}
|
||||
|
||||
private ProductSkuPO requireCompanySku(Long skuId) {
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
ProductSkuPO sku = productSkuService.getById(skuId);
|
||||
if (sku == null || !sku.getCompanyId().equals(companyId)) {
|
||||
throw new BusinessException("SKU不存在");
|
||||
}
|
||||
return sku;
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.ec.product.model.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* SKU 调价(按生效日追加/同日纠错)
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "SKU调价参数")
|
||||
public class AdjustSkuCostDTO {
|
||||
|
||||
@NotNull(message = "生效起始日不能为空")
|
||||
@Schema(description = "生效起始日(含)", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private LocalDate effectiveFrom;
|
||||
|
||||
@NotNull(message = "商品成本不能为空")
|
||||
@Schema(description = "商品成本", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private BigDecimal productCost;
|
||||
|
||||
@Schema(description = "运费成本")
|
||||
private BigDecimal shippingCost;
|
||||
|
||||
@Schema(description = "其他成本")
|
||||
private BigDecimal otherCost;
|
||||
|
||||
@Schema(description = "备注(如调价原因)")
|
||||
private String remark;
|
||||
}
|
||||
+3
-4
@@ -25,14 +25,13 @@ public class UpdateProductSkuDTO {
|
||||
@Schema(description = "SKU编码")
|
||||
private String skuCode;
|
||||
|
||||
@Schema(description = "商品成本", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotNull(message = "商品成本不能为空")
|
||||
@Schema(description = "商品成本(已废弃:调价请走 /product-sku/{id}/costs)")
|
||||
private BigDecimal productCost;
|
||||
|
||||
@Schema(description = "运费成本")
|
||||
@Schema(description = "运费成本(已废弃:调价请走独立成本接口)")
|
||||
private BigDecimal shippingCost;
|
||||
|
||||
@Schema(description = "其他成本")
|
||||
@Schema(description = "其他成本(已废弃:调价请走独立成本接口)")
|
||||
private BigDecimal otherCost;
|
||||
|
||||
@Schema(description = "备注")
|
||||
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.ec.product.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* SKU 成本历史条目
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "SKU成本历史条目")
|
||||
public class ProductSkuCostHistoryVO {
|
||||
|
||||
@Schema(description = "成本记录ID")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "生效起始日(含)")
|
||||
private LocalDate effectiveFrom;
|
||||
|
||||
@Schema(description = "商品成本")
|
||||
private BigDecimal productCost;
|
||||
|
||||
@Schema(description = "运费成本")
|
||||
private BigDecimal shippingCost;
|
||||
|
||||
@Schema(description = "其他成本")
|
||||
private BigDecimal otherCost;
|
||||
|
||||
@Schema(description = "备注")
|
||||
private String remark;
|
||||
|
||||
@Schema(description = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
}
|
||||
+4
@@ -4,6 +4,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
@@ -43,6 +44,9 @@ public class ProductSkuVO {
|
||||
@Schema(description = "其他成本(当前生效)")
|
||||
private BigDecimal otherCost;
|
||||
|
||||
@Schema(description = "当前成本生效起始日")
|
||||
private LocalDate costEffectiveFrom;
|
||||
|
||||
@Schema(description = "备注")
|
||||
private String remark;
|
||||
|
||||
|
||||
+18
-1
@@ -1,13 +1,16 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.ec.product.service;
|
||||
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.po.ProductSkuCostPO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.vo.ProductSkuCostHistoryVO;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* SKU 成本读写:当前成本查询与按日 upsert
|
||||
* SKU 成本读写:当前成本查询、历史列表与按日调价
|
||||
*/
|
||||
public interface ProductSkuCostService {
|
||||
|
||||
@@ -17,8 +20,22 @@ public interface ProductSkuCostService {
|
||||
/** 批量读当前成本,key=skuId */
|
||||
Map<Long, ProductSkuCostPO> getCurrentCosts(Collection<Long> skuIds);
|
||||
|
||||
/** 成本历史:按 effective_from 降序 */
|
||||
List<ProductSkuCostHistoryVO> listCostHistory(Long skuId);
|
||||
|
||||
/**
|
||||
* 按生效日调价:同日已有行则 UPDATE,否则 INSERT。
|
||||
* 相对「该生效日及之前最近一条」比较,无变化则不写。
|
||||
*
|
||||
* @return 是否实际写入(含同日纠错)
|
||||
*/
|
||||
boolean adjustCost(Long skuId, Long companyId, Long productId, LocalDate effectiveFrom,
|
||||
BigDecimal productCost, BigDecimal shippingCost, BigDecimal otherCost,
|
||||
String remark, Long userId);
|
||||
|
||||
/**
|
||||
* 成本有变则写入:当日已有行则 UPDATE,否则 INSERT effective_from=今天。
|
||||
* 供导入等旁路复用;商品编辑调价请走 {@link #adjustCost}。
|
||||
*
|
||||
* @return 成本是否相对当前生效值发生变化
|
||||
*/
|
||||
|
||||
+2
-8
@@ -361,19 +361,13 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
}
|
||||
}
|
||||
|
||||
// 更新规格(走标准更新流程,内部会检测成本是否变化)
|
||||
// 更新规格(成本变更请走独立调价接口)
|
||||
UpdateProductSkuDTO updateSpecDto = new UpdateProductSkuDTO();
|
||||
updateSpecDto.setName(specDTO.getName());
|
||||
updateSpecDto.setSkuCode(specDTO.getSkuCode());
|
||||
updateSpecDto.setProductCost(specDTO.getProductCost());
|
||||
updateSpecDto.setShippingCost(specDTO.getShippingCost());
|
||||
updateSpecDto.setOtherCost(specDTO.getOtherCost());
|
||||
updateSpecDto.setRemark(specDTO.getRemark());
|
||||
updateSpecDto.setImageId(specDTO.getImageId());
|
||||
ProductSkuVO specVO = productSkuService.updateProductSku(updateSpecDto, specDTO.getId());
|
||||
if (Boolean.TRUE.equals(specVO.getCostChanged())) {
|
||||
rawCostChangedSkuIds.add(specDTO.getId());
|
||||
}
|
||||
productSkuService.updateProductSku(updateSpecDto, specDTO.getId());
|
||||
}
|
||||
|
||||
for (UpdateProductDTO.ProductSkuUpdateDTO specDTO : newSpecs) {
|
||||
|
||||
+103
-34
@@ -3,12 +3,15 @@ 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 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;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.model.vo.ProductSkuCostHistoryVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ec.product.service.ProductSkuCostService;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -44,7 +47,6 @@ public class ProductSkuCostServiceImpl extends ServiceImpl<ProductSkuCostMapper,
|
||||
if (ids.isEmpty()) {
|
||||
return result;
|
||||
}
|
||||
// 一次查出后按 sku 取 effective_from 最新一条,避免 N+1
|
||||
List<ProductSkuCostPO> rows = list(new LambdaQueryWrapper<ProductSkuCostPO>()
|
||||
.in(ProductSkuCostPO::getSkuId, ids)
|
||||
.orderByDesc(ProductSkuCostPO::getEffectiveFrom));
|
||||
@@ -54,17 +56,87 @@ public class ProductSkuCostServiceImpl extends ServiceImpl<ProductSkuCostMapper,
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ProductSkuCostHistoryVO> listCostHistory(Long skuId) {
|
||||
if (skuId == null) {
|
||||
return List.of();
|
||||
}
|
||||
List<ProductSkuCostPO> rows = list(new LambdaQueryWrapper<ProductSkuCostPO>()
|
||||
.eq(ProductSkuCostPO::getSkuId, skuId)
|
||||
.orderByDesc(ProductSkuCostPO::getEffectiveFrom)
|
||||
.orderByDesc(ProductSkuCostPO::getId));
|
||||
List<ProductSkuCostHistoryVO> list = new ArrayList<>(rows.size());
|
||||
for (ProductSkuCostPO row : rows) {
|
||||
ProductSkuCostHistoryVO vo = new ProductSkuCostHistoryVO();
|
||||
vo.setId(row.getId());
|
||||
vo.setEffectiveFrom(row.getEffectiveFrom());
|
||||
vo.setProductCost(row.getProductCost());
|
||||
vo.setShippingCost(row.getShippingCost());
|
||||
vo.setOtherCost(row.getOtherCost());
|
||||
vo.setRemark(row.getRemark());
|
||||
vo.setCreateTime(row.getCreateTime());
|
||||
list.add(vo);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean adjustCost(Long skuId, Long companyId, Long productId, LocalDate effectiveFrom,
|
||||
BigDecimal productCost, BigDecimal shippingCost, BigDecimal otherCost,
|
||||
String remark, Long userId) {
|
||||
if (skuId == null || companyId == null || productId == null) {
|
||||
throw new BusinessException("SKU/公司/商品不能为空");
|
||||
}
|
||||
if (effectiveFrom == null) {
|
||||
throw new BusinessException("生效起始日不能为空");
|
||||
}
|
||||
|
||||
ProductSkuCostPO baseline = getBaselineCost(skuId, effectiveFrom);
|
||||
ProductSkuCostPO sameDay = getCostOnDate(skuId, effectiveFrom);
|
||||
|
||||
BigDecimal pc = nz(productCost);
|
||||
BigDecimal sc = nz(shippingCost);
|
||||
BigDecimal oc = nz(otherCost);
|
||||
|
||||
if (sameDay != null) {
|
||||
boolean changed = isCostChanged(sameDay, pc, sc, oc)
|
||||
|| !Objects.equals(trimToNull(remark), trimToNull(sameDay.getRemark()));
|
||||
if (!changed) {
|
||||
return false;
|
||||
}
|
||||
sameDay.setProductCost(pc);
|
||||
sameDay.setShippingCost(sc);
|
||||
sameDay.setOtherCost(oc);
|
||||
sameDay.setRemark(trimToNull(remark));
|
||||
sameDay.setUpdateBy(userId);
|
||||
updateById(sameDay);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!isCostChanged(baseline, pc, sc, oc)) {
|
||||
return false;
|
||||
}
|
||||
ProductSkuCostPO row = new ProductSkuCostPO();
|
||||
row.setCompanyId(companyId);
|
||||
row.setProductId(productId);
|
||||
row.setSkuId(skuId);
|
||||
row.setProductCost(pc);
|
||||
row.setShippingCost(sc);
|
||||
row.setOtherCost(oc);
|
||||
row.setEffectiveFrom(effectiveFrom);
|
||||
row.setRemark(trimToNull(remark));
|
||||
row.setCreateBy(userId);
|
||||
row.setUpdateBy(userId);
|
||||
save(row);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean upsertCostIfChanged(Long skuId, Long companyId, Long productId,
|
||||
BigDecimal productCost, BigDecimal shippingCost, BigDecimal otherCost,
|
||||
Long userId) {
|
||||
ProductSkuCostPO current = getCurrentCost(skuId);
|
||||
boolean changed = isCostChanged(current, productCost, shippingCost, otherCost);
|
||||
if (!changed) {
|
||||
return false;
|
||||
}
|
||||
writeCostForToday(skuId, companyId, productId, productCost, shippingCost, otherCost, userId);
|
||||
return true;
|
||||
return adjustCost(skuId, companyId, productId, LocalDate.now(),
|
||||
productCost, shippingCost, otherCost, null, userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -74,35 +146,24 @@ public class ProductSkuCostServiceImpl extends ServiceImpl<ProductSkuCostMapper,
|
||||
if (!hasAnyCostValue(productCost, shippingCost, otherCost)) {
|
||||
return;
|
||||
}
|
||||
writeCostForToday(skuId, companyId, productId, productCost, shippingCost, otherCost, userId);
|
||||
adjustCost(skuId, companyId, productId, LocalDate.now(),
|
||||
productCost, shippingCost, otherCost, null, userId);
|
||||
}
|
||||
|
||||
private void writeCostForToday(Long skuId, Long companyId, Long productId,
|
||||
BigDecimal productCost, BigDecimal shippingCost, BigDecimal otherCost,
|
||||
Long userId) {
|
||||
LocalDate today = LocalDate.now();
|
||||
/** 取 effective_from <= 指定日 的最新一条(含当日) */
|
||||
private ProductSkuCostPO getBaselineCost(Long skuId, LocalDate onOrBefore) {
|
||||
LambdaQueryWrapper<ProductSkuCostPO> w = new LambdaQueryWrapper<>();
|
||||
w.eq(ProductSkuCostPO::getSkuId, skuId).eq(ProductSkuCostPO::getEffectiveFrom, today);
|
||||
ProductSkuCostPO existing = getOne(w);
|
||||
if (existing != null) {
|
||||
existing.setProductCost(nz(productCost));
|
||||
existing.setShippingCost(nz(shippingCost));
|
||||
existing.setOtherCost(nz(otherCost));
|
||||
existing.setUpdateBy(userId);
|
||||
updateById(existing);
|
||||
return;
|
||||
}
|
||||
ProductSkuCostPO row = new ProductSkuCostPO();
|
||||
row.setCompanyId(companyId);
|
||||
row.setProductId(productId);
|
||||
row.setSkuId(skuId);
|
||||
row.setProductCost(nz(productCost));
|
||||
row.setShippingCost(nz(shippingCost));
|
||||
row.setOtherCost(nz(otherCost));
|
||||
row.setEffectiveFrom(today);
|
||||
row.setCreateBy(userId);
|
||||
row.setUpdateBy(userId);
|
||||
save(row);
|
||||
w.eq(ProductSkuCostPO::getSkuId, skuId)
|
||||
.le(ProductSkuCostPO::getEffectiveFrom, onOrBefore)
|
||||
.orderByDesc(ProductSkuCostPO::getEffectiveFrom)
|
||||
.last("LIMIT 1");
|
||||
return getOne(w);
|
||||
}
|
||||
|
||||
private ProductSkuCostPO getCostOnDate(Long skuId, LocalDate effectiveFrom) {
|
||||
LambdaQueryWrapper<ProductSkuCostPO> w = new LambdaQueryWrapper<>();
|
||||
w.eq(ProductSkuCostPO::getSkuId, skuId).eq(ProductSkuCostPO::getEffectiveFrom, effectiveFrom);
|
||||
return getOne(w);
|
||||
}
|
||||
|
||||
private static boolean isCostChanged(ProductSkuCostPO current,
|
||||
@@ -139,4 +200,12 @@ public class ProductSkuCostServiceImpl extends ServiceImpl<ProductSkuCostMapper,
|
||||
private static BigDecimal nz(BigDecimal v) {
|
||||
return v != null ? v : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
private static String trimToNull(String s) {
|
||||
if (s == null) {
|
||||
return null;
|
||||
}
|
||||
String t = s.trim();
|
||||
return t.isEmpty() ? null : t;
|
||||
}
|
||||
}
|
||||
|
||||
+9
-16
@@ -108,30 +108,22 @@ public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, Product
|
||||
throw new BusinessException("SKU名称已存在");
|
||||
}
|
||||
|
||||
ProductSkuCostPO currentCost = productSkuCostService.getCurrentCost(id);
|
||||
boolean costChanged = ProductSkuCostServiceImpl.isSingleCostChanged(
|
||||
currentCost != null ? currentCost.getProductCost() : null, dto.getProductCost())
|
||||
|| ProductSkuCostServiceImpl.isSingleCostChanged(
|
||||
currentCost != null ? currentCost.getShippingCost() : null, dto.getShippingCost())
|
||||
|| ProductSkuCostServiceImpl.isSingleCostChanged(
|
||||
currentCost != null ? currentCost.getOtherCost() : null, dto.getOtherCost());
|
||||
|
||||
// 调价走独立成本接口;此处只更新 SKU 基础字段
|
||||
ProductSkuPO sku = new ProductSkuPO();
|
||||
BeanCopyUtils.copyProperties(dto, sku);
|
||||
sku.setId(id);
|
||||
sku.setCompanyId(companyId);
|
||||
sku.setProductId(existing.getProductId());
|
||||
sku.setName(dto.getName());
|
||||
sku.setSkuCode(dto.getSkuCode());
|
||||
sku.setRemark(dto.getRemark());
|
||||
if (dto.getImageId() != null) {
|
||||
sku.setImageId(dto.getImageId());
|
||||
}
|
||||
sku.setUpdateBy(currentUserContext.getCurrentUserId());
|
||||
this.updateById(sku);
|
||||
|
||||
if (costChanged) {
|
||||
productSkuCostService.upsertCostIfChanged(id, companyId, existing.getProductId(),
|
||||
dto.getProductCost(), dto.getShippingCost(), dto.getOtherCost(),
|
||||
currentUserContext.getCurrentUserId());
|
||||
}
|
||||
|
||||
ProductSkuVO vo = toVo(this.getById(id));
|
||||
vo.setCostChanged(costChanged);
|
||||
vo.setCostChanged(false);
|
||||
return vo;
|
||||
}
|
||||
|
||||
@@ -437,6 +429,7 @@ public class ProductSkuServiceImpl extends ServiceImpl<ProductSkuMapper, Product
|
||||
vo.setProductCost(cost.getProductCost());
|
||||
vo.setShippingCost(cost.getShippingCost());
|
||||
vo.setOtherCost(cost.getOtherCost());
|
||||
vo.setCostEffectiveFrom(cost.getEffectiveFrom());
|
||||
}
|
||||
// 有系统图 id 则覆盖外链
|
||||
if (sku.getImageId() != null) {
|
||||
|
||||
Reference in New Issue
Block a user