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

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 lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * TODO
 * 百度ocr配置
 * @author crystal
 * @date 2023/10/24 15:59
 */
@Slf4j
@Service
public class BaiduOCRApiService 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();
        BaiduClient client = createClient();
        BaiduClient.IdCard result = client.icOCR(imgUrl);
        // 身份证姓名
        if (StringUtils.isNotEmpty(result.getName())) {
            ocrImgParams.setIdcardName(result.getName());
        }
        // 地址
        if (StringUtils.isNotEmpty(result.getAddress())) {
            ocrImgParams.setIdcardAddress(result.getAddress());
        }
        // 身份证号
        if (StringUtils.isNotEmpty(result.getIdNum())) {
            ocrImgParams.setIdcardNo(result.getIdNum());
        }
        // 有效日期 [日期格式转换]
        if (StringUtils.isNotEmpty(result.getValidDate())) {
            OCRUtil.setIdCardDate(result.getValidDate(), ocrImgParams);
        }
        return ocrImgParams;
    }

    @Override
    public OCRImgParams getBusinessLicenceInfo(String imgUrl) {
        OCRImgParams ocrImgParams = new OCRImgParams();
        BaiduClient client = createClient();
        BaiduClient.BusinessLicence result = client.blOCR(imgUrl);
        // 社会统一代码
        ocrImgParams.setLicenseNo(result.getBusNo());
        // 公司名称
        ocrImgParams.setMchFullName(result.getName());
        // 法人
        ocrImgParams.setLicensePerson(result.getPerson());
        // 地址
        ocrImgParams.setLicenseAddress(result.getAddress());
        // 经营范围
        ocrImgParams.setLicenseBusiness(result.getBusiness());
        //经营范围
        ocrImgParams.setLicenseScope(result.getBusiness());
        // 有效期
        ocrImgParams.setLicenseEffectBegin(result.getStartDate());
        ocrImgParams.setLicenseEffectEnd(result.getEndDate());
        return ocrImgParams;
    }

    @Override
    public OCRImgParams getBankCardInfo(String imgUrl) {
        OCRImgParams ocrImgParams = new OCRImgParams();
        BaiduClient client = createClient();
        BaiduClient.BankCard result = client.bkOCR(imgUrl);
        // 银行卡信息
        ocrImgParams.setSettAccountBankName(result.getBankName());
        // 卡号
        ocrImgParams.setSettAccountNo(result.getCardNo());
        return ocrImgParams;
    }

    private BaiduClient createClient() {
        DBOCRConfig dbocrConfig = getocrConfig();
        BaiduClient client = new BaiduClient(dbocrConfig.getBaiduApiKey(),dbocrConfig.getBaiduSecretKey());
        return client;
    }
}
