puzhibing
2024-09-27 0ce1f94a0f262a8831f8eb9fd5cf67422dd1d69b
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java
@@ -25,6 +25,7 @@
import com.ruoyi.order.mapper.TShoppingOrderMapper;
import com.ruoyi.order.service.TShoppingOrderRefundService;
import com.ruoyi.order.service.TShoppingOrderService;
import com.ruoyi.order.util.RedisLock;
import com.ruoyi.other.api.domain.TCoupon;
import com.ruoyi.other.api.domain.TGoods;
import com.ruoyi.other.api.feignClient.CouponClient;
@@ -37,6 +38,8 @@
import com.ruoyi.payment.api.vo.AliQueryOrder;
import com.ruoyi.payment.api.vo.NotifyV3PayDecodeRespBody;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@@ -85,6 +88,9 @@
    
    @Resource
    private TShoppingOrderRefundService shoppingOrderRefundService;
    @Autowired
    public RedisTemplate redisTemplate;
    
    
    
@@ -201,7 +207,8 @@
            wrapper.eq(TShoppingOrder::getStatus, query.getStatus());
        }
        long count = this.count(wrapper);
        List<TShoppingOrder> list = this.list(wrapper.orderByDesc(TShoppingOrder::getCreateTime).last(" limit " + query.getPageCurr() + ", " + query.getPageSize()));
        Integer pageCurr = (query.getPageCurr() - 1) * query.getPageSize();
        List<TShoppingOrder> list = this.list(wrapper.orderByDesc(TShoppingOrder::getCreateTime).last(" limit " + pageCurr + ", " + query.getPageSize()));
        List<MyShoppingOrderList> pageList = new ArrayList<>();
        for (TShoppingOrder tShoppingOrder : list) {
            MyShoppingOrderList myShoppingOrderList = new MyShoppingOrderList();
@@ -308,7 +315,7 @@
    
        //退款金额
        BigDecimal refundAmount = shoppingOrder.getPaymentAmount().subtract(bigDecimal);
        //先查询第三方订单状态订单是否退款
        //支付方式(1=微信,2=支付宝)
        Integer paymentType = shoppingOrder.getPaymentType();
@@ -347,7 +354,7 @@
        shoppingOrderRefund.setPayType(shoppingOrder.getPaymentType());
        shoppingOrderRefund.setShoppingOrderId(shoppingOrder.getId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        shoppingOrderRefund.setRefundCode("GDF" + sdf.format(new Date()) + (Math.random() * 1000));
        shoppingOrderRefund.setRefundCode("GDF" + sdf.format(new Date()) + Double.valueOf((Math.random() * 1000)).intValue());
        shoppingOrderRefund.setRefundAmount(refundAmount);
        shoppingOrderRefund.setRefundStatus(1);
        shoppingOrderRefund.setCode(shoppingOrder.getCode());
@@ -363,7 +370,7 @@
            model.setTransaction_id(shoppingOrder.getSerialNumber());
            model.setOut_refund_no(shoppingOrderRefund.getRefundCode());
            model.setReason("取消订单");
            model.setNotify_url("http://221.182.45.100:9000/order/t-shopping-order/cancelShoppingOrderWxRefund");
            model.setNotify_url("/order/t-shopping-order/cancelShoppingOrderWxRefund");
            WxPaymentRefundModel.RefundAmount amount = new WxPaymentRefundModel.RefundAmount();
            amount.setRefund(refundAmount.multiply(new BigDecimal(100)).intValue());
            amount.setTotal(shoppingOrder.getPaymentAmount().multiply(new BigDecimal(100)).intValue());
@@ -386,6 +393,44 @@
                AjaxResult success = cancelShoppingOrderWxRefund(resp.getOutTradeNo(), resp.getTradeNo(), "SUCCESS", sdf1.format(new Date()));
                if(success.isSuccess()){
                    shoppingOrderRefundService.save(shoppingOrderRefund);
                    //商品
                    if(shoppingOrder.getOrderType() == 1){
                        //redis锁 和支付使用同一个锁
                        RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_GOODS_LOCK", 5, 30000);
                        try {
                            redisLock.lock();
                            TGoods goods = goodsClient.getGoodsById(shoppingOrder.getGoodsId()).getData();
                            Integer inventory = goods.getInventory();
                            if(-1 != inventory){
                                goods.setInventory(inventory + shoppingOrder.getPurchaseQuantity());
                                goodsClient.updateGoods(goods);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }finally {
                            //解锁
                            redisLock.unlock();
                        }
                    }
                    //优惠券
                    if(shoppingOrder.getOrderType() == 2){
                        //redis锁 和支付使用同一个锁
                        RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_COUPON_LOCK", 5, 30000);
                        try {
                            redisLock.lock();
                            TCoupon coupon = couponClient.getCouponById1(shoppingOrder.getCouponId()).getData();
                            Integer inventory = coupon.getInventoryQuantity();
                            if(-1 != inventory){
                                coupon.setInventoryQuantity(inventory + shoppingOrder.getPurchaseQuantity());
                                couponClient.updateCoupon(coupon);
                            }
                        }catch (Exception e){
                            e.printStackTrace();
                        }finally {
                            //解锁
                            redisLock.unlock();
                        }
                    }
                }
            }
        }
@@ -414,26 +459,41 @@
            TShoppingOrder shoppingOrder = this.getById(one.getShoppingOrderId());
            //商品
            if(shoppingOrder.getOrderType() == 1){
                // todo 需完善redis锁
                //redis锁 和支付使用同一个锁
                TGoods goods = goodsClient.getGoodsById(shoppingOrder.getGoodsId()).getData();
                Integer inventory = goods.getInventory();
                if(-1 != inventory){
                    goods.setInventory(inventory + shoppingOrder.getPurchaseQuantity());
                    goodsClient.updateGoods(goods);
                RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_GOODS_LOCK", 5, 30000);
                try {
                    redisLock.lock();
                    TGoods goods = goodsClient.getGoodsById(shoppingOrder.getGoodsId()).getData();
                    Integer inventory = goods.getInventory();
                    if(-1 != inventory){
                        goods.setInventory(inventory + shoppingOrder.getPurchaseQuantity());
                        goodsClient.updateGoods(goods);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    //解锁
                    redisLock.unlock();
                }
                //解锁
            }
            //优惠券
            if(shoppingOrder.getOrderType() == 2){
                //redis锁
                TCoupon coupon = couponClient.getCouponById1(shoppingOrder.getCouponId()).getData();
                Integer inventory = coupon.getInventoryQuantity();
                if(-1 != inventory){
                    coupon.setInventoryQuantity(inventory + shoppingOrder.getPurchaseQuantity());
                    couponClient.updateCoupon(coupon);
                //redis锁 和支付使用同一个锁
                RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_COUPON_LOCK", 5, 30000);
                try {
                    redisLock.lock();
                    TCoupon coupon = couponClient.getCouponById1(shoppingOrder.getCouponId()).getData();
                    Integer inventory = coupon.getInventoryQuantity();
                    if(-1 != inventory){
                        coupon.setInventoryQuantity(inventory + shoppingOrder.getPurchaseQuantity());
                        couponClient.updateCoupon(coupon);
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }finally {
                    //解锁
                    redisLock.unlock();
                }
                //解锁
            }
        }
        return AjaxResult.success();
@@ -447,7 +507,8 @@
    @Override
    public List<MyShoppingOrderList> getNoInvoicedOrder(GetNoInvoicedOrder query) {
        Long userId = tokenService.getLoginUserApplet().getUserId();
        List<TShoppingOrder> list = this.baseMapper.getNoInvoicedOrder(userId, query.getMonth(), query.getPageCurr(), query.getPageSize());
        Integer pageCurr = (query.getPageCurr() - 1) * query.getPageSize();
        List<TShoppingOrder> list = this.baseMapper.getNoInvoicedOrder(userId, query.getMonth(), pageCurr, query.getPageSize());
        List<MyShoppingOrderList> pageList = new ArrayList<>();
        for (TShoppingOrder tShoppingOrder : list) {
            MyShoppingOrderList myShoppingOrderList = new MyShoppingOrderList();
@@ -459,7 +520,7 @@
                name = goods.getName();
                imgUrl = goods.getCoverPicture();
            }else{
                TCoupon coupon = couponClient.getCouponById1(tShoppingOrder.getGoodsId()).getData();
                TCoupon coupon = couponClient.getCouponById1(tShoppingOrder.getCouponId()).getData();
                name = coupon.getName();
                imgUrl = coupon.getCoverPicture();
            }
@@ -482,6 +543,7 @@
        TShoppingOrder shoppingOrder = this.baseMapper.selectOne(Wrappers.lambdaQuery(TShoppingOrder.class).eq(TShoppingOrder::getCode, code));
        shoppingOrder.setPaymentStatus(2);
        shoppingOrder.setSerialNumber(outTradeNo);
        shoppingOrder.setPayTime(LocalDateTime.now());
        this.updateById(shoppingOrder);
        //如果是优惠券,赠送优惠券
        if (shoppingOrder.getOrderType()==2){
@@ -522,6 +584,7 @@
                    if (data1!=null){
                        tShoppingOrder.setName(data1.getName());
                    }
                    tShoppingOrder.setStatus(3);
                    break;
            }
            if (tShoppingOrder.getAppUserId() != null){