package com.malk.service.aliyun.impl;

import com.alibaba.fastjson.JSONException;
import com.malk.server.aliyun.ALYR;
import com.malk.server.common.McException;
import com.malk.service.aliyun.ALYInvoice;
import com.malk.utils.UtilHttp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class ALYInvoiceImpl implements ALYInvoice {

    // 初始化参数: 加载appcode
    private Map<String, String> initHeaderAppCode(String appcode) {
        Map headers = new HashMap();
        headers.put("Authorization", "APPCODE " + appcode);
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
        return headers;
    }

    // 接口异常, 请检查阿里云发票次数余额
    private Map<String, ?> doPostForResource(String url, String appcode, Map param, Map body, Map form) {
        ALYR r = null;
        // 可用余额不足时, 返回403, JSON解析异常
        try {
            r = (ALYR) UtilHttp.doPost(url, initHeaderAppCode(appcode), param, body, form, ALYR.class);
        } catch (JSONException e) {
            log.info("接口异常, {}", e);
            McException.exceptionAccess("接口异常, 请检查阿里云发票次数余额");
        }
        return (Map<String, ?>) r.getData();
    }

    /**
     * 阿里发票验真: PDF文件
     */
    @Override
    public Map<String, ?> invoiceCheckPDF(String appcode, String pdfUrl) {
        Map<String, Object> param = new HashMap<>();
        param.put("pdfUrl", pdfUrl);
        return doPostForResource("https://fapiao.market.alicloudapi.com/v2/invoice/pdf", appcode, param, null, null);
    }

    /**
     * 阿里发票验真: 发票查验 [参数]
     */
    @Override
    public Map<String, ?> invoiceCheckCode(String appcode, String fpdm, String fphm, String kprq, String noTaxAmount, String checkCode) {
        Map<String, String> bodys = new HashMap<>();
        bodys.put("fpdm", fpdm);
        bodys.put("fphm", fphm);
        bodys.put("kprq", kprq);
        bodys.put("noTaxAmount", noTaxAmount);
        if (StringUtils.isNotBlank(checkCode) && checkCode.length() > 6) {
            checkCode = checkCode.substring(checkCode.length() - 6);
        }
        bodys.put("checkCode", checkCode);
        return doPostForResource("https://fapiao.market.alicloudapi.com/v2/invoice/query", appcode, null, null, bodys);
    }

    /**
     * 阿里发票验真: 二维码发票查验
     */
    @Override
    public Map<String, ?> invoiceCheckCode(String appcode, String code) {
        Map<String, String> bodys = new HashMap<>();
        bodys.put("code", code);
        return doPostForResource("https://fapiao.market.alicloudapi.com/v2/invoice/qrcode", appcode, null, bodys, null);
    }

    /**
     * 阿里发票验真: image
     */
    @Override
    public Map<String, ?> invoiceCheckOCR(String appcode, String imgUrl) {
        Map<String, Object> param = new HashMap<>();
        param.put("url", imgUrl);
//        param.put("extType", true);
        return doPostForResource("https://invoice.market.alicloudapi.com/v2/invoice/ocr", appcode, null, null, param);
    }
}
