|
@@ -1,10 +1,35 @@
|
|
|
package com.chelvc.cloud.vehicle.server.service.impl;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.chelvc.cloud.vehicle.api.constant.CouponClaimType;
|
|
|
+import com.chelvc.cloud.vehicle.api.constant.PromotionStatus;
|
|
|
+import com.chelvc.cloud.vehicle.api.constant.UserCouponStatus;
|
|
|
+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;
|
|
|
import com.chelvc.cloud.vehicle.server.entity.UserCoupon;
|
|
|
+import com.chelvc.cloud.vehicle.server.service.CouponService;
|
|
|
import com.chelvc.cloud.vehicle.server.service.UserCouponService;
|
|
|
-import org.springframework.stereotype.Service;
|
|
|
+import com.chelvc.framework.base.context.SessionContextHolder;
|
|
|
+import com.chelvc.framework.base.exception.ResourceUnavailableException;
|
|
|
+import com.chelvc.framework.base.util.ResourceUtils;
|
|
|
+import com.chelvc.framework.common.model.Pagination;
|
|
|
+import com.chelvc.framework.database.util.PagingUtils;
|
|
|
+import lombok.NonNull;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import org.apache.dubbo.config.annotation.DubboService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* 优惠券领取业务操作实现
|
|
@@ -12,6 +37,138 @@ import org.springframework.stereotype.Service;
|
|
|
* @author Woody
|
|
|
* @date 2023/7/17
|
|
|
*/
|
|
|
-@Service
|
|
|
-public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements UserCouponService {
|
|
|
+@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
|
|
+@DubboService(interfaceClass = com.chelvc.cloud.vehicle.api.service.UserCouponService.class)
|
|
|
+public class UserCouponServiceImpl extends ServiceImpl<UserCouponMapper, UserCoupon> implements UserCouponService,
|
|
|
+ com.chelvc.cloud.vehicle.api.service.UserCouponService {
|
|
|
+
|
|
|
+ private final CouponService couponService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<UserCouponDTO> listUserCoupons(@NonNull UserCouponQueryParam param) {
|
|
|
+ Long userId = SessionContextHolder.getId();
|
|
|
+ // 查询过期的优惠券
|
|
|
+ if (UserCouponStatus.EXPIRED.equals(param.getStatus())) {
|
|
|
+ List<UserCoupon> userCoupons = this.baseMapper.listExpiredUserCoupons(userId, param);
|
|
|
+ if (CollectionUtils.isEmpty(userCoupons)) {
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ return userCoupons
|
|
|
+ .stream()
|
|
|
+ .map(o -> UserCouponCopier.INSTANCE.copying(o, o.getCoupon()))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ }
|
|
|
+ // 查询未使用或已使用的优惠券
|
|
|
+ List<UserCoupon> userCoupons = this.baseMapper.listUserCoupons(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 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);
|
|
|
+ this.save(userCoupon);
|
|
|
+ return userCoupon.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateUserCoupon(@NonNull Long id, @NonNull UserCouponModifyParam param) {
|
|
|
+ UserCoupon userCoupon = ResourceUtils.required(this.getById(id), "优惠券领取记录不存在");
|
|
|
+ UserCouponCopier.INSTANCE.copying(param, userCoupon);
|
|
|
+ this.updateById(userCoupon);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public UserCouponDTO getUserCoupon(@NonNull Long id) {
|
|
|
+ return this.convert(this.getById(id));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Pagination<UserCouponDTO> getUserCouponPaging(@NonNull UserCouponPagingParam param) {
|
|
|
+ // 查询优惠券领取记录列表
|
|
|
+ Page<UserCoupon> page = this.lambdaQuery()
|
|
|
+ .eq(Objects.nonNull(param.getType()), UserCoupon::getType, param.getType().name())
|
|
|
+ .eq(UserCouponStatus.USED.equals(param.getStatus()), UserCoupon::getUsed, 1)
|
|
|
+ .eq(UserCouponStatus.UNUSED.equals(param.getStatus()), UserCoupon::getUsed, 0)
|
|
|
+ .orderByDesc(UserCoupon::getId).page(PagingUtils.convert(param.getPaging()));
|
|
|
+ List<UserCoupon> records = page.getRecords();
|
|
|
+ if (CollectionUtils.isEmpty(records)) {
|
|
|
+ return Pagination.empty();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建优惠券领取记录信息
|
|
|
+ List<UserCouponDTO> userCoupons = records.stream().map(this::convert).collect(Collectors.toList());
|
|
|
+ return PagingUtils.convert(page, userCoupons);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Long claimCoupon(@NonNull Long couponId) {
|
|
|
+ Coupon coupon = ResourceUtils.required(couponService.getById(couponId), "优惠券不存在");
|
|
|
+ if (CouponClaimType.FREE.equals(coupon.getClaimType())) {
|
|
|
+ throw new ResourceUnavailableException("当前优惠券不允许免费领取");
|
|
|
+ }
|
|
|
+ Long userId = SessionContextHolder.getId();
|
|
|
+ // 检查该用户可领取的优惠券的数量
|
|
|
+ this.checkCouponLimit(coupon, userId);
|
|
|
+ // 保存优惠券领取记录
|
|
|
+ UserCoupon userCoupon = UserCoupon.builder().userId(userId).couponId(couponId)
|
|
|
+ .type(coupon.getType()).used(false).build();
|
|
|
+ this.save(userCoupon);
|
|
|
+ // 修改优惠券的领取数量
|
|
|
+ couponService.updateCouponReceivedNum(couponId, 1);
|
|
|
+ return userCoupon.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查该用户可领取的优惠券的数量
|
|
|
+ *
|
|
|
+ * @param coupon 要领取的优惠券
|
|
|
+ * @param userId 用户ID
|
|
|
+ */
|
|
|
+ public void checkCouponLimit(Coupon coupon, Long userId) {
|
|
|
+ Integer haveCoupons = this.lambdaQuery()
|
|
|
+ .eq(UserCoupon::getUserId, userId)
|
|
|
+ .eq(UserCoupon::getCouponId, coupon.getId()).count();
|
|
|
+ if (!PromotionStatus.START.name().equals(coupon.getPromotionStatus())) {
|
|
|
+ throw new ResourceUnavailableException("当前优惠券已经被领取完了,下次要早点来哦");
|
|
|
+ }
|
|
|
+ if (coupon.getPublishNum() != 0 && coupon.getReceivedNum() >= coupon.getPublishNum()) {
|
|
|
+ throw new ResourceUnavailableException("优惠券剩余领取数量不足");
|
|
|
+ }
|
|
|
+ if (!coupon.getLimitNum().equals(0) && haveCoupons >= coupon.getLimitNum()) {
|
|
|
+ throw new ResourceUnavailableException("超出领取限制,此优惠券最多领取" + coupon.getLimitNum() + "张");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 转换优惠券领取记录信息
|
|
|
+ *
|
|
|
+ * @param userCoupon 优惠券领取记录实例
|
|
|
+ * @return 优惠券领取记录信息
|
|
|
+ */
|
|
|
+ private UserCouponDTO convert(UserCoupon userCoupon) {
|
|
|
+ return UserCouponCopier.INSTANCE.copying(userCoupon);
|
|
|
+ }
|
|
|
}
|