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


import com.heyu.api.data.annotation.CustomPath;
import com.heyu.api.data.constants.ApiConstants;
import com.heyu.api.data.utils.StringUtils;
import com.heyu.api.baidu.BaiduBaseHandle;
import com.heyu.api.baidu.request.common.ATableRequest;
import com.heyu.api.baidu.response.common.ATableResp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * https://console.bce.baidu.com/support/?_=1740219852952&timestamp=1740297734240#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E9%80%9A%E7%94%A8%E5%9C%BA%E6%99%AFOCR&api=rest%2F2.0%2Focr%2Fv1%2Ftable&method=post
 *
 * 表格识别
 * 将图片/PDF中的表格结构化输出，支持返回Excel（base64编码）和结构化JSON数据
 */
@Component
@Slf4j
@CustomPath("table")
public class BTableHandle extends BaiduBaseHandle<ATableRequest, ATableResp> {

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

    @Override
    public String check(ATableRequest aTableRequest) {
        if (!ApiConstants.trueOrFalse.contains(aTableRequest.getReturnExcel())) {
            return "returnExcel 仅能传 " + ApiConstants.trueOrFalse + ",是否输出excel文件";
        }
        if (!ApiConstants.trueOrFalse.contains(aTableRequest.getCellContents())) {
            return "cellContents 仅能传 " + ApiConstants.trueOrFalse + ",是否输出单元格文字位置信息";
        }
        return checkImageUri(aTableRequest);
    }

    @Override
    public String getContent(ATableRequest aTableRequest) {
        StringBuffer sb = getImageContent(aTableRequest);
        if (StringUtils.isNotBlank(aTableRequest.getReturnExcel())) {
            sb.append("&return_excel=").append(aTableRequest.getReturnExcel());
        }
        if (StringUtils.isNotBlank(aTableRequest.getCellContents())) {
            sb.append("&cell_contents=").append(aTableRequest.getCellContents());
        }
        return sb.toString();
    }
}
