feat: 成本同步功能完整实现

- ProductSpecController 新增 POST /ec/product-spec/sync-costs-to-orders 接口
- OrderMapper.updateCostFields() 批量更新订单成本字段
- OrderService 新增 syncCostsToOrders 方法
- UpdateProductResultDTO 返回完整成本变更规格列表(含 id/name/specCode/成本字段)
- 前端 ProductFormModal 同步弹窗:勾选规格 + 选择时间范围 + 显示更新条数
This commit is contained in:
小蟑螂
2026-04-14 22:04:38 +08:00
parent 26cb1a9a41
commit 83ed7478d2
11 changed files with 341 additions and 13 deletions
@@ -2,11 +2,36 @@ package top.xyquan.ecomdmbbackend.modules.ecom.order.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import top.xyquan.ecomdmbbackend.modules.ecom.order.model.po.OrderPO;
import java.math.BigDecimal;
import java.time.LocalDateTime;
/**
* 订单Mapper接口
*/
@Mapper
public interface OrderMapper extends BaseMapper<OrderPO> {
/**
* 批量更新订单的成本字段
* @param productCost 商品成本
* @param shippingCost 运费成本
* @param otherCost 其他成本
* @param productSpecId 规格ID
* @param startTime 开始时间
* @param endTime 结束时间
* @return 更新行数
*/
@Update("UPDATE ec_order SET product_cost = #{productCost}, shipping_cost = #{shippingCost}, " +
"other_cost = #{otherCost}, update_time = NOW() " +
"WHERE product_spec_id = #{productSpecId} AND order_time >= #{startTime} AND order_time <= #{endTime}")
int updateCostFields(@Param("productCost") BigDecimal productCost,
@Param("shippingCost") BigDecimal shippingCost,
@Param("otherCost") BigDecimal otherCost,
@Param("productSpecId") Long productSpecId,
@Param("startTime") LocalDateTime startTime,
@Param("endTime") LocalDateTime endTime);
}
@@ -73,4 +73,18 @@ public interface OrderService extends IService<OrderPO> {
* @return 订单VO
*/
OrderVO createOrderForImport(CreateOrderDTO createOrderDTO, Long companyId, Long userId);
/**
* 同步规格成本到历史订单
* @param specId 规格ID
* @param productCost 商品成本
* @param shippingCost 运费成本
* @param otherCost 其他成本
* @param startTime 开始时间
* @param endTime 结束时间
* @return 更新行数
*/
int syncCostsToOrders(Long specId, java.math.BigDecimal productCost,
java.math.BigDecimal shippingCost, java.math.BigDecimal otherCost,
java.time.LocalDateTime startTime, java.time.LocalDateTime endTime);
}
@@ -453,4 +453,11 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderPO> implemen
return orderVO;
}
@Override
public int syncCostsToOrders(Long specId, java.math.BigDecimal productCost,
java.math.BigDecimal shippingCost, java.math.BigDecimal otherCost,
java.time.LocalDateTime startTime, java.time.LocalDateTime endTime) {
return baseMapper.updateCostFields(productCost, shippingCost, otherCost, specId, startTime, endTime);
}
}
@@ -8,13 +8,18 @@ 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.ecom.product.model.dto.CreateProductSpecDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.ProductSpecFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.SyncCostDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.UpdateProductSpecDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductSpecPO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.vo.ProductSpecVO;
import top.xyquan.ecomdmbbackend.modules.ecom.order.service.OrderService;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.vo.ProductVO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductSpecService;
import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductSpecService.ProductSpecQueryDTO;
@@ -28,9 +33,14 @@ import top.xyquan.ecomdmbbackend.modules.ecom.product.service.ProductSpecService
public class ProductSpecController {
private final ProductSpecService productSpecService;
private final OrderService orderService;
private final CurrentUserContext currentUserContext;
public ProductSpecController(ProductSpecService productSpecService) {
public ProductSpecController(ProductSpecService productSpecService, OrderService orderService,
CurrentUserContext currentUserContext) {
this.productSpecService = productSpecService;
this.orderService = orderService;
this.currentUserContext = currentUserContext;
}
@Operation(summary = "创建商品规格", description = "创建新的商品规格")
@@ -86,4 +96,25 @@ public class ProductSpecController {
return ApiResponse.success(productVOs);
}
@Operation(summary = "同步规格成本到历史订单", description = "将指定规格的成本批量更新到符合时间范围的订单")
@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();
// 验证规格是否存在且属于该公司
ProductSpecPO spec = productSpecService.getById(syncCostDTO.getSpecId());
if (spec == null || !spec.getCompanyId().equals(companyId)) {
throw new BusinessException("规格不存在");
}
int updatedCount = orderService.syncCostsToOrders(
syncCostDTO.getSpecId(),
syncCostDTO.getProductCost(),
syncCostDTO.getShippingCost(),
syncCostDTO.getOtherCost(),
syncCostDTO.getStartTime(),
syncCostDTO.getEndTime()
);
return ApiResponse.success("成本同步成功,共更新" + updatedCount + "条订单", updatedCount);
}
}
@@ -0,0 +1,57 @@
package top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
/**
* 同步规格成本到订单的请求DTO
*/
@Data
@Schema(description = "同步规格成本到订单的请求DTO")
public class SyncCostDTO {
/**
* 规格ID
*/
@Schema(description = "规格ID", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "规格ID不能为空")
private Long specId;
/**
* 商品成本
*/
@Schema(description = "商品成本", example = "100.00")
private BigDecimal productCost;
/**
* 运费成本
*/
@Schema(description = "运费成本", example = "10.00")
private BigDecimal shippingCost;
/**
* 其他成本
*/
@Schema(description = "其他成本", example = "5.00")
private BigDecimal otherCost;
/**
* 订单开始时间(包含),格式 yyyy-MM-dd HH:mm:ss
*/
@Schema(description = "订单开始时间", example = "2024-01-01 00:00:00")
@NotNull(message = "开始时间不能为空")
private LocalDateTime startTime;
/**
* 订单结束时间(包含),格式 yyyy-MM-dd HH:mm:ss
*/
@Schema(description = "订单结束时间", example = "2024-12-31 23:59:59")
@NotNull(message = "结束时间不能为空")
private LocalDateTime endTime;
}
@@ -13,8 +13,51 @@ import java.util.List;
public class UpdateProductResultDTO {
/**
* 本次更新中成本发生变更的规格ID列表
* 本次更新中成本发生变更的规格列表
*/
@Schema(description = "成本发生变更的规格ID列表")
private List<Long> costChangedSpecIds;
}
@Schema(description = "成本发生变更的规格列表")
private List<CostChangedSpec> costChangedSpecs;
/**
* 成本变更的规格简要信息
*/
@Data
@Schema(description = "成本变更的规格简要信息")
public static class CostChangedSpec {
/**
* 规格ID
*/
@Schema(description = "规格ID", example = "1")
private Long id;
/**
* 规格名称
*/
@Schema(description = "规格名称", example = "128GB 深空黑色")
private String name;
/**
* 规格编码
*/
@Schema(description = "规格编码", example = "IPHONE15PRO-128G-BLACK")
private String specCode;
/**
* 商品成本
*/
@Schema(description = "商品成本", example = "8000.00")
private java.math.BigDecimal productCost;
/**
* 运费成本
*/
@Schema(description = "运费成本", example = "10.00")
private java.math.BigDecimal shippingCost;
/**
* 其他成本
*/
@Schema(description = "其他成本", example = "5.00")
private java.math.BigDecimal otherCost;
}
}
@@ -16,6 +16,7 @@ import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductDTO
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.ProductFilterDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.UpdateProductDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.UpdateProductResultDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.UpdateProductResultDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.UpdateProductSpecDTO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductPO;
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.po.ProductSpecPO;
@@ -226,7 +227,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
this.updateById(product);
// 如果提供了规格列表,则更新对应的规格
List<Long> costChangedSpecIds = new ArrayList<>();
List<UpdateProductResultDTO.CostChangedSpec> costChangedSpecs = new ArrayList<>();
if (updateProductDTO.getSpecs() != null && !updateProductDTO.getSpecs().isEmpty()) {
List<UpdateProductDTO.ProductSpecUpdateDTO> specDTOs = updateProductDTO.getSpecs();
for (UpdateProductDTO.ProductSpecUpdateDTO specDTO : specDTOs) {
@@ -270,14 +271,21 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
updateSpecDto.setRemark(specDTO.getRemark());
ProductSpecVO specVO = productSpecService.updateProductSpec(updateSpecDto, specDTO.getId());
if (Boolean.TRUE.equals(specVO.getCostChanged())) {
costChangedSpecIds.add(specDTO.getId());
UpdateProductResultDTO.CostChangedSpec changedSpec = new UpdateProductResultDTO.CostChangedSpec();
changedSpec.setId(specDTO.getId());
changedSpec.setName(specDTO.getName());
changedSpec.setSpecCode(specDTO.getSpecCode());
changedSpec.setProductCost(specDTO.getProductCost());
changedSpec.setShippingCost(specDTO.getShippingCost());
changedSpec.setOtherCost(specDTO.getOtherCost());
costChangedSpecs.add(changedSpec);
}
}
}
// 返回结果
UpdateProductResultDTO result = new UpdateProductResultDTO();
result.setCostChangedSpecIds(costChangedSpecIds);
result.setCostChangedSpecs(costChangedSpecs);
return result;
}