From 714980b8662e3f300f35b6985836d9dd2bd22b83 Mon Sep 17 00:00:00 2001 From: hurui <177768073@qq.com> Date: Tue, 21 May 2024 14:54:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CouponDiscountUserRelService.java | 6 + .../CouponDiscountUserRelServiceImpl.java | 5 + .../service/order/OrderCreateService.java | 113 +++++------ .../order/impl/BsOrderRefundServiceImpl.java | 3 + .../order/impl/BsOrderServiceImpl.java | 182 ++++++++++++------ 5 files changed, 186 insertions(+), 123 deletions(-) diff --git a/service/src/main/java/com/hfkj/service/discount/CouponDiscountUserRelService.java b/service/src/main/java/com/hfkj/service/discount/CouponDiscountUserRelService.java index 089c4b3..97e5390 100644 --- a/service/src/main/java/com/hfkj/service/discount/CouponDiscountUserRelService.java +++ b/service/src/main/java/com/hfkj/service/discount/CouponDiscountUserRelService.java @@ -18,6 +18,12 @@ public interface CouponDiscountUserRelService { */ void create(CouponDiscountUserRel couponDiscountUserRel); + /** + * 修改 + * @param couponDiscountUserRel + */ + void update(CouponDiscountUserRel couponDiscountUserRel); + /** * @Author Sum1Dream * @Name getList diff --git a/service/src/main/java/com/hfkj/service/discount/impl/CouponDiscountUserRelServiceImpl.java b/service/src/main/java/com/hfkj/service/discount/impl/CouponDiscountUserRelServiceImpl.java index afa40ad..c7decfa 100644 --- a/service/src/main/java/com/hfkj/service/discount/impl/CouponDiscountUserRelServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/discount/impl/CouponDiscountUserRelServiceImpl.java @@ -25,6 +25,11 @@ public class CouponDiscountUserRelServiceImpl implements CouponDiscountUserRelSe couponDiscountUserRelMapper.insert(couponDiscountUserRel); } + @Override + public void update(CouponDiscountUserRel couponDiscountUserRel) { + couponDiscountUserRelMapper.updateByPrimaryKey(couponDiscountUserRel); + } + @Override public List getList(Map map) { CouponDiscountUserRelExample example = new CouponDiscountUserRelExample(); diff --git a/service/src/main/java/com/hfkj/service/order/OrderCreateService.java b/service/src/main/java/com/hfkj/service/order/OrderCreateService.java index 24564ec..2d14c3b 100644 --- a/service/src/main/java/com/hfkj/service/order/OrderCreateService.java +++ b/service/src/main/java/com/hfkj/service/order/OrderCreateService.java @@ -81,69 +81,62 @@ public class OrderCreateService { orderChild.setProductImg(goodsMsg.getListImg()); orderChild.setProductSpecName(goodsSpecs.getName()); orderChild.setProductPrice(goodsSpecs.getSalePrice()); - - if (goodsMsg.getType() == 1) { - // TODO 实物 - - - } else if (goodsMsg.getType() == 2) { - // 虚拟 - // 查询商品虚拟信息 - GoodsVpd goodsVpd = goodsVpdService.queryDetailBySpecsId(goodsSpecs.getId()); - if (goodsVpd == null) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商品虚拟规格"); - } - - // 创建卡券订单 - BsOrderCoupon orderCoupon = new BsOrderCoupon(); - orderCoupon.setOrderNo(orderChild.getOrderNo()); - orderCoupon.setChildOrderNo(orderChild.getChildOrderNo()); - orderCoupon.setMerId(orderChild.getMerId()); - orderCoupon.setUserId(order.getUserId()); - orderCoupon.setUserPhone(order.getUserPhone()); - orderCoupon.setGoodsId(goodsMsg.getId()); - orderCoupon.setGoodsName(goodsMsg.getTitle()); - orderCoupon.setGoodsTypeId(goodsMsg.getGoodsType()); - orderCoupon.setGoodsTypeName(goodsMsg.getGoodsTypeName()); - orderCoupon.setGoodsBrandId(goodsMsg.getGoodsBrand()); - orderCoupon.setGoodsBrandName(goodsMsg.getGoodsBrandName()); - orderCoupon.setGoodsSpecsId(goodsSpecs.getId()); - orderCoupon.setGoodsSpecsName(goodsSpecs.getName()); - orderCoupon.setGoodsSpescOriginalPrice(goodsSpecs.getOriginalPrice()); - orderCoupon.setGoodsVpdId(goodsVpd.getId()); - orderCoupon.setGoodsVpdKey(goodsVpd.getKey()); - orderCoupon.setGoodsVpdSource(goodsVpd.getSource()); - orderCoupon.setGoodsCount(orderChild.getProductCount()); - orderCoupon.setStatus(OrderChildStatusEnum.status1.getCode()); - orderCouponService.editData(orderCoupon); - - for (int i = 0; i < orderCoupon.getGoodsCount(); i++) { - // 卡券订单卡密 - BsOrderCouponNo orderCouponNo = new BsOrderCouponNo(); - orderCouponNo.setCouponOrderId(orderCoupon.getId()); - orderCouponNo.setOrderNo(orderCoupon.getId() + RandomUtils.number(6, false)); - orderCouponNo.setExpireStatus(false); - orderCouponNo.setExceptionStatus(false); - orderCouponNo.setStatus(OrderCouponNoStatusEnum.status1.getCode()); - - if (goodsVpd.getSource().equals(GoodsVpdSourceEnum.type4.getCode())) { - // 下单请求 - JSONObject jsonObject = HuiLianTongCouponService.recharge(orderCouponNo.getOrderNo(), - 1, - orderCoupon.getGoodsVpdKey(), - orderCoupon.getUserPhone()); - if (!jsonObject.getString("respCode").equals("0000")) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "下单失败"); - } - JSONObject response = HuiLianTongUnionCardService.resolveResponse(jsonObject.getString("data")); - if (!response.getString("respCode").equals("0000")) { - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, response.getString("msg")); - } - orderCouponNo.setChannelOrderNo(response.getJSONObject("data").getString("rechargeOrderId")); + // 虚拟 + // 查询商品虚拟信息 + GoodsVpd goodsVpd = goodsVpdService.queryDetailBySpecsId(goodsSpecs.getId()); + if (goodsVpd == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商品虚拟规格"); + } + // 创建卡券订单 + BsOrderCoupon orderCoupon = new BsOrderCoupon(); + orderCoupon.setOrderNo(orderChild.getOrderNo()); + orderCoupon.setChildOrderNo(orderChild.getChildOrderNo()); + orderCoupon.setMerId(orderChild.getMerId()); + orderCoupon.setUserId(order.getUserId()); + orderCoupon.setUserPhone(order.getUserPhone()); + orderCoupon.setGoodsId(goodsMsg.getId()); + orderCoupon.setGoodsName(goodsMsg.getTitle()); + orderCoupon.setGoodsTypeId(goodsMsg.getGoodsType()); + orderCoupon.setGoodsTypeName(goodsMsg.getGoodsTypeName()); + orderCoupon.setGoodsBrandId(goodsMsg.getGoodsBrand()); + orderCoupon.setGoodsBrandName(goodsMsg.getGoodsBrandName()); + orderCoupon.setGoodsSpecsId(goodsSpecs.getId()); + orderCoupon.setGoodsSpecsName(goodsSpecs.getName()); + orderCoupon.setGoodsSpescOriginalPrice(goodsSpecs.getOriginalPrice()); + orderCoupon.setGoodsVpdId(goodsVpd.getId()); + orderCoupon.setGoodsVpdKey(goodsVpd.getKey()); + orderCoupon.setGoodsVpdSource(goodsVpd.getSource()); + orderCoupon.setGoodsCount(orderChild.getProductCount()); + orderCoupon.setStatus(OrderChildStatusEnum.status1.getCode()); + orderCouponService.editData(orderCoupon); + + for (int i = 0; i < orderCoupon.getGoodsCount(); i++) { + // 卡券订单卡密 + BsOrderCouponNo orderCouponNo = new BsOrderCouponNo(); + orderCouponNo.setCouponOrderId(orderCoupon.getId()); + orderCouponNo.setOrderNo(orderCoupon.getId() + RandomUtils.number(6, false)); + orderCouponNo.setExpireStatus(false); + orderCouponNo.setExceptionStatus(false); + orderCouponNo.setStatus(OrderCouponNoStatusEnum.status1.getCode()); + + if (goodsVpd.getSource().equals(GoodsVpdSourceEnum.type4.getCode())) { + // 下单请求 + JSONObject jsonObject = HuiLianTongCouponService.recharge(orderCouponNo.getOrderNo(), + 1, + orderCoupon.getGoodsVpdKey(), + orderCoupon.getUserPhone()); + if (!jsonObject.getString("respCode").equals("0000")) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "下单失败"); } - orderCouponNoService.editData(orderCouponNo); + JSONObject response = HuiLianTongUnionCardService.resolveResponse(jsonObject.getString("data")); + if (!response.getString("respCode").equals("0000")) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, response.getString("msg")); + } + orderCouponNo.setChannelOrderNo(response.getJSONObject("data").getString("rechargeOrderId")); } + orderCouponNoService.editData(orderCouponNo); } + return orderChild; } diff --git a/service/src/main/java/com/hfkj/service/order/impl/BsOrderRefundServiceImpl.java b/service/src/main/java/com/hfkj/service/order/impl/BsOrderRefundServiceImpl.java index 4b65768..33d7da8 100644 --- a/service/src/main/java/com/hfkj/service/order/impl/BsOrderRefundServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/order/impl/BsOrderRefundServiceImpl.java @@ -185,6 +185,9 @@ public class BsOrderRefundServiceImpl implements BsOrderRefundService { orderChild.setStatus(orderChild.getSurplusRefundCount()==0?OrderChildStatusEnum.status4.getCode():orderChild.getStatus()); orderChildService.editData(orderChild); } + + // 删除缓存 + orderService.cacheDelete(order.getOrderNo()); return orderRefund; } diff --git a/service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java b/service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java index eb8422a..cae0ba1 100644 --- a/service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/order/impl/BsOrderServiceImpl.java @@ -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 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 opUser = new HashMap<>(); if (system) { @@ -297,8 +351,10 @@ public class BsOrderServiceImpl implements BsOrderService { source ); } + // 取消订单业务 orderCancelService.orderBusHandle(order); + // 更新缓存 cache(order); return order;