Pu Zhibing
2024-10-16 c4664502dfdaffff555b532e65b51a57ac8b29c2
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;
    
    
    
@@ -196,7 +202,7 @@
    public Map<String, Object> getMyShoppingOrderList(GetMyShoppingOrderList query) {
        Long userId = tokenService.getLoginUserApplet().getUserId();
        LambdaQueryWrapper<TShoppingOrder> wrapper = new LambdaQueryWrapper<TShoppingOrder>().eq(TShoppingOrder::getDelFlag, 0)
                .eq(TShoppingOrder::getAppUserId, userId);
                .eq(TShoppingOrder::getAppUserId, userId).eq(TShoppingOrder::getPaymentStatus, 2);
        if(query.getStatus() != 0){
            wrapper.eq(TShoppingOrder::getStatus, query.getStatus());
        }
@@ -249,7 +255,7 @@
        TAppUserAddress userAddress = appUserAddressClient.getAppUserAddressById(shoppingOrder.getAppUserAddressId()).getData();
        info.setConsignee(userAddress.getName());
        info.setPhone(userAddress.getPhone());
        info.setAddress(userAddress.getAddress());
        info.setAddress(userAddress.getProvince() + userAddress.getCity() + userAddress.getDistrict() + userAddress.getAddress());
        info.setExpressCompany(shoppingOrder.getExpressCompany());
        info.setExpressNumber(shoppingOrder.getExpressNumber());
        String name = "";
@@ -309,7 +315,7 @@
    
        //退款金额
        BigDecimal refundAmount = shoppingOrder.getPaymentAmount().subtract(bigDecimal);
        //先查询第三方订单状态订单是否退款
        //支付方式(1=微信,2=支付宝)
        Integer paymentType = shoppingOrder.getPaymentType();
@@ -358,13 +364,16 @@
        shoppingOrderRefund.setRefundRemark("全额退款");
        shoppingOrderRefund.setRefundTotalAmount(refundAmount.add(bigDecimal));
        shoppingOrderRefund.setPayAmount(shoppingOrder.getPaymentAmount());
        shoppingOrder.setCancellationTime(LocalDateTime.now());
        shoppingOrder.setCancellationId(shoppingOrder.getAppUserId());
        if(1 == paymentType){
            WxPaymentRefundModel model = new WxPaymentRefundModel();
            model.setOut_trade_no(shoppingOrder.getCode());
            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());
@@ -372,6 +381,7 @@
            model.setAmount(amount);
            R<String> orderR = wxPaymentClient.refundOrderR(model);
            if(200 == orderR.getCode()){
                this.updateById(shoppingOrder);
                shoppingOrderRefundService.save(shoppingOrderRefund);
            }
        }
@@ -383,11 +393,9 @@
            dto.setRefundReason("取消订单");
            RefundResp resp = aliPaymentClient.refund(dto).getData();
            if(null != resp){
                this.updateById(shoppingOrder);
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-DDTHH:mm:ss+TIMEZONE");
                AjaxResult success = cancelShoppingOrderWxRefund(resp.getOutTradeNo(), resp.getTradeNo(), "SUCCESS", sdf1.format(new Date()));
                if(success.isSuccess()){
                    shoppingOrderRefundService.save(shoppingOrderRefund);
                }
            }
        }
        return AjaxResult.success();
@@ -413,28 +421,50 @@
            shoppingOrderRefundService.updateById(one);
            //判断是否需要回退库存
            TShoppingOrder shoppingOrder = this.getById(one.getShoppingOrderId());
            shoppingOrder.setStatus(4);
            shoppingOrder.setRefundCode(one.getRefundSerialNumber());
            shoppingOrder.setRefundAmount(one.getRefundAmount());
            shoppingOrder.setRefundStatus(2);
            shoppingOrder.setRefundTime(one.getRefundTime());
            this.updateById(shoppingOrder);
            //商品
            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();