package com.staffing.custom.service;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.ValidationException;
import com.wechat.pay.java.core.notification.NotificationConfig;
import com.wechat.pay.java.core.notification.NotificationParser;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.transferbatch.TransferBatchServiceBase;
import com.wechat.pay.java.service.transferbatch.model.*;
import org.apache.ibatis.transaction.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;


/** TransferBatchService使用示例 */
@Component
public class TransferBatchService {
  private final static Logger logger = LoggerFactory.getLogger(TransferBatchService.class);

  public static String appId = "wx7bc7df6eb945a84f";

  /** 商户号 */
  public static String merchantId = "1684628563";

  /** 商户API私钥路径 */
  public static String privateKeyPath = "apiclient_key.pem";

  /** 商户证书序列号 执行获得： openssl x509 -noout -serial -in apiclient_cert.pem */
  public static String merchantSerialNumber = "4710F9274C2CB57B1222BC511C852ADE740B07C1";

  /** 商户APIV3密钥 */
  public static String apiV3Key = "Ps0123456789abcdefghijklmnopqrst";

  public TransferBatchServiceBase service;

  public static void main(String[] args) {
//    // 初始化商户配置
//    Config config =
//        new RSAAutoCertificateConfig.Builder()
//            .merchantId(merchantId)
//            // 使用 com.wechat.pay.java.core.util
//            // 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
//            .privateKeyFromPath(privateKeyPath)
//            .merchantSerialNumber(merchantSerialNumber)
//            .apiV3Key(apiV3Key)
//            .build();
//
//    // 初始化服务
//    service = new TransferBatchService.Builder().config(config).build();
//    // ... 调用接口
//    initiateBatchTransfer();
  }
  @PostConstruct
  public void init(){
    // 初始化商户配置
    Config config =
            new RSAAutoCertificateConfig.Builder()
                    .merchantId(merchantId)
                    // 使用 com.wechat.pay.java.core.util
                    // 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                    .privateKeyFromPath(privateKeyPath)
                    .merchantSerialNumber(merchantSerialNumber)
                    .apiV3Key(apiV3Key)
                    .build();

    // 初始化服务
    service = new TransferBatchServiceBase.Builder().config(config).build();
    // ... 调用接口
//    initiateBatchTransfer();
  }

  /** 通过微信批次单号查询批次单 */
  public TransferBatchEntity getTransferBatchByNo() {

    GetTransferBatchByNoRequest request = new GetTransferBatchByNoRequest();
    return service.getTransferBatchByNo(request);
  }

  /** 通过商家批次单号查询批次单 */
  public TransferBatchEntity getTransferBatchByOutNo() {

    GetTransferBatchByOutNoRequest request = new GetTransferBatchByOutNoRequest();
    return service.getTransferBatchByOutNo(request);
  }

  /** 发起商家转账 */
  public InitiateBatchTransferResponse initiateBatchTransfer() {
    InitiateBatchTransferRequest request = new InitiateBatchTransferRequest();
    return service.initiateBatchTransfer(request);
  }
  /** 发起商家转账 */
  public InitiateBatchTransferResponse initiateBatchTransfer(InitiateBatchTransferRequest request) {
    return service.initiateBatchTransfer(request);
  }

  /** 通过微信明细单号查询明细单 */
  public TransferDetailEntity getTransferDetailByNo() {

    GetTransferDetailByNoRequest request = new GetTransferDetailByNoRequest();
    return service.getTransferDetailByNo(request);
  }

  /** 通过商家明细单号查询明细单 */
  public TransferDetailEntity getTransferDetailByOutNo() {

    GetTransferDetailByOutNoRequest request = new GetTransferDetailByOutNoRequest();
    return service.getTransferDetailByOutNo(request);
  }


  public ResponseEntity.BodyBuilder parse(RequestParam requestParam){


// 如果已经初始化了 RSAAutoCertificateConfig，可直接使用
// 没有的话，则构造一个
    NotificationConfig config = new RSAAutoCertificateConfig.Builder()
            .merchantId(merchantId)
            .privateKeyFromPath(privateKeyPath)
            .merchantSerialNumber(merchantSerialNumber)
            .apiV3Key(apiV3Key)
            .build();

// 初始化 NotificationParser
    NotificationParser parser = new NotificationParser(config);

    try {
      // 以支付通知回调为例，验签、解密并转换成 Transaction
      Transaction transaction = parser.parse(requestParam, Transaction.class);
    } catch (ValidationException e) {
      // 签名验证失败，返回 401 UNAUTHORIZED 状态码
      logger.error("sign verification failed", e);
      return ResponseEntity.status(HttpStatus.UNAUTHORIZED);
    }

// 如果处理失败，应返回 4xx/5xx 的状态码，例如 500 INTERNAL_SERVER_ERROR
//    if (/* process error */) {
//      return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR);
//    }

// 处理成功，返回 200 OK 状态码
    return ResponseEntity.status(HttpStatus.OK);

  }
}
