|
|
|
@@ -65,6 +65,56 @@ public class WxOrderGroupServiceImpl implements WxOrderGroupService { |
|
|
|
@Autowired |
|
|
|
WxCouponChannelMapper wxCouponChannelMapper; |
|
|
|
|
|
|
|
@Override |
|
|
|
public void afterPaySuccessUpdate(WxOrder order) { |
|
|
|
// 参与拼团 |
|
|
|
WxOrderGroup wxOrderGroup = wxOrderGroupMapper.selectById(order.getOrderGroupId()); |
|
|
|
if (null == wxOrderGroup) { |
|
|
|
//错误,更新状态,定时任务会发起退款,回滚库存 |
|
|
|
order.setOrderStatus(EnumOrderStatus.ORDER_STATUS_COOPERATING_UNPAID.getCode()); |
|
|
|
wxOrderMapper.updateById(order); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"WxOrderGroup is null."+order.getOrderGroupId()); |
|
|
|
} |
|
|
|
WxCoupon wxCoupon = wxCouponMapper.selectById(order.getProductId()); |
|
|
|
if (null == wxCoupon) { |
|
|
|
throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"WxCoupon is null. orderId:"+order.getId()); |
|
|
|
} |
|
|
|
int remainPeople = updateOrderGroup(wxOrderGroup,order,wxCoupon); |
|
|
|
if (remainPeople == 0) { |
|
|
|
actionAfterOrderGroupSuccess(order, order.getOrderGroupId()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private int updateOrderGroup(WxOrderGroup wxOrderGroup,WxOrder order, WxCoupon wxCoupon) { |
|
|
|
//锁住人数 |
|
|
|
long time = System.currentTimeMillis() + RedisLock.LONG_TIMEOUT; |
|
|
|
String timeStr = String.valueOf(time); |
|
|
|
boolean grouppeopleLock = redisLock.lock("grouppeopleLock_"+order.getOrderGroupId(), timeStr); |
|
|
|
if (!grouppeopleLock) { |
|
|
|
throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"参团锁定失败,请稍后再试。"); |
|
|
|
} |
|
|
|
//此处拼团数量应从订单总数查询 |
|
|
|
int ordercount = wxOrderService.countGroupOrder(order.getOrderGroupId(),true); |
|
|
|
Integer remainPeople = wxCoupon.getPressLimitNum()-ordercount; |
|
|
|
//Integer remainPeople = wxOrderGroup.getRemainPeople()-1; |
|
|
|
wxOrderGroup.setStatus(remainPeople <= 0 ? EnumOrderStatus.ORDER_STATUS_COOPERATING_COMPLETE.getCode() : wxOrderGroup.getStatus()); |
|
|
|
wxOrderGroup.setUpdateDate(new Date()); |
|
|
|
wxOrderGroup.setRemainPeople(remainPeople); |
|
|
|
logger.info("非首次拼团,更新拼团信息, 剩余人数-1"); |
|
|
|
int row = wxOrderGroupMapper.updateOrderGroup(wxOrderGroup); |
|
|
|
if (row != 1) { |
|
|
|
//错误,更新状态,定时任务会发起退款,回滚库存 |
|
|
|
order.setOrderStatus(EnumOrderStatus.ORDER_STATUS_COOPERATING_UNPAID.getCode()); |
|
|
|
wxOrderMapper.updateById(order); |
|
|
|
redisLock.unlock("grouppeopleLock_"+order.getOrderGroupId(), timeStr); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"参团失败,将稍后退款."); |
|
|
|
}else { |
|
|
|
redisLock.unlock("grouppeopleLock_"+order.getOrderGroupId(), timeStr); |
|
|
|
} |
|
|
|
return remainPeople; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class}) |
|
|
|
public void createOrderGroup(WxOrder order, WxCoupon wxCoupon) { |
|
|
|
@@ -87,32 +137,8 @@ public class WxOrderGroupServiceImpl implements WxOrderGroupService { |
|
|
|
throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"参团人数已满,将稍后退款。"); |
|
|
|
} |
|
|
|
|
|
|
|
//锁住人数 |
|
|
|
long time = System.currentTimeMillis() + RedisLock.LONG_TIMEOUT; |
|
|
|
String timeStr = String.valueOf(time); |
|
|
|
boolean grouppeopleLock = redisLock.lock("grouppeopleLock_"+orderGroupId, timeStr); |
|
|
|
if (!grouppeopleLock) { |
|
|
|
throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"参团锁定失败,请稍后再试。"); |
|
|
|
} |
|
|
|
//此处拼团数量应从订单总数查询 |
|
|
|
int ordercount = wxOrderService.countGroupOrder(orderGroupId,true); |
|
|
|
Integer remainPeople = wxCoupon.getPressLimitNum()-ordercount; |
|
|
|
//Integer remainPeople = wxOrderGroup.getRemainPeople()-1; |
|
|
|
wxOrderGroup.setStatus(remainPeople <= 0 ? EnumOrderStatus.ORDER_STATUS_COOPERATING_COMPLETE.getCode() : wxOrderGroup.getStatus()); |
|
|
|
wxOrderGroup.setUpdateDate(new Date()); |
|
|
|
wxOrderGroup.setRemainPeople(remainPeople); |
|
|
|
logger.info("非首次拼团,更新拼团信息, 剩余人数-1"); |
|
|
|
int row = wxOrderGroupMapper.updateOrderGroup(wxOrderGroup); |
|
|
|
if (row != 1) { |
|
|
|
//错误,更新状态,定时任务会发起退款,回滚库存 |
|
|
|
order.setOrderStatus(EnumOrderStatus.ORDER_STATUS_COOPERATING_UNPAID.getCode()); |
|
|
|
wxOrderMapper.updateById(order); |
|
|
|
redisLock.unlock("grouppeopleLock_"+orderGroupId, timeStr); |
|
|
|
throw new MallinkException(ErrorCode.ORDER_GROUP_COOPERATING_FAILD.getCode(),"参团失败,将稍后退款."); |
|
|
|
}else { |
|
|
|
redisLock.unlock("grouppeopleLock_"+orderGroupId, timeStr); |
|
|
|
} |
|
|
|
|
|
|
|
int remainPeople = updateOrderGroup(wxOrderGroup,order,wxCoupon); |
|
|
|
|
|
|
|
// 更新订单信息 |
|
|
|
order.setOrderStatus(wxOrderGroup.getStatus()); |
|
|
|
order.setPressEndDate(wxOrderGroup.getExpiredDate()); |
|
|
|
@@ -399,4 +425,5 @@ public class WxOrderGroupServiceImpl implements WxOrderGroupService { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |