Kaynağa Gözat

[订单][新增]:增加免费券发放接口

release_toaliyun_real
Stormeye.Wu 7 yıl önce
ebeveyn
işleme
5baa69b496
3 değiştirilmiş dosya ile 151 ekleme ve 1 silme
  1. +1
    -0
      mallinkService/src/main/java/com/simple/common/ErrorCode.java
  2. +8
    -0
      mallinkService/src/main/java/com/simple/service/WxOrderService.java
  3. +142
    -1
      mallinkService/src/main/java/com/simple/service/impl/WxOrderServiceImpl.java

+ 1
- 0
mallinkService/src/main/java/com/simple/common/ErrorCode.java Dosyayı Görüntüle

@@ -50,6 +50,7 @@ public enum ErrorCode{


COUPON_IS_EMPTY(2020, "券不存在"),
COUPON_IS_NOT_FREE(2021, "券不存在"),

/**
* 车流 2040


+ 8
- 0
mallinkService/src/main/java/com/simple/service/WxOrderService.java Dosyayı Görüntüle

@@ -44,6 +44,14 @@ public interface WxOrderService {
*/
WxOrder saveOrder(WxCUser user, Long couponId);

/**
* 免费券订单接口
* @param userId
* @param couponId
* @return 订单id
*/
WxOrder sendUserFreeCoupon(Long userId, Long couponId);

/**
* 更新订单状态
* @param orderId


+ 142
- 1
mallinkService/src/main/java/com/simple/service/impl/WxOrderServiceImpl.java Dosyayı Görüntüle

@@ -166,8 +166,8 @@ public class WxOrderServiceImpl implements WxOrderService {
record.setCreateDate(curr);
record.setUpdateDate(curr);
wxOrderMapper.insertSelective(record);

} catch (RuntimeException e) {
// TODO 增库存
logger.error("保存订单:" + e.getMessage());
throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
}
@@ -188,6 +188,147 @@ public class WxOrderServiceImpl implements WxOrderService {
wxCouponOrderMapper.insertSelective(couponOrder);

} 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());
throw new MallinkException(ErrorCode.ORDER_IS_FAIL);
}


Yükleniyor…
İptal
Kaydet