Browse Source

机构购物车列表优惠券

chao 3 years ago
parent
commit
0c89228c1e

+ 6 - 3
src/main/java/com/caimei365/order/controller/CartClubApi.java

@@ -34,13 +34,16 @@ public class CartClubApi {
      * 购物车列表详细数据
      */
     @ApiOperation("购物车列表详细数据(旧:/shoppingCart/list)")
-    @ApiImplicitParam(required = true, name = "userId", value = "用户Id")
+    @ApiImplicitParams({
+            @ApiImplicitParam(required = true, name = "userId", value = "用户Id"),
+            @ApiImplicitParam(required = false, name = "source", value = "来源 : 1 网站 ; 2 小程序")
+    })
     @GetMapping("/cart/list")
-    public ResponseJson<Map<String,Object>> getShoppingCartList(Integer userId) {
+    public ResponseJson<Map<String,Object>> getShoppingCartList(Integer userId, Integer source) {
         if (null == userId) {
             return ResponseJson.error("用户Id不能为空!", null);
         }
-        return cartClubService.getShoppingCartList(userId);
+        return cartClubService.getShoppingCartList(userId, source);
     }
 
     /**

+ 10 - 1
src/main/java/com/caimei365/order/mapper/CartClubMapper.java

@@ -4,6 +4,7 @@ import com.caimei365.order.model.dto.CartDto;
 import com.caimei365.order.model.po.CartPo;
 import com.caimei365.order.model.vo.CartItemVo;
 import com.caimei365.order.model.vo.CartShopVo;
+import com.caimei365.order.model.vo.CouponVo;
 import com.caimei365.order.model.vo.PromotionsVo;
 import org.apache.ibatis.annotations.Mapper;
 import org.apache.ibatis.annotations.Param;
@@ -65,5 +66,13 @@ public interface CartClubMapper {
      * @param userId 用户Id
      */
     List<CartItemVo> getCartProductsByShopIdAndProductIds(@Param("userId") Integer userId,@Param("shopId")  Integer shopId, @Param("productIds") List<String> productIds);
-
+    /**
+     * 获取用户可用优惠券列表
+     * @param userId 用户Id
+     */
+    List<CouponVo> getClubCouponList(Integer userId);
+    /**
+     * 活动券商品Ids
+     */
+    List<Integer> getCouponProductIds(Integer couponId, Integer source);
 }

+ 4 - 0
src/main/java/com/caimei365/order/model/vo/CartItemVo.java

@@ -106,4 +106,8 @@ public class CartItemVo implements Serializable {
      * 前端商品勾选状态
      */
     private Boolean isChecked;
+    /**
+     * 商品属性:1产品,2仪器
+     */
+    private Integer commodityType;
 }

+ 4 - 5
src/main/java/com/caimei365/order/model/vo/CartShopVo.java

@@ -46,13 +46,12 @@ public class CartShopVo implements Serializable {
      * 商品列表
      */
     private List<CartItemVo> cartList;
-
     /**
      * 促销活动
      */
     private PromotionsVo promotions;
-
-
-
-
+    /**
+     * 领券标识
+     */
+    private Boolean couponsLogo;
 }

+ 95 - 0
src/main/java/com/caimei365/order/model/vo/CouponVo.java

@@ -0,0 +1,95 @@
+package com.caimei365.order.model.vo;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.Date;
+import java.util.List;
+
+/**
+ * Description
+ *
+ * @author : Charles
+ * @date : 2021/8/30
+ */
+@Data
+public class CouponVo implements Serializable {
+    /**
+     * 用户关联优惠id
+     */
+    private Integer clubCouponId;
+
+    /**
+     * 优惠券id
+     */
+    private Integer couponId;
+
+    /**
+     * 优惠券金额(面值)
+     */
+    private BigDecimal couponAmount;
+
+    /**
+     * 优惠满减条件金额
+     */
+    private BigDecimal touchPrice;
+
+    /**
+     * 使用开始时间(有效期)
+     */
+    @JsonFormat(pattern = "yyyy.MM.dd", timezone = "GMT+8")
+    private Date startDate;
+
+    /**
+     * 使用结束时间(有效期)
+     */
+    @JsonFormat(pattern = "yyyy.MM.dd", timezone = "GMT+8")
+    private Date endDate;
+
+    /**
+     * 劵类型 0活动券 1品类券 2用户专享券 3店铺券 4新用户券
+     */
+    private Integer couponType;
+
+    /**
+     * 机构用户id(用户专享券有效)
+     */
+    private Integer userId;
+
+    /**
+     * 供应商id(店铺券有效)
+     */
+    private Integer shopId;
+
+    /**
+     * 优惠商品:1全商城商品 2指定商品(活动券有效)
+     */
+    private Integer productType;
+
+    /**
+     * 优惠品类:1产品 2仪器(品类券有效)
+     */
+    private Integer categoryType;
+
+    /**
+     * 供应商名称
+     */
+    private String shopName;
+
+    /**
+     * 使用状态: 1未使用 2已使用 3已失效
+     */
+    private Integer useStatus;
+
+    /**
+     * 领取状态: 0未领取 1已领取 (前端使用)
+     */
+    private Integer couponBtnType = 0;
+
+    /**
+     * 活动券-指定商品下商品id集合
+     */
+    private List<Integer> productIds;
+}

+ 2 - 1
src/main/java/com/caimei365/order/service/CartClubService.java

@@ -16,8 +16,9 @@ public interface CartClubService {
     /**
      * 购物车列表详细数据
      * @param userId 用户Id
+     * @param source 来源 : 1 网站 ; 2 小程序
      */
-    ResponseJson<Map<String, Object>> getShoppingCartList(Integer userId);
+    ResponseJson<Map<String, Object>> getShoppingCartList(Integer userId, Integer source);
 
     /**
      * 网站顶部购物车数据

+ 113 - 1
src/main/java/com/caimei365/order/service/impl/CartClubServiceImpl.java

@@ -12,6 +12,7 @@ import com.caimei365.order.utils.MathUtil;
 import com.google.common.collect.Lists;
 import com.google.common.util.concurrent.AtomicDouble;
 import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang.StringUtils;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
@@ -46,9 +47,10 @@ public class CartClubServiceImpl implements CartClubService {
      * 购物车列表详细数据
      *
      * @param userId 用户Id
+     * @param source 来源 : 1 网站 ; 2 小程序
      */
     @Override
-    public ResponseJson<Map<String, Object>> getShoppingCartList(Integer userId) {
+    public ResponseJson<Map<String, Object>> getShoppingCartList(Integer userId, Integer source) {
         /*
          * 初始化返回数据
          */
@@ -72,6 +74,9 @@ public class CartClubServiceImpl implements CartClubService {
         // 促销活动(总)
         List<PromotionsVo> totalPromotions = new ArrayList<>();
         List<Integer> promotionsIds = new ArrayList<>();
+        // 用户可用优惠券(总)
+        List<CouponVo> couponList = cartClubMapper.getClubCouponList(userId);
+        List<CartItemVo> cartAllProducts = new ArrayList<>();
         // 用户身份
         Integer userIdentity = baseMapper.getIdentityByUserId(userId);
         if (null != shopInfoList && shopInfoList.size()>0) {
@@ -191,6 +196,8 @@ public class CartClubServiceImpl implements CartClubService {
                         productIterator.remove();
                     }
                 }
+                cartAllProducts.addAll(productList);
+
                 // 供应商下商品种类
                 shop.setCount(shopKindCount.get());
                 if (shopKindCount.get() > 0) {
@@ -226,6 +233,10 @@ public class CartClubServiceImpl implements CartClubService {
                             }
                         }
                     }
+                    // 是否可领取优惠券
+                    Boolean couponsLogo = setCouponsLogo(shop.getShopId(), source, productList, couponList);
+                    shop.setCouponsLogo(couponsLogo);
+
                     // 供应商商品
                     shop.setCartList(productList);
                     // 供应商总价
@@ -261,6 +272,8 @@ public class CartClubServiceImpl implements CartClubService {
                 }
             });
         }
+        // 过滤与当前购物车商品无关的优惠券
+        filterCoupon(source, cartAllProducts, couponList);
         /*
          * 返回结果
          */
@@ -273,10 +286,109 @@ public class CartClubServiceImpl implements CartClubService {
         resultMap.put("reducedPrice", reducedPrice);
         resultMap.put("totalOriginalPrice", totalOriginalPrice);
         resultMap.put("promotions", totalPromotions);
+        resultMap.put("couponList", couponList);
         // 返回数据
         return ResponseJson.success(resultMap);
     }
 
+    /**
+     * 过滤与当前商品列表无关的优惠券
+     */
+    private void filterCoupon(Integer source, List<CartItemVo> cartItems, List<CouponVo> couponList) {
+        if (couponList != null && couponList.size() > 0) {
+            Iterator<CouponVo> iterator = couponList.iterator();
+            A:
+            while (iterator.hasNext()) {
+                CouponVo coupon = iterator.next();
+                if (coupon.getCouponType() == 0 && coupon.getProductType() == 2) {
+                    // 活动券商品Ids
+                    List<Integer> productIds = cartClubMapper.getCouponProductIds(coupon.getCouponId(), source);
+                    coupon.setProductIds(productIds);
+                    for (CartItemVo item : cartItems) {
+                        if (!productIds.contains(item.getProductId())) {
+                            iterator.remove();
+                            continue A;
+                        }
+                    }
+                }
+                if (coupon.getCouponType() == 1) {
+                    for (CartItemVo item : cartItems) {
+                        if (!coupon.getCategoryType().equals(item.getCommodityType())) {
+                            iterator.remove();
+                            continue A;
+                        }
+                    }
+                }
+                if (coupon.getCouponType() == 3) {
+                    for (CartItemVo item : cartItems) {
+                        if (!coupon.getShopId().equals(item.getShopId())) {
+                            iterator.remove();
+                            continue A;
+                        }
+                    }
+                }
+                if (coupon.getCouponType() == 3 && coupon.getShopId() != null) {
+                    //店铺券
+                    String shopName = baseMapper.getShopNameById(coupon.getShopId());
+                    if (StringUtils.isNotBlank(shopName)) {
+                        if (shopName.length() > 7) {
+                            shopName = shopName.substring(0, 6) + "...";
+                        }
+                        coupon.setShopName(shopName);
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * 优惠券标识是否显示
+     */
+    public Boolean setCouponsLogo(Integer shopId, Integer source, List<CartItemVo> cartItems,  List<CouponVo> couponList) {
+        boolean couponsLogo = false;
+        if (couponList != null && couponList.size() > 0) {
+            A:
+            for (CouponVo coupon : couponList) {
+                if (coupon.getCouponType() == 4 || coupon.getCouponType() == 2) {
+                    //用户券与用户专享券
+                    couponsLogo = true;
+                    break;
+                }
+                if (coupon.getCouponType() == 0) {
+                    if (coupon.getProductType() == 1) {
+                        //活动券-全商城商品
+                        couponsLogo = true;
+                        break;
+                    } else {
+                        // 活动券商品Ids
+                        List<Integer> productIds = cartClubMapper.getCouponProductIds(coupon.getCouponId(), source);
+                        for (CartItemVo item : cartItems) {
+                            if (productIds.contains(item.getProductId())) {
+                                couponsLogo = true;
+                                break A;
+                            }
+                        }
+                    }
+                }
+                if (coupon.getCouponType() == 3 && shopId.equals(coupon.getShopId())) {
+                    //店铺券
+                    couponsLogo = true;
+                    break;
+                }
+                if (coupon.getCouponType() == 1) {
+                    //品类券
+                    for (CartItemVo item : cartItems) {
+                        if (coupon.getCategoryType().equals(item.getCommodityType())) {
+                            couponsLogo = true;
+                            break A;
+                        }
+                    }
+                }
+            }
+        }
+        return couponsLogo;
+    }
+
     /**
      * 网站顶部购物车数据
      *

+ 34 - 0
src/main/resources/mapper/CartClubMapper.xml

@@ -120,6 +120,40 @@
         ORDER BY c.cm_cartID DESC
         limit 1
     </select>
+    <select id="getClubCouponList" resultType="com.caimei365.order.model.vo.CouponVo">
+        SELECT
+            a.id AS clubCouponId,
+            cc.`id` AS couponId,
+            cc.`couponAmount`,
+            cc.`touchPrice`,
+            cc.`startDate`,
+            cc.`endDate`,
+            cc.`couponType`,
+            cc.`userId`,
+            cc.`shopId`,
+            cc.`productType`,
+            cc.`categoryType`
+        FROM cm_coupon_club a
+        LEFT JOIN cm_coupon cc ON a.couponId = cc.id
+        WHERE cc.delFlag = 0
+            AND a.delFlag = 0
+            AND a.userId = #{userId}
+            AND a.status = 1
+            AND NOW() BETWEEN cc.startDate
+            AND cc.endDate
+            AND cc.status != 2
+        ORDER BY a.createDate DESC
+    </select>
+    <select id="getCouponProductIds" resultType="java.lang.Integer">
+        SELECT productId FROM cm_coupon_product
+        WHERE couponId = #{couponId}
+        <if test="source == 1">
+            AND pcStatus = 1
+        </if>
+        <if test="source == 2">
+            AND appletsStatus = 1
+        </if>
+    </select>
     <update id="updateCart" parameterType="com.caimei365.order.model.po.CartPo">
         UPDATE cm_cart set
            productCount = #{productCount},