|
|
|
@@ -8,14 +8,12 @@ import com.github.pagehelper.PageInfo; |
|
|
|
import com.simple.common.ErrorCode; |
|
|
|
import com.simple.domain.po.*; |
|
|
|
import com.simple.domain.vo.OrderVo; |
|
|
|
import com.simple.enums.EnumCouponOrderStatus; |
|
|
|
import com.simple.enums.EnumOrderStatus; |
|
|
|
import com.simple.enums.EnumPayType; |
|
|
|
import com.simple.enums.EnumValidStatus; |
|
|
|
import com.simple.enums.*; |
|
|
|
import com.simple.exception.MallinkException; |
|
|
|
import com.simple.mapper.*; |
|
|
|
import com.simple.service.WxOrderService; |
|
|
|
import com.simple.service.WxPayAccountService; |
|
|
|
import com.simple.service.WxUserCouponService; |
|
|
|
import com.simple.utils.RedisLock; |
|
|
|
import org.apache.log4j.Logger; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
@@ -71,29 +69,32 @@ public class WxOrderServiceImpl implements WxOrderService { |
|
|
|
return wxOrderMapper.queryObject(orderVo); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) |
|
|
|
public WxOrder saveOrder(WxCUser user, Long couponId) { |
|
|
|
String couponIdStr = String.valueOf(couponId); |
|
|
|
WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(couponId); |
|
|
|
if (coupon == null) { |
|
|
|
logger.error("券不存在, couponId: " + couponIdStr); |
|
|
|
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); |
|
|
|
private int getUserCouponOrderCount(WxCUser user, WxCoupon counpon) { |
|
|
|
// 查找 |
|
|
|
int count = 0; |
|
|
|
WxCouponOrder query = new WxCouponOrder(); |
|
|
|
query.setCouponId(counpon.getId()); |
|
|
|
query.setCUserId(user.getId()); |
|
|
|
query.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode()); |
|
|
|
try { |
|
|
|
count = wxCouponOrderMapper.selectCount(query); |
|
|
|
}catch (Exception e) { |
|
|
|
logger.error("购买是否超限-DB, couponId: " + counpon.getId() + ", e:" + e.getMessage()); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
|
} |
|
|
|
//加锁 |
|
|
|
|
|
|
|
return count; |
|
|
|
} |
|
|
|
|
|
|
|
private void stockReduce(WxCUser user, WxCoupon coupon, String couponIdStr) { |
|
|
|
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) { |
|
|
|
//解锁 |
|
|
|
@@ -105,17 +106,13 @@ public class WxOrderServiceImpl implements WxOrderService { |
|
|
|
// 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) { |
|
|
|
count = getUserCouponOrderCount(user, coupon); |
|
|
|
} catch (Exception e) { |
|
|
|
//解锁 |
|
|
|
redisLock.unlock(couponIdStr, timeStr); |
|
|
|
logger.error("购买是否超限-DB, couponId: " + couponIdStr + ", e:" + e.getMessage()); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
|
throw new MallinkException(ErrorCode.DB_FAIL); |
|
|
|
} |
|
|
|
|
|
|
|
if (count > coupon.getUseLimitQuantity()) { |
|
|
|
//解锁 |
|
|
|
redisLock.unlock(couponIdStr, timeStr); |
|
|
|
@@ -135,69 +132,146 @@ public class WxOrderServiceImpl implements WxOrderService { |
|
|
|
logger.error("此券减库存失败, couponId: " + couponIdStr); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
payPrice = coupon.getSalePrice(); |
|
|
|
payment = coupon.getSalePrice(); |
|
|
|
private void stockBack(WxOrder updateOrder) { |
|
|
|
// 已取消/已退款,库存加1 |
|
|
|
// 获取订单相关coupon |
|
|
|
Long couponId = updateOrder.getCouponId(); |
|
|
|
String couponIdStr = "" + couponId; |
|
|
|
//加锁 |
|
|
|
long time = System.currentTimeMillis() + RedisLock.TIMEOUT; |
|
|
|
String timeStr = String.valueOf(time); |
|
|
|
|
|
|
|
valid_date = (coupon.getValidType() == EnumValidStatus.VALID_RANGE.getCode())? |
|
|
|
coupon.getValidEndDate(): |
|
|
|
new Date((curr.getTime()/1000+coupon.getValidDays()*24*60*60)*1000); |
|
|
|
if(!redisLock.lock(couponIdStr, timeStr)) { |
|
|
|
logger.error("此券被锁定, couponId: " + couponIdStr); |
|
|
|
throw new MallinkException(ErrorCode.TOO_MANY_REQUEST); |
|
|
|
} |
|
|
|
try { |
|
|
|
WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(couponId); |
|
|
|
coupon.setRemainInventory(coupon.getRemainInventory() + 1); |
|
|
|
wxCouponMapper.updateByPrimaryKeySelective(coupon); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("库存+1失败, e:" + e.getMessage()); |
|
|
|
throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "库存+1失败, e:" + e.getMessage()); |
|
|
|
} finally { |
|
|
|
//解锁 |
|
|
|
redisLock.unlock(couponIdStr, timeStr); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) |
|
|
|
public WxOrder saveOrder(WxCUser user, Long couponId) { |
|
|
|
// 检查用户 |
|
|
|
if (user == null) { |
|
|
|
logger.error("用户不存在"); |
|
|
|
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.getStatus() == EnumCouponStatus.COUPON_STATUS_TAKE_OFFF.getCode()) { |
|
|
|
logger.error("券已下架, couponId: " + couponIdStr); |
|
|
|
throw new MallinkException(ErrorCode.COUPON_ORDER_IS_INVALID); |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
WxMerchant wxMerchant = wxMerchantMapper.selectByPrimaryKey(coupon.getMerchantId()); |
|
|
|
if (wxMerchant == null) { |
|
|
|
logger.error("商户不存在, couponId: " + couponIdStr); |
|
|
|
throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_FOUND); |
|
|
|
} |
|
|
|
if (wxMerchant.getStatus() == EnumMerchantStatus.NOT_VALID.getCode()) { |
|
|
|
logger.error("商户已禁用, couponId: " + couponIdStr); |
|
|
|
throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_VALID); |
|
|
|
} |
|
|
|
*/ |
|
|
|
|
|
|
|
// 减库存操作 |
|
|
|
try { |
|
|
|
stockReduce(user, coupon, couponIdStr); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("减库存失败, couponId: " + couponIdStr); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(), "减库存失败, couponId: " + couponIdStr); |
|
|
|
} |
|
|
|
|
|
|
|
Date curr = new Date(); |
|
|
|
|
|
|
|
int payment = coupon.getSalePrice(); |
|
|
|
|
|
|
|
final IdWorker idWorker = IdWorker.get(); |
|
|
|
Long orderNumber = idWorker.nextId(); |
|
|
|
|
|
|
|
// body |
|
|
|
// tenant_id + merchant_id + title + subtitle |
|
|
|
String bodyStr = coupon.getTitle() + "/" + coupon.getSubTitle(); |
|
|
|
String bodyStr = coupon.getTitle() + "-" + coupon.getSubTitle(); |
|
|
|
|
|
|
|
WxOrder record = new WxOrder(); |
|
|
|
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_PENDING_PAYMENT.getCode()); |
|
|
|
record.setDetail(bodyStr); |
|
|
|
record.setCreateDate(curr); |
|
|
|
record.setUpdateDate(curr); |
|
|
|
|
|
|
|
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_PENDING_PAYMENT.getCode()); |
|
|
|
record.setDetail(bodyStr); |
|
|
|
record.setCreateDate(curr); |
|
|
|
record.setUpdateDate(curr); |
|
|
|
wxOrderMapper.insertSelective(record); |
|
|
|
} catch (RuntimeException e) { |
|
|
|
// TODO 增库存 |
|
|
|
// 加库存 |
|
|
|
stockBack(record); |
|
|
|
logger.error("保存订单:" + e.getMessage()); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(), "保存订单失败:" + record.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
return record; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 创建 couponOrder |
|
|
|
* @param user |
|
|
|
* @param order |
|
|
|
* @param coupon |
|
|
|
*/ |
|
|
|
private void createCouponOrder(WxCUser user, WxOrder order, WxCoupon coupon) { |
|
|
|
Date curr = new Date(); |
|
|
|
Date 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(); |
|
|
|
try { |
|
|
|
WxCouponOrder couponOrder = new WxCouponOrder(); |
|
|
|
couponOrder.setId(idWorker.nextId()); |
|
|
|
couponOrder.setTenantId(user.getTenantId()); |
|
|
|
couponOrder.setCouponId(couponId); |
|
|
|
couponOrder.setCouponId(order.getCouponId()); |
|
|
|
couponOrder.setCUserId(user.getId()); |
|
|
|
couponOrder.setOrderId(orderNumber); |
|
|
|
couponOrder.setOrderId(order.getOrderNumber()); |
|
|
|
couponOrder.setExpiredTime(valid_date); |
|
|
|
couponOrder.setCouponOrderStatus(EnumCouponOrderStatus.COUPON_ORDER_USE_WAIT.getCode()); |
|
|
|
couponOrder.setCreateDate(curr); |
|
|
|
couponOrder.setUpdateDate(curr); |
|
|
|
couponOrder.setCouponPrice(payPrice); |
|
|
|
couponOrder.setCouponPrice(order.getPayment()); |
|
|
|
|
|
|
|
wxCouponOrderMapper.insertSelective(couponOrder); |
|
|
|
|
|
|
|
} catch (RuntimeException e) { |
|
|
|
// TODO 增库存 |
|
|
|
logger.error("WxCouponOrder:" + e.getMessage()); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(), "couponOrder保存失败!"); |
|
|
|
} |
|
|
|
|
|
|
|
return record; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public WxOrder sendUserFreeCoupon(Long userId, Long couponId) { |
|
|
|
// check 用户状态 |
|
|
|
WxCUser user = null; |
|
|
|
try { |
|
|
|
user = wxCUserMapper.selectByPrimaryKey(userId); |
|
|
|
@@ -209,78 +283,25 @@ public class WxOrderServiceImpl implements WxOrderService { |
|
|
|
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.getStatus() == EnumCouponStatus.COUPON_STATUS_TAKE_OFFF.getCode()) { |
|
|
|
logger.error("券已下架, couponId: " + couponIdStr); |
|
|
|
throw new MallinkException(ErrorCode.COUPON_ORDER_IS_INVALID); |
|
|
|
} |
|
|
|
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; |
|
|
|
int payment = coupon.getSalePrice(); |
|
|
|
|
|
|
|
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(); |
|
|
|
@@ -290,95 +311,106 @@ public class WxOrderServiceImpl implements WxOrderService { |
|
|
|
String bodyStr = coupon.getTitle() + "/" + coupon.getSubTitle(); |
|
|
|
|
|
|
|
WxOrder record = new WxOrder(); |
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
// 保存订单 |
|
|
|
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 增库存 |
|
|
|
} catch (Exception e) { |
|
|
|
//加库存 |
|
|
|
stockBack(record); |
|
|
|
logger.error("保存订单:" + e.getMessage()); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
|
} |
|
|
|
|
|
|
|
// 创建couponOrder |
|
|
|
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()); |
|
|
|
createCouponOrder(user, record, coupon); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("保存订单:" + e.getMessage()); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return record; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) |
|
|
|
public int updateOrderStatus(Long orderId,EnumOrderStatus enumOrderStatus) { |
|
|
|
WxOrder updateRecord = wxOrderMapper.selectByPrimaryKey(orderId); |
|
|
|
if (updateRecord == null) { |
|
|
|
logger.error("order updateStatus, order not found, orderId:" + updateRecord.getId()); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_NOT_FIND); |
|
|
|
} |
|
|
|
public int orderSuccess(WxOrder updateOrder) { |
|
|
|
Date currentDate = new Date(); |
|
|
|
WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(updateOrder.getCouponId()); |
|
|
|
if (coupon == null) { |
|
|
|
logger.error("券不存在, couponId: " + updateOrder.getCouponId()); |
|
|
|
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); |
|
|
|
} |
|
|
|
if (coupon.getStatus() == EnumCouponStatus.COUPON_STATUS_TAKE_OFFF.getCode()) { |
|
|
|
logger.error("券已下架, couponId: " + updateOrder.getCouponId()); |
|
|
|
throw new MallinkException(ErrorCode.COUPON_ORDER_IS_INVALID); |
|
|
|
} |
|
|
|
WxCUser user = wxCUserMapper.selectByPrimaryKey(updateOrder.getCUserId()); |
|
|
|
if (user == null) { |
|
|
|
logger.error("用户不存在, userId: " + updateOrder.getCUserId()); |
|
|
|
throw new MallinkException(ErrorCode.USER_IS_EMPTY); |
|
|
|
} |
|
|
|
// 已支付,更新支付时间 |
|
|
|
updateOrder.setPaymentTime(currentDate); |
|
|
|
updateOrder.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS.getCode()); |
|
|
|
updateOrder.setUpdateDate(currentDate); |
|
|
|
int ret = 0; |
|
|
|
try { |
|
|
|
ret = wxOrderMapper.updateByPrimaryKey(updateOrder); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("订单更新失败:" + e.getMessage()); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL.getCode(), "订单更新失败:" + e.getMessage()); |
|
|
|
} |
|
|
|
// 创建couponOrder |
|
|
|
try { |
|
|
|
createCouponOrder(user, updateOrder, coupon); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("保存订单:" + e.getMessage()); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_IS_FAIL); |
|
|
|
} |
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) |
|
|
|
public int updateOrderStatus(WxOrder updateOrder, EnumOrderStatus enumOrderStatus) { |
|
|
|
Date currentDate = new Date(); |
|
|
|
switch (enumOrderStatus) { |
|
|
|
case ORDER_STATUS_PAYMENT_SUCCESS: { |
|
|
|
// 已支付,更新支付时间 |
|
|
|
updateRecord.setPaymentTime(currentDate); |
|
|
|
break; |
|
|
|
} |
|
|
|
case ORDER_STATUS_OVERTIME_CANCEL: |
|
|
|
case ORDER_STATUS_REFUND_SUCCESS: |
|
|
|
{ |
|
|
|
// 已取消/已退款,库存加1 |
|
|
|
// 获取订单相关coupon |
|
|
|
WxCouponOrder couponOrder = new WxCouponOrder(); |
|
|
|
couponOrder.setOrderId(orderId); |
|
|
|
List<WxCouponOrder> colist = wxCouponOrderMapper.findList(couponOrder); |
|
|
|
for(WxCouponOrder couponOrderX : colist) { |
|
|
|
String couponIdStr = String.valueOf(couponOrderX.getCouponId()); |
|
|
|
//加锁 |
|
|
|
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); |
|
|
|
} |
|
|
|
WxCoupon coupon = wxCouponMapper.selectByPrimaryKey(couponOrderX.getCouponId()); |
|
|
|
coupon.setRemainInventory(coupon.getRemainInventory() + 1); |
|
|
|
wxCouponMapper.updateByPrimaryKeySelective(coupon); |
|
|
|
//解锁 |
|
|
|
redisLock.unlock(couponIdStr, timeStr); |
|
|
|
{ |
|
|
|
try { |
|
|
|
stockBack(updateOrder); |
|
|
|
} catch (Exception e) { |
|
|
|
logger.error("库存+1失败, e:" + e.getMessage()); |
|
|
|
throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "库存+1失败, e:" + e.getMessage()); |
|
|
|
} |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
updateRecord.setOrderStatus(enumOrderStatus.getCode()); |
|
|
|
updateRecord.setUpdateDate(currentDate); |
|
|
|
return wxOrderMapper.updateByPrimaryKey(updateRecord); |
|
|
|
int ret = 0; |
|
|
|
try { |
|
|
|
updateOrder.setOrderStatus(enumOrderStatus.getCode()); |
|
|
|
updateOrder.setUpdateDate(currentDate); |
|
|
|
ret = wxOrderMapper.updateByPrimaryKey(updateOrder); |
|
|
|
}catch (Exception e) { |
|
|
|
logger.error("订单状态更新失败, e:" + e.getMessage()); |
|
|
|
throw new MallinkException(ErrorCode.DB_FAIL.getCode(), "订单状态更新失败, e:" + e.getMessage()); |
|
|
|
} |
|
|
|
return ret; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
|