package com.hzs.third.pay.controller.api;

import com.hzs.common.core.enums.EPayChannel;
import com.hzs.common.core.enums.EPayStatus;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.core.web.domain.AjaxResult;
import com.hzs.common.domain.third.pay.TOnlinePayment;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.third.pay.controller.base.PayBaseController;
import com.hzs.third.pay.dto.RefundDTO;
import com.hzs.third.pay.param.PayParam;
import com.hzs.third.pay.service.IRefundService;
import com.hzs.third.pay.service.ITOnlinePaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description: 京东退款控制器
 * @Author: jiang chao
 * @Time: 2023/8/17 15:11
 * @Classname: JdRefundController
 * @PackageName: com.hzs.third.pay.controller.api
 */
@Slf4j
@RestController
@RequestMapping("/jd-refund")
public class JdRefundController extends PayBaseController {

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

    @GetMapping("/test/{orderCode}")
    public AjaxResult test(@PathVariable String orderCode) {
        iRefundService.jdRefundDivision(orderCode, SecurityUtils.getUserId());
        return AjaxResult.success();
    }

    @GetMapping("/test2")
    public AjaxResult test2() {
        iRefundService.jdQueryEnterpriseWalletBalance();
        return AjaxResult.success();
    }

    /**
     * 京东退款接口
     *
     * @param param
     * @return
     */
    @PostMapping("/refund")
    public AjaxResult refund(@RequestBody PayParam param) {
        if (null == param.getBusinessType() || StringUtils.isEmpty(param.getBusinessCode())) {
            // 缺少参数
            return AjaxResult.error("缺少参数");
        }
        // 查询在线支付明细
        TOnlinePayment tOnlinePayment = itOnlinePaymentService.queryByBusiness(param.getBusinessType(), param.getBusinessCode(), SecurityUtils.getPkCountry());
        if (null == tOnlinePayment || EPayStatus.UNPAID.getValue() == tOnlinePayment.getPayStatus()) {
            // 在线支付明细为空 或者 未支付成功，则不能进行退款
            return AjaxResult.error("订单不存在或未支付成功，不能进行退款");
        }
        // 获取支付渠道
        EPayChannel ePayChannel = EPayChannel.getEnumByValue(tOnlinePayment.getPayChannel());
        if (null == ePayChannel) {
            return AjaxResult.error("订单支付信息有误，不能进行退款");
        }

        try {
            // 京东收银台退款处理
            String str = iRefundService.jdCashRefundHandle(RefundDTO.builder()
                            .businessType(param.getBusinessType())
                            .businessCode(param.getBusinessCode())
                            .refundAmount(tOnlinePayment.getPayMoney())
                            .userId(SecurityUtils.getUserId())
                            .pkCountry(SecurityUtils.getPkCountry())
                            .build(),
                    tOnlinePayment);
            // 京东银行卡退款处理 -- 旧
//            String str = iRefundService.jdRefundHandle(RefundDTO.builder()
//                            .businessType(param.getBusinessType())
//                            .businessCode(param.getBusinessCode())
//                            .refundAmount(tOnlinePayment.getPayMoney())
//                            .userId(SecurityUtils.getUserId())
//                            .pkCountry(SecurityUtils.getPkCountry())
//                            .build(),
//                    tOnlinePayment);
            if (null == str) {
                return AjaxResult.success();
            }
            return AjaxResult.error("退款失败：" + str);
        } catch (Exception e) {
            log.error("调用京东退款处理返回异常!", e);
            return AjaxResult.error("退款异常，请刷新后重试");
        }
    }

}
