package com.heyu.api.controller.common;


import com.heyu.api.baidu.handle.common.BSealHandle;
import com.heyu.api.baidu.request.common.BSealRequest;
import com.heyu.api.baidu.response.common.BSealResp;
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.resp.common.SealOtherResp;
import com.heyu.api.resp.common.SealResp;
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/?_=1740219852952&timestamp=1742702349425#/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%2Fseal&method=post
 *
 * 印章识别
 *
 *接口描述
 * 检测并识别合同文件或常用票据中的印章，输出文字内容、印章位置信息以及相关置信度，支持识别印章编码，可覆盖圆形章、椭圆形章、方形章等常见种类的印章。
 *
 *
 *
 */
@Slf4j
@RestController
@RequestMapping("/seal")
@NotIntercept
public class SealController extends BaseController {


    @Autowired
    private BSealHandle bSealHandle;

    @RequestMapping("/recognize")
    @CacheResult
    public R recognize(BSealRequest request) {

        List<SealResp> respList = new ArrayList<>();

        ApiR<BSealResp> bR = bSealHandle.handle(request);
        if (bR.isSuccess()) {
            BSealResp bSealResp = bR.getData();

            List<BSealResp.ResultDTO> resultDTOList = bSealResp.getResult();
            if (CollectionUtils.isNotEmpty(resultDTOList)) {
                for (BSealResp.ResultDTO resultDTO : resultDTOList) {
                    SealResp resp = new SealResp();
                    resp.setType(resultDTO.getType());
                    resp.setMajorWords(resultDTO.getMajor().getWords());

                    if (CollectionUtils.isNotEmpty(resultDTO.getMinor())) {
                        List<SealOtherResp> sealOtherResps = new ArrayList<>();
                        for (BSealResp.ResultDTO.MinorDTO minorDTO : resultDTO.getMinor()) {

                            SealOtherResp otherResp = new SealOtherResp();

                            otherResp.setMinorWords(minorDTO.getWords());
                            otherResp.setMinorProbability(minorDTO.getProbability() + "");

                            sealOtherResps.add(otherResp);

                        }
                        resp.setOtherSealList(sealOtherResps);
                    }
                    respList.add(resp);
                }
                return R.ok().setData(respList);
            }

        }

        return R.error();
    }

}
