map映射

This commit is contained in:
2025-11-25 14:30:35 +08:00
parent 03a98de023
commit 043d09bee8
4 changed files with 16 additions and 9 deletions

View File

@@ -16,6 +16,7 @@ import java.time.LocalDate;
import java.util.List; import java.util.List;
/** /**
* 统计任务
* @author Administrator * @author Administrator
*/ */
@Component @Component

View File

@@ -15,4 +15,6 @@ public class ProductCostAmountVO {
private Long productId; private Long productId;
private Long skuId; private Long skuId;
private Long count; private Long count;
private BigDecimal costAmount;
} }

View File

@@ -268,7 +268,7 @@ public interface ShopOrderStatisticMapper extends BaseMapper<ShopOrderStatistic>
" product.shop_id = #{shopId} " + " product.shop_id = #{shopId} " +
" AND sku.cost_price > 0") " AND sku.cost_price > 0")
@MapKey("skuId") @MapKey("skuId")
Map<Long, BigDecimal> getSkuCostAmount(Long shopId); List<ProductCostAmountVO> getSkuCostAmount(Long shopId);
@Select("SELECT" + @Select("SELECT" +
" relation.product_id as productId," + " relation.product_id as productId," +
@@ -279,8 +279,7 @@ public interface ShopOrderStatisticMapper extends BaseMapper<ShopOrderStatistic>
" WHERE" + " WHERE" +
" relation.shop_id = #{shopId} " + " relation.shop_id = #{shopId} " +
" order by relation.product_id") " order by relation.product_id")
@MapKey("productId") List<ProductCostAmountVO> getConsCostAmount(Long shopId);
Map<Long, BigDecimal> getConsCostAmount(Long shopId);
@Select("SELECT " + @Select("SELECT " +

View File

@@ -23,6 +23,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
/** /**
@@ -192,9 +193,9 @@ public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisti
*/ */
private BigDecimal getProductCostAmount(Long shopId, LocalDate day) { private BigDecimal getProductCostAmount(Long shopId, LocalDate day) {
//获取商品sku成本价 productId skuId price //获取商品sku成本价 productId skuId price
Map<Long, BigDecimal> skuCostAmountList = mapper.getSkuCostAmount(shopId); List<ProductCostAmountVO> skuCostAmountList = mapper.getSkuCostAmount(shopId);
// //获取商品耗材成本价 productId price // //获取商品耗材成本价 productId price
Map<Long, BigDecimal> consCostAmountList = mapper.getConsCostAmount(shopId); List<ProductCostAmountVO> consCostAmountList = mapper.getConsCostAmount(shopId);
//获取orderDetail信息 productId skuId 数量 //获取orderDetail信息 productId skuId 数量
BigDecimal productCostAmount = BigDecimal.ZERO; BigDecimal productCostAmount = BigDecimal.ZERO;
if (CollUtil.isEmpty(skuCostAmountList) && CollUtil.isEmpty(consCostAmountList)) { if (CollUtil.isEmpty(skuCostAmountList) && CollUtil.isEmpty(consCostAmountList)) {
@@ -204,12 +205,16 @@ public class ShopOrderStatisticServiceImpl extends ServiceImpl<ShopOrderStatisti
if (CollUtil.isEmpty(orderDetailProduct)) { if (CollUtil.isEmpty(orderDetailProduct)) {
return productCostAmount; return productCostAmount;
} }
Map<Long, BigDecimal> skuCostAmountMap = skuCostAmountList.stream()
.collect(Collectors.toMap(ProductCostAmountVO::getSkuId, ProductCostAmountVO::getCostAmount));
Map<Long, BigDecimal> consCostAmountMap = consCostAmountList.stream()
.collect(Collectors.toMap(ProductCostAmountVO::getProductId, ProductCostAmountVO::getCostAmount));
for (ProductCostAmountVO productCostAmountVO : orderDetailProduct) { for (ProductCostAmountVO productCostAmountVO : orderDetailProduct) {
if (skuCostAmountList.containsKey(productCostAmountVO.getSkuId())) { if (skuCostAmountMap.containsKey(productCostAmountVO.getSkuId())) {
BigDecimal costAmount = skuCostAmountList.get(productCostAmountVO.getSkuId()); BigDecimal costAmount = skuCostAmountMap.get(productCostAmountVO.getSkuId());
productCostAmount = productCostAmount.add(costAmount.multiply(new BigDecimal(productCostAmountVO.getCount())).setScale(2, RoundingMode.HALF_UP)); productCostAmount = productCostAmount.add(costAmount.multiply(new BigDecimal(productCostAmountVO.getCount())).setScale(2, RoundingMode.HALF_UP));
} else if (consCostAmountList.containsKey(productCostAmountVO.getProductId())) { } else if (consCostAmountMap.containsKey(productCostAmountVO.getProductId())) {
BigDecimal costAmount = consCostAmountList.get(productCostAmountVO.getProductId()); BigDecimal costAmount = consCostAmountMap.get(productCostAmountVO.getProductId());
productCostAmount = productCostAmount.add(costAmount.multiply(new BigDecimal(productCostAmountVO.getCount())).setScale(2, RoundingMode.HALF_UP)); productCostAmount = productCostAmount.add(costAmount.multiply(new BigDecimal(productCostAmountVO.getCount())).setScale(2, RoundingMode.HALF_UP));
} }
} }