package com.qmth.paper.library.common.util;

import com.baidu.aip.ocr.AipOcr;
import com.qmth.paper.library.common.contant.SysSettingConstant;
import com.qmth.paper.library.common.contant.SystemConstant;
import com.qmth.paper.library.common.enums.ExceptionResultEnum;
import com.qmth.paper.library.common.service.CommonCacheService;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: ocr工具
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2022/10/12
 */
@Component
public class OcrUtil {

    private AipOcr client;

    private HashMap<String, String> options;

    @Resource
    CommonCacheService commonCacheService;

    //    @Bean
    public void init() {
        Map<String, String> map = commonCacheService.getSysSettingGlobal();
        String appId = map.get(SysSettingConstant.OCR_APP_ID);
        String appKey = map.get(SysSettingConstant.OCR_APP_KEY);
        String appSecret = map.get(SysSettingConstant.OCR_APP_SECRET);
        if (StringUtils.isAnyBlank(appId, appKey, appSecret)) {
            throw ExceptionResultEnum.ERROR.exception("OCR参数未配置，请联系管理员");
        }
        client = new AipOcr(appId, appKey, appSecret);
        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(SystemConstant.OCR_CONNECTION_TIMEOUT_IN_MILLIS);
        client.setSocketTimeoutInMillis(SystemConstant.OCR_SOCKET_TIMEOUT_IN_MILLIS);
        options = new HashMap<>();
        options.put("probability", "true");
    }


    public AipOcr getClient() {
        return client;
    }

    public Map<String, String> getOptions() {
        return options;
    }

    /**
     * ocr通用文字识别基础接口
     *
     * @param path
     * @return
     */
    public JSONObject callBasicGeneral(String path) {
        init();
        return client.basicGeneral(path, options);
    }

    /**
     * ocr通用文字识别高精度版接口
     *
     * @param path
     * @return
     */
    public JSONObject callBasicAccurateGeneral(String path) {
        init();
        return client.basicAccurateGeneral(path, options);
    }

    /**
     * ocr通用文字识别接口(含位置信息版)
     *
     * @param path
     * @return
     */
    public JSONObject callGeneral(String path) {
        init();
        return client.general(path, options);
    }

    /**
     * ocr通用文字识别接口(含位置高精度版)
     *
     * @param path
     * @return
     */
    public JSONObject callAccurateGeneral(String path) {
        init();
        return client.accurateGeneral(path, options);
    }

    /**
     * ocr手写文字识别接口
     *
     * @param path
     * @return
     */
    public JSONObject callHandwriting(String path) {
        init();
        return client.handwriting(path, options);
    }

    /**
     * ocr试卷分析与识别接口
     *
     * @param path
     * @return
     */
    public JSONObject callDocAnalysis(String path) {
        init();
        return client.docAnalysis(path, options);
    }
}