package com.dxhy.core.util;

import com.dxhy.api.model.sign.TDxInvoice;
import com.dxhy.api.model.invoice.TDxRecordInvoice;

import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * PushIntefaceUtil.java 艺龙推送接口
 *
 * @author X Yang
 * @version 1.0
 * @date 2017年7月27t日 下午3:31:29
 */
@Slf4j

public class PushIntefaceUtil {

    private static final String URL = "http://hotelcommission.elong.com/invoice/editScanInvoiceInfo";

    private static final String SECRET_KEY = "b7f65a9d715d3eaa6fdbfe2ee55e91fafb93c001";

    public static String validParams(String data) {
        try {
            // 拼接签名的后半部分字符串
            String str = "updateInfo=" + data +
            // data=请求数据
                "&data=" + Base64.encode(data);
            log.info("签权的字符串：{}", str);
            String hmacsha1 = HmacSHA1Util.hmacsha1(str, SECRET_KEY);
            log.info("加密之后：{}", hmacsha1);
            return hmacsha1;
        } catch (Exception e) {
            log.debug("签权失败" + e);
        }
        return "";
    }

    public static String updateInvoiceStatus(TDxInvoice invo) {
        String httpPost = "";
        try {
            String json = "{\"invoiceCode\":\"" + invo.getInvoiceCode() + "\",\"invoiceNo\":\"" + invo.getInvoiceNo()
                + "\",\"invoiceStatus\":" + invo.getInvoiceStatus() + "}";
            log.debug("校验加密数据" + json);
            String auth = validParams(json);
            String invoices = "{\"authorize\":{\"appSec\":\"" + auth + "\"},\"data\":\"" + Base64.encode(json) + "\"}";
            log.debug("请求数据" + invoices);
            httpPost = HttpUtil.post(URL, invoices, 30000);
            log.debug("推送接口返回数据" + httpPost);
        } catch (Exception e) {
            log.debug("请求异常信息为" + e);
        }
        return httpPost;
    }

    public static String get(TDxRecordInvoice record) {
        TDxInvoice invoice = new TDxInvoice();
        invoice.setInvoiceCode(record.getInvoiceCode());
        invoice.setInvoiceStatus(record.getInvoiceStatus());
        invoice.setInvoiceNo(record.getInvoiceNo());
        return updateInvoiceStatus(invoice);
    }

}
