package com.ruoyi.system.service.impl; import com.alibaba.fastjson2.JSONObject; 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.math.BigDecimal; import java.time.LocalDateTime; import java.util.List; /** *

* 退款申请 服务实现类 *

* * @author xiaochen * @since 2025-12-08 */ @Service @RequiredArgsConstructor(onConstructor_ = @Lazy) public class TRefundApplicationServiceImpl extends ServiceImpl implements TRefundApplicationService { private final TBigGiftPackageOrderService tBigGiftPackageOrderService; private final TAppUserService tAppUserService; private final TBigGiftPackageService tBigGiftPackageService; @Override public Page pageRefundList(RefundApplicationQuery query) { Page page = new Page<>(query.getPageNum(), query.getPageSize()); return this.baseMapper.pageRefundList(page, query); } @Override public List 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() .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()); vo.setPaymentType(order.getPaymentType()); vo.setRefundAmount(order.getPaymentPrice()); // 用户信息 TAppUser appUser = tAppUserService.getById(order.getAppUserId()); if(appUser!=null){ vo.setUserName(appUser.getName()); vo.setPhone(appUser.getPhone()); } String bigGiftPackage = order.getBigGiftPackage(); if(bigGiftPackage!=null){ JSONObject jsonObject = JSONObject.parseObject(bigGiftPackage); vo.setBigGiftPackageName(jsonObject.getString("name")); vo.setBigGiftPackageCover(jsonObject.getString("coverImage")); vo.setBigGiftPackagePrice(new BigDecimal(jsonObject.getString("totalPrice"))); vo.setBigGiftPackageIntro(jsonObject.getString("introduction")); } // 销量 long count = tBigGiftPackageOrderService.count(new LambdaQueryWrapper().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; } }