|  | @@ -0,0 +1,186 @@
 | 
	
		
			
				|  |  | +package com.yonge.cooleshow.biz.dal.service.impl;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.metadata.IPage;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 | 
	
		
			
				|  |  | +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.auth.api.entity.SysUser;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.dao.UserOrderRefundDao;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.dto.req.AuthOperaReq;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.dto.req.OrderRefundReq;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.dto.search.UserOrderRefundSearch;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.entity.UserOrderDetail;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.entity.UserOrderPayment;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.entity.UserOrderRefund;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.enums.AuthStatusEnum;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.enums.GoodTypeEnum;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.enums.OrderStatusEnum;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.enums.TradeStatusEnum;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.service.*;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.vo.UserOrderDetailVo;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.vo.UserOrderRefundVo;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.vo.UserOrderVo;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.biz.dal.vo.res.RefundCreateRes;
 | 
	
		
			
				|  |  | +import com.yonge.cooleshow.common.entity.HttpResponseResult;
 | 
	
		
			
				|  |  | +import com.yonge.toolset.utils.string.StringUtil;
 | 
	
		
			
				|  |  | +import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | +import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | +import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import javax.annotation.PostConstruct;
 | 
	
		
			
				|  |  | +import java.math.BigDecimal;
 | 
	
		
			
				|  |  | +import java.util.*;
 | 
	
		
			
				|  |  | +import java.util.function.Consumer;
 | 
	
		
			
				|  |  | +import java.util.function.Function;
 | 
	
		
			
				|  |  | +import java.util.stream.Collectors;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +import static java.util.stream.Collectors.joining;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +@Service
 | 
	
		
			
				|  |  | +public class UserOrderRefundServiceImpl extends ServiceImpl<UserOrderRefundDao, UserOrderRefund> implements UserOrderRefundService {
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private UserOrderService orderService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private UserOrderPaymentService orderPaymentService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private UserOrderDetailService orderDetailService;
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private CourseGroupService courseGroupService;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    //验证是否可以退款,获取退款金额信息
 | 
	
		
			
				|  |  | +    private static final Map<GoodTypeEnum, Function<OrderRefundReq, HttpResponseResult<RefundCreateRes>>> refundCreate = new HashMap<>();
 | 
	
		
			
				|  |  | +    //插入退款后执行
 | 
	
		
			
				|  |  | +    private static final Map<GoodTypeEnum, Consumer<UserOrderDetailVo>> refundAfter = new HashMap<>();
 | 
	
		
			
				|  |  | +    //退款完成后执行
 | 
	
		
			
				|  |  | +    private static final Map<GoodTypeEnum, Consumer<UserOrderDetailVo>> refundSuccess = new HashMap<>();
 | 
	
		
			
				|  |  | +    //退款未通过后执行
 | 
	
		
			
				|  |  | +    private static final Map<GoodTypeEnum, Consumer<UserOrderDetailVo>> refundCancel = new HashMap<>();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @PostConstruct
 | 
	
		
			
				|  |  | +    private void init() {
 | 
	
		
			
				|  |  | +        /**********退费单生成前******************/
 | 
	
		
			
				|  |  | +        //直播课退费
 | 
	
		
			
				|  |  | +        refundCreate.put(GoodTypeEnum.LIVE, courseGroupService::refundCreate);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        refundSuccess.put(GoodTypeEnum.LIVE, courseGroupService::refundSuccess);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public UserOrderRefundVo detail(Long id) {
 | 
	
		
			
				|  |  | +        UserOrderRefundVo detail = baseMapper.detail(id);
 | 
	
		
			
				|  |  | +        return detail;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public IPage<UserOrderRefundVo> selectPage(IPage<UserOrderRefundVo> page, UserOrderRefundSearch query) {
 | 
	
		
			
				|  |  | +        return page.setRecords(baseMapper.selectPage(page, query));
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | +    public HttpResponseResult<Boolean> orderRefund(OrderRefundReq refundReq) {
 | 
	
		
			
				|  |  | +        UserOrderVo detail = orderService.detail(refundReq.getOrderId());
 | 
	
		
			
				|  |  | +        if (null == detail || CollectionUtils.isEmpty(detail.getOrderDetailList())) {
 | 
	
		
			
				|  |  | +            return HttpResponseResult.failed("未找到订单信息");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (!OrderStatusEnum.PAID.equals(detail.getStatus())) {
 | 
	
		
			
				|  |  | +            return HttpResponseResult.failed("订单状态异常");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (StringUtil.isEmpty(detail.getTransNo())) {
 | 
	
		
			
				|  |  | +            return HttpResponseResult.failed("未找到订单付款信息");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        UserOrderPayment orderPayment = orderPaymentService.detailByTransNo(detail.getTransNo());
 | 
	
		
			
				|  |  | +        if (null == orderPayment || !TradeStatusEnum.succeeded.equals(orderPayment.getStatus())) {
 | 
	
		
			
				|  |  | +            return HttpResponseResult.failed("订单付款状态异常");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        if (CollectionUtils.isEmpty(refundReq.getOredrDetilIds())) {
 | 
	
		
			
				|  |  | +            //查询订单下未退款的所有详情订单
 | 
	
		
			
				|  |  | +            List<UserOrderDetail> orderDetails = baseMapper.selectOrderRefundDetils(refundReq.getOrderId());
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            refundReq.setOredrDetilIds(orderDetails.stream().map(UserOrderDetail::getId).collect(Collectors.toList()));
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        //退款的详情id
 | 
	
		
			
				|  |  | +        List<Long> detilIds = new ArrayList<>();
 | 
	
		
			
				|  |  | +        //退款金额
 | 
	
		
			
				|  |  | +        BigDecimal actualPrice = BigDecimal.ZERO;
 | 
	
		
			
				|  |  | +        for (UserOrderDetailVo vo : detail.getOrderDetailList()) {
 | 
	
		
			
				|  |  | +            List<Long> collect = refundReq.getOredrDetilIds().stream().filter(o -> o.equals(vo.getId())).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +            if (CollectionUtils.isNotEmpty(collect)) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +                Function<OrderRefundReq, HttpResponseResult<RefundCreateRes>> refundCreateFunction = refundCreate.get(vo.getGoodType());
 | 
	
		
			
				|  |  | +                if (Objects.isNull(refundCreateFunction)) {
 | 
	
		
			
				|  |  | +                    refundReq.setOredrDetil(vo);
 | 
	
		
			
				|  |  | +                    HttpResponseResult<RefundCreateRes> apply = refundCreateFunction.apply(refundReq);
 | 
	
		
			
				|  |  | +                    if (apply.getStatus()) {
 | 
	
		
			
				|  |  | +                        detilIds.add(vo.getId());
 | 
	
		
			
				|  |  | +                        actualPrice.add(apply.getData().getActualPrice());
 | 
	
		
			
				|  |  | +                    }
 | 
	
		
			
				|  |  | +                } else {
 | 
	
		
			
				|  |  | +                    detilIds.add(vo.getId());
 | 
	
		
			
				|  |  | +                    actualPrice.add(vo.getActualPrice());
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        String join = StringUtil.join(detilIds, ",");
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        UserOrderRefund orderRefunds = new UserOrderRefund();
 | 
	
		
			
				|  |  | +        orderRefunds.setUserId(refundReq.getUserId());
 | 
	
		
			
				|  |  | +        orderRefunds.setOrderId(refundReq.getOrderId());
 | 
	
		
			
				|  |  | +        orderRefunds.setOredrDetilIds(join);
 | 
	
		
			
				|  |  | +        orderRefunds.setStatus(AuthStatusEnum.DOING);
 | 
	
		
			
				|  |  | +        orderRefunds.setApplyAmount(actualPrice);
 | 
	
		
			
				|  |  | +        orderRefunds.setReason(refundReq.getReason());
 | 
	
		
			
				|  |  | +        save(orderRefunds);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        for (UserOrderDetailVo vo : detail.getOrderDetailList()) {
 | 
	
		
			
				|  |  | +            List<Long> collect = refundReq.getOredrDetilIds().stream().filter(o -> o.equals(vo.getId())).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +            if (CollectionUtils.isNotEmpty(collect)) {
 | 
	
		
			
				|  |  | +                Consumer<UserOrderDetailVo> refundAfterConsumer = refundAfter.get(vo.getGoodType());
 | 
	
		
			
				|  |  | +                if (Objects.isNull(refundAfterConsumer)) {
 | 
	
		
			
				|  |  | +                    refundAfterConsumer.accept(vo);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return HttpResponseResult.succeed(true);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  | +    public HttpResponseResult<Boolean> doAuth(AuthOperaReq authOperaReq, SysUser user) {
 | 
	
		
			
				|  |  | +        UserOrderRefund orderRefund = baseMapper.selectById(authOperaReq.getId());
 | 
	
		
			
				|  |  | +        if (!AuthStatusEnum.DOING.equals(orderRefund.getStatus())) {
 | 
	
		
			
				|  |  | +            return HttpResponseResult.failed("退款单已审核");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        String oredrDetilIds = orderRefund.getOredrDetilIds();
 | 
	
		
			
				|  |  | +        if (StringUtil.isEmpty(oredrDetilIds)) {
 | 
	
		
			
				|  |  | +            return HttpResponseResult.succeed(true);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        orderRefund.setStatus(authOperaReq.getPass() ? AuthStatusEnum.PASS : AuthStatusEnum.UNPASS);
 | 
	
		
			
				|  |  | +        orderRefund.setActualAmount(orderRefund.getApplyAmount());
 | 
	
		
			
				|  |  | +        orderRefund.setOperateId(user.getId());
 | 
	
		
			
				|  |  | +        orderRefund.setOperateReason(authOperaReq.getReason());
 | 
	
		
			
				|  |  | +        orderRefund.setUpdateTime(new Date());
 | 
	
		
			
				|  |  | +        updateById(orderRefund);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        if (authOperaReq.getPass()) {
 | 
	
		
			
				|  |  | +            //通过调用
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            List<String> detilIds = Arrays.asList(oredrDetilIds.split(","));
 | 
	
		
			
				|  |  | +            for (String id : detilIds) {
 | 
	
		
			
				|  |  | +                UserOrderDetailVo detail = orderDetailService.detail(Long.parseLong(id));
 | 
	
		
			
				|  |  | +                if (null == detail) {
 | 
	
		
			
				|  |  | +                    continue;
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +                //调用业务方法
 | 
	
		
			
				|  |  | +                Consumer<UserOrderDetailVo> refundCancelConsumer = refundCancel.get(detail.getGoodType());
 | 
	
		
			
				|  |  | +                if (Objects.isNull(refundCancelConsumer)) {
 | 
	
		
			
				|  |  | +                    refundCancelConsumer.accept(detail);
 | 
	
		
			
				|  |  | +                }
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return HttpResponseResult.succeed(true);
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +}
 |