<?php

namespace Application\Controller;
use Application\Common\StatusCode;
use Application\Form\project\AeForm;
use Application\Mvc\Controller\BasicController;
use Application\Common\Com;
use Application\Service\Log;
use Application\Common\Ocr;
use Laminas\Db\Sql\Predicate\Operator;
use Laminas\Mvc\Controller\AbstractActionController;

class CrontabController extends AbstractActionController {
    private $ossPath = 'http://yikeen.oss-cn-beijing.aliyuncs.com';

    //批量识别数据
    public function manyReidentificationAction()
    {
        //附件===未锁定
        $whereItemPatientworkannex['where'] = [
            'lock_state' => 0,
            'ocr_state' => 0,
            'confirm_state' => 0,
            'is_result' => 0,
            'annex_type' => 1,
            new Operator('ocr_id', Operator::OP_NE, 0)
        ];
        $itemPatientworkannexData = $this->LocalService()->itemPatientworkannex->fetchAll($whereItemPatientworkannex);
        if (empty($itemPatientworkannexData)){
            return false;
        }
        $dictionarySetocrfieldData = $this->LocalService()->dictionarySetocrfield->getAllGroupType();
        foreach ($itemPatientworkannexData as $itemPatientworkannexDataKey=>$itemPatientworkannexDataValue) {
            //识别数据
            $whereItemIdentificationresult['where'] = ['is_del' => 0, 'workannex_id' => $itemPatientworkannexDataValue['id']];
            $itemIdentificationresultInfo = $this->LocalService()->itemIdentificationresult->fetchOne($whereItemIdentificationresult);
            //识别模板设置数据
            $whereDictionaryOcr['where'] = ['is_del' => 0, 'id' => $itemPatientworkannexDataValue['ocr_id']];
            $whereDictionaryOcr['columns'] = ['id','name','key_val','separator'];
            $dictionaryOcrInfo = $this->LocalService()->dictionaryOcr->fetchOne($whereDictionaryOcr);
            if (empty($dictionaryOcrInfo) || empty($dictionaryOcrInfo['key_val']) || empty($dictionaryOcrInfo['separator']) || empty($itemPatientworkannexDataValue['annex_path'])){
                continue;
            }
            $imgPath = $this->ossPath.$itemPatientworkannexDataValue['annex_path'];
            $result = Ocr::ocr_result($imgPath, $dictionaryOcrInfo['key_val'], $dictionaryOcrInfo['separator'], $dictionarySetocrfieldData);
            if ($result['error_code'] != 0) {
                continue;
            }
            $upWorkannex['ocr_state'] = 1;
            $upWorkannex['is_result'] = 1;
            $upWorkannex['confirm_state'] = 1;
            $upWorkannex['id'] = $itemPatientworkannexDataValue['id'];
            $resultItemPatientworkannex = $this->LocalService()->itemPatientworkannex->save($upWorkannex);
            $arr['content'] = json_encode($result);
            $arr['workannex_id'] = $itemPatientworkannexDataValue['id'];
            if (!empty($itemIdentificationresultInfo)){
                $resultId = $arr['id'] = $itemIdentificationresultInfo['id'];
                $resultItemIdentificationresult = $this->LocalService()->itemIdentificationresult->save($arr);
            }else{
                $resultId = $this->LocalService()->itemIdentificationresult->save($arr);
            }

        }
        return true;




    }
}