puzhibing
10 小时以前 e1c8c9d51f1c6e42c9868743f16fa8e52bb419cd
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java
@@ -1,6 +1,8 @@
package com.ruoyi.order.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.account.api.dto.GrantCouponDto;
@@ -13,31 +15,33 @@
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.api.model.TExchangeOrder;
import com.ruoyi.order.api.model.TShoppingOrder;
import com.ruoyi.order.api.model.TShoppingOrderRefund;
import com.ruoyi.order.api.query.ShoppingOrderQuery;
import com.ruoyi.order.api.query.TActivityStatisticsQuery;
import com.ruoyi.order.api.vo.ChargingOrderVO;
import com.ruoyi.order.api.vo.TActivityStatisticslVO;
import com.ruoyi.order.api.vo.TActivityVO;
import com.ruoyi.order.dto.*;
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;
import com.ruoyi.other.api.feignClient.GoodsClient;
import com.ruoyi.payment.api.feignClient.AliPaymentClient;
import com.ruoyi.payment.api.feignClient.IcbcPaymentClient;
import com.ruoyi.payment.api.feignClient.WxPaymentClient;
import com.ruoyi.payment.api.model.RefundReq;
import com.ruoyi.payment.api.model.RefundResp;
import com.ruoyi.payment.api.model.WxPaymentRefundModel;
import com.ruoyi.payment.api.vo.AliQueryOrder;
import com.ruoyi.payment.api.vo.IcbcPaymentResp;
import com.ruoyi.payment.api.vo.IcbcRefundReq;
import com.ruoyi.payment.api.vo.IcbcRefundResp;
import com.ruoyi.payment.api.vo.NotifyV3PayDecodeRespBody;
import io.seata.spring.annotation.GlobalTransactional;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@@ -51,6 +55,7 @@
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
@@ -87,10 +92,16 @@
    private AliPaymentClient aliPaymentClient;
    @Resource
    private IcbcPaymentClient icbcPaymentClient;
    @Resource
    private TShoppingOrderRefundService shoppingOrderRefundService;
    @Autowired
    public RedisTemplate redisTemplate;
    @Autowired
    private RedissonClient redissonClient;
@@ -98,13 +109,9 @@
    @Override
    public TActivityVO activityStatistics(TActivityStatisticsQuery dto) {
        TActivityVO res = new TActivityVO();
        PageInfo<TActivityStatisticslVO> pageInfo = new PageInfo<>(dto.getPageCurr(),dto.getPageSize());
        PageInfo<TActivityStatisticslVO> pageInfo1 = new PageInfo<>(1,9999999);
        List<TActivityStatisticslVO> list = this.baseMapper.activityStatistics(pageInfo,dto);
        List<TActivityStatisticslVO> list1 = this.baseMapper.activityStatistics(pageInfo1,dto);
        List<TActivityStatisticslVO> list1 = this.baseMapper.activityStatisticsNolimit(dto);
        // 优惠券金额
        BigDecimal coupon = new BigDecimal("0");
@@ -165,9 +172,7 @@
                    break;
                case 3:
                    // 会员订单 优惠金额大于0
                    if (tActivityStatisticslVO.getVipDiscountAmount()!=null
                            &&
                            (tActivityStatisticslVO.getCouponDiscountAmount().compareTo(BigDecimal.ZERO)>0)){
                    if (tActivityStatisticslVO.getVipDiscountAmount()!=null){
                        vipActivity = vipActivity.add(tActivityStatisticslVO.getVipDiscountAmount());
                    }
                    tActivityStatisticslVO.setType("会员活动");
@@ -180,15 +185,23 @@
                        tActivityStatisticslVO.setType("赠送会员");
                        tActivityStatisticslVO.setTime(tActivityStatisticslVO.getCreateTime());
                    }else{
                        // 赠送
                        grantVip = grantVip.add(tActivityStatisticslVO.getVipDiscountAmount());
                        tActivityStatisticslVO.setType("赠送会员");
                        // 会员活动
                        vipActivity = vipActivity.add(tActivityStatisticslVO.getVipDiscountAmount());
                        tActivityStatisticslVO.setType("会员活动");
                        tActivityStatisticslVO.setTime(tActivityStatisticslVO.getCreateTime());
                    }
                    break;
            }
        }
        List<Long> appUserIds = list.stream().map(TActivityStatisticslVO::getAppUserId).collect(Collectors.toList());
        List<TAppUser> appUsers = appUserClient.getUserByIds(appUserIds).getData();
        for (TActivityStatisticslVO tActivityStatisticslVO : list) {
            TAppUser appUser = appUsers.stream().filter(user -> user.getId().equals(tActivityStatisticslVO.getAppUserId())).findFirst().orElse(null);
            if(Objects.nonNull(appUser)){
                tActivityStatisticslVO.setPhone(appUser.getPhone());
            }
            // 判断享有了哪些类型
            switch (tActivityStatisticslVO.getOrderType()){
                case 1:
@@ -197,9 +210,9 @@
                    // 充电订单 优惠券金额大于0
                    if (tActivityStatisticslVO.getCouponDiscountAmount()!=null
                            &&
                        (tActivityStatisticslVO.getCouponDiscountAmount().compareTo(BigDecimal.ZERO)>0)){
                        stringBuilder.append("优惠券抵扣").append("+");
                }
                            (tActivityStatisticslVO.getCouponDiscountAmount().compareTo(BigDecimal.ZERO)>0)){
                            stringBuilder.append("优惠券抵扣").append("+");
                    }
                    if (tActivityStatisticslVO.getVipDiscountAmount()!=null &&(tActivityStatisticslVO.getVipDiscountAmount().compareTo(BigDecimal.ZERO)>0)){
                        stringBuilder.append("会员抵扣").append("+");
                    }
@@ -317,7 +330,7 @@
        MyShoppingOrderInfo info = new MyShoppingOrderInfo();
        info.setId(id);
        info.setStatus(shoppingOrder.getStatus());
        TAppUserAddress userAddress = appUserAddressClient.getAppUserAddressById(shoppingOrder.getAppUserAddressId()).getData();
        TAppUserAddress userAddress = JSON.parseObject(shoppingOrder.getAddressJson(), TAppUserAddress.class);
        info.setConsignee(userAddress.getName());
        info.setPhone(userAddress.getPhone());
        info.setAddress(userAddress.getProvince() + userAddress.getCity() + userAddress.getDistrict() + userAddress.getAddress() + userAddress.getHouseNumber());
@@ -382,7 +395,7 @@
        BigDecimal refundAmount = shoppingOrder.getPaymentAmount().subtract(bigDecimal);
        //先查询第三方订单状态订单是否退款
        //支付方式(1=微信,2=支付宝)
        // 支付方式(1=微信,2=支付宝,3=工商银行)
        Integer paymentType = shoppingOrder.getPaymentType();
        if(1 == paymentType){
            NotifyV3PayDecodeRespBody data = wxPaymentClient.queryOrderInfo(shoppingOrder.getCode()).getData();
@@ -408,6 +421,13 @@
            }
            if("WAIT_BUYER_PAY".equals(tradeStatus)){
                return AjaxResult.error("订单正在支付中,不能操作退款");
            }
        }
        if (3 == paymentType) {
            R<IcbcPaymentResp> queryResult = icbcPaymentClient.query(shoppingOrder.getCode());
            IcbcPaymentResp queryResp = queryResult == null ? null : queryResult.getData();
            if (queryResult == null || queryResult.getCode() != 200 || queryResp == null || !"0".equals(queryResp.getStatus())) {
                return AjaxResult.error(queryResult == null ? "工行支付查询无响应" : "订单尚未支付成功,不能退款");
            }
        }
        //退款成功后需要判断商品库存类型后决定是否需要回退库存
@@ -444,10 +464,16 @@
            amount.setTotal(shoppingOrder.getPaymentAmount().multiply(new BigDecimal(100)).intValue());
            amount.setCurrency("CNY");
            model.setAmount(amount);
            R<String> orderR = wxPaymentClient.refundOrderR(model);
            R<Map<String, Object>> orderR = wxPaymentClient.refundOrderR(model);
            if(200 == orderR.getCode()){
                this.updateById(shoppingOrder);
                shoppingOrderRefundService.save(shoppingOrderRefund);
                //手续费
                Map<String, Object> amount1 = (Map<String, Object>) orderR.getData().get("amount");
                Object refund_fee1 = amount1.get("refund_fee");
                BigDecimal refund_fee = new BigDecimal(null == refund_fee1 ? "0" : refund_fee1.toString()).divide(new BigDecimal(100));
                shoppingOrderRefund.setRefundFee(refund_fee);
                shoppingOrderRefundService.updateById(shoppingOrderRefund);
            }
        }
        if(2 == paymentType){
@@ -463,6 +489,22 @@
                AjaxResult success = cancelShoppingOrderWxRefund(resp.getOutTradeNo(), resp.getTradeNo(), "SUCCESS", sdf1.format(new Date()));
            }
        }
        if (3 == paymentType) {
            IcbcRefundReq refundReq = new IcbcRefundReq();
            refundReq.setOutTradeNo(shoppingOrder.getCode());
            refundReq.setOrderId(shoppingOrder.getSerialNumber());
            refundReq.setOutRefundNo(shoppingOrderRefund.getRefundCode());
            refundReq.setRefundAmount(refundAmount);
            refundReq.setTotalAmount(shoppingOrder.getPaymentAmount());
            refundReq.setReason("取消商城订单");
            R<IcbcRefundResp> refundResult = icbcPaymentClient.refund(refundReq);
            IcbcRefundResp refundResp = refundResult == null ? null : refundResult.getData();
            if (refundResult == null || refundResult.getCode() != 200 || refundResp == null || !"SUCCESS".equals(refundResp.getStatus())) {
                return AjaxResult.error(refundResult == null ? "工行退款无响应" : refundResult.getMsg());
            }
            shoppingOrderRefundService.save(shoppingOrderRefund);
            cancelShoppingOrderWxRefund(shoppingOrderRefund.getRefundCode(), refundResp.getRefundId(), "SUCCESS", refundResp.getRefundTime());
        }
        return AjaxResult.success();
    }
@@ -476,7 +518,6 @@
     * @return
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)//分布式事务
    public AjaxResult cancelShoppingOrderWxRefund(String out_refund_no, String refund_id, String refund_status, String success_time) {
        TShoppingOrderRefund one = shoppingOrderRefundService.getOne(new LambdaQueryWrapper<TShoppingOrderRefund>().eq(TShoppingOrderRefund::getRefundCode, out_refund_no));
        one.setRefundStatus(2);
@@ -495,39 +536,44 @@
        //商品
        if(shoppingOrder.getOrderType() == 1){
            //redis锁 和支付使用同一个锁
            RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_GOODS_LOCK", 5, 30000);
            RLock lock = redissonClient.getLock("SHOPPING_GOODS_LOCK");
            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);
                boolean tryLock = lock.tryLock(5, 30, TimeUnit.SECONDS);
                if(tryLock){
                    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();
                lock.unlock();
            }
        }
        //优惠券
        if(shoppingOrder.getOrderType() == 2){
            //redis锁 和支付使用同一个锁
            RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_COUPON_LOCK", 5, 30000);
            RLock lock = redissonClient.getLock("SHOPPING_COUPON_LOCK");
            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);
                boolean tryLock = lock.tryLock(5, 5, TimeUnit.SECONDS);
                if(tryLock){
                    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();
                lock.unlock();
            }
        }
        return AjaxResult.success();
@@ -573,12 +619,19 @@
    @Override
    public void callBack(String code,String outTradeNo) {
        //改变订单状态
        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 == null) {
            throw new IllegalArgumentException("商城订单不存在:" + code);
        }
        boolean updated = this.update(new LambdaUpdateWrapper<TShoppingOrder>()
                .eq(TShoppingOrder::getId, shoppingOrder.getId())
                .eq(TShoppingOrder::getPaymentStatus, 1)
                .set(TShoppingOrder::getPaymentStatus, 2)
                .set(TShoppingOrder::getSerialNumber, outTradeNo)
                .set(TShoppingOrder::getPayTime, LocalDateTime.now()));
        if (!updated) {
            return;
        }
        //如果是优惠券,赠送优惠券
        if (shoppingOrder.getOrderType()==2){
            GrantCouponDto grantCouponDto = new GrantCouponDto();
@@ -637,7 +690,7 @@
    public List<SixShopDto> sixBefore(LocalDate sixBefore, Integer status) {
        return this.baseMapper.sixBefore(sixBefore,status);
    }
    @Override
    public BigDecimal getSumAmount(LocalDate sixBefore) {
        return this.baseMapper.getSumAmount(sixBefore);