From 552ccc657617b998b2aa2bcd33030491b97587f9 Mon Sep 17 00:00:00 2001 From: yuanye <418471657@qq.com> Date: Tue, 21 May 2024 13:47:21 +0800 Subject: [PATCH] Changes --- .../com/order/controller/OrderController.java | 4 +- .../java/com/hfkj/entity/BsOrderGoods.java | 4 +- .../service/goods/BsOrderGoodsService.java | 10 +++ .../hfkj/service/goods/GoodsSpecsService.java | 10 +++ .../goods/impl/BsOrderGoodsServiceImpl.java | 15 ++++ .../goods/impl/GoodsSpecsServiceImpl.java | 41 +++++++++ .../hfkj/service/order/BsOrderService.java | 7 ++ .../service/order/OrderCreateService.java | 83 +++++++++++++++++-- .../service/order/OrderPaySuccessService.java | 26 ++++-- .../order/impl/BsOrderRefundServiceImpl.java | 4 +- .../order/impl/BsOrderServiceImpl.java | 15 +++- .../sysenum/order/OrderGoodsStatusEnum.java | 60 ++++++++++++++ 12 files changed, 256 insertions(+), 23 deletions(-) create mode 100644 service/src/main/java/com/hfkj/sysenum/order/OrderGoodsStatusEnum.java diff --git a/order/src/main/java/com/order/controller/OrderController.java b/order/src/main/java/com/order/controller/OrderController.java index 67398ee..603ba12 100644 --- a/order/src/main/java/com/order/controller/OrderController.java +++ b/order/src/main/java/com/order/controller/OrderController.java @@ -73,11 +73,11 @@ public class OrderController { || orderChild.getProductCount() == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } - - if (orderChild.getProductType().equals(OrderChildProductTypeEnum.type2.getCode()) + if ( orderChild.getProductType().equals(OrderChildProductTypeEnum.type1.getCode()) || orderChild.getProductType().equals(OrderChildProductTypeEnum.type2.getCode()) && orderChild.getProductSpecId() == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } + } body.setUserId(userSession.getUser().getId()); diff --git a/service/src/main/java/com/hfkj/entity/BsOrderGoods.java b/service/src/main/java/com/hfkj/entity/BsOrderGoods.java index 4483963..55b22a8 100644 --- a/service/src/main/java/com/hfkj/entity/BsOrderGoods.java +++ b/service/src/main/java/com/hfkj/entity/BsOrderGoods.java @@ -155,7 +155,7 @@ public class BsOrderGoods implements Serializable { private Integer payType; /** - * 状态与子订单状态同步 + * 1:待发货 2:已发货 3:配送中 4:已送达 5:已取消 6:退货中 7:已退货 8:待支付 0:删除 */ private Integer status; @@ -646,4 +646,4 @@ public class BsOrderGoods implements Serializable { sb.append("]"); return sb.toString(); } -} \ No newline at end of file +} diff --git a/service/src/main/java/com/hfkj/service/goods/BsOrderGoodsService.java b/service/src/main/java/com/hfkj/service/goods/BsOrderGoodsService.java index 4d68a3a..11310f7 100644 --- a/service/src/main/java/com/hfkj/service/goods/BsOrderGoodsService.java +++ b/service/src/main/java/com/hfkj/service/goods/BsOrderGoodsService.java @@ -47,6 +47,16 @@ public interface BsOrderGoodsService { */ BsOrderGoods queryDetail(Long id); + /** + * @Author Sum1Dream + * @Name findGoodsOrder + * @Description // 查询详情 + * @Date 15:44 2023/4/20 + * @Param [orderNo] + * @Return com.hai.entity.GoodsOrder + */ + BsOrderGoods findGoodsOrder(String orderNo); + /** * @Author Sum1Dream * @Name queryDetailByMap diff --git a/service/src/main/java/com/hfkj/service/goods/GoodsSpecsService.java b/service/src/main/java/com/hfkj/service/goods/GoodsSpecsService.java index 9ff9f6c..aacc124 100644 --- a/service/src/main/java/com/hfkj/service/goods/GoodsSpecsService.java +++ b/service/src/main/java/com/hfkj/service/goods/GoodsSpecsService.java @@ -75,5 +75,15 @@ public interface GoodsSpecsService { * @return java.util.List */ List getList(Map map); + + /** + * @Author Sum1Dream + * @Name decreaseStockNum + * @Description // 减少库存 + * @Date 17:34 2023/4/19 + * @Param [skuId, num] + * @Return void + */ + void decreaseStockNum(Long specsId , Integer num); } diff --git a/service/src/main/java/com/hfkj/service/goods/impl/BsOrderGoodsServiceImpl.java b/service/src/main/java/com/hfkj/service/goods/impl/BsOrderGoodsServiceImpl.java index 79b4be0..f5d8264 100644 --- a/service/src/main/java/com/hfkj/service/goods/impl/BsOrderGoodsServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/goods/impl/BsOrderGoodsServiceImpl.java @@ -2,6 +2,7 @@ package com.hfkj.service.goods.impl; import com.hfkj.dao.BsOrderGoodsMapper; import com.hfkj.entity.BsOrderGoods; +import com.hfkj.entity.BsOrderGoodsExample; import com.hfkj.service.goods.BsOrderGoodsService; import org.springframework.stereotype.Service; @@ -39,6 +40,20 @@ public class BsOrderGoodsServiceImpl implements BsOrderGoodsService { } } + @Override + public BsOrderGoods findGoodsOrder(String orderNo) { + BsOrderGoodsExample example = new BsOrderGoodsExample(); + example.createCriteria().andStatusNotEqualTo(0).andOrderNoEqualTo(orderNo); + + List list = bsOrderGoodsMapper.selectByExample(example); + + if (!list.isEmpty()) { + return list.get(0); + } + + return null; + } + @Override public BsOrderGoods queryDetail(Long id) { return bsOrderGoodsMapper.selectByPrimaryKey(id); diff --git a/service/src/main/java/com/hfkj/service/goods/impl/GoodsSpecsServiceImpl.java b/service/src/main/java/com/hfkj/service/goods/impl/GoodsSpecsServiceImpl.java index 439bb31..5d751cc 100644 --- a/service/src/main/java/com/hfkj/service/goods/impl/GoodsSpecsServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/goods/impl/GoodsSpecsServiceImpl.java @@ -1,5 +1,8 @@ package com.hfkj.service.goods.impl; +import com.hfkj.common.exception.ErrorCode; +import com.hfkj.common.exception.ErrorHelp; +import com.hfkj.common.exception.SysCode; import com.hfkj.dao.GoodsSpecsMapper; import com.hfkj.entity.GoodsMsg; import com.hfkj.entity.GoodsSpecs; @@ -7,6 +10,8 @@ import com.hfkj.entity.GoodsSpecsExample; import com.hfkj.service.goods.GoodsSpecsService; import com.hfkj.service.goods.GoodsVpdService; import org.apache.commons.collections4.MapUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -24,6 +29,9 @@ public class GoodsSpecsServiceImpl implements GoodsSpecsService { @Resource private GoodsVpdService goodsVpdService; + @Autowired + private RedisTemplate redisTemplate; + @Override public void create(GoodsSpecs goodsSpecs) { @@ -88,4 +96,37 @@ public class GoodsSpecsServiceImpl implements GoodsSpecsService { return goodsSpecsMapper.selectByExample(goodsSpecsExample); } + + @Override + public void decreaseStockNum(Long specsId, Integer num) { + String key = "GOODS_STOCK_NUM"; + + GoodsSpecs goodsSpecs = queryDetailByGoodsId(specsId); + + try { + // 分布式锁占坑 + Boolean lock = redisTemplate.opsForValue().setIfAbsent(key, specsId); + + if(Boolean.TRUE.equals(lock)) { + if (goodsSpecs.getStock() < num) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, goodsSpecs.getName() + "库存不足"); + } + // 加锁成功,处理业务 + goodsSpecs.setStock(goodsSpecs.getStock() - num); + + update(goodsSpecs); + + } else { + // 加锁失败,重试 + Thread.sleep(100); + decreaseStockNum(specsId , num); + } + + } catch (Exception e) { + e.printStackTrace(); + } finally { + // 删除key,释放锁 + redisTemplate.delete(key); + } + } } diff --git a/service/src/main/java/com/hfkj/service/order/BsOrderService.java b/service/src/main/java/com/hfkj/service/order/BsOrderService.java index 5e15a8a..c5a4fb9 100644 --- a/service/src/main/java/com/hfkj/service/order/BsOrderService.java +++ b/service/src/main/java/com/hfkj/service/order/BsOrderService.java @@ -27,6 +27,13 @@ public interface BsOrderService { */ OrderModel cache(OrderModel order); + /** + * 删除缓存 + * @param orderNo + * @return + */ + void cacheDelete(String orderNo); + /** * 创建订单 * @param order 订单 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 12c3e7e..24564ec 100644 --- a/service/src/main/java/com/hfkj/service/order/OrderCreateService.java +++ b/service/src/main/java/com/hfkj/service/order/OrderCreateService.java @@ -12,14 +12,13 @@ import com.hfkj.model.order.OrderModel; import com.hfkj.service.coupon.BsOrderCouponNoService; import com.hfkj.service.coupon.BsOrderCouponService; import com.hfkj.service.coupon.channel.HuiLianTongCouponService; -import com.hfkj.service.goods.GoodsMsgService; -import com.hfkj.service.goods.GoodsSpecsService; -import com.hfkj.service.goods.GoodsVpdService; +import com.hfkj.service.goods.*; import com.hfkj.service.hlt.HuiLianTongUnionCardService; import com.hfkj.sysenum.GoodsVpdSourceEnum; import com.hfkj.sysenum.order.OrderChildProductTypeEnum; import com.hfkj.sysenum.order.OrderChildStatusEnum; import com.hfkj.sysenum.order.OrderCouponNoStatusEnum; +import com.hfkj.sysenum.order.OrderGoodsStatusEnum; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -40,22 +39,23 @@ import java.math.BigDecimal; @Component public class OrderCreateService { Logger log = LoggerFactory.getLogger(OrderCreateService.class); - @Autowired - private RedisUtil redisUtil; - @Resource - private BsOrderService orderService; + @Resource private BsOrderCouponService orderCouponService; @Resource private BsOrderCouponNoService orderCouponNoService; @Resource - private BsOrderChildService orderChildService; - @Resource private GoodsMsgService goodsMsgService; @Resource private GoodsSpecsService goodsSpecsService; @Resource private GoodsVpdService goodsVpdService; + @Resource + private GoodsUserAddressService goodsUserAddressService; + @Resource + private BsOrderGoodsService bsOrderGoodsService; + @Resource + private GoodsShoppingCartService shoppingCartService; /** * 事务产品 @@ -147,5 +147,70 @@ public class OrderCreateService { return orderChild; } + public OrderChildModel goods(OrderModel order, OrderChildModel orderChild) throws Exception { + // 查询商品 + GoodsMsg goodsMsg = goodsMsgService.queryDetail(orderChild.getProductId()); + if (goodsMsg == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商品"); + } + if (goodsMsg.getStatus() != 1) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, goodsMsg.getTitle()+"暂无法就进行购买"); + } + if (goodsMsg.getType() != 1) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前商品不属于实物产品不可下单!"); + } + // 查询商品规格 + GoodsSpecs goodsSpecs = goodsSpecsService.queryDetail(orderChild.getProductSpecId()); + + if (goodsSpecs == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商品规格"); + } + JSONObject object = (JSONObject) orderChild.getBusiness(); + + orderChild.setMerId(goodsMsg.getMerId()); + orderChild.setProductName(goodsMsg.getTitle()); + orderChild.setProductImg(goodsMsg.getListImg()); + orderChild.setProductSpecName(goodsSpecs.getName()); + orderChild.setProductPrice(goodsSpecs.getSalePrice()); + + if (object == null || object.getLong("deliveryAddressId") == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请选择收货地址!"); + } + + // 查询收货地址 + GoodsUserAddress address = goodsUserAddressService.queryDetail(object.getLong("deliveryAddressId")); + if (address == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的收货地址"); + } + + // 创建卡券订单 + BsOrderGoods orderGoods = new BsOrderGoods(); + orderGoods.setOrderNo(orderChild.getOrderNo()); + orderGoods.setChildOrderNo(orderChild.getChildOrderNo()); + orderGoods.setMerId(orderChild.getMerId()); + orderGoods.setUserId(order.getUserId()); + orderGoods.setUserPhone(order.getUserPhone()); + orderGoods.setGoodsId(goodsMsg.getId()); + orderGoods.setGoodsName(goodsMsg.getTitle()); + orderGoods.setGoodsTypeId(goodsMsg.getGoodsType()); + orderGoods.setGoodsTypeName(goodsMsg.getGoodsTypeName()); + orderGoods.setGoodsBrandId(goodsMsg.getGoodsBrand()); + orderGoods.setGoodsBrandName(goodsMsg.getGoodsBrandName()); + orderGoods.setGoodsSpecsId(goodsSpecs.getId()); + orderGoods.setGoodsSpecsName(goodsSpecs.getName()); + orderGoods.setGoodsSpescOriginalPrice(goodsSpecs.getOriginalPrice()); + orderGoods.setGoodsCount(orderChild.getProductCount()); + orderGoods.setStatus(OrderGoodsStatusEnum.status8.getCode()); + orderGoods.setDeliveryAddress(address.getAddress()); + orderGoods.setDeliveryAddressId(address.getId()); + + bsOrderGoodsService.create(orderGoods); + + if (object.getLong("shopCartId") != null) { + shoppingCartService.delete(object.getLong("shopCartId")); + } + + return orderChild; + } } diff --git a/service/src/main/java/com/hfkj/service/order/OrderPaySuccessService.java b/service/src/main/java/com/hfkj/service/order/OrderPaySuccessService.java index b405cce..b62f544 100644 --- a/service/src/main/java/com/hfkj/service/order/OrderPaySuccessService.java +++ b/service/src/main/java/com/hfkj/service/order/OrderPaySuccessService.java @@ -15,6 +15,7 @@ import com.hfkj.service.coupon.channel.ChongQingCNPCCouponService; import com.hfkj.service.coupon.channel.HuiLianTongCouponService; import com.hfkj.service.coupon.channel.PcytCNPCCouponService; import com.hfkj.service.coupon.channel.PetroConfig; +import com.hfkj.service.goods.BsOrderGoodsService; import com.hfkj.service.goods.GoodsMsgService; import com.hfkj.service.goods.GoodsVpdService; import com.hfkj.service.hlt.HuiLianTongUnionCardService; @@ -38,12 +39,8 @@ import java.util.List; @Component public class OrderPaySuccessService { Logger log = LoggerFactory.getLogger(OrderPaySuccessService.class); - @Autowired - private RedisUtil redisUtil; @Resource - private BsOrderService orderService; - @Resource - private BsOrderChildService orderChildService; + private BsOrderGoodsService bsOrderGoodsService; @Resource private BsOrderCouponService orderCouponService; @Resource @@ -61,6 +58,9 @@ public class OrderPaySuccessService { public void orderBusHandle(OrderModel order) { for (BsOrderChild childOrder : order.getOrderChildList()) { try { + if (childOrder.getProductType().equals(OrderChildProductTypeEnum.type1.getCode())) { + goods(order); + } if (childOrder.getProductType().equals(OrderChildProductTypeEnum.type2.getCode())) { product(order, childOrder); } @@ -195,4 +195,20 @@ public class OrderPaySuccessService { orderCoupon.setPaySerialNo(order.getPaySerialNo()); orderCouponService.editData(orderCoupon); } + /** + * 实物产品 + * @param order + */ + public void goods(OrderModel order) throws Exception { + BsOrderGoods goodsOrder = bsOrderGoodsService.findGoodsOrder(order.getOrderNo()); + + if (goodsOrder == null) { + throw ErrorHelp.genException(SysCode.System , ErrorCode.COMMON_ERROR , "未查询到相关内容"); + } + + goodsOrder.setUpdateTime(new Date()); + goodsOrder.setStatus(1); + bsOrderGoodsService.update(goodsOrder); + } + } 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 11b193d..4b65768 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 @@ -121,8 +121,8 @@ public class BsOrderRefundServiceImpl implements BsOrderRefundService { order.setOrderStatus(refundSuccess ? OrderStatusEnum.status4.getCode() : order.getOrderStatus()); order.setRefundTime(refundSuccess ? new Date() : null); orderService.editData(order); - // 更新缓存 - orderService.cache(order); + // 删除缓存 + orderService.cacheDelete(order.getOrderNo()); return order; } 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 80a60b2..eb8422a 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 @@ -92,6 +92,12 @@ public class BsOrderServiceImpl implements BsOrderService { return order; } + @Override + public void cacheDelete(String orderNo) { + // 缓存 + redisUtil.del(CACHE_KEY + orderNo); + } + @Override @Transactional( propagation= Propagation.REQUIRES_NEW, @@ -184,7 +190,10 @@ public class BsOrderServiceImpl implements BsOrderService { 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); @@ -305,8 +314,8 @@ public class BsOrderServiceImpl implements BsOrderService { childOrder.setStatus(OrderChildStatusEnum.status2.getCode()); orderChildService.editData(childOrder); } - // 更新缓存 - cache(order); + // 删除缓存 + cacheDelete(order.getOrderNo()); // 处理业务 orderPaySuccessService.orderBusHandle(order); } diff --git a/service/src/main/java/com/hfkj/sysenum/order/OrderGoodsStatusEnum.java b/service/src/main/java/com/hfkj/sysenum/order/OrderGoodsStatusEnum.java new file mode 100644 index 0000000..6fa6071 --- /dev/null +++ b/service/src/main/java/com/hfkj/sysenum/order/OrderGoodsStatusEnum.java @@ -0,0 +1,60 @@ +package com.hfkj.sysenum.order; + +import lombok.Getter; + +/** + * @className: OrderChildStatusEnum + * @author: HuRui + * @date: 2024/5/6 + **/ +@Getter +public enum OrderGoodsStatusEnum { + /** + * 待发货 + */ + status1(1, "待发货"), + + /** + * 已发货 + */ + status2(2, "已发货"), + + /** + * 配送中 + */ + status3(3, "配送中"), + + /** + * 已送达 + */ + status4(4, "已送达"), + + /** + * 已取消 + */ + status5(5, "已取消"), + + /** + * 退货中 + */ + status6(6, "退货中"), + /** + * 已退货 + */ + status7(7, "已退货"), + /** + * 待支付 + */ + status8(8, "待支付"), + ; + + private final int code; + + private final String name; + + + OrderGoodsStatusEnum(int code, String name) { + this.code = code; + this.name = name; + } +}