|
|
@@ -356,197 +356,6 @@ public class WxOrderServiceImpl implements WxOrderService { |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) |
|
|
|
|
|
public WxOrder saveCouponOrder(WxCUser user, Long couponChannelId, Long couponId, boolean isPress, Long orderGroupId, String formId) { |
|
|
|
|
|
// 检查用户 |
|
|
|
|
|
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: " + couponId); |
|
|
|
|
|
throw new MallinkException(ErrorCode.COUPON_IS_EMPTY); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (coupon.getStatus().equals(EnumCouponStatus.COUPON_STATUS_TAKE_OFFF.getCode())) { |
|
|
|
|
|
logger.error("券已下架, couponId: " + couponIdStr); |
|
|
|
|
|
throw new MallinkException(ErrorCode.COUPON_ORDER_IS_INVALID); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!isCouponMerchantValid(couponId)) { |
|
|
|
|
|
logger.error("商户不存在, couponId: " + couponIdStr); |
|
|
|
|
|
throw new MallinkException(ErrorCode.MERCHANT_INFO_NOT_FOUND); |
|
|
|
|
|
} |
|
|
|
|
|
// 拼团 |
|
|
|
|
|
if (isOrderGroup(orderGroupId)) { |
|
|
|
|
|
checkOrderGroup(user, orderGroupId); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 减库存操作 |
|
|
|
|
|
stockReduce(user, coupon, couponIdStr); |
|
|
|
|
|
|
|
|
|
|
|
Date curr = new Date(); |
|
|
|
|
|
|
|
|
|
|
|
int payment = coupon.getSalePrice(); |
|
|
|
|
|
Integer orderStatus = EnumOrderStatus.ORDER_STATUS_PENDING_PAYMENT.getCode(); |
|
|
|
|
|
if (isPress || coupon.getType().equals(EnumCouponType.COUPON_PRESS.getCode())) { |
|
|
|
|
|
// 砍价券 初始是全价即面额 |
|
|
|
|
|
payment = coupon.getPrice(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (coupon.getType().equals(EnumCouponType.COUPON_GROUP.getCode())) { |
|
|
|
|
|
if (orderGroupId == null) { |
|
|
|
|
|
// 拼团券 初始是全价即面额 |
|
|
|
|
|
payment = coupon.getPrice(); |
|
|
|
|
|
} else { |
|
|
|
|
|
orderStatus = EnumOrderStatus.ORDER_STATUS_COOPERATING_UNPAID.getCode(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
final IdWorker idWorker = IdWorker.get(); |
|
|
|
|
|
Long orderNumber = idWorker.nextId(); |
|
|
|
|
|
|
|
|
|
|
|
// body |
|
|
|
|
|
// tenant_id + merchant_id + title + subtitle |
|
|
|
|
|
String bodyStr = ""; |
|
|
|
|
|
try { |
|
|
|
|
|
bodyStr =// Utility.substring(wxMerchant.getName(), Math.min(64, wxMerchant.getName().getBytes().length), "utf-8") + |
|
|
|
|
|
Utility.substring(coupon.getTitle(), Math.min(62, coupon.getTitle().getBytes().length), "utf-8"); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
logger.error("body:" + e.getMessage()); |
|
|
|
|
|
bodyStr = /*wxMerchant.getName() + */ "-" + EnumCouponType.getEnum(coupon.getType()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
WxOrder record = new WxOrder(); |
|
|
|
|
|
record.setId(orderNumber); |
|
|
|
|
|
record.setTenantId(user.getTenantId()); |
|
|
|
|
|
record.setOrderNumber(orderNumber); |
|
|
|
|
|
record.setCouponChannelId(couponChannelId); |
|
|
|
|
|
record.setProductId(couponId); |
|
|
|
|
|
//当salePrice为0 并且为积分券或者积分停车券时 则进行积分支付 |
|
|
|
|
|
if (coupon.getCreditPrice() != null && coupon.getSalePrice() != null && coupon.getSalePrice() == 0 |
|
|
|
|
|
&& (coupon.getType() == EnumCouponType.COUPON_CREDIT.getCode() || coupon.getType() == EnumCouponType.COUPON_CREDIT_PARK.getCode())) { |
|
|
|
|
|
//若用户剩余积分比积分售价低则不能支付 |
|
|
|
|
|
if (user.getCredit() == null || user.getCredit() < coupon.getSalePrice()) { |
|
|
|
|
|
throw new MallinkException(ErrorCode.CREDIT_NOT_ENOUGH); |
|
|
|
|
|
} |
|
|
|
|
|
//-------此处为【积分支付】记录增加积分操作------- |
|
|
|
|
|
WxCreditHistory creditHistory = new WxCreditHistory(); |
|
|
|
|
|
//记录操作人类型 操作人id |
|
|
|
|
|
if (user.getOperatorType() == EnumUserType.MALLUSER.getCode()) { |
|
|
|
|
|
creditHistory.setOperatorType(EnumUserType.MALLUSER.getCode()); |
|
|
|
|
|
creditHistory.setOperatorId(user.getOperatorId()); |
|
|
|
|
|
} else { |
|
|
|
|
|
creditHistory.setOperatorType(EnumUserType.CUSER.getCode()); |
|
|
|
|
|
creditHistory.setOperatorId(user.getId()); |
|
|
|
|
|
} |
|
|
|
|
|
creditHistory.setCreditNum(coupon.getCreditPrice()); |
|
|
|
|
|
creditHistory.setCUserId(user.getId()); |
|
|
|
|
|
creditHistory.setCreateDate(new Date()); |
|
|
|
|
|
creditHistory.setTenantId(user.getTenantId()); |
|
|
|
|
|
creditHistory.setCreditType(EnumScoreType.CHANGE_CREDIT.getCode()); |
|
|
|
|
|
creditHistory.setCouponId(couponId); |
|
|
|
|
|
//如果券与商户一对一 则直接将消费商户更新为此商户 若一对多 则消费商户显示多商户 |
|
|
|
|
|
setMerchantId(coupon, creditHistory); |
|
|
|
|
|
wxCreditHistoryService.saveOrUpdate(creditHistory); |
|
|
|
|
|
record.setType(EnumOrderType.CREDIT.getCode()); |
|
|
|
|
|
//积分支付时 变更订单状态为已支付 |
|
|
|
|
|
record.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PAYMENT_SUCCESS.getCode()); |
|
|
|
|
|
} else { |
|
|
|
|
|
record.setType(EnumOrderType.COUPON.getCode()); |
|
|
|
|
|
record.setOrderStatus(orderStatus); |
|
|
|
|
|
} |
|
|
|
|
|
record.setcUserId(user.getId()); |
|
|
|
|
|
record.setPaymentType(EnumPayType.PAY_PAYMENT.getCode()); |
|
|
|
|
|
record.setPayment(payment); |
|
|
|
|
|
//record.setOrderStatus(orderStatus); |
|
|
|
|
|
record.setDetail(bodyStr); |
|
|
|
|
|
record.setCreateDate(curr); |
|
|
|
|
|
record.setUpdateDate(curr); |
|
|
|
|
|
record.setOrderGroupId(orderGroupId); |
|
|
|
|
|
record.setFormId(formId); |
|
|
|
|
|
|
|
|
|
|
|
if (isPress) { |
|
|
|
|
|
// 初始砍价信息 |
|
|
|
|
|
record.setPressCurrentNum(0); |
|
|
|
|
|
// 砍价券过期时间取券的过期时间和砍价限制时间中时间小的那个 |
|
|
|
|
|
Date pressEndDate = DateUtils.getHourTimeAfter(coupon.getPressLimitHours(), new Date()); |
|
|
|
|
|
if (pressEndDate.after(coupon.getValidEndDate())) |
|
|
|
|
|
pressEndDate = coupon.getValidEndDate(); |
|
|
|
|
|
record.setPressEndDate(pressEndDate); |
|
|
|
|
|
record.setPressCurrentValue(coupon.getPrice() - coupon.getSalePrice()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
// 保存订单 |
|
|
|
|
|
wxOrderMapper.insertSelective(record); |
|
|
|
|
|
} catch (RuntimeException e) { |
|
|
|
|
|
// 库存恢复 |
|
|
|
|
|
stockBack(record); |
|
|
|
|
|
logger.error("保存订单:" + e.getMessage()); |
|
|
|
|
|
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (isPress) { |
|
|
|
|
|
// 初始砍价 |
|
|
|
|
|
// 添加 wx_order_press |
|
|
|
|
|
int total = coupon.getPrice() - coupon.getSalePrice(); |
|
|
|
|
|
int left_total = total; |
|
|
|
|
|
WxOrderPress orderPress = new WxOrderPress(); |
|
|
|
|
|
orderPress.setId(idWorker.nextId()); |
|
|
|
|
|
orderPress.setOrderId(orderNumber); |
|
|
|
|
|
orderPress.setUserId(user.getId()); |
|
|
|
|
|
orderPress.setCreateDate(new Date()); |
|
|
|
|
|
orderPress.setFirst(EnumOrderPressType.FIRST.getCode()); |
|
|
|
|
|
int lPressValue = PressUtils.stateLessPressValue(total, left_total, coupon.getPressLimitNum(), 0); |
|
|
|
|
|
orderPress.setPressValue(lPressValue); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
// 保存订单 |
|
|
|
|
|
wxOrderPressMapper.insertSelective(orderPress); |
|
|
|
|
|
} catch (RuntimeException e) { |
|
|
|
|
|
// 库存恢复 |
|
|
|
|
|
stockBack(record); |
|
|
|
|
|
logger.error("保存砍价记录Err:" + e.getMessage()); |
|
|
|
|
|
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 更新砍价信息 |
|
|
|
|
|
WxOrder orderUpdatePress = new WxOrder(); |
|
|
|
|
|
orderUpdatePress.setId(record.getId()); |
|
|
|
|
|
orderUpdatePress.setPressCurrentNum(1); |
|
|
|
|
|
orderUpdatePress.setPressCurrentValue(left_total - orderPress.getPressValue()); |
|
|
|
|
|
orderUpdatePress.setOrderStatus(EnumOrderStatus.ORDER_STATUS_PRESSING.getCode()); |
|
|
|
|
|
orderUpdatePress.setUpdateDate(new Date()); |
|
|
|
|
|
try { |
|
|
|
|
|
// 保存订单 |
|
|
|
|
|
wxOrderMapper.updateByPrimaryKeySelective(orderUpdatePress); |
|
|
|
|
|
} catch (RuntimeException e) { |
|
|
|
|
|
// 库存恢复 |
|
|
|
|
|
stockBack(record); |
|
|
|
|
|
logger.error("更新订单记录Err:" + e.getMessage()); |
|
|
|
|
|
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// couponActionLog |
|
|
|
|
|
try { |
|
|
|
|
|
wxCouponActionLogService.addOne(user.getTenantId(), couponId, orderNumber, EnumCouponSendSendType.ACTIVE.getCode(), couponChannelId); |
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
// 库存恢复 |
|
|
|
|
|
stockBack(record); |
|
|
|
|
|
logger.error("保存订单:" + e.getMessage()); |
|
|
|
|
|
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return record; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) |
|
|
@Transactional(propagation = Propagation.REQUIRED, readOnly = false, rollbackFor = {Exception.class}) |
|
|
public WxOrder saveMicroPayOrder(WxMerchantBUser user, String totalFeeStr) { |
|
|
public WxOrder saveMicroPayOrder(WxMerchantBUser user, String totalFeeStr) { |
|
|
@@ -1365,8 +1174,6 @@ public class WxOrderServiceImpl implements WxOrderService { |
|
|
saveFreeOrder(record, user, coupon); |
|
|
saveFreeOrder(record, user, coupon); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
logger.error("下订单:" + e.getMessage()); |
|
|
logger.error("下订单:" + e.getMessage()); |
|
|
// 库存恢复 |
|
|
|
|
|
stockBack(record); |
|
|
|
|
|
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); |
|
|
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@@ -1375,8 +1182,6 @@ public class WxOrderServiceImpl implements WxOrderService { |
|
|
createCouponOrder(record, user, coupon); |
|
|
createCouponOrder(record, user, coupon); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
logger.error("couponOrder失败:" + e.getMessage()); |
|
|
logger.error("couponOrder失败:" + e.getMessage()); |
|
|
// 库存恢复 |
|
|
|
|
|
stockBack(record); |
|
|
|
|
|
throw new MallinkException(ErrorCode.COUPON_ORDER_SAVE_ERR); |
|
|
throw new MallinkException(ErrorCode.COUPON_ORDER_SAVE_ERR); |
|
|
} |
|
|
} |
|
|
return record; |
|
|
return record; |
|
|
@@ -1504,8 +1309,6 @@ public class WxOrderServiceImpl implements WxOrderService { |
|
|
try { |
|
|
try { |
|
|
orderNumber = saveNoFreeOrder(record, user, coupon, isPress); |
|
|
orderNumber = saveNoFreeOrder(record, user, coupon, isPress); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
// 库存恢复 |
|
|
|
|
|
stockBack(record); |
|
|
|
|
|
logger.error("保存订单:" + e.getMessage()); |
|
|
logger.error("保存订单:" + e.getMessage()); |
|
|
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); |
|
|
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); |
|
|
} |
|
|
} |
|
|
@@ -1515,8 +1318,6 @@ public class WxOrderServiceImpl implements WxOrderService { |
|
|
try { |
|
|
try { |
|
|
firstPress(record, user, coupon, orderNumber); |
|
|
firstPress(record, user, coupon, orderNumber); |
|
|
}catch (Exception e) { |
|
|
}catch (Exception e) { |
|
|
// 库存恢复 |
|
|
|
|
|
stockBack(record); |
|
|
|
|
|
logger.error("第一次砍价失败:" + e.getMessage()); |
|
|
logger.error("第一次砍价失败:" + e.getMessage()); |
|
|
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); |
|
|
throw new MallinkException(ErrorCode.ORDER_SAVE_ERR); |
|
|
} |
|
|
} |
|
|
|