package com.hfkj.channel.alipay.utils;

import com.alipay.api.AlipayClient;
import com.alipay.api.FileItem;
import com.alipay.api.domain.ZftSubMerchantOrder;
import com.alipay.api.request.AntMerchantExpandIndirectImageUploadRequest;
import com.alipay.api.request.AntMerchantExpandIndirectZftConsultRequest;
import com.alipay.api.request.AntMerchantExpandIndirectZftSettlementmodifyRequest;
import com.alipay.api.request.AntMerchantExpandIndirectZftorderQueryRequest;
import com.alipay.api.response.AntMerchantExpandIndirectImageUploadResponse;
import com.alipay.api.response.AntMerchantExpandIndirectZftConsultResponse;
import com.alipay.api.response.AntMerchantExpandIndirectZftSettlementmodifyResponse;
import com.alipay.api.response.AntMerchantExpandIndirectZftorderQueryResponse;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 支付业务请求
 * @className: MerchantRequest
 * @author: HuRui
 * @date: 2023/2/17
 **/
public class AlipayRequest {

    private static Logger log = LoggerFactory.getLogger(AlipayRequest.class);

    /**
     * 商户创建预校验咨询
     * @param bizContent 进件参数
     * @return
     * @throws Exception
     */
    public static void merchantIndirectConsult(String bizContent) {
        log.info("========== START 商户创建预校验咨询 ==========");
        log.info("请求参数：" + bizContent);
        try {
            AlipayClient alipayClient = AlipayUtils.initClient();
            AntMerchantExpandIndirectZftConsultRequest request = new AntMerchantExpandIndirectZftConsultRequest();
            request.setBizContent(bizContent);
            AntMerchantExpandIndirectZftConsultResponse response = alipayClient.certificateExecute(request);
            log.info("响应参数：" + response);
            if (response.isSuccess() != true || response.getAccountAudit() != true || response.getRiskAudit() != true) {
                throw new Exception(response.getSubMsg());
            }
        } catch (Exception e) {
            log.info("出现异常：" + e);
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, e.getMessage());
        } finally {
            log.info("========== END 商户创建预校验咨询 ==========");
        }
    }

    /**
     * 商户创建
     * @param bizContent 进件参数
     * @return 申请单号
     * @throws Exception
     */
    public static String merchantIndirectCreate(String bizContent) {
        log.info("========== START 商户创建 ==========");
        log.info("请求参数：" + bizContent);
        try {
            AlipayClient alipayClient = AlipayUtils.initClient();
            AntMerchantExpandIndirectZftConsultRequest request = new AntMerchantExpandIndirectZftConsultRequest();
            request.setBizContent(bizContent);
            AntMerchantExpandIndirectZftConsultResponse response = alipayClient.certificateExecute(request);
            log.info("响应参数：" + response);
            if (response.isSuccess()) {
                return response.getOrderId();
            }
            throw new Exception(response.getSubMsg());

        } catch (Exception e) {
            log.info("出现异常：" + e);
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, e.getMessage());

        } finally {
            log.info("========== END 商户创建 ==========");
        }
    }

    /**
     * 商户入驻进度查询
     * @param bizContent 进件参数
     * @return 申请单号
     * @throws Exception
     */
    public static List<ZftSubMerchantOrder> merchantIndirectQuery(String bizContent) {
        log.info("========== START 商户入驻进度查询 ==========");
        log.info("请求参数：" + bizContent);
        try {
            AlipayClient alipayClient = AlipayUtils.initClient();
            AntMerchantExpandIndirectZftorderQueryRequest request = new AntMerchantExpandIndirectZftorderQueryRequest();
            request.setBizContent(bizContent);
            AntMerchantExpandIndirectZftorderQueryResponse response = alipayClient.certificateExecute(request);
            log.info("响应参数：" + response);
            if (response.isSuccess()) {
                return response.getOrders();
            }
            throw new Exception(response.getSubMsg());

        } catch (Exception e) {
            log.info("出现异常：" + e);
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, e.getMessage());

        } finally {
            log.info("========== END 商户入驻进度查询 ==========");
        }
    }

    /**
     * 商户结算信息修改
     * @param bizContent 参数
     * @return
     * @throws Exception
     */
    public static String merchantIndirectSettlementModify(String bizContent) {
        log.info("========== START 商户结算信息修改 ==========");
        log.info("请求参数：" + bizContent);
        try {
            AlipayClient alipayClient = AlipayUtils.initClient();
            AntMerchantExpandIndirectZftSettlementmodifyRequest request = new AntMerchantExpandIndirectZftSettlementmodifyRequest ();
            request.setBizContent(bizContent);
            AntMerchantExpandIndirectZftSettlementmodifyResponse response = alipayClient.certificateExecute(request);
            log.info("响应参数：" + response);
            if (response.isSuccess()) {
                return response.getOrderId();
            }
            throw new Exception(response.getSubMsg());

        } catch (Exception e) {
            log.info("出现异常：" + e);
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, e.getMessage());

        } finally {
            log.info("========== END 商户结算信息修改 ==========");
        }
    }

    /**
     * 图片上传
     * @param fileUrl 本地路径
     * @return
     * @throws Exception
     */
    public static String imageUpload(String fileUrl) {
        log.info("========== START 图片上传 ==========");
        log.info("请求参数：" + fileUrl);
        try {
            AlipayClient alipayClient = AlipayUtils.initClient();
            // 读取本地文件
            FileItem imageContent = new FileItem(fileUrl);

            // 截取文件格式
            String imageType = StringUtils.substringAfterLast(fileUrl, ".");

            AntMerchantExpandIndirectImageUploadRequest request = new AntMerchantExpandIndirectImageUploadRequest();
            request.setImageContent(imageContent);
            request.setImageType(imageType);
            AntMerchantExpandIndirectImageUploadResponse response = alipayClient.certificateExecute(request);
            log.info("响应参数：" + response);
            if (response.isSuccess()) {
                return response.getImageId();
            }
            throw new Exception(response.getSubMsg());

        } catch (Exception e) {
            log.info("出现异常：" + e);
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, e.getMessage());

        } finally {
            log.info("========== END 图片上传 ==========");
        }
    }

}
