<?php

namespace app\api\controller;

use app\api\model\UploadFile;
use cores\exception\BaseException;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\response\Json;

class Ocr extends Controller
{
    /**
     * @notes:身份证识别
     * @return Json
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: wanghousheng
     */
    public function checkCard(): Json
    {
        $img_id = intval($this->request->post('img_id'));
        if (!$img_id) {
            return $this->renderError('图片ID不能为空');
        }
        $img_url = '';
        $file = UploadFile::detail($img_id);
        if (!empty($file)) {
            $file = $file->toArray();
            $img_url = $file['preview_url'];
        }
        if (!$img_url) {
            return $this->renderError('图片不存在');
        }
        $type = intval($this->request->post('type', 1));
        $result = \app\api\service\Ocr::checkCard($img_url, $type);
        if (!empty($result['status']) && !empty($result['data'])) {
            return $this->renderSuccess($result['data']);
        }
        return $this->renderError($result['msg']);
    }

    /**
     * @notes:营业执照识别
     * @return Json
     * @throws BaseException
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @author: wanghousheng
     */
    public function checkLicense(): Json
    {
        $img_id = intval($this->request->post('img_id'));
        if (!$img_id) {
            return $this->renderError('图片ID不能为空');
        }
        $img_url = '';
        $file = UploadFile::detail($img_id);
        if (!empty($file)) {
            $file = $file->toArray();
            $img_url = $file['preview_url'];
        }
        if (!$img_url) {
            return $this->renderError('图片不存在');
        }
        $result = \app\api\service\Ocr::checkLicense($img_url);
        if (!empty($result['status']) && !empty($result['data'])) {
            return $this->renderSuccess($result['data']);
        }
        return $this->renderError($result['msg']);
    }
}