|
@@ -2,16 +2,19 @@ package com.cyksj.service.coupon.impl;
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
import cn.hutool.core.date.DateTime;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
|
|
+import cn.hutool.core.lang.Assert;
|
|
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
import com.cyksj.common.util.Jsons;
|
|
|
|
|
+import com.cyksj.enums.GatewayApiCode;
|
|
|
|
|
+import com.cyksj.mapper.UserMapper;
|
|
|
|
|
+import com.cyksj.mapper.channel.ChannelPopularizeMapper;
|
|
|
import com.cyksj.mapper.manage.coupon.*;
|
|
import com.cyksj.mapper.manage.coupon.*;
|
|
|
-import com.cyksj.model.entity.Coupon;
|
|
|
|
|
-import com.cyksj.model.entity.CouponActive;
|
|
|
|
|
-import com.cyksj.model.entity.CouponUser;
|
|
|
|
|
-import com.cyksj.model.entity.OrderDon;
|
|
|
|
|
|
|
+import com.cyksj.model.entity.*;
|
|
|
|
|
+import com.cyksj.model.request.ChannelPopularizeCouponReq;
|
|
|
import com.cyksj.model.request.CouponCollectReq;
|
|
import com.cyksj.model.request.CouponCollectReq;
|
|
|
import com.cyksj.model.views.*;
|
|
import com.cyksj.model.views.*;
|
|
|
import com.cyksj.service.coupon.CouponFontService;
|
|
import com.cyksj.service.coupon.CouponFontService;
|
|
@@ -54,10 +57,14 @@ public class CouponFontServiceImpl implements CouponFontService {
|
|
|
|
|
|
|
|
private final CouponRecommendMapper recommendMapper;
|
|
private final CouponRecommendMapper recommendMapper;
|
|
|
|
|
|
|
|
|
|
+ private final ChannelPopularizeMapper channelPopularizeMapper;
|
|
|
|
|
+
|
|
|
|
|
+ private final UserMapper userMapper;
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
@Transactional(rollbackFor = Throwable.class)
|
|
@Transactional(rollbackFor = Throwable.class)
|
|
|
public void collectCoupon(CouponCollectReq couponCollectReq, Long userId) {
|
|
public void collectCoupon(CouponCollectReq couponCollectReq, Long userId) {
|
|
|
- collectCommonCheck(couponCollectReq, userId);
|
|
|
|
|
|
|
+ collectCommonCheck(couponCollectReq.getCouponId(), userId);
|
|
|
CouponActive couponActive = couponActiveMapper.selectById(couponCollectReq.getCouponActiveId());
|
|
CouponActive couponActive = couponActiveMapper.selectById(couponCollectReq.getCouponActiveId());
|
|
|
if (couponActive == null) {
|
|
if (couponActive == null) {
|
|
|
throw BusinessRuntimeException.getInstance("该发放优惠券不存在");
|
|
throw BusinessRuntimeException.getInstance("该发放优惠券不存在");
|
|
@@ -65,21 +72,8 @@ public class CouponFontServiceImpl implements CouponFontService {
|
|
|
if (couponActive.getRemainNum() <= 0) {
|
|
if (couponActive.getRemainNum() <= 0) {
|
|
|
throw BusinessRuntimeException.getInstance("该优惠券已领取完");
|
|
throw BusinessRuntimeException.getInstance("该优惠券已领取完");
|
|
|
}
|
|
}
|
|
|
- CouponUser couponUser = new CouponUser();
|
|
|
|
|
- couponUser.setUserId(userId);
|
|
|
|
|
- couponUser.setCouponId(couponCollectReq.getCouponId());
|
|
|
|
|
- //领券中心
|
|
|
|
|
- couponUser.setChannel(CouponUser.Channel.center);
|
|
|
|
|
- couponUser.setStatus(CouponUser.Status.unused);
|
|
|
|
|
- couponUser.setCouponActiveId(couponCollectReq.getCouponActiveId());
|
|
|
|
|
- //优惠券有效时间
|
|
|
|
|
- this.updateCouponUserValidTime(couponActive.getCouponId(), couponUser);
|
|
|
|
|
- try {
|
|
|
|
|
- couponUserMapper.insert(couponUser);
|
|
|
|
|
- } catch (DuplicateKeyException e) {
|
|
|
|
|
- log.info("用户:{}重复领取优惠券{}插入数据错误", userId, couponCollectReq.getCouponId());
|
|
|
|
|
- throw BusinessRuntimeException.getInstance("已领取成功");
|
|
|
|
|
- }
|
|
|
|
|
|
|
+
|
|
|
|
|
+ receiveCouponUser(couponCollectReq.getCouponId(), userId, CouponUser.Channel.center);
|
|
|
if (couponActive != null) {
|
|
if (couponActive != null) {
|
|
|
//发放优惠券记录剩余数量
|
|
//发放优惠券记录剩余数量
|
|
|
Integer remainNum = couponActive.getRemainNum();
|
|
Integer remainNum = couponActive.getRemainNum();
|
|
@@ -93,22 +87,9 @@ public class CouponFontServiceImpl implements CouponFontService {
|
|
|
@Override
|
|
@Override
|
|
|
@Transactional(rollbackFor = Throwable.class)
|
|
@Transactional(rollbackFor = Throwable.class)
|
|
|
public void collectRecommendCoupon(CouponCollectReq couponCollectReq, Long userId) {
|
|
public void collectRecommendCoupon(CouponCollectReq couponCollectReq, Long userId) {
|
|
|
- collectCommonCheck(couponCollectReq, userId);
|
|
|
|
|
- CouponUser couponUser = new CouponUser();
|
|
|
|
|
- couponUser.setUserId(userId);
|
|
|
|
|
- couponUser.setCouponId(couponCollectReq.getCouponId());
|
|
|
|
|
- //推荐渠道
|
|
|
|
|
- couponUser.setChannel(CouponUser.Channel.recommend);
|
|
|
|
|
- couponUser.setStatus(CouponUser.Status.unused);
|
|
|
|
|
- couponUser.setRecommendId(couponCollectReq.getCouponRecommendId());
|
|
|
|
|
- //优惠券有效时间
|
|
|
|
|
- this.updateCouponUserValidTime(couponCollectReq.getCouponId(), couponUser);
|
|
|
|
|
- try {
|
|
|
|
|
- couponUserMapper.insert(couponUser);
|
|
|
|
|
- } catch (DuplicateKeyException e) {
|
|
|
|
|
- log.info("用户:{}重复领取推荐优惠券{}插入数据错误", userId, couponCollectReq.getCouponId());
|
|
|
|
|
- throw BusinessRuntimeException.getInstance("已领取成功");
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ collectCommonCheck(couponCollectReq.getCouponId(), userId);
|
|
|
|
|
+
|
|
|
|
|
+ receiveCouponUser(couponCollectReq.getCouponId(), userId, CouponUser.Channel.recommend);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
@@ -235,8 +216,8 @@ public class CouponFontServiceImpl implements CouponFontService {
|
|
|
return recommendCouponList.size();
|
|
return recommendCouponList.size();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void collectCommonCheck(CouponCollectReq couponCollectReq, Long userId) {
|
|
|
|
|
- Coupon coupon = couponMapper.selectById(couponCollectReq.getCouponId());
|
|
|
|
|
|
|
+ public void collectCommonCheck(Long couponId, Long userId) {
|
|
|
|
|
+ Coupon coupon = couponMapper.selectById(couponId);
|
|
|
if (coupon == null || !coupon.getDeleted()) {
|
|
if (coupon == null || !coupon.getDeleted()) {
|
|
|
throw BusinessRuntimeException.getInstance("该优惠券不存在");
|
|
throw BusinessRuntimeException.getInstance("该优惠券不存在");
|
|
|
}
|
|
}
|
|
@@ -248,7 +229,7 @@ public class CouponFontServiceImpl implements CouponFontService {
|
|
|
throw BusinessRuntimeException.getInstance("该优惠券只能新用户才能领取");
|
|
throw BusinessRuntimeException.getInstance("该优惠券只能新用户才能领取");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- CouponUser couponUser = couponUserMapper.selectOne(Wrappers.lambdaQuery(CouponUser.class).eq(CouponUser::getUserId, userId).eq(CouponUser::getCouponId, couponCollectReq.getCouponId()));
|
|
|
|
|
|
|
+ CouponUser couponUser = couponUserMapper.selectOne(Wrappers.lambdaQuery(CouponUser.class).eq(CouponUser::getUserId, userId).eq(CouponUser::getCouponId, couponId));
|
|
|
if (couponUser != null) {
|
|
if (couponUser != null) {
|
|
|
if (couponUser.getChannel() == CouponUser.Channel.exCode) {
|
|
if (couponUser.getChannel() == CouponUser.Channel.exCode) {
|
|
|
throw BusinessRuntimeException.getInstance("您已兑换过该优惠券..");
|
|
throw BusinessRuntimeException.getInstance("您已兑换过该优惠券..");
|
|
@@ -290,4 +271,60 @@ public class CouponFontServiceImpl implements CouponFontService {
|
|
|
}
|
|
}
|
|
|
log.info("订单:{}退款,优惠券返还end...", orderDon.getId());
|
|
log.info("订单:{}退款,优惠券返还end...", orderDon.getId());
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void receiveChannelPopularizeCoupon(ChannelPopularizeCouponReq req) {
|
|
|
|
|
+ Long userId = req.getUserId();
|
|
|
|
|
+ Long popularizeId = req.getPopularizeId();
|
|
|
|
|
+ Long couponId = req.getCouponId();
|
|
|
|
|
+ //是否是该渠道来源新用户
|
|
|
|
|
+ User user = userMapper.selectById(userId);
|
|
|
|
|
+ if (user == null) {
|
|
|
|
|
+ throw BusinessRuntimeException.getGatewayApiCode(GatewayApiCode.CMS_USER_NO_FIND);
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!user.getPopularizeId().equals(req.getPopularizeId())) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("渠道推广新用户才能领取该优惠券");
|
|
|
|
|
+ }
|
|
|
|
|
+ //渠道链接是否存在
|
|
|
|
|
+ ChannelPopularize channelPopularize = channelPopularizeMapper.selectById(popularizeId);
|
|
|
|
|
+ Assert.notNull(channelPopularize, "链接不存在");
|
|
|
|
|
+ //是否关联该优惠券
|
|
|
|
|
+ String couponIds = channelPopularize.getCouponIds();
|
|
|
|
|
+ if (StrUtil.isEmpty(couponIds)) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("链接未关联该优惠券");
|
|
|
|
|
+ }
|
|
|
|
|
+ List<Long> couponIdList = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ couponIdList = Jsons.parseList(couponIds, Long.class);
|
|
|
|
|
+ if (!couponIdList.contains(couponId)) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("链接未关联该优惠券");
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("链接未关联该优惠券");
|
|
|
|
|
+ }
|
|
|
|
|
+ //优惠券领取状态校验
|
|
|
|
|
+ collectCommonCheck(couponId, userId);
|
|
|
|
|
+ //领取优惠券
|
|
|
|
|
+ receiveCouponUser(couponId, userId, CouponUser.Channel.channel_popularize);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 领取优惠券
|
|
|
|
|
+ */
|
|
|
|
|
+ public void receiveCouponUser(Long couponId, Long userId, CouponUser.Channel channel) {
|
|
|
|
|
+ CouponUser couponUser = new CouponUser();
|
|
|
|
|
+ couponUser.setUserId(userId);
|
|
|
|
|
+ couponUser.setCouponId(couponId);
|
|
|
|
|
+
|
|
|
|
|
+ couponUser.setChannel(channel);
|
|
|
|
|
+ couponUser.setStatus(CouponUser.Status.unused);
|
|
|
|
|
+ //优惠券有效时间
|
|
|
|
|
+ this.updateCouponUserValidTime(couponId, couponUser);
|
|
|
|
|
+ try {
|
|
|
|
|
+ couponUserMapper.insert(couponUser);
|
|
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
|
|
+ log.info("用户:{}重复领取优惠券{}插入数据错误", userId, couponId);
|
|
|
|
|
+ throw BusinessRuntimeException.getInstance("已领取成功");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|