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

namespace Application\Service\DB\Collect;

use Interop\Container\ContainerInterface;

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


    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2023/4/25 10:16
     *
     * @param $where
     * @param $where
     * @return array
     */
    public function fetchDataById($where,$columns){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoData){
                $id = $infoData['id'];
                $datas[$id] = $infoData;
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }


    /**
     * Notes:获取指定的数据，存入匹配信息表
     * User: lltyy
     * DateTime: 2023/5/26 15:00
     *
     * @param $where//条件
     * @param int $limit
     * @param int $offset
     * @param $word_arr//入院记录
     * @return int
     */
    public function mateData($where,$limit=0,$offset=0){
        //获取指定条件下的医嘱数据
        $cond = [
            'where'=>$where,
            'columns'=>['id','ocr_content'],
            'order'=>['id']
        ];
        if(!empty($limit) || !empty($offset)){
            $cond['limit'] = $limit;
            $cond['offset'] = $offset;
        }
        $datas = $this->fetchAll($cond);

        $last_id = 0;
        if(!empty($datas)){
            $delete_mate_arr = [];
            foreach($datas as $data_info){
                $last_id = $data_info['id'];
                $delete_mate_arr[] = $data_info['id'];
            }

            //删除原匹配数据
            $this->update(['mate_type'=>0,'is_lock'=>0,'content'=>json_encode([])],[
                'is_del' => 0,
                'id' => $delete_mate_arr,
            ]);
            unset($delete_mate_arr);

            $delete_str_arr = [":",",",".","·"];
            foreach ($datas as $dataKey=>$data) {
                $info_id = $data['id'];
                $ocr_content_arr = json_decode($data['ocr_content'],true);
                if(isset($ocr_content_arr['collect_date'])){
                    $ocr_content_arr['CheckDate'] = !empty($ocr_content_arr['collect_date']) ? $ocr_content_arr['collect_date'] : '';
                    unset($ocr_content_arr['collect_date']);
                }
                if(isset($ocr_content_arr['result'])){
                    $ocr_content_arr['Result'] = $ocr_content_arr['result'];
                    unset($ocr_content_arr['result']);
                }
                $up_data = [
                    'mate_type'=>1,
                    'content'=>json_encode($ocr_content_arr,true),
                    'is_lock'=>0
                ];
                $this->update($up_data, 'id = ' . $info_id);
                unset($datas[$dataKey]);
            }
        }
        unset($cond);
        unset($datas);

        return $last_id;
    }


    /**
     * Notes:识别数据 - 匹配
     * User: lltyy
     * DateTime: 2023/5/12 14:51
     *
     */
    public function scanData($where='') {
        //信息匹配
        if(empty($where)){
            $where = [
                'is_del' => 0,
                'is_lock' => 0,
            ];
        }

        $sig_id_arr = [];
        if(!empty($sig_id_arr)){
            $where['itemsig_id'] = $sig_id_arr;
        }
        $bool = true;
        $page = 0;
        $limit = 1000;
        $last_id = 0;
        while ($bool) {
            $page++;
            echo "ctPAGE {$page}：" . PHP_EOL;
            $last_id = $this->mateData($where.' and id > '.$last_id,$limit,0);
            if(!$last_id) $bool = false;
        }
        unset($wordDatas);
    }


    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2023/4/25 10:16
     *
     * @param $where
     * @param $where
     * @return array
     */
    public function fetchDataByKey($key,$is_more=0,$where,$columns){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoData){
                $key_str = $infoData[$key];
                if($is_more == 1){
                    $datas[$key_str][] = $infoData;
                }else{
                    $datas[$key_str] = $infoData;
                }
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }

}