mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:59:20 +08:00
feat: 创建商品时同步提示新增规格的成本;优化成本同步弹框日期范围交互
- 新增 CreateProductResultDTO:创建商品响应含 specIdsCostChangedWithLinkedOrders - ProductService.createProduct:新增规格(编码非空、三项成本至少一项大于 0、且有关联订单)时纳入同步提示 - ProductService.updateProduct:合并成本变更规格与新增规格中有关联订单的 ID集合 - OrderService:新增 companyHasOrdersForSpec() 查询单个规格是否存在关联订单 - BatchSyncCostDTO:注释明确 startTime/endTime 为含边界的 LocalDateTime 字符串 - 前端成本同步弹窗:日期选择器改为纯日期格式(YYYY-MM-DD),提交时自动补 00:00:00 / 23:59:59 - ProductFormModal:创建商品后若有待同步规格则自动弹出成本同步确认框
This commit is contained in:
+5
@@ -117,4 +117,9 @@ public interface OrderService extends IService<OrderPO> {
|
||||
* @return 存在关联订单的规格ID列表(去重)
|
||||
*/
|
||||
java.util.List<Long> specIdsHavingOrders(Long companyId, java.util.List<Long> specIds);
|
||||
|
||||
/**
|
||||
* 当前公司在库中是否存在至少一条订单关联该规格
|
||||
*/
|
||||
boolean companyHasOrdersForSpec(Long companyId, Long specId);
|
||||
}
|
||||
|
||||
+11
@@ -518,4 +518,15 @@ public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderPO> implemen
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean companyHasOrdersForSpec(Long companyId, Long specId) {
|
||||
if (companyId == null || specId == null) {
|
||||
return false;
|
||||
}
|
||||
Long c = baseMapper.selectCount(new LambdaQueryWrapper<OrderPO>()
|
||||
.eq(OrderPO::getCompanyId, companyId)
|
||||
.eq(OrderPO::getProductSpecId, specId));
|
||||
return c != null && c > 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+5
-4
@@ -10,6 +10,7 @@ import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductResultDTO;
|
||||
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;
|
||||
@@ -30,12 +31,12 @@ public class ProductController {
|
||||
this.productService = productService;
|
||||
}
|
||||
|
||||
@Operation(summary = "创建商品", description = "创建新的商品")
|
||||
@Operation(summary = "创建商品", description = "创建新的商品;响应含 specIdsCostChangedWithLinkedOrders:新增规格且规格编码非空、商品/运费/其他成本至少一项大于 0、且本公司在库中存在关联订单的规格ID(如同编码合并),用于提示同步成本")
|
||||
@PostMapping("/create")
|
||||
@RequirePermission(value = "ecom:product:create", description = "创建商品", allowSuper = true)
|
||||
public ApiResponse<ProductVO> createProduct(@Valid @RequestBody CreateProductDTO createProductDTO) {
|
||||
ProductVO productVO = productService.createProduct(createProductDTO);
|
||||
return ApiResponse.success("商品创建成功", productVO);
|
||||
public ApiResponse<CreateProductResultDTO> createProduct(@Valid @RequestBody CreateProductDTO createProductDTO) {
|
||||
CreateProductResultDTO result = productService.createProduct(createProductDTO);
|
||||
return ApiResponse.success("商品创建成功", result);
|
||||
}
|
||||
|
||||
@Operation(summary = "更新商品", description = "更新现有商品信息;响应中的 specIdsCostChangedWithLinkedOrders 为「成本已变更且存在关联订单」的规格ID,用于提示同步成本")
|
||||
|
||||
+4
-4
@@ -23,16 +23,16 @@ public class BatchSyncCostDTO {
|
||||
private List<Long> specIds;
|
||||
|
||||
/**
|
||||
* 订单开始时间(包含),格式 yyyy-MM-dd HH:mm:ss
|
||||
* 订单开始时间(包含),格式 yyyy-MM-dd HH:mm:ss;按自然日筛选时一般为起日 00:00:00
|
||||
*/
|
||||
@Schema(description = "订单开始时间", example = "2024-01-01 00:00:00")
|
||||
@Schema(description = "订单开始时间(含)", example = "2024-01-01 00:00:00")
|
||||
@NotNull(message = "开始时间不能为空")
|
||||
private LocalDateTime startTime;
|
||||
|
||||
/**
|
||||
* 订单结束时间(包含),格式 yyyy-MM-dd HH:mm:ss
|
||||
* 订单结束时间(包含),格式 yyyy-MM-dd HH:mm:ss;按自然日筛选时一般为止日 23:59:59
|
||||
*/
|
||||
@Schema(description = "订单结束时间", example = "2024-12-31 23:59:59")
|
||||
@Schema(description = "订单结束时间(含)", example = "2024-12-31 23:59:59")
|
||||
@NotNull(message = "结束时间不能为空")
|
||||
private LocalDateTime endTime;
|
||||
}
|
||||
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.vo.ProductVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 创建商品成功响应(含新增规格与历史订单关联时可同步成本的提示)
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "创建商品成功响应DTO")
|
||||
public class CreateProductResultDTO {
|
||||
|
||||
@Schema(description = "创建后的商品完整信息")
|
||||
private ProductVO product;
|
||||
|
||||
/**
|
||||
* 新增规格中:规格编码非空、商品/运费/其他成本至少一项大于 0、且本公司在库中存在关联订单的规格ID(通常为同编码合并后的已有规格)
|
||||
*/
|
||||
@Schema(description = "新增规格(有成本且有关联订单)的规格ID列表,用于同步成本提示")
|
||||
private List<Long> specIdsCostChangedWithLinkedOrders;
|
||||
}
|
||||
+2
-2
@@ -14,9 +14,9 @@ import java.util.List;
|
||||
public class UpdateProductResultDTO {
|
||||
|
||||
/**
|
||||
* 本次更新中成本发生变更、且当前公司在库中存在关联订单的规格ID列表(用于提示将成本同步到订单)
|
||||
* 需提示同步成本的规格:成本已变更且有关联订单;或本次新增规格(规格编码非空、三项成本至少一项大于 0)且有关联订单
|
||||
*/
|
||||
@Schema(description = "成本已变更且存在关联订单的规格ID列表,用于同步成本提示")
|
||||
@Schema(description = "成本变更或新增规格(有成本且有关联订单)的规格ID列表,用于同步成本提示")
|
||||
private List<Long> specIdsCostChangedWithLinkedOrders;
|
||||
|
||||
/**
|
||||
|
||||
+3
-2
@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductResultDTO;
|
||||
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;
|
||||
@@ -20,9 +21,9 @@ public interface ProductService extends IService<ProductPO> {
|
||||
* 创建商品
|
||||
*
|
||||
* @param createProductDTO 创建商品DTO
|
||||
* @return 商品VO
|
||||
* @return 创建结果(含商品信息与「新增规格且存在关联订单」的规格ID,用于同步成本提示)
|
||||
*/
|
||||
ProductVO createProduct(CreateProductDTO createProductDTO);
|
||||
CreateProductResultDTO createProduct(CreateProductDTO createProductDTO);
|
||||
|
||||
/**
|
||||
* 更新商品
|
||||
|
||||
+54
-10
@@ -13,6 +13,7 @@ import top.xyquan.ecomdmbbackend.common.model.dto.PageQueryDTO;
|
||||
import top.xyquan.ecomdmbbackend.common.model.vo.PageResultVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.mapper.ProductMapper;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductResultDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.CreateProductSpecDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.ProductFilterDTO;
|
||||
import top.xyquan.ecomdmbbackend.modules.ecom.product.model.dto.UpdateProductDTO;
|
||||
@@ -31,7 +32,10 @@ import top.xyquan.ecomdmbbackend.modules.sys.file.model.po.SysFilePO;
|
||||
import top.xyquan.ecomdmbbackend.utils.CurrentUserContext;
|
||||
import top.xyquan.ecomdmbbackend.utils.SysFileUtils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
@@ -62,11 +66,11 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
/**
|
||||
* 创建商品
|
||||
* @param createProductDTO 创建商品DTO
|
||||
* @return 商品VO
|
||||
* @return 创建结果(含商品与需提示同步成本的规格ID)
|
||||
*/
|
||||
@Override
|
||||
@Transactional
|
||||
public ProductVO createProduct(CreateProductDTO createProductDTO) {
|
||||
public CreateProductResultDTO createProduct(CreateProductDTO createProductDTO) {
|
||||
// 从token中获取公司ID
|
||||
Long companyId = currentUserContext.getCurrentCompanyId();
|
||||
|
||||
@@ -98,6 +102,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
// 保存商品
|
||||
this.save(product);
|
||||
|
||||
LinkedHashSet<Long> syncSpecIds = new LinkedHashSet<>();
|
||||
// 如果提供了规格列表,则创建商品规格
|
||||
if (createProductDTO.getSpecs() != null && !createProductDTO.getSpecs().isEmpty()) {
|
||||
validateSpecCodesForCreate(createProductDTO.getSpecs(), companyId);
|
||||
@@ -112,12 +117,16 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
createDto.setOtherCost(specDTO.getOtherCost());
|
||||
createDto.setRemark(specDTO.getRemark());
|
||||
createDto.setLinkName(specDTO.getLinkName());
|
||||
productSpecService.createProductSpec(createDto);
|
||||
ProductSpecVO createdVo = productSpecService.createProductSpec(createDto);
|
||||
appendSpecIdIfSpecCodeHasLinkedOrders(companyId, createdVo.getId(), specDTO.getSpecCode(),
|
||||
specDTO.getProductCost(), specDTO.getShippingCost(), specDTO.getOtherCost(), syncSpecIds);
|
||||
}
|
||||
}
|
||||
|
||||
// 转换为VO并返回
|
||||
return convertToVO(product);
|
||||
CreateProductResultDTO result = new CreateProductResultDTO();
|
||||
result.setProduct(convertToVO(product));
|
||||
result.setSpecIdsCostChangedWithLinkedOrders(new ArrayList<>(syncSpecIds));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -171,6 +180,36 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
return convertToVO(product);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建/新增规格:规格编码非空、且商品/运费/其他成本至少一项大于 0、且本公司在订单中存在该规格时,纳入同步提示。
|
||||
*/
|
||||
private void appendSpecIdIfSpecCodeHasLinkedOrders(Long companyId, Long specId, String rawSpecCode,
|
||||
BigDecimal productCost, BigDecimal shippingCost, BigDecimal otherCost, Collection<Long> out) {
|
||||
if (specId == null || rawSpecCode == null) {
|
||||
return;
|
||||
}
|
||||
String code = rawSpecCode.trim();
|
||||
if (code.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (!hasAnyPositiveCost(productCost, shippingCost, otherCost)) {
|
||||
return;
|
||||
}
|
||||
if (orderService.companyHasOrdersForSpec(companyId, specId)) {
|
||||
out.add(specId);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean hasAnyPositiveCost(BigDecimal productCost, BigDecimal shippingCost, BigDecimal otherCost) {
|
||||
return nz(productCost).compareTo(BigDecimal.ZERO) > 0
|
||||
|| nz(shippingCost).compareTo(BigDecimal.ZERO) > 0
|
||||
|| nz(otherCost).compareTo(BigDecimal.ZERO) > 0;
|
||||
}
|
||||
|
||||
private static BigDecimal nz(BigDecimal v) {
|
||||
return v != null ? v : BigDecimal.ZERO;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验创建商品时规格编码的唯一性(公司维度)。
|
||||
*/
|
||||
@@ -262,6 +301,7 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
|
||||
// 先删除规格(须属于本商品),再更新已有规格,最后新增规格
|
||||
List<Long> rawCostChangedSpecIds = new ArrayList<>();
|
||||
LinkedHashSet<Long> newSpecIdsFromCodeWithOrders = new LinkedHashSet<>();
|
||||
if (updateProductDTO.getRemovedSpecIds() != null && !updateProductDTO.getRemovedSpecIds().isEmpty()) {
|
||||
for (Long specId : updateProductDTO.getRemovedSpecIds()) {
|
||||
if (specId == null) {
|
||||
@@ -364,19 +404,23 @@ public class ProductServiceImpl extends ServiceImpl<ProductMapper, ProductPO> im
|
||||
if (Boolean.TRUE.equals(createdVo.getCostChanged()) && createdVo.getId() != null) {
|
||||
rawCostChangedSpecIds.add(createdVo.getId());
|
||||
}
|
||||
appendSpecIdIfSpecCodeHasLinkedOrders(companyId, createdVo.getId(), specDTO.getSpecCode(),
|
||||
specDTO.getProductCost(), specDTO.getShippingCost(), specDTO.getOtherCost(),
|
||||
newSpecIdsFromCodeWithOrders);
|
||||
}
|
||||
}
|
||||
|
||||
// 返回结果:仅当成本变更且库中存在该规格的订单时,才提示同步(顺序与成本检测顺序一致)
|
||||
// 返回结果:成本变更且有关联订单的规格 ∪ 新增规格(编码非空)且有关联订单的规格
|
||||
UpdateProductResultDTO result = new UpdateProductResultDTO();
|
||||
List<Long> specIdsForSyncPrompt = List.of();
|
||||
LinkedHashSet<Long> syncUnion = new LinkedHashSet<>();
|
||||
if (!rawCostChangedSpecIds.isEmpty()) {
|
||||
Set<Long> withOrders = new HashSet<>(orderService.specIdsHavingOrders(companyId, rawCostChangedSpecIds));
|
||||
specIdsForSyncPrompt = rawCostChangedSpecIds.stream()
|
||||
rawCostChangedSpecIds.stream()
|
||||
.filter(withOrders::contains)
|
||||
.collect(Collectors.toList());
|
||||
.forEach(syncUnion::add);
|
||||
}
|
||||
result.setSpecIdsCostChangedWithLinkedOrders(specIdsForSyncPrompt);
|
||||
syncUnion.addAll(newSpecIdsFromCodeWithOrders);
|
||||
result.setSpecIdsCostChangedWithLinkedOrders(new ArrayList<>(syncUnion));
|
||||
|
||||
// 重新查询更新后的商品完整信息
|
||||
ProductPO updatedProduct = this.getById(id);
|
||||
|
||||
Reference in New Issue
Block a user