package com.heyu.api.controller.financial;


import com.heyu.api.baidu.handle.financial.BInvoiceHandle;
import com.heyu.api.baidu.request.financial.BInvoiceRequest;
import com.heyu.api.baidu.response.financial.BInvoiceResp;
import com.heyu.api.data.annotation.CacheResult;
import com.heyu.api.data.annotation.NotIntercept;
import com.heyu.api.data.utils.ApiR;
import com.heyu.api.data.utils.R;
import com.heyu.api.resp.financial.InvoiceResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/***
 * https://console.bce.baidu.com/support/?_=1740575657628&timestamp=1742742293465#/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%2Finvoice&method=post
 *
 * 通用机打发票识别
 *
 *
 *
 * 接口描述
 * 支持对国家/地方税务局发行的横/竖版通用机打发票的23个关键字段进行结构化识别，包括发票类型、发票号码、发票代码、开票日期、合计金额大写、合计金额小写、商品名称、商品单位、商品单价、商品数量、商品金额、机打代码、
 * 机打号码、校验码、销售方名称、销售方纳税人识别号、购买方名称、购买方纳税人识别号、合计税额等。
 *
 *
 * 在线调试
 * 您可以在 示例代码中心 中调试该接口，可进行签名验证、查看在线调用的请求内容和返回结果、示例代码的自动生成。
 */
@Slf4j
@RestController
@RequestMapping("/invoice")
@NotIntercept
public class InvoiceController {


    @Autowired
    private BInvoiceHandle bInvoiceHandle;


    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BInvoiceRequest req) {

        InvoiceResp resp = new InvoiceResp();


        ApiR<BInvoiceResp> bR = bInvoiceHandle.handle(req);
        if (bR.isSuccess()) {
            BInvoiceResp bInvoiceResp = bR.getData();
            BInvoiceResp.WordsResultDTO wordsResultDTO = bInvoiceResp.getWordsResult();

            resp.setCity(wordsResultDTO.getCity());                    //市
            resp.setInvoiceNum(wordsResultDTO.getInvoiceNum());              // 发票号码
            resp.setSellerName(wordsResultDTO.getSellerName());              // 	销售方名称
            resp.setIndustrySort(wordsResultDTO.getIndustrySort());            //	行业分类
            resp.setProvince(wordsResultDTO.getProvince());                //	省

            List<InvoiceResp.CommodityDTO> commodityAmountList = new ArrayList<>();

            for (BInvoiceResp.WordsResultDTO.CommodityAmountDTO commodityAmountDTO : wordsResultDTO.getCommodityAmount()) {
                InvoiceResp.CommodityDTO commodityDTO = new InvoiceResp.CommodityDTO();
                commodityDTO.setWord(commodityAmountDTO.getWord());
                commodityDTO.setRow(commodityAmountDTO.getRow());
                commodityAmountList.add(commodityDTO);
            }

            resp.setCommodityAmount(commodityAmountList);               // 商品金额

            resp.setInvoiceDate(wordsResultDTO.getInvoiceDate());                 //开票日期
            resp.setPurchaserName(wordsResultDTO.getPurchaserName());           //购买方名称
            resp.setCommodityNum(wordsResultDTO.getCommodityNum());           //商品数量
            resp.setInvoiceCode(wordsResultDTO.getCheckCode());         // 发票代码
            resp.setCommodityUnit(wordsResultDTO.getCommodityUnit());      //商品单位
            resp.setSheetNum(wordsResultDTO.getSheetNum());            //联次
            resp.setPurchaserRegisterNum(wordsResultDTO.getPurchaserRegisterNum());                //购买方纳税人识别号
            resp.setTime(wordsResultDTO.getTime());                          //时间
            resp.setCommodityPrice(wordsResultDTO.getCommodityPrice());     //	商品单价
            resp.setAmountInFiguers(wordsResultDTO.getAmountInFiguers());     //合计金额小写
            resp.setAmountInWords(wordsResultDTO.getAmountInWords());       //合计金额大写
            resp.setCheckCode(wordsResultDTO.getCheckCode());           //校验码
            resp.setTotalTax(wordsResultDTO.getTotalTax());            //合计税额
            resp.setInvoiceType(wordsResultDTO.getInvoiceType());         // 发票类型
            resp.setSellerRegisterNum(wordsResultDTO.getSellerRegisterNum());       //	销售方纳税人识别号

            List<InvoiceResp.CommodityDTO> commodityNameDTOS = new ArrayList<>();
            for (BInvoiceResp.WordsResultDTO.CommodityNameDTO commodityNameDTO : wordsResultDTO.getCommodityName()) {
                InvoiceResp.CommodityDTO commodityDTO = new InvoiceResp.CommodityDTO();

                commodityDTO.setRow(commodityNameDTO.getRow());
                commodityDTO.setWord(commodityNameDTO.getWord());

                commodityNameDTOS.add(commodityDTO);
            }

            resp.setCommodityName(commodityNameDTOS);           //	商品名称
        }

        return R.error();
    }
}
