package com.heyu.api.baidu.handle.financial;


import com.google.common.collect.Maps;
import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.baidu.BaiduBaseHandle;
import com.heyu.api.baidu.request.financial.BVatInvoiceVerificationRequest;
import com.heyu.api.baidu.response.financial.BVatInvoiceVerificationResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/***
 *https://console.bce.baidu.com/support/?_=1740494347073&timestamp=1740500368284#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E8%B4%A2%E5%8A%A1%E7%A5%A8%E6%8D%AEOCR&api=rest%2F2.0%2Focr%2Fv1%2Fvat_invoice_verification&method=post
 *
 * 增值税发票验真
 */
@Component
@Slf4j
@CustomPath("vatInvoiceVerification")
public class BVatInvoiceVerificationHandle extends BaiduBaseHandle<BVatInvoiceVerificationRequest, BVatInvoiceVerificationResp> {


    public static Map<String, Object> suportInvoice = Maps.newHashMap();

    static {
        suportInvoice.put("special_vat_invoice", "增值税专用发票");
        suportInvoice.put("elec_special_vat_invoice", "增值税电子专用发票");
        suportInvoice.put("normal_invoice", "增值税普通发票");
        suportInvoice.put("elec_normal_invoice", "增值税普通发票（电子）");
        suportInvoice.put("roll_normal_invoice", "增值税普通发票（卷式）");
        suportInvoice.put("toll_elec_normal_invoice", "通行费增值税电子普通发票");
        suportInvoice.put("blockchain_invoice", "区块链电子发票（目前仅支持深圳地区）");
        suportInvoice.put("elec_invoice_special", "全电发票（专用发票）");
        suportInvoice.put("elec_invoice_normal", "全电发票（普通发票）");
        suportInvoice.put("special_freight_transport_invoice", "货运运输业增值税专用发票");
        suportInvoice.put("motor_vehicle_invoice", "机动车销售发票");
        suportInvoice.put("used_vehicle_invoice", "二手车销售发票");
        suportInvoice.put("elec_flight_itinerary_invoice", "航空运输电子客票行程单（电子发票）");
        suportInvoice.put("elec_train_ticket_invoice", "铁路电子客票（电子发票）");
    }

    /**
     * 发票代码。
     * 全电发票（专用发票）、全电发票（普通发票）此参数可为空，其他类型发票均不可为空
     */
    public static final List<String> invoiceCodeSuport = Arrays.asList("elec_invoice_special", "elec_invoice_normal");
    /**
     * 校验码。填写发票校验码后6位。
     * 增值税电子专票、普票、电子普票、卷票、区块链电子发票、通行费增值税电子普通发票此参数必填；
     * 其他类型发票此参数可为空
     */
    public static final List<String> checkCodeSuport = Arrays.asList("elec_special_vat_invoice"
            , "normal_invoice"
            , "elec_normal_invoice"
            , "roll_normal_invoice"
            , "toll_elec_normal_invoice"
    );

    /**
     * 发票金额。
     * 增值税专票、电子专票、区块链电子发票、机动车销售发票、货运专票填写不含税金额；
     * 二手车销售发票填写车价合计；
     * 全电发票（专用发票）、全电发票（普通发票）填写价税合计金额，其他类型发票可为空
     */
    public static final List<String> totalAmountSuport = Arrays.asList(
            "special_vat_invoice"
            , "elec_special_vat_invoice"
            , "blockchain_invoice"
            , "motor_vehicle_invoice"
            , "special_freight_transport_invoice"
            , "used_vehicle_invoice"
            , "elec_invoice_special"
            , "elec_invoice_normal"
    );


    @Override
    public String getUri() {
        return "/rest/2.0/ocr/v1/vat_invoice_verification";
    }

    @Override
    public String check(BVatInvoiceVerificationRequest bVatInvoiceVerificationRequest) {
        if (!invoiceCodeSuport.contains(bVatInvoiceVerificationRequest.getInvoiceType()) &&
                StringUtils.isNotBlank(bVatInvoiceVerificationRequest.getInvoiceCode())) {
            return "invoiceCode 发票代码, 全电发票（专用发票）、全电发票（普通发票）此参数可为空，其他类型发票均不可为空";
        }
        if (StringUtils.isBlank(bVatInvoiceVerificationRequest.getInvoiceNum())) {
            return "invoiceNum 发票号码不能为空";
        }

        if (StringUtils.isBlank(bVatInvoiceVerificationRequest.getInvoiceDate())) {
            return "invoiceDate 开票日期。格式YYYYMMDD，例：20210101";
        }

        if (StringUtils.isBlank(bVatInvoiceVerificationRequest.getInvoiceType())) {
            return "invoiceType不能为空";
        }

        if (suportInvoice.containsKey(bVatInvoiceVerificationRequest.getInvoiceType())) {
            return "invoiceType目前支持的类型是：" + StringUtils.printMap(suportInvoice);
        }
        /**
         * 校验码。填写发票校验码后6位。
         * 增值税电子专票、普票、电子普票、卷票、区块链电子发票、通行费增值税电子普通发票此参数必填；
         * 其他类型发票此参数可为空
         */
        if (checkCodeSuport.contains(bVatInvoiceVerificationRequest.getInvoiceType()) &&
                StringUtils.isBlank(bVatInvoiceVerificationRequest.getCheckCode())) {
            return "checkCode 不能为空， 校验码。填写发票校验码后6位。增值税电子专票、普票、电子普票、卷票、区块链电子发票、通行费增值税电子普通发票此参数必填";
        }

        if (totalAmountSuport.contains(bVatInvoiceVerificationRequest.getInvoiceType())
                && StringUtils.isBlank(bVatInvoiceVerificationRequest.getTotalAmount())) {
            return "totalAmount不能为空， 增值税专票、电子专票、区块链电子发票、机动车销售发票、货运专票填写不含税金额，二手车销售发票填写车价合计 " +
                    "全电发票（专用发票）、全电发票（普通发票）填写价税合计金额";
        }
        return null;
    }

    @Override
    public String getContent(BVatInvoiceVerificationRequest bVatInvoiceVerificationRequest) {
        StringBuffer sb = new StringBuffer();
        if (StringUtils.isNotBlank(bVatInvoiceVerificationRequest.getInvoiceCode())) {
            sb.append("invoice_code=" + bVatInvoiceVerificationRequest.getInvoiceCode());
        }
        if (StringUtils.isNotBlank(bVatInvoiceVerificationRequest.getInvoiceNum())) {
            sb.append("&invoice_num=" + bVatInvoiceVerificationRequest.getInvoiceNum());
        }
        if (StringUtils.isNotBlank(bVatInvoiceVerificationRequest.getInvoiceDate())) {
            sb.append("&invoice_date=" + bVatInvoiceVerificationRequest.getInvoiceDate());
        }
        if (StringUtils.isNotBlank(bVatInvoiceVerificationRequest.getInvoiceType())) {
            sb.append("&invoice_type=" + bVatInvoiceVerificationRequest.getInvoiceType());
        }
        if (StringUtils.isNotBlank(bVatInvoiceVerificationRequest.getCheckCode())) {
            sb.append("&check_code=" + bVatInvoiceVerificationRequest.getCheckCode());
        }
        if (StringUtils.isNotBlank(bVatInvoiceVerificationRequest.getTotalAmount())) {
            sb.append("&total_amount=" + bVatInvoiceVerificationRequest.getTotalAmount());
        }
        return sb.toString();
    }
}
