|
@@ -629,68 +629,76 @@ public class UserCouponServiceImpl extends ServiceImpl<MtUserCouponMapper, MtUse
|
|
|
* @param couponId 卡券ID
|
|
|
* @param userId 会员ID
|
|
|
* @param mobile 手机号
|
|
|
+ * @param num 购买数量
|
|
|
* @return
|
|
|
* */
|
|
|
- public boolean buyCouponItem(Integer orderId, Integer couponId, Integer userId, String mobile) throws BusinessCheckException {
|
|
|
- MtCoupon couponInfo = couponService.queryCouponById(couponId);
|
|
|
- MtUserCoupon userCoupon = new MtUserCoupon();
|
|
|
- userCoupon.setCouponId(couponId);
|
|
|
- userCoupon.setMerchantId(couponInfo.getMerchantId());
|
|
|
- userCoupon.setStoreId(couponInfo.getStoreId());
|
|
|
- userCoupon.setType(couponInfo.getType());
|
|
|
- userCoupon.setGroupId(couponInfo.getGroupId());
|
|
|
- userCoupon.setMobile(mobile);
|
|
|
- userCoupon.setUserId(userId);
|
|
|
- userCoupon.setStatus(UserCouponStatusEnum.UNUSED.getKey());
|
|
|
- userCoupon.setCreateTime(new Date());
|
|
|
- userCoupon.setUpdateTime(new Date());
|
|
|
- userCoupon.setExpireTime(couponInfo.getEndTime());
|
|
|
- if (couponInfo.getExpireType().equals(CouponExpireTypeEnum.FLEX.getKey())) {
|
|
|
- Date expireTime = new Date();
|
|
|
- Calendar c = Calendar.getInstance();
|
|
|
- c.setTime(expireTime);
|
|
|
- c.add(Calendar.DATE, couponInfo.getExpireTime());
|
|
|
- expireTime = c.getTime();
|
|
|
- userCoupon.setExpireTime(expireTime);
|
|
|
- }
|
|
|
- userCoupon.setOrderId(orderId);
|
|
|
-
|
|
|
- // 如果购买的是储值卡
|
|
|
- if (couponInfo.getType().equals(CouponTypeEnum.PRESTORE.getKey()) && couponInfo.getInRule() != null) {
|
|
|
- String[] paramArr = couponInfo.getInRule().split(","); // 100_200,300_500
|
|
|
- MtOrder orderInfo = orderService.getOrderInfo(orderId);
|
|
|
- if (orderInfo != null) {
|
|
|
- BigDecimal payAmount = orderInfo.getPayAmount();
|
|
|
- BigDecimal totalAmount = new BigDecimal(0);
|
|
|
- if (paramArr.length > 0) {
|
|
|
- for (int i = 0; i < paramArr.length; i++) {
|
|
|
- String[] storeItem = paramArr[i].split("_");
|
|
|
- if (storeItem.length > 0) {
|
|
|
- BigDecimal amount = new BigDecimal(paramArr[i].split("_")[0]);
|
|
|
- if (payAmount.compareTo(amount) >= 0) {
|
|
|
- totalAmount = new BigDecimal(paramArr[i].split("_")[1]);
|
|
|
- payAmount = payAmount.subtract(amount);
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public boolean buyCouponItem(Integer orderId, Integer couponId, Integer userId, String mobile, Integer num) throws BusinessCheckException {
|
|
|
+ if (num == null || num <= 0) {
|
|
|
+ num = 1;
|
|
|
+ }
|
|
|
+ for (int j = 0; j < num; j++) {
|
|
|
+ MtCoupon couponInfo = couponService.queryCouponById(couponId);
|
|
|
+ MtUserCoupon userCoupon = new MtUserCoupon();
|
|
|
+ userCoupon.setCouponId(couponId);
|
|
|
+ userCoupon.setMerchantId(couponInfo.getMerchantId());
|
|
|
+ userCoupon.setStoreId(couponInfo.getStoreId());
|
|
|
+ userCoupon.setType(couponInfo.getType());
|
|
|
+ userCoupon.setGroupId(couponInfo.getGroupId());
|
|
|
+ userCoupon.setMobile(mobile);
|
|
|
+ userCoupon.setUserId(userId);
|
|
|
+ userCoupon.setStatus(UserCouponStatusEnum.UNUSED.getKey());
|
|
|
+ userCoupon.setCreateTime(new Date());
|
|
|
+ userCoupon.setUpdateTime(new Date());
|
|
|
+ userCoupon.setExpireTime(couponInfo.getEndTime());
|
|
|
+ if (couponInfo.getExpireType().equals(CouponExpireTypeEnum.FLEX.getKey())) {
|
|
|
+ Date expireTime = new Date();
|
|
|
+ Calendar c = Calendar.getInstance();
|
|
|
+ c.setTime(expireTime);
|
|
|
+ c.add(Calendar.DATE, couponInfo.getExpireTime());
|
|
|
+ expireTime = c.getTime();
|
|
|
+ userCoupon.setExpireTime(expireTime);
|
|
|
+ }
|
|
|
+ userCoupon.setOrderId(orderId);
|
|
|
+
|
|
|
+ // 如果购买的是储值卡
|
|
|
+ if (couponInfo.getType().equals(CouponTypeEnum.PRESTORE.getKey()) && couponInfo.getInRule() != null) {
|
|
|
+ String[] paramArr = couponInfo.getInRule().split(","); // 100_200,300_500
|
|
|
+ MtOrder orderInfo = orderService.getOrderInfo(orderId);
|
|
|
+ if (orderInfo != null) {
|
|
|
+ BigDecimal payAmount = orderInfo.getPayAmount();
|
|
|
+ BigDecimal totalAmount = new BigDecimal(0);
|
|
|
+ if (paramArr.length > 0) {
|
|
|
+ for (int i = 0; i < paramArr.length; i++) {
|
|
|
+ String[] storeItem = paramArr[i].split("_");
|
|
|
+ if (storeItem.length > 0) {
|
|
|
+ BigDecimal amount = new BigDecimal(paramArr[i].split("_")[0]);
|
|
|
+ if (payAmount.compareTo(amount) >= 0) {
|
|
|
+ totalAmount = new BigDecimal(paramArr[i].split("_")[1]);
|
|
|
+ payAmount = payAmount.subtract(amount);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ couponInfo.setAmount(totalAmount);
|
|
|
}
|
|
|
- couponInfo.setAmount(totalAmount);
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- userCoupon.setAmount(couponInfo.getAmount());
|
|
|
- userCoupon.setBalance(couponInfo.getAmount());
|
|
|
+ userCoupon.setAmount(couponInfo.getAmount());
|
|
|
+ userCoupon.setBalance(couponInfo.getAmount());
|
|
|
|
|
|
- // 12位随机数
|
|
|
- StringBuffer code = new StringBuffer();
|
|
|
- code.append(SeqUtil.getRandomNumber(4));
|
|
|
- code.append(SeqUtil.getRandomNumber(4));
|
|
|
- code.append(SeqUtil.getRandomNumber(4));
|
|
|
- code.append(SeqUtil.getRandomNumber(4));
|
|
|
- userCoupon.setCode(code.toString());
|
|
|
- userCoupon.setUuid(code.toString());
|
|
|
+ // 12位随机数
|
|
|
+ StringBuffer code = new StringBuffer();
|
|
|
+ code.append(SeqUtil.getRandomNumber(4));
|
|
|
+ code.append(SeqUtil.getRandomNumber(4));
|
|
|
+ code.append(SeqUtil.getRandomNumber(4));
|
|
|
+ code.append(SeqUtil.getRandomNumber(4));
|
|
|
+ userCoupon.setCode(code.toString());
|
|
|
+ userCoupon.setUuid(code.toString());
|
|
|
|
|
|
- mtUserCouponMapper.insert(userCoupon);
|
|
|
+ mtUserCouponMapper.insert(userCoupon);
|
|
|
+ }
|
|
|
return true;
|
|
|
}
|
|
|
|