package com.fs.course.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.fs.FSAdminApplication;
import com.fs.course.config.RedPacketConfig;
import com.fs.statis.service.IStatisticsCompanyService;
import com.fs.statis.service.impl.StatisticsCompanyServiceImpl;
import com.fs.system.service.ISysConfigService;
import com.github.binarywang.wxpay.bean.request.WxPayRefundRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderResult;
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.cert.ocsp.Req;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

@RunWith(org.springframework.test.context.junit4.SpringRunner.class)
@SpringBootTest(classes = FSAdminApplication.class)
@Slf4j
class FsCourseDomainNameControllerTest {

    @Autowired
    private StatisticsCompanyServiceImpl statisticsCompanyService;
    @Autowired
    private WxPayService wxPayService;

    @Autowired
    private ISysConfigService configService;

    @Test
    public void test(){
        statisticsCompanyService.rewardMoneyTaskEverydayCompanyUser();
    }
    @Test
    public void nativePay() throws WxPayException {

        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId("wx961fadab9bcb792b");
        payConfig.setMchId("1716217886");
        payConfig.setMchKey("a7Fc5B9dE2h8J3kL4mN6pQ7rS9tU2vW1");
        payConfig.setPrivateKeyPath("C:\\cert\\1716217886_20250509_cert\\apiclient_key.pem");
        payConfig.setPrivateCertPath("C:\\cert\\1716217886_20250509_cert\\apiclient_cert.pem");
        payConfig.setCertSerialNo("4E8BD68BC2BFD37CA58244D660E5FDCCE475D82E");
        payConfig.setUseSandboxEnv(false);
        payConfig.setSignType(WxPayConstants.SignType.HMAC_SHA256);

        wxPayService.setConfig(payConfig);

        // 构建支付请求参数
        WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();

        // 设置商品描述
        orderRequest.setBody("测试");
        // 设置商户订单号
        orderRequest.setOutTradeNo("20250621170609");
        // 设置订单金额（单位：分）
        orderRequest.setTotalFee(1);
        // 设置终端IP
        orderRequest.setSpbillCreateIp("127.0.0.1");
        // 设置通知地址
        orderRequest.setNotifyUrl("https://userapp.fbylive.com/app/wxpay/v3TransferNotify");
        // 设置交易类型为NATIVE
        orderRequest.setTradeType(WxPayConstants.TradeType.NATIVE);
        orderRequest.setProductId("1");


        Object order = wxPayService.createOrder(orderRequest);


        log.info("请求结果: {}",order);
    }

    @Test
    public void refund() throws WxPayException {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId("wx961fadab9bcb792b");
        payConfig.setMchId("1716217886");
        payConfig.setMchKey("a7Fc5B9dE2h8J3kL4mN6pQ7rS9tU2vW1");
        payConfig.setPrivateKeyPath("C:\\cert\\1716217886_20250509_cert\\apiclient_key.pem");
        payConfig.setPrivateCertPath("C:\\cert\\1716217886_20250509_cert\\apiclient_cert.pem");
        payConfig.setKeyPath("C:\\cert\\1716217886_20250509_cert\\apiclient_cert.p12");
        payConfig.setCertSerialNo("4E8BD68BC2BFD37CA58244D660E5FDCCE475D82E");
        payConfig.setUseSandboxEnv(false);
        payConfig.setSignType(WxPayConstants.SignType.HMAC_SHA256);

        wxPayService.setConfig(payConfig);
        WxPayRefundRequest wxPayRefundRequest = new WxPayRefundRequest();
        wxPayRefundRequest.setTransactionId("4200002745202506242171692915");
        wxPayRefundRequest.setRefundFee(1);
        wxPayRefundRequest.setTotalFee(1);
        wxPayRefundRequest.setOutRefundNo("202506241450250001");
        wxPayService.refund(wxPayRefundRequest);
    }

}
