|
@@ -7,6 +7,7 @@ import java.util.List;
|
|
|
import com.fs.common.core.domain.R;
|
|
import com.fs.common.core.domain.R;
|
|
|
import com.fs.common.core.redis.RedisCache;
|
|
import com.fs.common.core.redis.RedisCache;
|
|
|
import com.fs.common.exception.CustomException;
|
|
import com.fs.common.exception.CustomException;
|
|
|
|
|
+import com.fs.common.exception.ServiceException;
|
|
|
import com.fs.common.utils.DateUtils;
|
|
import com.fs.common.utils.DateUtils;
|
|
|
import com.fs.hisStore.domain.*;
|
|
import com.fs.hisStore.domain.*;
|
|
|
import com.fs.hisStore.mapper.*;
|
|
import com.fs.hisStore.mapper.*;
|
|
@@ -235,4 +236,97 @@ public class FsStoreCouponIssueScrmServiceImpl implements IFsStoreCouponIssueScr
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 用户领取优惠券
|
|
|
|
|
+ *
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
|
|
+ public R receiveCoupon(Long issueId, Long userId) {
|
|
|
|
|
+ if (issueId == null) {
|
|
|
|
|
+ throw new CustomException("优惠券发布ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (userId == null) {
|
|
|
|
|
+ throw new CustomException("用户ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 1. 查询发布详情和优惠券模板
|
|
|
|
|
+ FsStoreCouponIssueScrm issue = fsStoreCouponIssueMapper.selectFsStoreCouponIssueById(issueId);
|
|
|
|
|
+ if (issue == null) {
|
|
|
|
|
+ throw new CustomException("优惠券不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ if(issue.getStatus().equals(0)){
|
|
|
|
|
+ return R.error("此优惠券已停止领取");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ Date now = new Date();
|
|
|
|
|
+
|
|
|
|
|
+ // 2. 校验领取时间
|
|
|
|
|
+ if (issue.getStartTime() != null && now.before(issue.getStartTime())) {
|
|
|
|
|
+ throw new CustomException("优惠券领取未开始");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (issue.getLimitTime() != null && now.after(issue.getLimitTime())) {
|
|
|
|
|
+ throw new CustomException("优惠券领取已结束");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 3. 校验是否已领取
|
|
|
|
|
+ int receivedCount = fsStoreCouponIssueUserMapper.countUserReceived(userId, issueId);
|
|
|
|
|
+ if (receivedCount > 0) {
|
|
|
|
|
+ throw new CustomException("您已经领取过该优惠券");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //4. 非无限张数,需要扣减库存
|
|
|
|
|
+ if (issue.getIsPermanent() == null || issue.getIsPermanent() != 1) {
|
|
|
|
|
+ int deductRows = fsStoreCouponIssueMapper.deductRemainCount(issueId);
|
|
|
|
|
+
|
|
|
|
|
+ if (deductRows <= 0) {
|
|
|
|
|
+ throw new CustomException("优惠券已领完");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ //5. 插入领取记录
|
|
|
|
|
+ try {
|
|
|
|
|
+ FsStoreCouponIssueUserScrm couponIssueUser = new FsStoreCouponIssueUserScrm();
|
|
|
|
|
+ couponIssueUser.setUserId(userId);
|
|
|
|
|
+ couponIssueUser.setIssueId(issueId);
|
|
|
|
|
+ couponIssueUser.setCreateTime(new Date());
|
|
|
|
|
+ couponIssueUser.setIsDel(0);
|
|
|
|
|
+ fsStoreCouponIssueUserMapper.insertFsStoreCouponIssueUser(couponIssueUser);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ throw new CustomException("插入领取记录失败");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 6. 计算用户优惠券有效期
|
|
|
|
|
+// Date userCouponLimitTime = buildUserCouponLimitTime(issue.getCouponTime());
|
|
|
|
|
+
|
|
|
|
|
+ // 7. 插入用户实际可用优惠券
|
|
|
|
|
+// couponReceiveMapper.insertUserCoupon(
|
|
|
|
|
+// issue,
|
|
|
|
|
+// userId,
|
|
|
|
|
+// userCouponLimitTime,
|
|
|
|
|
+// "receive"
|
|
|
|
|
+// );
|
|
|
|
|
+
|
|
|
|
|
+ FsStoreCouponScrm coupon=couponMapper.selectFsStoreCouponById(issue.getCouponId());
|
|
|
|
|
+ FsStoreCouponUserScrm couponUser=new FsStoreCouponUserScrm();
|
|
|
|
|
+ couponUser.setCouponId(issue.getCouponId());
|
|
|
|
|
+ couponUser.setUserId(userId);
|
|
|
|
|
+ couponUser.setCouponTitle(issue.getCouponName());
|
|
|
|
|
+ couponUser.setCouponPrice(coupon.getCouponPrice());
|
|
|
|
|
+ couponUser.setUseMinPrice(coupon.getUseMinPrice());
|
|
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
|
|
+ calendar.setTime(new Date());
|
|
|
|
|
+ calendar.add(Calendar.DATE,coupon.getCouponTime().intValue());
|
|
|
|
|
+ couponUser.setLimitTime(calendar.getTime());
|
|
|
|
|
+ couponUser.setCreateTime(new Date());
|
|
|
|
|
+ couponUser.setType("Claim");
|
|
|
|
|
+ couponUser.setStatus(0);
|
|
|
|
|
+ fsStoreCouponUserMapper.insertFsStoreCouponUser(couponUser);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+//
|
|
|
|
|
+ return R.ok("领取成功");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|