|
|
|
@ -25,6 +25,7 @@ import org.apache.commons.lang3.StringUtils; |
|
|
|
|
import org.apache.rocketmq.spring.core.RocketMQTemplate; |
|
|
|
|
import org.springframework.beans.BeanUtils; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
|
import org.springframework.messaging.Message; |
|
|
|
|
import org.springframework.messaging.support.MessageBuilder; |
|
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
@ -53,6 +54,8 @@ public class BsOrderServiceImpl implements BsOrderService { |
|
|
|
|
private RedisUtil redisUtil; |
|
|
|
|
@Resource |
|
|
|
|
private RocketMQTemplate rocketMQTemplate; |
|
|
|
|
@Autowired |
|
|
|
|
private RedisTemplate redisTemplate; |
|
|
|
|
@Resource |
|
|
|
|
private BsOrderMapper orderMapper; |
|
|
|
|
@Resource |
|
|
|
@ -109,46 +112,42 @@ public class BsOrderServiceImpl implements BsOrderService { |
|
|
|
|
order.setOrderNo(DateUtil.date2String(new Date(), "yyMMddHHmmss") + RandomUtils.number(6, false)); |
|
|
|
|
editData(order); |
|
|
|
|
|
|
|
|
|
// 交易优惠
|
|
|
|
|
// 订单总金额
|
|
|
|
|
BigDecimal totalPrice = new BigDecimal("0"); |
|
|
|
|
// 商品总金额
|
|
|
|
|
BigDecimal productTotalPrice = new BigDecimal("0"); |
|
|
|
|
|
|
|
|
|
/************** 处理业务 ***************/ |
|
|
|
|
for (OrderChildModel child : order.getOrderChildList()) { |
|
|
|
|
child.setOrderNo(order.getOrderNo()); |
|
|
|
|
// 子订单号 交易id + 4位随机数
|
|
|
|
|
child.setChildOrderNo(order.getId()+RandomUtils.number(4, false)); |
|
|
|
|
// 提交订单前实物商品处理
|
|
|
|
|
if (child.getProductType().equals(OrderChildProductTypeEnum.type1.getCode())) { |
|
|
|
|
child = orderCreateService.goods(order, child); |
|
|
|
|
} |
|
|
|
|
// 提交订单前产品处理
|
|
|
|
|
if (child.getProductType().equals(OrderChildProductTypeEnum.type2.getCode())) { |
|
|
|
|
child = orderCreateService.product(order, child); |
|
|
|
|
} |
|
|
|
|
child.setProductTotalPrice(child.getProductPrice().multiply(new BigDecimal(child.getProductCount().toString()))); |
|
|
|
|
child.setStatus(OrderChildStatusEnum.status1.getCode()); |
|
|
|
|
|
|
|
|
|
productTotalPrice = productTotalPrice.add(child.getProductTotalPrice()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
totalPrice = productTotalPrice; |
|
|
|
|
order.setTotalPrice(totalPrice); |
|
|
|
|
order.setProductTotalPrice(productTotalPrice); |
|
|
|
|
|
|
|
|
|
/************ 交易优惠 **************/ |
|
|
|
|
BsOrderDeduction deduction = order.getDeduction()==null?new BsOrderDeduction():order.getDeduction(); |
|
|
|
|
deduction.setOrderId(order.getId()); |
|
|
|
|
deduction.setOrderNo(order.getOrderNo()); |
|
|
|
|
deduction.setIntegralDiscountPrice(deduction.getIntegralDiscountPrice()==null?0L: deduction.getIntegralDiscountPrice()); |
|
|
|
|
if (order.getDeduction().getUserCouponDiscountId() != null) { |
|
|
|
|
// 查询用户优惠券
|
|
|
|
|
CouponDiscountUserRel discountUserRel = couponDiscountUserRelService.getRel(order.getDeduction().getUserCouponDiscountId()); |
|
|
|
|
if (discountUserRel == null) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知优惠券"); |
|
|
|
|
} |
|
|
|
|
if (discountUserRel.getStatus() != 1) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券状态不处于待使用"); |
|
|
|
|
} |
|
|
|
|
// 查询优惠券
|
|
|
|
|
CouponDiscount discount = couponDiscountService.queryDetail(discountUserRel.getDiscountId()); |
|
|
|
|
if (discount == null) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知优惠券"); |
|
|
|
|
} |
|
|
|
|
// 计算优惠
|
|
|
|
|
deduction.setCouponDiscountId(discount.getId()); |
|
|
|
|
deduction.setCouponDiscountType(discount.getType()); |
|
|
|
|
deduction.setCouponDiscountPrice(discount.getPrice()); |
|
|
|
|
// 卡卷类型 1:满减 2:抵扣 3:折扣
|
|
|
|
|
if (1 == discount.getType()) { |
|
|
|
|
deduction.setCouponDiscountActualPrice(discount.getPrice()); |
|
|
|
|
} else if (2 == discount.getType()) { |
|
|
|
|
if (discount.getCondition().compareTo(order.getProductTotalPrice()) < 0) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未满足优惠券满减条件"); |
|
|
|
|
} |
|
|
|
|
deduction.setCouponDiscountActualPrice(discount.getPrice()); |
|
|
|
|
} else if (3 == discount.getType()) { |
|
|
|
|
deduction.setCouponDiscountActualPrice( |
|
|
|
|
order.getTotalPrice() |
|
|
|
|
.multiply(discount.getPrice().divide(new BigDecimal("100"))) |
|
|
|
|
.setScale(2, BigDecimal.ROUND_DOWN) |
|
|
|
|
); |
|
|
|
|
} else { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的优惠券优惠类型"); |
|
|
|
|
} |
|
|
|
|
if (deduction.getUserCouponDiscountId() != null) { |
|
|
|
|
// 使用优惠券
|
|
|
|
|
useDeduction(order, deduction, order.getDeduction().getUserCouponDiscountId()); |
|
|
|
|
} else { |
|
|
|
|
deduction.setCouponDiscountPrice(new BigDecimal("0")); |
|
|
|
|
deduction.setCouponDiscountActualPrice(new BigDecimal("0")); |
|
|
|
@ -159,6 +158,7 @@ public class BsOrderServiceImpl implements BsOrderService { |
|
|
|
|
orderDeductionService.editData(deduction); |
|
|
|
|
order.setDeduction(deduction); |
|
|
|
|
|
|
|
|
|
// 使用积分抵扣
|
|
|
|
|
if (order.getDeduction().getIntegralDiscountPrice() > 0) { |
|
|
|
|
Map<String,Object> opUser = new HashMap<>(); |
|
|
|
|
opUser.put("opUserType", UserIntegralRecordOpUserTypeEnum.type3.getCode()); |
|
|
|
@ -180,36 +180,10 @@ public class BsOrderServiceImpl implements BsOrderService { |
|
|
|
|
source |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
/************ 交易优惠 **************/ |
|
|
|
|
|
|
|
|
|
// 订单总金额
|
|
|
|
|
BigDecimal totalPrice = new BigDecimal("0"); |
|
|
|
|
// 商品总金额
|
|
|
|
|
BigDecimal productTotalPrice = new BigDecimal("0"); |
|
|
|
|
|
|
|
|
|
for (OrderChildModel child : order.getOrderChildList()) { |
|
|
|
|
child.setOrderNo(order.getOrderNo()); |
|
|
|
|
// 子订单号 交易id + 4位随机数
|
|
|
|
|
child.setChildOrderNo(order.getId()+RandomUtils.number(4, false)); |
|
|
|
|
// 提交订单前实物商品处理
|
|
|
|
|
if (child.getProductType().equals(OrderChildProductTypeEnum.type1.getCode())) { |
|
|
|
|
child = orderCreateService.goods(order, child); |
|
|
|
|
} |
|
|
|
|
// 提交订单前产品处理
|
|
|
|
|
if (child.getProductType().equals(OrderChildProductTypeEnum.type2.getCode())) { |
|
|
|
|
child = orderCreateService.product(order, child); |
|
|
|
|
} |
|
|
|
|
child.setProductTotalPrice(child.getProductPrice().multiply(new BigDecimal(child.getProductCount().toString()))); |
|
|
|
|
child.setStatus(OrderChildStatusEnum.status1.getCode()); |
|
|
|
|
|
|
|
|
|
productTotalPrice = productTotalPrice.add(child.getProductTotalPrice()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
totalPrice = productTotalPrice; |
|
|
|
|
order.setTotalPrice(totalPrice); |
|
|
|
|
order.setProductTotalPrice(productTotalPrice); |
|
|
|
|
order.setPayRealPrice(totalPrice.subtract(order.getDeduction().getTotalDeductionPrice())); |
|
|
|
|
order.setOrderStatus(order.getPayRealPrice().equals(new BigDecimal("0"))?OrderStatusEnum.status2.getCode():OrderStatusEnum.status1.getCode()); |
|
|
|
|
|
|
|
|
|
// 订单入库前处理
|
|
|
|
|
for (OrderChildModel childOrder : order.getOrderChildList()) { |
|
|
|
|
childOrder.setSurplusRefundCount(childOrder.getProductCount()); |
|
|
|
@ -251,6 +225,74 @@ public class BsOrderServiceImpl implements BsOrderService { |
|
|
|
|
return order; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 订单使用优惠券 |
|
|
|
|
* @param order |
|
|
|
|
* @param deduction |
|
|
|
|
* @param userCouponDiscountId |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
private BsOrderDeduction useDeduction(OrderModel order,BsOrderDeduction deduction, Long userCouponDiscountId) { |
|
|
|
|
String key = "ORDER_USE_DEDUCTION_" + userCouponDiscountId; |
|
|
|
|
// 获取锁
|
|
|
|
|
Boolean lock = redisTemplate.opsForValue().setIfAbsent(key, userCouponDiscountId); |
|
|
|
|
if (Boolean.FALSE.equals(lock)) { |
|
|
|
|
try { |
|
|
|
|
Thread.sleep(100); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
useDeduction(order, deduction,userCouponDiscountId); |
|
|
|
|
} |
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "积分交易异常"); |
|
|
|
|
} finally { |
|
|
|
|
redisTemplate.delete(key); |
|
|
|
|
} |
|
|
|
|
// 查询用户优惠券
|
|
|
|
|
CouponDiscountUserRel discountUserRel = couponDiscountUserRelService.getRel(userCouponDiscountId); |
|
|
|
|
if (discountUserRel == null) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知优惠券"); |
|
|
|
|
} |
|
|
|
|
if (discountUserRel.getStatus() != 1) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券状态不处于待使用"); |
|
|
|
|
} |
|
|
|
|
// 查询优惠券
|
|
|
|
|
CouponDiscount discount = couponDiscountService.queryDetail(discountUserRel.getDiscountId()); |
|
|
|
|
if (discount == null) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知优惠券"); |
|
|
|
|
} |
|
|
|
|
// 计算优惠
|
|
|
|
|
deduction.setCouponDiscountId(discount.getId()); |
|
|
|
|
deduction.setCouponDiscountType(discount.getType()); |
|
|
|
|
deduction.setCouponDiscountPrice(discount.getPrice()); |
|
|
|
|
// 卡卷类型 1:满减 2:抵扣 3:折扣
|
|
|
|
|
if (1 == discount.getType()) { |
|
|
|
|
deduction.setCouponDiscountActualPrice(discount.getPrice()); |
|
|
|
|
} else if (2 == discount.getType()) { |
|
|
|
|
if (discount.getCondition().compareTo(order.getProductTotalPrice()) < 0) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未满足优惠券满减条件"); |
|
|
|
|
} |
|
|
|
|
deduction.setCouponDiscountActualPrice(discount.getPrice()); |
|
|
|
|
} else if (3 == discount.getType()) { |
|
|
|
|
deduction.setCouponDiscountActualPrice( |
|
|
|
|
order.getTotalPrice() |
|
|
|
|
.multiply(discount.getPrice().divide(new BigDecimal("100"))) |
|
|
|
|
.setScale(2, BigDecimal.ROUND_DOWN) |
|
|
|
|
); |
|
|
|
|
} else { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的优惠券优惠类型"); |
|
|
|
|
} |
|
|
|
|
// 优惠券优惠金额 + 积分抵扣实际金额
|
|
|
|
|
deduction.setTotalDeductionPrice(deduction.getCouponDiscountActualPrice() |
|
|
|
|
.add(new BigDecimal(deduction.getIntegralDiscountPrice().toString()).divide(new BigDecimal("100")))); |
|
|
|
|
orderDeductionService.editData(deduction); |
|
|
|
|
|
|
|
|
|
return deduction; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
@Transactional(propagation= Propagation.REQUIRES_NEW, rollbackFor= {RuntimeException.class}, timeout = 10) |
|
|
|
|
public OrderModel cancel(String orderNo, boolean system) { |
|
|
|
@ -271,6 +313,18 @@ public class BsOrderServiceImpl implements BsOrderService { |
|
|
|
|
orderChildService.editData(orderChild); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 退回优惠券
|
|
|
|
|
if (order.getDeduction().getUserCouponDiscountId() != null) { |
|
|
|
|
// 查询用户优惠券
|
|
|
|
|
CouponDiscountUserRel discountUserRel = couponDiscountUserRelService.getRel(order.getDeduction().getUserCouponDiscountId()); |
|
|
|
|
if (discountUserRel == null) { |
|
|
|
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知优惠券"); |
|
|
|
|
} |
|
|
|
|
discountUserRel.setStatus(1); |
|
|
|
|
couponDiscountUserRelService.update(discountUserRel); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 退款用户积分
|
|
|
|
|
if (order.getDeduction().getIntegralDiscountPrice() > 0) { |
|
|
|
|
Map<String,Object> opUser = new HashMap<>(); |
|
|
|
|
if (system) { |
|
|
|
@ -297,8 +351,10 @@ public class BsOrderServiceImpl implements BsOrderService { |
|
|
|
|
source |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 取消订单业务
|
|
|
|
|
orderCancelService.orderBusHandle(order); |
|
|
|
|
|
|
|
|
|
// 更新缓存
|
|
|
|
|
cache(order); |
|
|
|
|
return order; |
|
|
|
|