Переглянути джерело

获取当前用户对于当前商品可使用的优惠券列表接口开发

WangChanghua 1 рік тому
батько
коміт
1771afff2a

+ 6 - 1
vehicle-api/src/main/java/com/chelvc/cloud/vehicle/api/constant/UserCouponStatus.java

@@ -24,7 +24,12 @@ public enum UserCouponStatus implements Enumeration {
     /**
      * 已过期
      */
-    EXPIRED("已过期");
+    EXPIRED("已过期"),
+
+    /**
+     * 作废
+     */
+    CLOSED("作废");
 
     /**
      * 状态描述

+ 17 - 0
vehicle-api/src/main/java/com/chelvc/cloud/vehicle/api/param/UserCouponQueryParam.java

@@ -11,6 +11,8 @@ import javax.validation.constraints.Max;
 import javax.validation.constraints.Min;
 import javax.validation.constraints.NotNull;
 import java.io.Serializable;
+import java.math.BigDecimal;
+import java.util.List;
 
 /**
  * 用户优惠券查询参数
@@ -49,4 +51,19 @@ public class UserCouponQueryParam implements Serializable {
      */
     @NotNull(message = "用户优惠券状态不能为空")
     private UserCouponStatus status;
+
+    /**
+     * 总金额(元)
+     */
+    private BigDecimal totalAmount;
+
+    /**
+     * 商家ID列表
+     */
+    private List<String> merchantIds;
+
+    /**
+     * 商家ID,如有多个','分割
+     */
+    private String merchantId;
 }

+ 8 - 0
vehicle-api/src/main/java/com/chelvc/cloud/vehicle/api/service/UserCouponService.java

@@ -24,6 +24,14 @@ public interface UserCouponService {
      */
     List<UserCouponDTO> listUserCoupons(UserCouponQueryParam param);
 
+    /**
+     * 获取当前用户对于当前商品可使用的优惠券列表
+     *
+     * @param param 查询参数
+     * @return 用户可用优惠券信息列表
+     */
+    List<UserCouponDTO> listUserCanUseCoupons(UserCouponQueryParam param);
+
     /**
      * 新增优惠券领取记录
      *

+ 19 - 0
vehicle-api/src/main/java/com/chelvc/cloud/vehicle/api/util/PromotionUtils.java

@@ -0,0 +1,19 @@
+package com.chelvc.cloud.vehicle.api.util;
+
+import lombok.extern.slf4j.Slf4j;
+
+/**
+ * 优惠活动通用验证类
+ *
+ * @author qizai
+ * @date 2023/10/16
+ */
+@Slf4j
+public class PromotionUtils {
+
+    public static final String START_TIME_COLUMN = "start_time";
+    public static final String END_TIME_COLUMN = "end_time";
+    public static final String PLATFORM_ID = "0";
+    public static final String PLATFORM_NAME = "platform";
+
+}

+ 8 - 0
vehicle-server/src/main/java/com/chelvc/cloud/vehicle/server/dao/UserCouponMapper.java

@@ -32,4 +32,12 @@ public interface UserCouponMapper extends BaseMapper<UserCoupon> {
      * @return 用户优惠券信息列表
      */
     List<UserCoupon> listUserCoupons(@Param("userId") Long userId, @Param("param") UserCouponQueryParam param);
+
+    /**
+     * 获取当前用户对于当前商品可使用的优惠券列表
+     *
+     * @param param 查询参数
+     * @return 用户可用优惠券信息列表
+     */
+    List<UserCoupon> listUserCanUseCoupons(@Param("userId") Long userId, @Param("param") UserCouponQueryParam param);
 }

+ 19 - 3
vehicle-server/src/main/java/com/chelvc/cloud/vehicle/server/service/impl/UserCouponServiceImpl.java

@@ -10,6 +10,7 @@ import com.chelvc.cloud.vehicle.api.dto.UserCouponDTO;
 import com.chelvc.cloud.vehicle.api.param.UserCouponModifyParam;
 import com.chelvc.cloud.vehicle.api.param.UserCouponPagingParam;
 import com.chelvc.cloud.vehicle.api.param.UserCouponQueryParam;
+import com.chelvc.cloud.vehicle.api.util.PromotionUtils;
 import com.chelvc.cloud.vehicle.server.copier.UserCouponCopier;
 import com.chelvc.cloud.vehicle.server.dao.UserCouponMapper;
 import com.chelvc.cloud.vehicle.server.entity.Coupon;
@@ -27,9 +28,7 @@ import org.apache.dubbo.config.annotation.DubboService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.util.CollectionUtils;
 
-import java.util.Collections;
-import java.util.List;
-import java.util.Objects;
+import java.util.*;
 import java.util.stream.Collectors;
 
 /**
@@ -70,6 +69,23 @@ public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCou
                 .collect(Collectors.toList());
     }
 
+    @Override
+    public List<UserCouponDTO> listUserCanUseCoupons(@NonNull UserCouponQueryParam param) {
+        List<String> merchantIds = new ArrayList<>(Arrays.asList(param.getMerchantId().split(",")));
+        merchantIds.add(PromotionUtils.PLATFORM_ID);
+        param.setMerchantIds(merchantIds);
+        Long userId = SessionContextHolder.getId();
+        // 当前用户对于当前商品可使用的优惠券列表
+        List<UserCoupon> userCoupons = this.baseMapper.listUserCanUseCoupons(userId, param);
+        if (CollectionUtils.isEmpty(userCoupons)) {
+            return Collections.emptyList();
+        }
+        return userCoupons
+                .stream()
+                .map(o -> UserCouponCopier.INSTANCE.copying(o, o.getCoupon()))
+                .collect(Collectors.toList());
+    }
+
     @Override
     public Long addUserCoupon(@NonNull UserCouponModifyParam param) {
         UserCoupon userCoupon = UserCouponCopier.INSTANCE.copying(param);

+ 23 - 0
vehicle-server/src/main/resources/mapper/UserCouponMapper.xml

@@ -53,4 +53,27 @@
         order by u.create_time desc
         limit #{param.size}
     </select>
+
+    <select id="listUserCanUseCoupons" resultMap="USERCOUPON_RESULT_MAP">
+        select ucn.id, ucn.coupon_id, cn.name, cn.amount, cn.reduce, cn.discount, cn.description,cn.expiration,
+        cn.merchant_id
+        from `user_coupon` ucn
+        inner join `coupon` cn on ucn.coupon_id = cn.id and ucn.user_id = #{userId}
+        <where>
+            ucn.used = 0
+            <if test="merchantIds != null and !merchantIds.isEmpty()">
+                and cn.merchant_id in
+                <foreach collection="merchantIds" item="id" open="(" close=")" separator=",">
+                    #{id}
+                </foreach>
+            </if>
+            and cn.expiration > now() and cn.status = 'ONLINE'
+            <if test="param.offset != null">
+                and ucn.id > #{param.offset}
+            </if>
+        </where>
+        order by ucn.create_time desc
+        limit #{param.size}
+    </select>
+
 </mapper>