提交代码

master
胡锐 6 months ago
parent 7e1dfc4476
commit 714980b866
  1. 6
      service/src/main/java/com/hfkj/service/discount/CouponDiscountUserRelService.java
  2. 5
      service/src/main/java/com/hfkj/service/discount/impl/CouponDiscountUserRelServiceImpl.java
  3. 9
      service/src/main/java/com/hfkj/service/order/OrderCreateService.java
  4. 3
      service/src/main/java/com/hfkj/service/order/impl/BsOrderRefundServiceImpl.java
  5. 182
      service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java

@ -18,6 +18,12 @@ public interface CouponDiscountUserRelService {
*/ */
void create(CouponDiscountUserRel couponDiscountUserRel); void create(CouponDiscountUserRel couponDiscountUserRel);
/**
* 修改
* @param couponDiscountUserRel
*/
void update(CouponDiscountUserRel couponDiscountUserRel);
/** /**
* @Author Sum1Dream * @Author Sum1Dream
* @Name getList * @Name getList

@ -25,6 +25,11 @@ public class CouponDiscountUserRelServiceImpl implements CouponDiscountUserRelSe
couponDiscountUserRelMapper.insert(couponDiscountUserRel); couponDiscountUserRelMapper.insert(couponDiscountUserRel);
} }
@Override
public void update(CouponDiscountUserRel couponDiscountUserRel) {
couponDiscountUserRelMapper.updateByPrimaryKey(couponDiscountUserRel);
}
@Override @Override
public List<CouponDiscountUserRel> getList(Map<String, Object> map) { public List<CouponDiscountUserRel> getList(Map<String, Object> map) {
CouponDiscountUserRelExample example = new CouponDiscountUserRelExample(); CouponDiscountUserRelExample example = new CouponDiscountUserRelExample();

@ -81,19 +81,12 @@ public class OrderCreateService {
orderChild.setProductImg(goodsMsg.getListImg()); orderChild.setProductImg(goodsMsg.getListImg());
orderChild.setProductSpecName(goodsSpecs.getName()); orderChild.setProductSpecName(goodsSpecs.getName());
orderChild.setProductPrice(goodsSpecs.getSalePrice()); orderChild.setProductPrice(goodsSpecs.getSalePrice());
if (goodsMsg.getType() == 1) {
// TODO 实物
} else if (goodsMsg.getType() == 2) {
// 虚拟 // 虚拟
// 查询商品虚拟信息 // 查询商品虚拟信息
GoodsVpd goodsVpd = goodsVpdService.queryDetailBySpecsId(goodsSpecs.getId()); GoodsVpd goodsVpd = goodsVpdService.queryDetailBySpecsId(goodsSpecs.getId());
if (goodsVpd == null) { if (goodsVpd == null) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商品虚拟规格"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商品虚拟规格");
} }
// 创建卡券订单 // 创建卡券订单
BsOrderCoupon orderCoupon = new BsOrderCoupon(); BsOrderCoupon orderCoupon = new BsOrderCoupon();
orderCoupon.setOrderNo(orderChild.getOrderNo()); orderCoupon.setOrderNo(orderChild.getOrderNo());
@ -143,7 +136,7 @@ public class OrderCreateService {
} }
orderCouponNoService.editData(orderCouponNo); orderCouponNoService.editData(orderCouponNo);
} }
}
return orderChild; return orderChild;
} }

@ -185,6 +185,9 @@ public class BsOrderRefundServiceImpl implements BsOrderRefundService {
orderChild.setStatus(orderChild.getSurplusRefundCount()==0?OrderChildStatusEnum.status4.getCode():orderChild.getStatus()); orderChild.setStatus(orderChild.getSurplusRefundCount()==0?OrderChildStatusEnum.status4.getCode():orderChild.getStatus());
orderChildService.editData(orderChild); orderChildService.editData(orderChild);
} }
// 删除缓存
orderService.cacheDelete(order.getOrderNo());
return orderRefund; return orderRefund;
} }

@ -25,6 +25,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate; import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder; import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -53,6 +54,8 @@ public class BsOrderServiceImpl implements BsOrderService {
private RedisUtil redisUtil; private RedisUtil redisUtil;
@Resource @Resource
private RocketMQTemplate rocketMQTemplate; private RocketMQTemplate rocketMQTemplate;
@Autowired
private RedisTemplate redisTemplate;
@Resource @Resource
private BsOrderMapper orderMapper; private BsOrderMapper orderMapper;
@Resource @Resource
@ -109,46 +112,42 @@ public class BsOrderServiceImpl implements BsOrderService {
order.setOrderNo(DateUtil.date2String(new Date(), "yyMMddHHmmss") + RandomUtils.number(6, false)); order.setOrderNo(DateUtil.date2String(new Date(), "yyMMddHHmmss") + RandomUtils.number(6, false));
editData(order); 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(); BsOrderDeduction deduction = order.getDeduction()==null?new BsOrderDeduction():order.getDeduction();
deduction.setOrderId(order.getId()); deduction.setOrderId(order.getId());
deduction.setOrderNo(order.getOrderNo()); deduction.setOrderNo(order.getOrderNo());
deduction.setIntegralDiscountPrice(deduction.getIntegralDiscountPrice()==null?0L: deduction.getIntegralDiscountPrice()); deduction.setIntegralDiscountPrice(deduction.getIntegralDiscountPrice()==null?0L: deduction.getIntegralDiscountPrice());
if (order.getDeduction().getUserCouponDiscountId() != null) { if (deduction.getUserCouponDiscountId() != null) {
// 查询用户优惠券 // 使用优惠券
CouponDiscountUserRel discountUserRel = couponDiscountUserRelService.getRel(order.getDeduction().getUserCouponDiscountId()); useDeduction(order, deduction, 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, "未知的优惠券优惠类型");
}
} else { } else {
deduction.setCouponDiscountPrice(new BigDecimal("0")); deduction.setCouponDiscountPrice(new BigDecimal("0"));
deduction.setCouponDiscountActualPrice(new BigDecimal("0")); deduction.setCouponDiscountActualPrice(new BigDecimal("0"));
@ -159,6 +158,7 @@ public class BsOrderServiceImpl implements BsOrderService {
orderDeductionService.editData(deduction); orderDeductionService.editData(deduction);
order.setDeduction(deduction); order.setDeduction(deduction);
// 使用积分抵扣
if (order.getDeduction().getIntegralDiscountPrice() > 0) { if (order.getDeduction().getIntegralDiscountPrice() > 0) {
Map<String,Object> opUser = new HashMap<>(); Map<String,Object> opUser = new HashMap<>();
opUser.put("opUserType", UserIntegralRecordOpUserTypeEnum.type3.getCode()); opUser.put("opUserType", UserIntegralRecordOpUserTypeEnum.type3.getCode());
@ -180,36 +180,10 @@ public class BsOrderServiceImpl implements BsOrderService {
source 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.setPayRealPrice(totalPrice.subtract(order.getDeduction().getTotalDeductionPrice()));
order.setOrderStatus(order.getPayRealPrice().equals(new BigDecimal("0"))?OrderStatusEnum.status2.getCode():OrderStatusEnum.status1.getCode()); order.setOrderStatus(order.getPayRealPrice().equals(new BigDecimal("0"))?OrderStatusEnum.status2.getCode():OrderStatusEnum.status1.getCode());
// 订单入库前处理 // 订单入库前处理
for (OrderChildModel childOrder : order.getOrderChildList()) { for (OrderChildModel childOrder : order.getOrderChildList()) {
childOrder.setSurplusRefundCount(childOrder.getProductCount()); childOrder.setSurplusRefundCount(childOrder.getProductCount());
@ -251,6 +225,74 @@ public class BsOrderServiceImpl implements BsOrderService {
return order; 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 @Override
@Transactional(propagation= Propagation.REQUIRES_NEW, rollbackFor= {RuntimeException.class}, timeout = 10) @Transactional(propagation= Propagation.REQUIRES_NEW, rollbackFor= {RuntimeException.class}, timeout = 10)
public OrderModel cancel(String orderNo, boolean system) { public OrderModel cancel(String orderNo, boolean system) {
@ -271,6 +313,18 @@ public class BsOrderServiceImpl implements BsOrderService {
orderChildService.editData(orderChild); 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) { if (order.getDeduction().getIntegralDiscountPrice() > 0) {
Map<String,Object> opUser = new HashMap<>(); Map<String,Object> opUser = new HashMap<>();
if (system) { if (system) {
@ -297,8 +351,10 @@ public class BsOrderServiceImpl implements BsOrderService {
source source
); );
} }
// 取消订单业务 // 取消订单业务
orderCancelService.orderBusHandle(order); orderCancelService.orderBusHandle(order);
// 更新缓存 // 更新缓存
cache(order); cache(order);
return order; return order;

Loading…
Cancel
Save