package com.heyu.api.controller.doc;


import com.heyu.api.baidu.handle.doc.BDocClassifyHandle;
import com.heyu.api.baidu.request.doc.BDocClassifyRequest;
import com.heyu.api.baidu.response.doc.BDocClassifyResp;
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.doc.DocClassifyResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
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/?_=1740667714316&timestamp=1742716755429#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E6%96%87%E6%A1%A3%E5%9B%BE%E5%83%8F%E5%A4%84%E7%90%86&api=rest%2F2.0%2Focr%2Fv1%2Fdoc_classify&method=post
 *
 *
 * 文件检测分类
 *
 * 接口描述
 * 对图片中的文档、卡证、票据等含文字的主体进行检测、分类，可同时支持一张图片中多张主体的情况，返回每个主体的类别及位置信息。
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/doc")
@NotIntercept
public class DocClassifyController {

    @Autowired
    private BDocClassifyHandle bDocClassifyHandle;


    @RequestMapping("/classify")
    @CacheResult
    public R classify(BDocClassifyRequest request) {
        List<DocClassifyResp> respList = new ArrayList<>();
        ApiR<BDocClassifyResp> bR = bDocClassifyHandle.handle(request);
        if (bR.isSuccess()) {
            BDocClassifyResp bDocClassifyResp = bR.getData();
            if (CollectionUtils.isNotEmpty(bDocClassifyResp.getWordsResult())) {
                for (BDocClassifyResp.WordsResultDTO wordsResultDTO : bDocClassifyResp.getWordsResult()) {
                    DocClassifyResp resp = new DocClassifyResp();
                    resp.setProbablity(wordsResultDTO.getProbablity());
                    resp.setType(wordsResultDTO.getType());
                    respList.add(resp);
                }
                return R.ok().setData(respList);
            }
        }
        return R.error(bR.getErrorMsg());
    }

}