package com.ruoyi.database.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.database.domain.dto.InvoiceRequest;
import com.ruoyi.database.domain.dto.InvoiceRequestData;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

/**
 * 发票对接
 */
public class InvoiceUtil {


    private static final String API_URL = "http://58.210.14.10:9300/ckl/einvoice/api/issue";


    public static void main(String[] args) throws JsonProcessingException {
        String appid = "0c091ef6-a30b-4022-9cb3-e105429c397c";
        ObjectMapper objectMapper = new ObjectMapper();
        InvoiceRequest invoiceRequest = new InvoiceRequest();
        invoiceRequest.setBuyerName("个人");
        invoiceRequest.setBuyerTaxNo("91110108MA00XXXXXX");
        invoiceRequest.setPriceTaxMark(1);
        invoiceRequest.setTotalAmount(new BigDecimal(100.00));
        InvoiceRequest.InvoiceDetail invoiceDetail = new InvoiceRequest.InvoiceDetail();
        ArrayList<InvoiceRequest.InvoiceDetail> invoiceDetails = new ArrayList<>();
        invoiceDetail.setAmount(new BigDecimal(100.00));
        invoiceDetail.setBillNo("3012512122CZWSUFLKU8I");
        invoiceDetails.add(invoiceDetail);
        invoiceRequest.setDetailList(invoiceDetails);
        String compactJsonStr = objectMapper.writeValueAsString(invoiceRequest);
        System.out.println( "JSON字符串:" + compactJsonStr);
        String encrypt = EncryptUtils.encrypt(compactJsonStr, "UTF-8");
        System.out.println( "加密字符串:" + encrypt);
        long timestamp = System.currentTimeMillis() / 1000L;

        String aaa ="appid=" + appid + "&data=" + encrypt + "&timestamp=" + timestamp + "&token=123456789";
        System.out.println("拼接字符串:" + aaa);

        String md5 = EncryptUtils.getMd5(aaa);
        System.out.println("md5字符串:" + md5);

        InvoiceRequestData invoiceRequestData = new InvoiceRequestData();
        invoiceRequestData.setData(encrypt);
        invoiceRequestData.setAppid(appid);
        invoiceRequestData.setSign(md5);
        invoiceRequestData.setTimestamp(timestamp);

        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {

            // 2. 创建 POST 请求
            HttpPost httpPost = new HttpPost(API_URL);

            // 3. 设置请求头
            httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

            // 转为 JSON 字符串
            String jsonBody = objectMapper.writeValueAsString(invoiceRequestData);
            StringEntity entity = new StringEntity(jsonBody, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);

            // 5. 执行请求
            try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
                // 6. 处理响应
                int statusCode = response.getStatusLine().getStatusCode();
                HttpEntity responseEntity = response.getEntity();
                String responseBody = EntityUtils.toString(responseEntity, StandardCharsets.UTF_8);

                if (statusCode >= 200 && statusCode < 300) {
                    System.out.println("接口调用成功！");
                    System.out.println("响应状态码：" + statusCode);
                    System.out.println("响应体：" + responseBody);
                } else {
                    System.out.println("接口调用失败，状态码：" + statusCode);
                    System.out.println("错误响应体：" + responseBody);
                }
            }

        } catch (Exception e) {
            System.err.println("接口调用异常：" + e.getMessage());
            e.printStackTrace();
        }
    }

}
