liujie
2026-01-04 00c70fe8a833450fa8bc2bea7357e4e23d84f2f3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.ruoyi.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.dto.RefundApplyDTO;
import com.ruoyi.system.enums.BigGiftPackageOrderStatusEnum;
import com.ruoyi.system.model.TAppUser;
import com.ruoyi.system.model.TBigGiftPackage;
import com.ruoyi.system.model.TBigGiftPackageOrder;
import com.ruoyi.system.model.TRefundApplication;
import com.ruoyi.system.mapper.TRefundApplicationMapper;
import com.ruoyi.system.query.BigGiftPackageQuery;
import com.ruoyi.system.query.RefundApplicationQuery;
import com.ruoyi.system.service.TAppUserService;
import com.ruoyi.system.service.TBigGiftPackageOrderService;
import com.ruoyi.system.service.TBigGiftPackageService;
import com.ruoyi.system.service.TRefundApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.vo.TRefundApplicationVo;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDateTime;
import java.util.List;
 
/**
 * <p>
 * 退款申请 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2025-12-08
 */
@Service
@RequiredArgsConstructor(onConstructor_ = @Lazy)
public class TRefundApplicationServiceImpl extends ServiceImpl<TRefundApplicationMapper, TRefundApplication> implements TRefundApplicationService {
 
    private final TBigGiftPackageOrderService tBigGiftPackageOrderService;
    private final TAppUserService tAppUserService;
    private final TBigGiftPackageService tBigGiftPackageService;
 
    @Override
    public Page<TBigGiftPackageOrder> pageRefundList(RefundApplicationQuery query) {
        Page<TBigGiftPackageOrder> page = new Page<>(query.getPageNum(), query.getPageSize());
        return this.baseMapper.pageRefundList(page, query);
    }
 
    @Override
    public List<TBigGiftPackageOrder> pageRefundExportList(BigGiftPackageQuery query) {
        return  this.baseMapper.pageRefundExportList(query);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void applyRefund(RefundApplyDTO dto, Integer userId) {
        // 校验订单是否存在
        TBigGiftPackageOrder order = tBigGiftPackageOrderService.getById(dto.getOrderId());
        if (order == null) {
            throw new ServiceException("订单不存在");
        }
 
        // 校验订单状态:只有已付订金、完成支付、确认收货的订单可以申请退款
        Integer status = order.getStatus();
        boolean canApplyRefund = status.equals(BigGiftPackageOrderStatusEnum.DEPOSIT_PAID.getCode())
                || status.equals(BigGiftPackageOrderStatusEnum.PAID.getCode())
                || status.equals(BigGiftPackageOrderStatusEnum.CONFIRM_RECEIPT.getCode());
        if (!canApplyRefund) {
            throw new ServiceException("当前订单状态不支持退款申请");
        }
 
        // 校验是否已存在待审核的退款申请
        long pendingCount = this.count(new LambdaQueryWrapper<TRefundApplication>()
                .eq(TRefundApplication::getBigGiftPackageOrderId, dto.getOrderId())
                .eq(TRefundApplication::getStatus, 1)
                .eq(TRefundApplication::getDelFlag, 0));
        if (pendingCount > 0) {
            throw new ServiceException("该订单已存在待审核的退款申请");
        }
 
        // 创建退款申请记录
        TRefundApplication refundApplication = new TRefundApplication();
        refundApplication.setBigGiftPackageOrderId(dto.getOrderId());
        refundApplication.setApplicantUserId(userId);
        refundApplication.setApplicantTime(LocalDateTime.now());
        refundApplication.setRefundReason(dto.getRefundReason());
        refundApplication.setVoucherPicture(dto.getVoucherPicture());
        refundApplication.setStatus(1); // 待审核
        refundApplication.setDelFlag(0);
        this.save(refundApplication);
    }
 
    @Override
    public TRefundApplicationVo refundDetail(Integer id) {
        TRefundApplication refundApplication = this.getById(id);
        TRefundApplicationVo vo = new TRefundApplicationVo();
        vo.setId(refundApplication.getId());
        vo.setBigGiftPackageOrderId(refundApplication.getBigGiftPackageOrderId());
 
        // 订单信息
        TBigGiftPackageOrder order = tBigGiftPackageOrderService.getById(refundApplication.getBigGiftPackageOrderId());
        vo.setCode(order.getCode());
        vo.setCreateTime(order.getCreateTime());
        vo.setOrderPrice(order.getOrderPrice());
 
        // 用户信息
        TAppUser appUser = tAppUserService.getById(order.getAppUserId());
        vo.setUserName(appUser.getName());
        vo.setPhone(appUser.getPhone());
 
 
        // 商品信息
        TBigGiftPackage tBigGiftPackage = tBigGiftPackageService.getById(order.getBigGiftPackageId());
        vo.setBigGiftPackageName(tBigGiftPackage.getName());
        vo.setBigGiftPackageCover(tBigGiftPackage.getCoverImage());
        vo.setBigGiftPackagePrice(tBigGiftPackage.getTotalPrice());
        vo.setBigGiftPackageIntro(tBigGiftPackage.getIntroduction());
 
        // 销量
        long count = tBigGiftPackageOrderService.count(new LambdaQueryWrapper<TBigGiftPackageOrder>().eq(TBigGiftPackageOrder::getBigGiftPackageId, order.getBigGiftPackageId()).ne(TBigGiftPackageOrder::getStatus, 5));
        vo.setBigGiftPackageSales((int)count);
 
        // 支付信息
        vo.setStatus(order.getStatus());
        vo.setOrderAmount(order.getOrderPrice());
        vo.setFinalPayment(order.getFinalPayment());
        vo.setPaidTime(order.getFinalPaymentTime());
        vo.setRemark(order.getRemark());
 
        // 退款信息
        vo.setRefundReason(refundApplication.getRefundReason());
        vo.setVoucherPicture(refundApplication.getVoucherPicture());
        vo.setRefundStatus(refundApplication.getStatus());
 
        // 审核信息
        vo.setAuditRemark(refundApplication.getAuditRemark());
        vo.setAuditTime(refundApplication.getAuditTime());
        return vo;
    }
}