package com.jeequan.jeepay.service.impl;

import com.jeequan.jeepay.core.constants.CS;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.core.interfaces.IOCRApiService;
import com.jeequan.jeepay.core.model.DBOCRConfig;
import com.jeequan.jeepay.core.model.OCRImgParams;
import com.jeequan.jeepay.core.utils.SpringBeansUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author xiaoyu
 * @date 2022/1/12 15:21
 */
@Service
public class ImgInfoService {

    @Autowired private SysConfigService sysConfigService;

    public OCRImgParams getImgInfo(String imgUrl, String type) {
        OCRImgParams ocrImgParams = new OCRImgParams();
        // OCR配置
        DBOCRConfig ocrConfig = sysConfigService.getOcrConfig();
        // 是否开启了ocr图片识别
        if (ocrConfig.getOcrState() == CS.NO) {
            return ocrImgParams;
        }

        // 当前OCR开启类型
        byte ocrType = ocrConfig.getOcrType();
        String ocrTypeStr = "";
        if (DBOCRConfig.OCR_TYPE_TENCENT == ocrType) {
            ocrTypeStr = DBOCRConfig.OCR_TENCENT;
        }else if (DBOCRConfig.OCR_TYPE_ALI == ocrType) {
            ocrTypeStr = DBOCRConfig.OCR_ALI;
        }else if (DBOCRConfig.OCR_TYPE_BAIDU == ocrType) {
            ocrTypeStr = DBOCRConfig.OCR_BAIDU;
        }

        IOCRApiService ocrApiService = SpringBeansUtil.getBean( ocrTypeStr + "OCRApiService", IOCRApiService.class);

        // 身份证信息
        if (DBOCRConfig.OCR_TYPE_ID_CARD.equals(type)) {
            ocrImgParams = ocrApiService.getIdCardInfo(imgUrl);

        //  银行卡信息
        }else if (DBOCRConfig.OCR_TYPE_BANK_CARD.equals(type)) {
            ocrImgParams = ocrApiService.getBankCardInfo(imgUrl);

        // 营业执照信息
        }else if (DBOCRConfig.OCR_TYPE_LICENSE.equals(type)) {
            ocrImgParams = ocrApiService.getBusinessLicenceInfo(imgUrl);

        }
        if (ocrImgParams == null) {
            throw new BizException("图片识别异常，请联系管理员");
        }
        return ocrImgParams;
    }

}
