|
|
@@ -166,8 +166,8 @@ public class WxOrderServiceImpl implements WxOrderService { |
|
|
record.setCreateDate(curr); |
|
|
record.setCreateDate(curr); |
|
|
record.setUpdateDate(curr); |
|
|
record.setUpdateDate(curr); |
|
|
wxOrderMapper.insertSelective(record); |
|
|
wxOrderMapper.insertSelective(record); |
|
|
|
|
|
|
|
|
} catch (RuntimeException e) { |
|
|
} catch (RuntimeException e) { |
|
|
|
|
|
// TODO 增库存 |
|
|
logger.error("保存订单:" + e.getMessage()); |
|
|
logger.error("保存订单:" + e.getMessage()); |
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
} |
|
|
} |
|
|
@@ -188,6 +188,147 @@ public class WxOrderServiceImpl implements WxOrderService { |
|
|
wxCouponOrderMapper.insertSelective(couponOrder); |
|
|
wxCouponOrderMapper.insertSelective(couponOrder); |
|
|
|
|
|
|
|
|
} catch (RuntimeException e) { |
|
|
} catch (RuntimeException e) { |
|
|
|
|
|
// TODO 增库存 |
|
|
|
|
|
logger.error("WxCouponOrder:" + e.getMessage()); |
|
|
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return record; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
public WxOrder sendUserFreeCoupon(Long userId, Long couponId) { |
|
|
|
|
|
WxCUser user = null; |
|
|
|
|
|
WxCUser userQ = new WxCUser(); |
|
|
|
|
|
userQ.setId(userId); |
|
|
|
|
|
try { |
|
|
|
|
|
user = wxCUserMapper.selectOne(userQ); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
logger.error("userId : " + userId + ", e: " + e.getMessage()); |
|
|
|
|
|
} |
|
|
|
|
|
if (user == null) { |
|
|
|
|
|
logger.error("用户不存在, userId: " + userId); |
|
|
|
|
|
throw new MallinkException(ErrorCode.USER_IS_EMPTY); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String couponIdStr = String.valueOf(couponId); |
|
|
|
|
|
WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(couponId); |
|
|
|
|
|
if (coupon == null) { |
|
|
|
|
|
logger.error("券不存在, couponId: " + couponIdStr); |
|
|
|
|
|
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); |
|
|
|
|
|
} |
|
|
|
|
|
if (coupon.getSalePrice() != 0) { |
|
|
|
|
|
logger.error("券不免费, couponId: " + couponIdStr); |
|
|
|
|
|
throw new MallinkException(ErrorCode.COUPON_IS_NOT_FREE); |
|
|
|
|
|
} |
|
|
|
|
|
//加锁 |
|
|
|
|
|
long time = System.currentTimeMillis() + RedisLock.TIMEOUT; |
|
|
|
|
|
String timeStr = String.valueOf(time); |
|
|
|
|
|
if(!redisLock.lock(couponIdStr, timeStr)) { |
|
|
|
|
|
logger.error("此券被锁定, couponId: " + couponIdStr); |
|
|
|
|
|
throw new MallinkException(ErrorCode.TOO_MANY_REQUEST); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int payPrice = 0; |
|
|
|
|
|
int payment = 0; |
|
|
|
|
|
|
|
|
|
|
|
Date curr = new Date(); |
|
|
|
|
|
Date valid_date = null; |
|
|
|
|
|
|
|
|
|
|
|
// 检查 优惠券 库存 |
|
|
|
|
|
if (coupon.getRemainInventory() <= 0) { |
|
|
|
|
|
//解锁 |
|
|
|
|
|
redisLock.unlock(couponIdStr, timeStr); |
|
|
|
|
|
logger.error("此券库存为0, couponId: " + couponIdStr); |
|
|
|
|
|
throw new MallinkException(ErrorCode.REMAIN_IS_EMPTY); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// check 购买是否超限 |
|
|
|
|
|
int count = 0; |
|
|
|
|
|
try { |
|
|
|
|
|
WxCouponOrder query = new WxCouponOrder(); |
|
|
|
|
|
query.setCouponId(couponId); |
|
|
|
|
|
query.setCUserId(user.getId()); |
|
|
|
|
|
query.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode()); |
|
|
|
|
|
count = wxCouponOrderMapper.selectCount(query); |
|
|
|
|
|
}catch (Exception e) { |
|
|
|
|
|
//解锁 |
|
|
|
|
|
redisLock.unlock(couponIdStr, timeStr); |
|
|
|
|
|
logger.error("购买是否超限-DB, couponId: " + couponIdStr + ", e:" + e.getMessage()); |
|
|
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
|
|
|
} |
|
|
|
|
|
if (count > coupon.getUseLimitQuantity()) { |
|
|
|
|
|
//解锁 |
|
|
|
|
|
redisLock.unlock(couponIdStr, timeStr); |
|
|
|
|
|
logger.error("此券购买数量已超限, couponId: " + couponIdStr + ", count: " + count); |
|
|
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_LIMITED); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
// 减库存 |
|
|
|
|
|
coupon.setRemainInventory(coupon.getRemainInventory() - 1); |
|
|
|
|
|
wxCouponMapper.updateByPrimaryKeySelective(coupon); |
|
|
|
|
|
//解锁 |
|
|
|
|
|
redisLock.unlock(couponIdStr, timeStr); |
|
|
|
|
|
} catch (RuntimeException e) { |
|
|
|
|
|
//解锁 |
|
|
|
|
|
redisLock.unlock(couponIdStr, timeStr); |
|
|
|
|
|
logger.error("此券减库存失败, couponId: " + couponIdStr); |
|
|
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
payPrice = coupon.getSalePrice(); |
|
|
|
|
|
payment = coupon.getSalePrice(); |
|
|
|
|
|
|
|
|
|
|
|
valid_date = (coupon.getValidType() == EnumValidStatus.VALID_RANGE.getCode())? |
|
|
|
|
|
coupon.getValidEndDate(): |
|
|
|
|
|
new Date((curr.getTime()/1000+coupon.getValidDays()*24*60*60)*1000); |
|
|
|
|
|
|
|
|
|
|
|
final IdWorker idWorker = IdWorker.get(); |
|
|
|
|
|
Long orderNumber = idWorker.nextId(); |
|
|
|
|
|
|
|
|
|
|
|
// body |
|
|
|
|
|
// tenant_id + merchant_id + title + subtitle |
|
|
|
|
|
String bodyStr = coupon.getTitle() + "/" + coupon.getSubTitle(); |
|
|
|
|
|
|
|
|
|
|
|
WxOrder record = new WxOrder(); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
// 保存订单 |
|
|
|
|
|
record.setId(orderNumber); |
|
|
|
|
|
record.setTenantId(user.getTenantId()); |
|
|
|
|
|
record.setOrderNumber(orderNumber); |
|
|
|
|
|
record.setCUserId(user.getId()); |
|
|
|
|
|
record.setMerchantId(coupon.getMerchantId()); |
|
|
|
|
|
record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode()); |
|
|
|
|
|
record.setPayment(payment); |
|
|
|
|
|
record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS.getCode()); |
|
|
|
|
|
record.setDetail(bodyStr); |
|
|
|
|
|
record.setCreateDate(curr); |
|
|
|
|
|
record.setUpdateDate(curr); |
|
|
|
|
|
wxOrderMapper.insertSelective(record); |
|
|
|
|
|
} catch (RuntimeException e) { |
|
|
|
|
|
// TODO 增库存 |
|
|
|
|
|
logger.error("保存订单:" + e.getMessage()); |
|
|
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
WxCouponOrder couponOrder = new WxCouponOrder(); |
|
|
|
|
|
couponOrder.setId(idWorker.nextId()); |
|
|
|
|
|
couponOrder.setTenantId(user.getTenantId()); |
|
|
|
|
|
couponOrder.setCouponId(couponId); |
|
|
|
|
|
couponOrder.setCUserId(user.getId()); |
|
|
|
|
|
couponOrder.setOrderId(orderNumber); |
|
|
|
|
|
couponOrder.setExpiredTime(valid_date); |
|
|
|
|
|
couponOrder.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode()); |
|
|
|
|
|
couponOrder.setCreateDate(curr); |
|
|
|
|
|
couponOrder.setUpdateDate(curr); |
|
|
|
|
|
couponOrder.setCouponPrice(payPrice); |
|
|
|
|
|
|
|
|
|
|
|
wxCouponOrderMapper.insertSelective(couponOrder); |
|
|
|
|
|
} catch (RuntimeException e) { |
|
|
|
|
|
// TODO 增库存 |
|
|
logger.error("WxCouponOrder:" + e.getMessage()); |
|
|
logger.error("WxCouponOrder:" + e.getMessage()); |
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
} |
|
|
} |
|
|
|