package com.ghy.web.controller.pay;

import com.alibaba.fastjson.JSONObject;
import com.ghy.common.adapay.model.AdapayStatusEnum;
import com.ghy.common.adapay.model.PaymentDTO;
import com.ghy.common.core.domain.AjaxResult;
import com.ghy.order.domain.OrderMaster;
import com.ghy.order.service.OrderMasterService;
import com.ghy.payment.domain.FinancialMaster;
import com.ghy.payment.service.AdapayService;
import com.ghy.payment.service.FinancialMasterService;
import com.huifu.adapay.core.exception.BaseAdaPayException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @author HH 2022/6/1
 */
@Controller
@RequestMapping("pay")
public class PayController {

    private static final Logger logger = LoggerFactory.getLogger(PayController.class);

    @Resource
    private AdapayService adapayService;
    @Resource
    private OrderMasterService orderMasterService;
    @Resource
    private FinancialMasterService financialMasterService;

//    /**
//     * 发起退款
//     *
//     * @param orderMasterId 主订单ID
//     * @param refundAmt     退款金额 保留两位小数
//     */
//    @PostMapping("refund")
//    @ResponseBody
//    public AjaxResult refund(Long orderMasterId, String refundAmt) throws BaseAdaPayException {
//        OrderMaster orderMaster = orderMasterService.selectById(orderMasterId);
//        Assert.notNull(orderMaster, "找不到对应的订单");
//        FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(orderMasterId);
//        Assert.notNull(financialMaster, "找不到订单");
//        PaymentDTO payment = financialMasterService.selectPaymentById(financialMaster.getPaymentId());
//        Assert.notNull(payment, "找不到支付记录");
//        Assert.isTrue(financialMaster.getPayStatus() == 1, "订单未支付");
//        Assert.hasText(financialMaster.getPaymentId(), "找不到订单的支付记录，请联系管理员");
//        JSONObject response = adapayService.refund(orderMaster.getDeptId(), financialMaster.getPaymentId(), payment.getOrderNo(), refundAmt);
//        if (AdapayStatusEnum.succeeded.code.equals(response.getString("status"))) {
//            return AjaxResult.success();
//        } else {
//            return AjaxResult.error(response.getString("error_msg"));
//        }
//    }

//    /**
//     * 撤销支付
//     *
//     * @param orderMasterId 主订单ID
//     * @param reverseAmt    撤销金额 保留两位小数
//     */
//    @PostMapping("reverse")
//    @ResponseBody
//    public AjaxResult reverse(Long orderMasterId, String reverseAmt) throws BaseAdaPayException {
//        OrderMaster orderMaster = orderMasterService.selectById(orderMasterId);
//        Assert.notNull(orderMaster, "找不到对应的订单");
//        FinancialMaster financialMaster = financialMasterService.selectByOrderMasterId(orderMasterId);
//        Assert.notNull(financialMaster, "找不到订单");
//        Assert.isTrue(financialMaster.getPayStatus() == 1, "订单未支付");
//        Assert.hasText(financialMaster.getPaymentId(), "找不到订单的支付记录，请联系管理员");
//        JSONObject response = adapayService.payReverse(orderMaster.getDeptId(), financialMaster.getPaymentId(), reverseAmt);
//        if (AdapayStatusEnum.succeeded.code.equals(response.getString("status"))) {
//            return AjaxResult.success();
//        } else {
//            return AjaxResult.error(response.getString("error_msg"));
//        }
//    }
}
