package com.heyu.api.controller.convert;


import com.heyu.api.baidu.handle.convert.BParserTextHandle;
import com.heyu.api.baidu.handle.convert.BParserTextQueryQueryHandle;
import com.heyu.api.baidu.request.convert.BParserTextQueryRequest;
import com.heyu.api.baidu.response.convert.BParserTextQueryResp;
import com.heyu.api.baidu.response.convert.BParserTextResp;
import com.heyu.api.controller.BaseController;
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.data.utils.StringUtils;
import com.heyu.api.request.convert.ParserTextReq;
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;


/***
 * https://console.bce.baidu.com/support/?_=1740703582749&timestamp=1740704125877#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E6%99%BA%E8%83%BD%E6%96%87%E6%A1%A3%E5%88%86%E6%9E%90&api=rest%2F2.0%2Fbrain%2Fonline%2Fv2%2Fparser%2Ftask&method=post
 *
 * 文档解析-提交请求
 *
 *
 *https://console.bce.baidu.com/support/?_=1740703582749&timestamp=1742714833134#/api?product=AI&project=%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB&parent=%E6%99%BA%E8%83%BD%E6%96%87%E6%A1%A3%E5%88%86%E6%9E%90&api=rest%2F2.0%2Fbrain%2Fonline%2Fv2%2Fparser%2Ftask%2Fquery&method=post
 *
 *
 * 文档解析-获取结果
 *
 */
@Slf4j
@RestController
@RequestMapping("/text")
@NotIntercept
public class ParserTextController extends BaseController {


    @Autowired
    private BParserTextHandle bParserTextHandle;

    @Autowired
    private BParserTextQueryQueryHandle bParserTextQueryHandle;

    @RequestMapping("/parse")
    @CacheResult
    public R parse(ParserTextReq request) {
        if (StringUtils.isNotBlank(request.getTaskId())) {
            // 有 taskId → 查询文档解析结果
            BParserTextQueryRequest bParserTextQueryRequest = new BParserTextQueryRequest();
            bParserTextQueryRequest.setTaskId(request.getTaskId());

            ApiR<BParserTextQueryResp> bR = bParserTextQueryHandle.handle(bParserTextQueryRequest);
            if (bR.isSuccess()) {
                return R.ok().setData(bR.getData());
            }
            return R.error(bR.getErrorMsg());
        } else {
            // 无 taskId → 提交文档解析任务
            ApiR<BParserTextResp> bR = bParserTextHandle.handle(request);

            if (bR.isSuccess()) {
                BParserTextResp bParserTextResp = bR.getData();
                return R.ok().setData(bParserTextResp.getResult());
            }
            return R.error(bR.getErrorMsg());
        }

    }
}
