From dc67dd90c6840d0770cb5bcd3cf62c58f9e0bd14 Mon Sep 17 00:00:00 2001
From: KKSU <15274802129@163.com>
Date: Thu, 01 Feb 2024 16:02:35 +0800
Subject: [PATCH] 会员列表增加波卷功能
---
src/main/java/cc/mrbird/febs/mall/dto/MallMemberCouponDto.java | 3
src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsSkuMapper.java | 6 +
src/test/java/cc/mrbird/febs/AgentTest.java | 16 +++
src/main/java/cc/mrbird/febs/mall/service/IApiMallOrderInfoService.java | 3
src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallOrderInfoServiceImpl.java | 171 +++++++++++++++++++++++++---------
src/main/resources/mapper/modules/MallGoodsSkuMapper.xml | 26 +++++
src/main/java/cc/mrbird/febs/mall/mapper/CouponGoodsMapper.java | 2
src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberServiceImpl.java | 5
src/main/resources/mapper/modules/CouponGoodsMapper.xml | 9 +
9 files changed, 195 insertions(+), 46 deletions(-)
diff --git a/src/main/java/cc/mrbird/febs/mall/dto/MallMemberCouponDto.java b/src/main/java/cc/mrbird/febs/mall/dto/MallMemberCouponDto.java
index 55a07c1..44a3617 100644
--- a/src/main/java/cc/mrbird/febs/mall/dto/MallMemberCouponDto.java
+++ b/src/main/java/cc/mrbird/febs/mall/dto/MallMemberCouponDto.java
@@ -14,6 +14,9 @@
@ApiModelProperty(value = "商品ID", example = "1")
private Long goodsId;
+ @ApiModelProperty(value = "商品ID集合", example = "1")
+ private List<Long> goodsIdList;
+
@ApiModelProperty(value = "1:待使用 2:已使用 3:已过期", example = "1")
private Integer state;
diff --git a/src/main/java/cc/mrbird/febs/mall/mapper/CouponGoodsMapper.java b/src/main/java/cc/mrbird/febs/mall/mapper/CouponGoodsMapper.java
index 358ed36..e44943f 100644
--- a/src/main/java/cc/mrbird/febs/mall/mapper/CouponGoodsMapper.java
+++ b/src/main/java/cc/mrbird/febs/mall/mapper/CouponGoodsMapper.java
@@ -13,4 +13,6 @@
List<Long> selectByCouponId(Long id);
List<CouponGoods> selectByGoodIdAndCouponId(@Param("goodsId")Long goodsId, @Param("couponId")Long couponId);
+
+ List<Long> selectByGoodIdList(@Param("list")List<Long> goodsIdList);
}
diff --git a/src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsSkuMapper.java b/src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsSkuMapper.java
index 41152a7..7dc7f9f 100644
--- a/src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsSkuMapper.java
+++ b/src/main/java/cc/mrbird/febs/mall/mapper/MallGoodsSkuMapper.java
@@ -23,4 +23,10 @@
Integer upDateStockAndVolumeBySkuId(@Param("id")Long id, @Param("cnt")Integer cnt);
Integer updateStockAndVolumeBySkuId(@Param("id")Long id, @Param("cnt")Integer cnt);
+
+ List<Long> selectGoodsIdBySkuIds(@Param("list")List<Long> skuIds);
+
+ List<MallGoodsSku> selectSkuIdsByGoodsId(@Param("list")List<Long> goodsIdsAll);
+
+ List<MallGoodsSku> selectByIds(@Param("list")List<Long> skuIds);
}
diff --git a/src/main/java/cc/mrbird/febs/mall/service/IApiMallOrderInfoService.java b/src/main/java/cc/mrbird/febs/mall/service/IApiMallOrderInfoService.java
index 3c4c494..5632b26 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/IApiMallOrderInfoService.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/IApiMallOrderInfoService.java
@@ -7,6 +7,7 @@
import cc.mrbird.febs.mall.vo.OrderListVo;
import com.baomidou.mybatisplus.extension.service.IService;
+import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
@@ -22,6 +23,8 @@
OrderDetailVo findOrderDetailsById(Long id);
+ Map<Long, BigDecimal> getCouponAmountMap(Long memberCouponId, List<AddOrderItemDto> items);
+
void confirmOrder(Long id);
void delOrder(Long id);
diff --git a/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberServiceImpl.java b/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberServiceImpl.java
index 059ea38..1940992 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberServiceImpl.java
@@ -1004,7 +1004,10 @@
Long memberId = LoginUserUtil.getLoginUser().getId();
mallMemberCouponDto.setMemberId(memberId);
mallMemberCouponDto.setExpireTime(DateUtil.date());
- List<Long> couponIds = couponGoodsMapper.selectByGoodId(mallMemberCouponDto.getGoodsId());
+// List<Long> couponIds = couponGoodsMapper.selectByGoodId(mallMemberCouponDto.getGoodsId());
+ List<Long> goodsIdList = mallMemberCouponDto.getGoodsIdList();
+ List<Long> couponIds = couponGoodsMapper.selectByGoodIdList(goodsIdList);
+
List<MallMemberCouponVo> mallMemberCouponVos = new ArrayList<>();
if(CollUtil.isNotEmpty(couponIds)){
mallMemberCouponVos = mallMemberCouponMapper.selectListCreateInPage(mallMemberCouponDto,couponIds);
diff --git a/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallOrderInfoServiceImpl.java b/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallOrderInfoServiceImpl.java
index e7aa9be..924fbef 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallOrderInfoServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallOrderInfoServiceImpl.java
@@ -124,6 +124,24 @@
this.baseMapper.insert(orderInfo);
BigDecimal total = BigDecimal.ZERO;
+ /**
+ * 根据传入的优惠卷ID和商品明细集合计算出每个商品获取的实际支付金额
+ */
+ Map<Long, BigDecimal> couponAmountMap = new HashMap<>();
+ MallMemberCoupon mallMemberCoupon = new MallMemberCoupon();
+ if(ObjectUtil.isNotEmpty(addOrderDto.getMemberCouponId())){
+ Long memberCouponId = addOrderDto.getMemberCouponId();
+ mallMemberCoupon = mallMemberCouponMapper.selectById(memberCouponId);
+ if(ObjectUtil.isEmpty(mallMemberCoupon)){
+ throw new FebsException(mallMemberCoupon.getCouponName()+"无法使用");
+ }
+ Integer state = mallMemberCoupon.getState();
+ if(1 != state){
+ throw new FebsException(mallMemberCoupon.getCouponName()+"无法使用");
+ }
+ List<AddOrderItemDto> items = addOrderDto.getItems();
+ couponAmountMap = getCouponAmountMap(memberCouponId, items);
+ }
for (AddOrderItemDto item : addOrderDto.getItems()) {
MallOrderItem orderItem = new MallOrderItem();
@@ -201,49 +219,11 @@
//是否使用优惠卷
if(ObjectUtil.isNotEmpty(addOrderDto.getMemberCouponId())){
Long memberCouponId = addOrderDto.getMemberCouponId();
- MallMemberCoupon mallMemberCoupon = mallMemberCouponMapper.selectById(memberCouponId);
- if(ObjectUtil.isEmpty(mallMemberCoupon)){
- throw new FebsException(mallMemberCoupon.getCouponName()+"无法使用");
+ BigDecimal skuCouponAmount = couponAmountMap.get(item.getSkuId());//使用折扣卷后的应该支付的钱
+ if(skuCouponAmount.compareTo(BigDecimal.ZERO) > 0){
+ amount = skuCouponAmount;
}
- Integer state = mallMemberCoupon.getState();
- if(1 != state){
- throw new FebsException(mallMemberCoupon.getCouponName()+"无法使用");
- }
- //卷是否和商品绑定
- Long skuGoodsId = sku.getGoodsId();
- Long couponId = mallMemberCoupon.getCouponId();
- MallGoodsCoupon mallGoodsCoupon = mallGoodsCouponMapper.selectById(mallMemberCoupon.getCouponId());
- if(ObjectUtil.isEmpty(mallGoodsCoupon)){
- throw new FebsException(mallGoodsCoupon.getName()+"无法使用");
- }
- Integer stateCoupon = mallGoodsCoupon.getState();
- if(2 != stateCoupon){
- throw new FebsException(mallGoodsCoupon.getName()+"无法使用");
- }
- List<CouponGoods> couponGoods = couponGoodsMapper.selectByGoodIdAndCouponId(skuGoodsId, couponId);
- if(ObjectUtil.isEmpty(couponGoods)){
- throw new FebsException(sku.getGoodsName()+"无法使用"+mallMemberCoupon.getCouponName());
- }
-// Long goodsId = mallMemberCoupon.getGoodsId();
-// if(goodsId != sku.getGoodsId()){
-// throw new FebsException(sku.getGoodsName()+"无法使用"+mallMemberCoupon.getCouponName());
-// }
- BigDecimal costAmount = mallGoodsCoupon.getCostAmount();
- BigDecimal realAmount = mallGoodsCoupon.getRealAmount();
- if(amount.compareTo(costAmount) >= 0){
- Integer type = mallGoodsCoupon.getType();
- if(MallGoodsCoupon.TYPE_TWO == type){
- BigDecimal divideTime = amount.divide(costAmount, 0, BigDecimal.ROUND_DOWN);//累计减免次数
- BigDecimal multiply = divideTime.multiply(realAmount);//实际减免金额
- amount = (amount.subtract(multiply).compareTo(BigDecimal.ZERO) > 0 ? amount.subtract(multiply) : BigDecimal.ZERO);
- }
- if(MallGoodsCoupon.TYPE_ONE == type){
- amount = (amount.subtract(realAmount).compareTo(BigDecimal.ZERO) > 0 ? amount.subtract(realAmount) : BigDecimal.ZERO);
- }
- orderItem.setMemberCouponId(memberCouponId);
- mallMemberCoupon.setState(2);
- mallMemberCouponMapper.updateById(mallMemberCoupon);
- }
+ orderItem.setMemberCouponId(memberCouponId);
}
orderItem.setAmount(amount);
orderItem.setCnt(item.getCnt());
@@ -278,6 +258,9 @@
}
mallOrderItemMapper.insert(orderItem);
}
+
+ mallMemberCoupon.setState(2);
+ mallMemberCouponMapper.updateById(mallMemberCoupon);
//运费
BigDecimal delivaryAmount = addOrderDto.getDeliveryAmount() == null ? BigDecimal.ZERO : addOrderDto.getDeliveryAmount();
orderInfo.setCarriage(delivaryAmount);
@@ -301,10 +284,108 @@
return orderInfo.getId();
}
+ /**
+ * 根据传入的优惠卷ID和商品明细集合计算出每个商品获取的实际支付金额
+ * @param memberCouponId 优惠卷ID
+ * @param items 商品明细
+ * @return Map<Long,BigDecimal> skuId,实际支付金额
+ */
+ @Override
+ public Map<Long,BigDecimal> getCouponAmountMap(Long memberCouponId,List<AddOrderItemDto> items){
+ HashMap<Long,BigDecimal> couponAmountMap = new HashMap<>();
+ MallMemberCoupon mallMemberCoupon = mallMemberCouponMapper.selectById(memberCouponId);
+ if(ObjectUtil.isEmpty(mallMemberCoupon)){
+ return couponAmountMap;
+ }
+ Integer state = mallMemberCoupon.getState();
+ if(1 != state){
+ return couponAmountMap;
+ }
+ Long couponId = mallMemberCoupon.getCouponId();//优惠卷ID
+ List<Long> goodsIdsAll = couponGoodsMapper.selectByCouponId(couponId);
+ List<MallGoodsSku> skusAll = mallGoodsSkuMapper.selectSkuIdsByGoodsId(goodsIdsAll);//获取该优惠卷绑定的全部商品sku
+ List<Long> skuIdsAll = skusAll.stream().map(MallGoodsSku::getId).collect(Collectors.toList());//获取该优惠卷绑定的全部商品skuIDS
+ List<Long> skuIds = items.stream().map(AddOrderItemDto::getSkuId).collect(Collectors.toList());//订单中的全部skuIds
+
+ /**
+ * 获取这张优惠卷生效的skuIds
+ */
+ Set<Long> intersection = skuIdsAll.stream()
+ .filter(item -> skuIds.contains(item))
+ .collect(Collectors.toSet());//获取两个集合的交集
+ if(CollUtil.isEmpty(intersection)){
+ return couponAmountMap;
+ }
+ HashMap<Long,BigDecimal> skuMap = new HashMap<>();//每个订单详情的ID和amount的map
+ List<MallGoodsSku> skus = mallGoodsSkuMapper.selectByIds(new ArrayList<>(intersection));
+ BigDecimal totalAmount = BigDecimal.ZERO;//订单中的总金额
+ Map<Long, AddOrderItemDto> itemMap = new HashMap<>();// 预先将items转换为Map,以skuId作为键
+ for (AddOrderItemDto addOrderItemDto : items) {
+ itemMap.put(addOrderItemDto.getSkuId(), addOrderItemDto);
+ }
+ for(MallGoodsSku mallGoodsSku : skus){
+ Long id = mallGoodsSku.getId();
+ BigDecimal presentPrice = mallGoodsSku.getPresentPrice();
+ // 检查itemMap中是否存在该商品
+ AddOrderItemDto addOrderItemDto = itemMap.get(id);
+ if (addOrderItemDto != null) {
+ BigDecimal skuAmount = presentPrice.multiply(BigDecimal.valueOf(addOrderItemDto.getCnt()));
+ totalAmount = totalAmount.add(skuAmount);
+ skuMap.put(id,skuAmount);
+ }
+ }
+
+ /**
+ * 计算每个商品利用优惠卷打折的具体金额
+ */
+ MallGoodsCoupon mallGoodsCoupon = mallGoodsCouponMapper.selectById(mallMemberCoupon.getCouponId());
+ BigDecimal costAmount = mallGoodsCoupon.getCostAmount();
+ BigDecimal realAmount = mallGoodsCoupon.getRealAmount();
+ if(totalAmount.compareTo(costAmount) < 0){
+ return couponAmountMap;
+ }
+ HashMap<Long,BigDecimal> skuMapPercent = new HashMap<>();//每个订单详情的ID和amount的map
+ for (Map.Entry<Long,BigDecimal> entry : skuMap.entrySet()) {
+ Long keySkuId = entry.getKey();
+ BigDecimal valueSkuAmount = entry.getValue();
+ BigDecimal divide = valueSkuAmount.divide(totalAmount, 4, BigDecimal.ROUND_DOWN);//每个商品占符合满减的总额的比例
+ skuMapPercent.put(keySkuId,divide);
+ Integer type = mallGoodsCoupon.getType();
+ if(MallGoodsCoupon.TYPE_ONE == type){
+ BigDecimal bigDecimal = realAmount.multiply(divide).setScale(2, BigDecimal.ROUND_DOWN);//每个SKU的减免金额(比例*减免金额)
+ BigDecimal skuRealAmount = valueSkuAmount.subtract(bigDecimal.compareTo(BigDecimal.ZERO) > 0 ? bigDecimal : BigDecimal.ZERO);//实际支付金额
+ couponAmountMap.put(keySkuId,skuRealAmount);
+ }
+ if(MallGoodsCoupon.TYPE_TWO == type){
+ BigDecimal divideTime = totalAmount.divide(costAmount, 0, BigDecimal.ROUND_DOWN);//累计减免次数
+ BigDecimal multiply = divideTime.multiply(realAmount);//实际减免金额
+ BigDecimal bigDecimal = multiply.multiply(divide).setScale(2, BigDecimal.ROUND_DOWN);//每个SKU的减免金额(比例*实际减免金额)
+ BigDecimal skuRealAmount = valueSkuAmount.subtract(bigDecimal.compareTo(BigDecimal.ZERO) > 0 ? bigDecimal : BigDecimal.ZERO);//实际支付金额
+ couponAmountMap.put(keySkuId,skuRealAmount);
+ }
+ }
+ return couponAmountMap;
+ }
+
public static void main(String[] args) {
- BigDecimal bigDecimal = new BigDecimal(980);
- BigDecimal divide = bigDecimal.divide(new BigDecimal(100), 0, BigDecimal.ROUND_DOWN);
- System.out.println(divide);
+ List<Integer> a = new ArrayList<>();
+ a.add(1);
+ a.add(2);
+ a.add(3);
+ List<Integer> b = new ArrayList<>();
+ b.add(3);
+ b.add(4);
+ b.add(5);
+ boolean b1 = a.retainAll(b);
+ System.out.println(b1);
+ System.out.println(a);
+ System.out.println(b);
+ List<Integer> intersection = a.stream()
+ .filter(item -> b.contains(item))
+ .collect(Collectors.toList());
+
+ System.out.println(intersection);
+
}
diff --git a/src/main/resources/mapper/modules/CouponGoodsMapper.xml b/src/main/resources/mapper/modules/CouponGoodsMapper.xml
index c5f1acf..f3039df 100644
--- a/src/main/resources/mapper/modules/CouponGoodsMapper.xml
+++ b/src/main/resources/mapper/modules/CouponGoodsMapper.xml
@@ -21,4 +21,13 @@
and coupon_id = #{couponId}
</select>
+ <select id="selectByGoodIdList" resultType="java.lang.Long">
+ select a.coupon_id
+ from coupon_goods a
+ where goods_id IN
+ <foreach collection = "list" item = "item" separator="," open = "(" close = ")" >
+ #{item}
+ </foreach >
+ </select>
+
</mapper>
\ No newline at end of file
diff --git a/src/main/resources/mapper/modules/MallGoodsSkuMapper.xml b/src/main/resources/mapper/modules/MallGoodsSkuMapper.xml
index 2757dce..507ef26 100644
--- a/src/main/resources/mapper/modules/MallGoodsSkuMapper.xml
+++ b/src/main/resources/mapper/modules/MallGoodsSkuMapper.xml
@@ -52,4 +52,30 @@
sku_volume = sku_volume - #{cnt}
where id=#{id} and sku_volume - #{cnt} <![CDATA[ >= ]]> 0
</update>
+
+ <select id="selectGoodsIdBySkuIds" resultType="java.lang.Long">
+ select goods_id from mall_goods_sku
+ where id IN
+ <foreach collection = "list" item = "item" separator="," open = "(" close = ")" >
+ #{item}
+ </foreach >
+ </select>
+
+ <select id="selectSkuIdsByGoodsId" resultType="cc.mrbird.febs.mall.entity.MallGoodsSku">
+ select * from mall_goods_sku
+ where goods_id IN
+ <foreach collection = "list" item = "item" separator="," open = "(" close = ")" >
+ #{item}
+ </foreach >
+ order by id desc
+ </select>
+
+ <select id="selectByIds" resultType="cc.mrbird.febs.mall.entity.MallGoodsSku">
+ select * from mall_goods_sku
+ where id IN
+ <foreach collection = "list" item = "item" separator="," open = "(" close = ")" >
+ #{item}
+ </foreach >
+ order by id desc
+ </select>
</mapper>
\ No newline at end of file
diff --git a/src/test/java/cc/mrbird/febs/AgentTest.java b/src/test/java/cc/mrbird/febs/AgentTest.java
index 7be46b8..3a28cd9 100644
--- a/src/test/java/cc/mrbird/febs/AgentTest.java
+++ b/src/test/java/cc/mrbird/febs/AgentTest.java
@@ -12,6 +12,7 @@
import cc.mrbird.febs.mall.mapper.*;
import cc.mrbird.febs.mall.quartz.ProfitJob;
import cc.mrbird.febs.mall.service.IAgentService;
+import cc.mrbird.febs.mall.service.IApiMallOrderInfoService;
import cc.mrbird.febs.mall.vo.*;
import cc.mrbird.febs.pay.util.WechatConfigure;
import cc.mrbird.febs.rabbit.consumer.AgentConsumer;
@@ -82,6 +83,21 @@
private MallGoodsMapper mallGoodsMapper;
@Autowired
private MallGoodsImagesMapper goodsImagesMapper;
+ @Autowired
+ private IApiMallOrderInfoService iApiMallOrderInfoService;
+ @Test
+ public void getCouponAmountMap(){
+ Long memberCouponId = 139L;
+ List<AddOrderItemDto> items = new ArrayList<>();
+ AddOrderItemDto addOrderItemDto = new AddOrderItemDto();
+ addOrderItemDto.setSkuId(185L);
+ addOrderItemDto.setCnt(2);
+ items.add(addOrderItemDto);
+ Map<Long, BigDecimal> couponAmountMap = iApiMallOrderInfoService.getCouponAmountMap(memberCouponId, items);
+ for (Map.Entry<Long, BigDecimal> entry : couponAmountMap.entrySet()) {
+ System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue());
+ }
+ }
@Autowired
--
Gitblit v1.9.1