package com.jeequan.jeepay.thirdparty.service.ocr;

import com.alibaba.fastjson.JSON;
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.service.impl.SysConfigService;
import com.jeequan.jeepay.thirdparty.util.OCRUtil;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 腾讯云OCR接口
 *
 * @author xiaoyu
 * 
 * @date 2022/1/11 15:31
 */
@Slf4j
@Service
public class TencentOCRApiService implements IOCRApiService {

    @Autowired private SysConfigService sysConfigService;

    private DBOCRConfig getocrConfig() {
        DBOCRConfig dbocrConfig = sysConfigService.getOcrConfig();
        return dbocrConfig;
    }

    @Override
    public OCRImgParams getIdCardInfo(String imgUrl){
        OCRImgParams ocrImgParams = new OCRImgParams();
        try {
            // 创建请求对象
            IDCardOCRRequest request = new IDCardOCRRequest();
            OcrClient client = createClient();
            request.setImageUrl(imgUrl);

            // 发起请求
            log.info("【OCR识别】请求的图片地址：{}", imgUrl);
            IDCardOCRResponse response = client.IDCardOCR(request);
            log.info("【OCR识别】响应结果：{}", JSON.toJSONString(response));

            // 身份证姓名
            if (StringUtils.isNotEmpty(response.getName())) {
                ocrImgParams.setIdcardName(response.getName());
            }
            // 身份证姓名
            if (StringUtils.isNotEmpty(response.getSex())) {
                ocrImgParams.setIdcardSex(response.getSex());
            }
            // 地址
            if (StringUtils.isNotEmpty(response.getAddress())) {
                ocrImgParams.setIdcardAddress(response.getAddress());
            }
            // 身份证号
            if (StringUtils.isNotEmpty(response.getIdNum())) {
                ocrImgParams.setIdcardNo(response.getIdNum());
            }
            // 有效日期 [日期格式转换]
            if (StringUtils.isNotEmpty(response.getValidDate())) {
                String validPeriod = response.getValidDate();
                OCRUtil.setIdCardDate(validPeriod, ocrImgParams);
            }

        }catch (Exception e) {
            log.info("【腾讯云OCR】身份证识别异常", e);
        }
        return ocrImgParams;
    }

    @Override
    public OCRImgParams getBusinessLicenceInfo(String imgUrl) {
        OCRImgParams ocrImgParams = new OCRImgParams();
        try {
            // 创建请求对象
            BizLicenseOCRRequest request = new BizLicenseOCRRequest();
            OcrClient client = createClient();
            request.setImageUrl(imgUrl);

            // 发起请求
            log.info("【OCR识别】请求的图片地址：{}", imgUrl);
            BizLicenseOCRResponse response = client.BizLicenseOCR(request);
            log.info("【OCR识别】响应结果：{}", JSON.toJSONString(response));

            // 社会统一代码
            ocrImgParams.setLicenseNo(response.getRegNum());
            // 公司名称
            ocrImgParams.setMchFullName(response.getName());
            // 法人
            ocrImgParams.setLicensePerson(response.getPerson());
            // 地址
            ocrImgParams.setLicenseAddress(response.getAddress());
            // 经营范围
            ocrImgParams.setLicenseBusiness(response.getBusiness());
            // 有效期
            OCRUtil.setLicenseDate(response.getPeriod(), ocrImgParams);

        }catch (Exception e) {
            log.info("【腾讯云OCR】营业执照识别异常", e);
        }
        return ocrImgParams;
    }

    @Override
    public OCRImgParams getBankCardInfo(String imgUrl) {
        OCRImgParams ocrImgParams = new OCRImgParams();
        try {
            // 创建请求对象
            BankCardOCRRequest request = new BankCardOCRRequest();
            OcrClient client = createClient();
            request.setImageUrl(imgUrl);

            // 发起请求
            log.info("【OCR识别】请求的图片地址：{}", imgUrl);
            BankCardOCRResponse response = client.BankCardOCR(request);
            log.info("【OCR识别】响应结果：{}", JSON.toJSONString(response));

            // 银行卡信息
            ocrImgParams.setSettAccountBankName(response.getBankInfo());
            // 卡号
            ocrImgParams.setSettAccountNo(response.getCardNo());

        }catch (Exception e) {
            log.info("【腾讯云OCR】银行卡识别异常", e);
        }
        return ocrImgParams;
    }

    /**
     * @author: xiaoyu
     * @date: 2022/1/11 15:50
     * @describe: 初始化账号Client
     */
    private OcrClient createClient() {
        // SecretId
        String secretId = getocrConfig().getTencentSecretId();
        // SecretKey
        String secretKey = getocrConfig().getTencentSecretKey();
        Credential cred = new Credential(secretId, secretKey);
        return new OcrClient(cred, "ap-shanghai");
    }
}
