package com.hzs.third.pay.provider;

import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EPayChannel;
import com.hzs.common.core.enums.EPayStatus;
import com.hzs.common.domain.third.pay.TOnlinePayment;
import com.hzs.third.pay.IRefundServiceApi;
import com.hzs.third.pay.dto.RefundDTO;
import com.hzs.third.pay.service.IRefundService;
import com.hzs.third.pay.service.ITOnlinePaymentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 退款dubbo服务
 * @Author: jiang chao
 * @Time: 2023/8/17 18:57
 * @Classname: RefundServiceProvider
 * @PackageName: com.hzs.third.pay.provider
 */
@Slf4j
@DubboService
public class RefundServiceProvider implements IRefundServiceApi {

    @Autowired
    private IRefundService iRefundService;
    @Autowired
    private ITOnlinePaymentService itOnlinePaymentService;

    @Override
    public R refund(RefundDTO refundDTO) {
        String str = null;
        try {
            // 查询在线支付明细
            TOnlinePayment tOnlinePayment = itOnlinePaymentService.queryByBusiness(refundDTO.getBusinessType(), refundDTO.getBusinessCode(), refundDTO.getPkCountry());
            if (null == tOnlinePayment || EPayStatus.UNPAID.getValue() == tOnlinePayment.getPayStatus()) {
                // 在线支付明细为空 或者 未支付成功，则不能进行退款
                return R.fail("订单不存在或未支付成功，不能进行退款");
            }
            // 获取支付渠道
            EPayChannel ePayChannel = EPayChannel.getEnumByValue(tOnlinePayment.getPayChannel());
            if (null == ePayChannel) {
                return R.fail("订单支付信息有误，不能进行退款");
            }

            switch (ePayChannel) {
                case HUIFU:
                    // 新汇付
                    break;
                default:
            }
            if (null == str) {
                return R.ok();
            }
        } catch (Exception e) {
            log.error("退款处理异常，refundDTO: {}", refundDTO, e);
        }
        return R.fail(str);
    }

}
