<?php
/**
 *
 * @author：llbjj
 * @DateTime：2022/5/6 10:57
 * @Description：
 *
 */

namespace Application\Service\DB\Ocr;

use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Predicate\Operator;
use Laminas\Stdlib\ArrayUtils;

class Ctcase extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_ocr_ctcase';
        parent::__construct($dbAdapter, $container);
    }

    /**
     * Notes:原始数据拆分
     * User: lltyy
     * DateTime: 2023/9/19 10:27
     *
     * @param $where
     * @param int $limit
     * @param int $offset
     * @param $keyword_arr
     * @return int
     */
    public function mateData($where,$limit=0,$offset=0){
        //获取指定条件下的识别原始数据
        $cond = [
            'where'=>$where,
            'columns'=>['id','content'],
            'order'=>['id']
        ];
        if(!empty($limit) || !empty($offset)){
            $cond['limit'] = $limit;
            $cond['offset'] = $offset;
        }
        $datas = $this->fetchAll($cond);
        $last_id = 0;
        if(!empty($datas)){
            //数据拆分
            foreach($datas as $Key=>$data){
                $last_id = $data['id'];
                $content_str = json_decode($data['content'],true);
                $content_str = implode('',$content_str);

                $this->update(['ocr_data'=>$content_str],'id = '.$data['id']);

                unset($content_str);
                unset($datas[$Key]);
            }
        }
        unset($cond);
        unset($datas);

        return $last_id;
    }


    /**
     * Notes: CT原始数据
     * User: lltyy
     * DateTime: 2023/9/19 9:25
     *
     * @param array $where
     */
    public function scanData(array $where = []){
        if(empty($where)) {
            $where = [
                'is_del' => 0,
            ];
        }
        $sig_id_arr = [];
        if(!empty($sig_id_arr)){
            $where['sig_id'] = StringHelper::toArray($sig_id_arr);
        }

        $annex_id_arr = [];
        if(!empty($annex_id_arr)){
            $where['annex_id'] = StringHelper::toArray($annex_id_arr);
        }
        $bool = true;
        $page = 0;
        $limit = 1000;
        $last_id = 0;
        while ($bool) {
            $page++;
            echo "ctcasePAGE {$page}：" . PHP_EOL;

            $last_id = $this->mateData(ArrayUtils::merge($where, [
                new Operator('id', Operator::OP_GT, $last_id),
            ]), $limit,0);
            if(!$last_id) $bool = false;
        }
    }
}