Pu Zhibing
2024-10-16 c4664502dfdaffff555b532e65b51a57ac8b29c2
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java
@@ -6,31 +6,52 @@
import com.ruoyi.account.api.dto.GrantCouponDto;
import com.ruoyi.account.api.feignClient.AppCouponClient;
import com.ruoyi.account.api.feignClient.AppUserAddressClient;
import com.ruoyi.account.api.feignClient.AppUserClient;
import com.ruoyi.account.api.model.TAppUser;
import com.ruoyi.account.api.model.TAppUserAddress;
import com.ruoyi.common.core.domain.R;
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.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.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;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
 * <p>
@@ -51,11 +72,25 @@
    
    @Resource
    private CouponClient couponClient;
    @Resource
    private AppUserClient appUserClient;
    
    @Resource
    private AppUserAddressClient appUserAddressClient;
    @Resource
    private AppCouponClient appCouponClient;
    @Resource
    private WxPaymentClient wxPaymentClient;
    @Resource
    private AliPaymentClient aliPaymentClient;
    @Resource
    private TShoppingOrderRefundService shoppingOrderRefundService;
    @Autowired
    public RedisTemplate redisTemplate;
    
    
    
@@ -164,14 +199,16 @@
     * @return
     */
    @Override
    public List<MyShoppingOrderList> getMyShoppingOrderList(GetMyShoppingOrderList query) {
    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());
        }
        List<TShoppingOrder> list = this.list(wrapper.orderByDesc(TShoppingOrder::getCreateTime).last(" limit " + query.getPageCurr() + ", " + query.getPageSize()));
        long count = this.count(wrapper);
        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();
@@ -183,7 +220,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();
            }
@@ -194,9 +231,13 @@
            myShoppingOrderList.setUnitPrice(unitPrice);
            myShoppingOrderList.setNumber(tShoppingOrder.getPurchaseQuantity());
            myShoppingOrderList.setPaymentAmount(tShoppingOrder.getPaymentAmount());
            myShoppingOrderList.setOrderType(tShoppingOrder.getOrderType());
            pageList.add(myShoppingOrderList);
        }
        return pageList;
        Map<String, Object> map = new HashMap<>();
        map.put("list", pageList);
        map.put("total", count);
        return map;
    }
    
    
@@ -214,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 = "";
@@ -224,7 +265,7 @@
            name = goods.getName();
            imgUrl = goods.getCoverPicture();
        }else{
            TCoupon coupon = couponClient.getCouponById1(shoppingOrder.getGoodsId()).getData();
            TCoupon coupon = couponClient.getCouponById1(shoppingOrder.getCouponId()).getData();
            info.setCouponType(coupon.getType());
            info.setDays(coupon.getDays());
            info.setEndTime(coupon.getEndTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
@@ -238,10 +279,15 @@
        info.setUnitPrice(unitPrice);
        info.setCode(shoppingOrder.getCode());
        info.setCreateTime(shoppingOrder.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        info.setOrderAmount(shoppingOrder.getOrderAmount());
        info.setPaymentAmount(shoppingOrder.getPaymentAmount());
        info.setRemark(shoppingOrder.getRemark());
        info.setDeliveryTime(shoppingOrder.getConsignerTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        info.setFinishTime(shoppingOrder.getReceivingTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        if(null != shoppingOrder.getConsignerTime()){
            info.setDeliveryTime(shoppingOrder.getConsignerTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        if(null != shoppingOrder.getReceivingTime()){
            info.setFinishTime(shoppingOrder.getReceivingTime().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        }
        return info;
    }
    
@@ -260,25 +306,169 @@
        if(shoppingOrder.getStatus() == 3){
            return AjaxResult.error("订单已完成,不能取消");
        }
        if(shoppingOrder.getStatus() == 4){
        //退款状态并且所有金额退完
        List<TShoppingOrderRefund> list = shoppingOrderRefundService.list(new LambdaQueryWrapper<TShoppingOrderRefund>().eq(TShoppingOrderRefund::getShoppingOrderId, id).eq(TShoppingOrderRefund::getRefundStatus, 2));
        BigDecimal bigDecimal = list.stream().map(TShoppingOrderRefund::getRefundAmount).reduce(BigDecimal.ZERO, BigDecimal::add).setScale(2, RoundingMode.HALF_EVEN);
        if(shoppingOrder.getPaymentAmount().compareTo(bigDecimal) == 0 && shoppingOrder.getStatus() == 4){
            return AjaxResult.error("订单已取消,不能重复操作");
        }
        //退款金额
        BigDecimal refundAmount = shoppingOrder.getPaymentAmount().subtract(bigDecimal);
        //先查询第三方订单状态订单是否退款
        //支付方式(1=微信,2=支付宝) todo 待完善
        //支付方式(1=微信,2=支付宝)
        Integer paymentType = shoppingOrder.getPaymentType();
        if(1 == paymentType){
            NotifyV3PayDecodeRespBody data = wxPaymentClient.queryOrderInfo(shoppingOrder.getCode()).getData();
            String trade_state = data.getTrade_state();
            if("NOTPAY".equals(trade_state) || "REVOKED".equals(trade_state) || "PAYERROR".equals(trade_state)){
                return AjaxResult.error("订单还未支付,不能操作退款");
            }
            if("CLOSED".equals(trade_state)){
                return AjaxResult.error("订单支付已关闭,不能操作退款");
            }
            if("USERPAYING".equals(trade_state)){
                return AjaxResult.error("订单正在支付中,不能操作退款");
            }
        }
        if(2 == paymentType){
            AliQueryOrder aliQueryOrder = aliPaymentClient.query(shoppingOrder.getCode()).getData();
            String tradeStatus = aliQueryOrder.getTradeStatus();
            if("TRADE_CLOSED".equals(tradeStatus)){
                return AjaxResult.error("订单还未支付,不能操作退款");
            }
            if("TRADE_FINISHED".equals(tradeStatus)){
                return AjaxResult.error("订单支付已关闭,不能操作退款");
            }
            if("WAIT_BUYER_PAY".equals(tradeStatus)){
                return AjaxResult.error("订单正在支付中,不能操作退款");
            }
        }
        //退款成功后需要判断商品库存类型后决定是否需要回退库存
        //加redis锁处理高并发
        //构建退款明细
        TShoppingOrderRefund shoppingOrderRefund = new TShoppingOrderRefund();
        shoppingOrderRefund.setPayTime(shoppingOrder.getPayTime());
        shoppingOrderRefund.setPayCode(shoppingOrder.getSerialNumber());
        shoppingOrderRefund.setPayType(shoppingOrder.getPaymentType());
        shoppingOrderRefund.setShoppingOrderId(shoppingOrder.getId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        shoppingOrderRefund.setRefundCode("GDF" + sdf.format(new Date()) + Double.valueOf((Math.random() * 1000)).intValue());
        shoppingOrderRefund.setRefundAmount(refundAmount);
        shoppingOrderRefund.setRefundStatus(1);
        shoppingOrderRefund.setCode(shoppingOrder.getCode());
        shoppingOrderRefund.setRefundTitle("取消订单");
        shoppingOrderRefund.setRefundContent("取消订单");
        shoppingOrderRefund.setRefundReason("取消订单");
        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("/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());
            amount.setCurrency("CNY");
            model.setAmount(amount);
            R<String> orderR = wxPaymentClient.refundOrderR(model);
            if(200 == orderR.getCode()){
                this.updateById(shoppingOrder);
                shoppingOrderRefundService.save(shoppingOrderRefund);
            }
        }
        if(2 == paymentType){
            RefundReq dto = new RefundReq();
            dto.setOutTradeNo(shoppingOrder.getCode());
            dto.setOutRequestNo(shoppingOrderRefund.getCode());
            dto.setRefundAmount(refundAmount.toString());
            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()));
            }
        }
        return AjaxResult.success();
    }
    
    /**
     * 商城订单取消订单微信退款处理
     * @param out_refund_no 退款单号
     * @param refund_id 退款流水号
     * @param refund_status 退款状态 SUCCESS:退款成功  CLOSED:退款关闭  ABNORMAL:退款异常
     * @param success_time 退款成功时间
     * @return
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)//分布式事务
    public AjaxResult cancelShoppingOrderWxRefund(String out_refund_no, String refund_id, String refund_status, String success_time) {
        if("SUCCESS".equals(refund_status)){
            TShoppingOrderRefund one = shoppingOrderRefundService.getOne(new LambdaQueryWrapper<TShoppingOrderRefund>().eq(TShoppingOrderRefund::getRefundCode, out_refund_no));
            one.setRefundStatus(2);
            one.setRefundSerialNumber(refund_id);
            one.setRefundTime(LocalDateTime.parse(success_time, DateTimeFormatter.ofPattern("yyyy-MM-DDTHH:mm:ss+TIMEZONE")));
            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){
                //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();
                }
            }
        }
        return AjaxResult.success();
    }
    
    /**
     * 获取未开票的订单列表
@@ -288,7 +478,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();
@@ -300,7 +491,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();
            }
@@ -311,6 +502,7 @@
            myShoppingOrderList.setUnitPrice(unitPrice);
            myShoppingOrderList.setNumber(tShoppingOrder.getPurchaseQuantity());
            myShoppingOrderList.setPaymentAmount(tShoppingOrder.getPaymentAmount());
            myShoppingOrderList.setCreateTime(tShoppingOrder.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy.MM.dd HH:mm:ss")));
            pageList.add(myShoppingOrderList);
        }
        return pageList;
@@ -322,6 +514,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){
@@ -334,4 +527,63 @@
            appCouponClient.grantCoupon(grantCouponDto);
        }
    }
    @Override
    public PageInfo<TShoppingOrder> pageList(ShoppingOrderQuery query) {
        String startTime1 = null;
        String startTime2 = null;
        if (StringUtils.hasLength(query.getStartTime())){
            String[] split = query.getStartTime().split(" - ");
            startTime1 = split[0];
            startTime2 = split[1];
        }
        PageInfo<TShoppingOrder> pageInfo = new PageInfo<>(query.getPageCurr(),query.getPageSize());
        List<TShoppingOrder> list = this.baseMapper.pageList(pageInfo,query,startTime1,startTime2);
        for (TShoppingOrder tShoppingOrder : list) {
            tShoppingOrder.setUid(tShoppingOrder.getId().toString());
            switch (tShoppingOrder.getOrderType()){
                case 1:
                    TGoods data = goodsClient.getGoodsById(tShoppingOrder.getGoodsId()).getData();
                    if (data!=null){
                        tShoppingOrder.setName(data.getName());
                    }
                    break;
                case 2:
                    TCoupon data1 = couponClient.getCouponById1(tShoppingOrder.getCouponId()).getData();
                    if (data1!=null){
                        tShoppingOrder.setName(data1.getName());
                    }
                    tShoppingOrder.setStatus(3);
                    break;
            }
            if (tShoppingOrder.getAppUserId() != null){
                TAppUser data = appUserClient.getUserById(tShoppingOrder.getAppUserId()).getData();
                if (data!=null){
                    tShoppingOrder.setPhone(data.getPhone());
                }
            }
        }
        pageInfo.setRecords(list);
        return pageInfo;
    }
    @Override
    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);
    }
    @Override
    public List<Map<String, Object>> getData(ChargingStatisticsQueryDto statisticsQueryDto) {
        return this.baseMapper.getData(statisticsQueryDto);
    }
}