package org.springblade.file.service.impl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.CollectionUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.file.convert.OCRConverter;
import org.springblade.file.pojo.dto.FileCertificateBatchRecognitionDTO;
import org.springblade.file.service.IOCRConvertService;
import org.springblade.thirdparty.ocr.pojo.dto.CertificateBatchRecognitionDTO;
import org.springblade.thirdparty.ocr.pojo.vo.TransportCertificateVO;
import org.springblade.thirdparty.ocr.service.IOCRService;
import org.springframework.stereotype.Service;

/**
 * @author bfhuange
 * @since 2025/3/13
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class OCRConvertServiceImpl implements IOCRConvertService {

    private final IOCRService ocrService;
    private final OCRConverter converter;

    @Override
    public TransportCertificateVO recognitionTransportCertificate(FileCertificateBatchRecognitionDTO param) {
        Func.requireNotNull(param, "参数不能为空");
        if (CollectionUtil.isEmpty(param.getObjectKeys())) {
            throw new ServiceException("图像 URL 列表不能为空");
        }
        CertificateBatchRecognitionDTO ocrParam = converter.dto2ocr(param);
        ocrParam.setPoundUrls(param.getPoundUrls());
        ocrParam.setCarFrontUrls(param.getCarFrontUrls());
        ocrParam.setDriverLicenseUrls(param.getDriverLicenseUrls());
        ocrParam.setVehicleLicenseUrls(param.getVehicleLicenseUrls());
        if (!hasTypedUrls(param)) {
            ocrParam.setUrls(param.getObjectKeys());
        } else {
            ocrParam.setUrls(null);
        }
        return ocrService.recognitionTransportCertificate(ocrParam);
    }

    private boolean hasTypedUrls(FileCertificateBatchRecognitionDTO param) {
        return CollectionUtil.isNotEmpty(param.getPoundUrls())
                || CollectionUtil.isNotEmpty(param.getCarFrontUrls())
                || CollectionUtil.isNotEmpty(param.getDriverLicenseUrls())
                || CollectionUtil.isNotEmpty(param.getVehicleLicenseUrls());
    }

}
