<?php
namespace Application\Service\DB\Item;

use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Predicate\Operator;
use Laminas\Stdlib\ArrayUtils;

class Detectpatient extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        if (!$this->tableName) {
            $this->tableName = 'yikeen_item_detectpatient';
        }

        parent::__construct($dbAdapter, $container);
    }

    /**
     * Notes:获取指定条件下的数据
     * User: lltyy
     * DateTime: 2024/5/15 9:20
     *
     * @param $where 条件
     * @param $columns 字段
     * @param string $key 键值
     * @param int $is_more 是否有多笔数据【0 否 1 是】
     * @return array
     */
    public function fetchDataByKey($where,$columns,$key='id',$is_more = 0){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>['id']
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoKey=>$infoData){
                $data_key = isset($infoData[$key]) && !empty($infoData[$key]) ? $infoData[$key] : '';
                if($is_more == 1){
                    $datas[$data_key][] = $infoData;
                }else{
                    $datas[$data_key] = $infoData;
                }
                unset($infoDatas[$data_key]);
            }
        }
        unset($cond);
        unset($infoDatas);

        return $datas;
    }

    /**
     * Notes: 重点事件监控
     * User: lltyy
     * DateTime: 2024/8/28 11:02
     *
     * @param array $params
     */
    public function scanName(array $params){
        $type = isset($params['type']) && !empty($params['type']) ? $params['type'] : 0;
        $item_id = isset($params['item_id']) && !empty($params['item_id']) ? $params['item_id'] : 0;
        $itemsig_id = isset($params['itemsig_id']) && !empty($params['itemsig_id']) ? $params['itemsig_id'] : 0;
        $patient_id = isset($params['patient_id']) && !empty($params['patient_id']) ? $params['patient_id'] : 0;
        $name_datas = isset($params['name_data']) && !empty($params['name_data']) ? $params['name_data'] : [];
        $name_id = isset($params['name_id']) && !empty($params['name_id']) ? $params['name_id'] : 0;
        if(empty($name_datas)){
            $name_where = [
                'is_del' => 0,
            ];
            if(!empty($item_id)){
                $name_where['item_id'] = $item_id;
            }
            if(!empty($name_id)){
                $name_where['id'] = $name_id;
            }
            $name_datas = $this->LocalService()->dictionaryDetectname->fetchAll([
                'where'=>$name_where,
                'columns'=>['id','name','cut_keyword','cut_type','must_keyword','informal_keyword','name_remark'],
                'order'=>['name_order']
            ]);
            unset($name_where);
            if(!empty($name_datas)){
                //查询关键字数组
                $keywords_arr = $this->LocalService()->dictionaryDetectkeyword->getFiledRelation([
                    'is_del' => 0,
                    'name_id' => array_values(array_column($name_datas,'id')),
                ], ['id', 'name','name_id']);
                //查询黑名单数组
                $blackword_arr = $this->LocalService()->dictionaryDetectblackword->getFiledRelation([
                    'is_del' => 0,
                    'name_id' => array_values(array_column($name_datas,'id')),
                ], ['id', 'name','name_id']);
                foreach($name_datas as &$name_data){
                    $name_data['informal_keyword'] = json_decode($name_data['informal_keyword'],true);
                    $name_data['must_keyword'] = json_decode($name_data['must_keyword'],true);
                    $name_data['keyword'] = isset($keywords_arr[$name_data['id']]) && !empty($keywords_arr[$name_data['id']])  ? $keywords_arr[$name_data['id']] : [];
                    $name_data['blackword'] = isset($blackword_arr[$name_data['id']]) && !empty($blackword_arr[$name_data['id']])  ? $blackword_arr[$name_data['id']] : [];
                    if(!empty($name_data['must_keyword'])){
                        $name_data['keyword'] = !empty($name_data['keyword']) ? array_merge($name_data['must_keyword'],$name_data['keyword']) : $name_data['must_keyword'];
                    }
                    $name_data['keyword'] = array_values(array_unique($name_data['keyword']));
                    unset($name_data['must_keyword']);
                }
                unset($keywords_arr);
                unset($blackword_arr);
            }
        }

        $where = [
            'is_del' => 0,
        ];
        if($type == 1){
            $where = [
                'is_del' => 0,
                'is_count' => 1,
                new Operator('pid', Operator::OP_GT, 0),
            ];
        }
        if(!empty($item_id)){
            $where['item_id'] = $item_id;
        }
        if(!empty($itemsig_id)){
            if($type == 1){
                $where['pid'] = $itemsig_id;
            }else{
                $where['itemsig_id'] = $itemsig_id;
            }
        }
        if(!empty($patient_id)){
            $where['id'] = $patient_id;
        }

        //获取附件
        $annexs = [
            'lockCt'=>[],//已锁定影像
            'loseCt'=>[],//弃用影像
            'noCategory'=>[],//非【化验单、医嘱、影像、首页】类数据
            'lockCost'=>[],//未弃用首页
            'lockCostNew'=>[],//第二批数据未弃用首页
            'noCategoryNew'=>[],//第二批数据非【化验单、医嘱、影像、首页】类数据
            'annexType'=>[],//附件类型
            'newannexType'=>[]//第二批数据附件类型
        ];
        //第一批附件
        $annexDatas = $this->LocalService()->ocrOcrannex->fetchAll([
            'where'=>'is_del = 0 and (annex_type in (3,17,18,16,8) or annex_type not in (1,2,3,17,18,16,8))',
            'columns'=>['id','is_lock','annex_type']
        ]);
        if(!empty($annexDatas)){
            foreach($annexDatas as $annexKey=>$annexData){
                $annexs['annexType'][$annexData['id']] = $annexData['annex_type'];
                if(!in_array($annexData['annex_type'],[3,8,17,18,16])){
                    $annexs['noCategory'][] = $annexData['id'];
                }else{
                    if(in_array($annexData['annex_type'],[3,17,18,16])){
                        if($annexData['is_lock'] == 1){
                            $annexs['lockCt'][] = $annexData['id'];
                        }else{
                            $annexs['loseCt'][] = $annexData['id'];
                        }
                    }
                    if(in_array($annexData['annex_type'],[8])){
                        $annexs['lockCost'][] = $annexData['id'];
                    }
                }
                unset($annexDatas[$annexKey]);
            }
        }
        unset($annexDatas);

        //第二批附件
        $annexNewDatas = $this->LocalService()->ocrOcrannexnew->fetchAll([
            'where'=>'is_del = 0 and (annex_type in (8) or annex_type not in (1,2,3,17,18,16,8))',
            'columns'=>['id','is_lock','annex_type']
        ]);
        if(!empty($annexNewDatas)){
            foreach($annexNewDatas as $annexNewKey=>$annexNewData){
                $annexs['newannexType'][$annexNewData['id']] = $annexNewData['annex_type'];
                if(!in_array($annexNewData['annex_type'],[8])){
                    $annexs['noCategoryNew'][] = $annexNewData['id'];
                }else{
                    $annexs['lockCostNew'][] = $annexNewData['id'];
                }
                unset($annexNewData[$annexNewKey]);
            }
        }
        unset($annexNewDatas);

        //中心ID
        $itemsig_arr = [];
        if($type == 1){
            $itemsig_arr = $this->LocalService()->ocrSigpatient->fetchCol('pid',$where);
            $itemsig_arr = !empty($itemsig_arr) ? array_values(array_unique(array_values($itemsig_arr))) : [];
            if(!empty($itemsig_arr)){
                $this->LocalService()->ocrSigpatient->update(['scan_state'=>0],'id in ('.implode(',',$itemsig_arr).')');
            }
        }

        $bool = true;
        $page = 0;
        $limit = 100;
        $last_id = 0;
        while ($bool) {
            $page++;
            //echo "CountPAGE {$page}：" . PHP_EOL;
            $last_id = $this->updateSerchCount(ArrayUtils::merge($where, [
                new Operator('id', Operator::OP_GT, $last_id)
            ]),$limit,0,$type,$name_datas,$annexs);
            if(!$last_id){
                $bool = false;
                //更新中心匹配状态
                if(!empty($itemsig_arr) && $type == 1){
                    $this->LocalService()->ocrSigpatient->update(['scan_state'=>1],[
                        'id' => $itemsig_arr
                    ]);
                }
            }
        }
        unset($where);
        unset($annexs);
        unset($itemsig_arr);
    }

    /**
     * Notes: 获取指定条件下的检索数据，存入受试者重点事件监控信息表
     * User: lltyy
     * DateTime: 2024/8/28 9:31
     *
     * @param $where
     * @param int $limit
     * @param int $offset
     * @param $name_data
     * @return int
     */
    public function updateSerchCount($where,$limit=0,$offset=0,&$type,&$name_datas,&$annexs){

        $patientTable = $this->LocalService()->patient;
        $columns = ['id','item_id','itemsig_id'];
        if($type == 1){
            $patientTable = $this->LocalService()->ocrSigpatient;
            $columns = ['id','item_id','itemsig_id'=>'pid'];
        }

        //获取指定条件下的患者数据
        $cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>['id']
        ];
        if(!empty($limit)){
            $cond['limit'] = $limit;
            $cond['offset'] = $offset;
        }
        $datas = $patientTable->fetchAll($cond);
        $last_id = 0;
        if(!empty($datas) && !empty($name_datas)){
            $patient_id_arr = array_values(array_column($datas,'id'));

            //获取已锁定影像识别数据
            $patientLockCtOcrDatas = [];
            if(!empty($annexs['lockCt'])){
                $ctOcrDatas = $this->LocalService()->ocrCtcase->fetchAll([
                    'where'=> [
                        'is_del' => 0,
                        'annex_id' => $annexs['lockCt'],
                        'patient_id' => $patient_id_arr
                    ],
                    'columns'=>['annex_id','patient_id','ocr_data'=>'ocr_new_data']
                ]);
                if(!empty($ctOcrDatas)){
                    foreach($ctOcrDatas as $ctOcrDataKey=>$ctOcrData){
                        $patientLockCtOcrDatas[$ctOcrData['patient_id']][] = $ctOcrData;
                        unset($ctOcrDatas[$ctOcrDataKey]);
                    }
                }
                unset($ctOcrDatas);
            }

            //获取已锁定首页\弃用影像\未分类附件识别数据
            $patientLockCostOcrDatas = $patientLoseCtOcrDatas = $patientNocategoryOcrDatas = [];
            if(!empty($annexs['lockCost']) || !empty($annexs['loseCt']) || !empty($annexs['noCategory'])){
                $caseWhere = [];
                if(!empty($annexs['lockCost'])){
                    $caseWhere[] = [
                        'is_new_ocr' => 0,
                        'annex_id' => $annexs['lockCost'],
                    ];
                }
                if(!empty($annexs['loseCt'])){
                    $caseWhere[] = [
                        'is_new_ocr' => 0,
                        'annex_id' => $annexs['loseCt'],
                    ];
                }
                if(!empty($annexs['noCategory'])){
                    $caseWhere[] = [
                        'is_new_ocr' => 0,
                        'annex_id' => $annexs['noCategory'],
                    ];
                }
                $caseOcrDatas = $this->LocalService()->itemOcrCsae->fetchAll([
                    'where'=> [
                        'is_del' => 0,
                        'patient_id' => $patient_id_arr,
                        [
                            'or' => $caseWhere,
                        ]
                    ],
                    'columns'=>['annex_id','is_new_ocr','patient_id','ocr_data'=>'ocr_new_data']
                ]);
                unset($caseWhere);
                if(!empty($caseOcrDatas)){
                    foreach($caseOcrDatas as $caseOcrDataKey=>&$caseOcrData){
                        $caseOcrData_newocr = $caseOcrData['is_new_ocr'];
                        unset($caseOcrData['is_new_ocr']);
                        if($caseOcrData_newocr == 1){
                            if(in_array($caseOcrData['annex_id'],$annexs['loseCt'])){
                                $patientLoseCtOcrDatas[$caseOcrData['patient_id']][] = $caseOcrData;
                            }
                            if(in_array($caseOcrData['annex_id'],$annexs['noCategory'])){
                                $patientNocategoryOcrDatas[$caseOcrData['patient_id']][] = $caseOcrData;
                            }
                        }else{
                            $patientLockCostOcrDatas[$caseOcrData['patient_id']][] = $caseOcrData;
                        }
                        unset($caseOcrDatas[$caseOcrDataKey]);
                    }
                }
                unset($caseOcrDatas);
            }

            //获取第二批首页\未分类附件识别数据
            $patientLockCostnewOcrDatas = $patientNocategorynewOcrDatas = [];
            if(!empty($annexs['lockCostNew']) || !empty($annexs['noCategoryNew'])){
                $casenew_annex = !empty($annexs['lockCostNew']) ? $annexs['lockCostNew'] : [];
                if(!empty($annexs['noCategoryNew'])){
                    $casenew_annex = !empty($casenew_annex) ? array_merge($casenew_annex,$annexs['noCategoryNew']) : $annexs['noCategoryNew'];
                }
                $caseOcrnewDatas = $this->LocalService()->itemOcrCsaeNew->fetchAll([
                    'where'=> [
                        'is_del' => 0,
                        'annex_id' => $casenew_annex,
                        'patient_id' => $patient_id_arr,
                    ],
                    'columns'=>['annex_id','patient_id','ocr_data'=>'ocr_new_data']
                ]);
                unset($casenew_annex);
                if(!empty($caseOcrnewDatas)){
                    foreach($caseOcrnewDatas as $caseOcrnewDataKey=>$caseOcrnewData){
                        if(in_array($caseOcrnewData['annex_id'],$annexs['lockCostNew'])){
                            $patientLockCostnewOcrDatas[$caseOcrnewData['patient_id']][] = $caseOcrnewData;
                        }
                        if(in_array($caseOcrnewData['annex_id'],$annexs['noCategoryNew'])){
                            $patientNocategorynewOcrDatas[$caseOcrnewData['patient_id']][] = $caseOcrnewData;
                        }
                        unset($caseOcrnewDatas[$caseOcrnewDataKey]);
                    }
                }
                unset($caseOcrnewDatas);
            }


            //获取已存储锁定的数据
            $patient_name_old = [];
            $old_datas = $this->fetchAll([
                'where'=> [
                    'is_del' => 0,
                    'is_lock' => 1,
                    'name_id' => array_values(array_column($name_datas,'id')),
                    'patient_id' => $patient_id_arr,
                ],
                'columns'=>['id','name_id','patient_id']
            ]);
            if(!empty($old_datas)){
                foreach($old_datas as $old_datakey=>$old_data){
                    $patient_name_old[$old_data['patient_id']][$old_data['name_id']] = $old_data['id'];
                    unset($old_datas[$old_datakey]);
                }
            }
            unset($old_datas);

            //删除该重点事件监控的患者信息
            $this->update(['is_del'=>1],[
                'is_del' => 0,
                'is_lock' => 0,
                'name_id' => array_values(array_column($name_datas,'id')),
                'patient_id' => $patient_id_arr,
            ]);
            unset($patient_id_arr);
            foreach($datas as $dataKey=>$data){
                $last_id = $data['id'];
                $lockCts = isset($patientLockCtOcrDatas[$data['id']]) && !empty($patientLockCtOcrDatas[$data['id']]) ? $patientLockCtOcrDatas[$data['id']] : [];
                $loseCts = isset($patientLoseCtOcrDatas[$data['id']]) && !empty($patientLoseCtOcrDatas[$data['id']]) ? $patientLoseCtOcrDatas[$data['id']] : [];
                $lockCosts = isset($patientLockCostOcrDatas[$data['id']]) && !empty($patientLockCostOcrDatas[$data['id']]) ? $patientLockCostOcrDatas[$data['id']] : [];
                $lockCostnews = isset($patientLockCostnewOcrDatas[$data['id']]) && !empty($patientLockCostnewOcrDatas[$data['id']]) ? $patientLockCostnewOcrDatas[$data['id']] : [];
                $noCategorys = isset($patientNocategoryOcrDatas[$data['id']]) && !empty($patientNocategoryOcrDatas[$data['id']]) ? $patientNocategoryOcrDatas[$data['id']] : [];
                $noCategorynews = isset($patientNocategorynewOcrDatas[$data['id']]) && !empty($patientNocategorynewOcrDatas[$data['id']]) ? $patientNocategorynewOcrDatas[$data['id']] : [];
                foreach($name_datas as $name_data){
                    $old_id = isset($patient_name_old[$data['id']]) && isset($patient_name_old[$data['id']][$name_data['id']]) ? $patient_name_old[$data['id']][$name_data['id']] : 0;
                    if(empty($old_id)){
                        $is_serch_ct = $is_serch_cost = 0;
                        $serchOtherAnnex = [];
                        if(strpos($name_data['name_remark'],'CT') !== false){
                            $is_serch_ct = 1;
                            $serchOtherAnnex = !empty($loseCts) ? $loseCts : [];
                        }else{
                            $serchOtherAnnex = !empty($noCategorys) ? $noCategorys : [];
                            if(!empty($loseCts)){
                                $serchOtherAnnex = !empty($serchOtherAnnex) ? array_merge($loseCts,$serchOtherAnnex) : $loseCts;
                            }
                        }
                        if(strpos($name_data['name_remark'],'首页') !== false){
                            $is_serch_cost = 1;
                        }
                        //查询监控附件
                        $serch_annex = $serch_annex_new = [];
                        $serch_keyword = [];//中标关键词
                        $serch_annex_type = [];//中标附件类型

                        //锁定影像数据
                        if($is_serch_ct == 1 || ($is_serch_ct == 0 && $is_serch_cost == 0)) {
                            //已锁定CT信息
                            $ctDatas = $this->scanOcrKeyword($lockCts, $name_data);
                            if (isset($ctDatas['serch_annex']) && !empty($ctDatas['serch_annex'])) {
                                $serch_annex = !empty($serch_annex) ? array_merge($serch_annex, $ctDatas['serch_annex']) : $ctDatas['serch_annex'];
                            }
                            if (isset($ctDatas['serch_keyword']) && !empty($ctDatas['serch_keyword'])) {
                                $serch_keyword = !empty($serch_keyword) ? array_merge($serch_keyword, $ctDatas['serch_keyword']) : $ctDatas['serch_keyword'];
                            }
                            if (isset($ctDatas['serch_annex_type']) && !empty($ctDatas['serch_annex_type'])) {
                                $serch_annex_type = !empty($serch_annex_type) ? array_merge($serch_annex_type, $ctDatas['serch_annex_type']) : $ctDatas['serch_annex_type'];
                            }
                            unset($ctDatas);
                        }

                        //首页数据
                        if($is_serch_cost == 1 || ($is_serch_ct == 0 && $is_serch_cost == 0)) {
                            //已锁定首页信息
                            $costDatas = $this->scanOcrKeyword($lockCosts,$name_data);
                            if(isset($costDatas['serch_annex']) && !empty($costDatas['serch_annex'])){
                                $serch_annex = !empty($serch_annex) ? array_merge($serch_annex,$costDatas['serch_annex']) : $costDatas['serch_annex'];
                            }
                            if(isset($costDatas['serch_keyword']) && !empty($costDatas['serch_keyword'])){
                                $serch_keyword = !empty($serch_keyword) ? array_merge($serch_keyword,$costDatas['serch_keyword']) : $costDatas['serch_keyword'];
                            }
                            if(isset($costDatas['serch_annex_type']) && !empty($costDatas['serch_annex_type'])){
                                $serch_annex_type = !empty($serch_annex_type) ? array_merge($serch_annex_type,$costDatas['serch_annex_type']) : $costDatas['serch_annex_type'];
                            }
                            unset($costDatas);

                            //已锁定新首页信息
                            $costNewDatas = $this->scanOcrKeyword($lockCostnews,$name_data);
                            if(isset($costNewDatas['serch_annex']) && !empty($costNewDatas['serch_annex'])){
                                $serch_annex_new = !empty($serch_annex_new) ? array_merge($serch_annex_new,$costNewDatas['serch_annex']) : $costNewDatas['serch_annex'];
                            }
                            if(isset($costNewDatas['serch_keyword']) && !empty($costNewDatas['serch_keyword'])){
                                $serch_keyword = !empty($serch_keyword) ? array_merge($serch_keyword,$costNewDatas['serch_keyword']) : $costNewDatas['serch_keyword'];
                            }
                            if(isset($costNewDatas['serch_annex_type']) && !empty($costNewDatas['serch_annex_type'])){
                                $serch_annex_type = !empty($serch_annex_type) ? array_merge($serch_annex_type,$costNewDatas['serch_annex_type']) : $costNewDatas['serch_annex_type'];
                            }
                            unset($costNewDatas);
                         }

                        //查找其他数据【未分类、弃用影像数据】
                        if($is_serch_ct == 1 || ($is_serch_ct == 0 && $is_serch_cost == 0)) {
                            $otherDatas = $this->scanOcrKeyword($serchOtherAnnex, $name_data);
                            if (isset($otherDatas['serch_annex']) && !empty($otherDatas['serch_annex'])) {
                                $serch_annex = !empty($serch_annex) ? array_merge($serch_annex, $otherDatas['serch_annex']) : $otherDatas['serch_annex'];
                            }
                            if (isset($otherDatas['serch_keyword']) && !empty($otherDatas['serch_keyword'])) {
                                $serch_keyword = !empty($serch_keyword) ? array_merge($serch_keyword, $otherDatas['serch_keyword']) : $otherDatas['serch_keyword'];
                            }
                            if (isset($otherDatas['serch_annex_type']) && !empty($otherDatas['serch_annex_type'])) {
                                $serch_annex_type = !empty($serch_annex_type) ? array_merge($serch_annex_type, $otherDatas['serch_annex_type']) : $otherDatas['serch_annex_type'];
                            }
                            unset($otherDatas);
                        }

                        //新未分类信息
                        if($is_serch_ct == 0 && $is_serch_cost == 0){
                            $noCategorynewDatas = $this->scanOcrKeyword($noCategorynews,$name_data);
                            if(isset($noCategorynewDatas['serch_annex']) && !empty($noCategorynewDatas['serch_annex'])){
                                $serch_annex_new = !empty($serch_annex_new) ? array_merge($serch_annex_new,$noCategorynewDatas['serch_annex']) : $noCategorynewDatas['serch_annex'];
                            }
                            if(isset($noCategorynewDatas['serch_keyword']) && !empty($noCategorynewDatas['serch_keyword'])){
                                $serch_keyword = !empty($serch_keyword) ? array_merge($serch_keyword,$noCategorynewDatas['serch_keyword']) : $noCategorynewDatas['serch_keyword'];
                            }
                            if(isset($noCategorynewDatas['serch_annex_type']) && !empty($noCategorynewDatas['serch_annex_type'])){
                                $serch_annex_type = !empty($serch_annex_type) ? array_merge($serch_annex_type,$noCategorynewDatas['serch_annex_type']) : $noCategorynewDatas['serch_annex_type'];
                            }
                            unset($noCategorynewDatas);
                        }

                        $serch_keyword = !empty($serch_keyword) ? array_values(array_unique($serch_keyword)) : [];
                        $serch_annex_type = !empty($serch_annex_type) ? array_values(array_unique($serch_annex_type)) : [];
                        if(empty($serch_annex) && empty($serch_annex_new)){
                            $serch_keyword = $serch_annex_type = [];
                        }
                        $save_data = [
                            'item_id'=>$data['item_id'],
                            'itemsig_id'=>$data['itemsig_id'],
                            'patient_id'=>$data['id'],
                            'name_id'=>$name_data['id'],
                            'name_keyword'=>json_encode($name_data['keyword'],true),
                            'name_blackword'=>json_encode($name_data['blackword'],true),
                            'name_remark'=>$name_data['name_remark'],
                            'is_have'=>!empty($serch_annex) || !empty($serch_annex_new) ? 1 : 0,
                            'have_annextype'=>json_encode($serch_annex_type,true),
                            'have_keyword'=>json_encode($serch_keyword,true),
                            'have_annex'=>json_encode($serch_annex,true),
                            'have_annex_new'=>json_encode($serch_annex_new,true),
                            'is_lock'=>0
                        ];
                        $this->save($save_data);

                        //释放空间
                        unset($save_data);
                        unset($serch_annex);
                        unset($serch_annex_new);
                        unset($serchOtherAnnex);
                        unset($is_serch_ct);
                        unset($is_serch_cost);
                    }

                    //释放空间
                    unset($old_id);
                }

                //释放空间
                unset($lockCts);
                unset($loseCts);
                unset($lockCosts);
                unset($lockCostnews);
                unset($noCategorys);
                unset($noCategorynews);
                unset($datas[$dataKey]);
            }

            //释放空间
            unset($patientLockCtOcrDatas);
            unset($patientLoseCtOcrDatas);
            unset($patientLockCostOcrDatas);
            unset($patientLockCostnewOcrDatas);
            unset($patientNocategoryOcrDatas);
            unset($patientNocategorynewOcrDatas);
            unset($patient_name_old);
        }
        unset($cond);
        unset($datas);
        return $last_id;
    }

    /**
     * Notes: 识别数据匹配
     * User: lltyy
     * DateTime: 2024/9/13 17:21
     *
     * @param $ocrDatas
     * @param $nameData
     * @return array[]
     */
    public function scanOcrKeyword($annexOcrDatas,&$nameData){
        $cut_keyword = isset($nameData['cut_keyword']) && $nameData['cut_keyword'] != '' ? trim($nameData['cut_keyword']) : '';//截取标识
        $cut_type = isset($nameData['cut_type']) && !empty($nameData['cut_type']) ? $nameData['cut_type'] : 0;//判断标识【1 前置判断（查找截取标识前面的数据） 2 后置判断（查找截取标识后面的数据）】
        $informal_keyword_arr = isset($nameData['informal_keyword']) && !empty($nameData['informal_keyword']) ? $nameData['informal_keyword'] : [];//必有关键词
        $name_keyword_arr = isset($nameData['keyword']) && !empty($nameData['keyword']) ? $nameData['keyword'] : [];//关键词
        $blackword_arr = isset($nameData['blackword']) && !empty($nameData['blackword']) ? $nameData['blackword'] : [];//黑名单
        $noword_arr = ['未见','未见明显','无'];

        $serch_annex = [];//中标附件
        $serch_keyword = [];//中标关键词
        $serch_annex_type = [];//中标附件类型
        if(!empty($annexOcrDatas)){
            foreach($annexOcrDatas as $annexOcrKey=>$annexOcrData){
                //获取查找原始数据字符串
                $ocrData = $annexOcrData['ocr_data'];
                $ocrDataSerch = $ocrData;
                if(!empty($cut_keyword) && strpos($ocrData,$cut_keyword) !== false){
                    $ocrDataArr = explode($cut_keyword,$ocrData);
                    if($cut_type == 1){//前置判断【查找截取标识前面的字符】
                        $ocrDataSerch = isset($ocrDataArr[0]) && $ocrDataArr[0] != '' ? $ocrDataArr[0] : $ocrData;
                    }
                    if($cut_type == 2){//后置判断【查找截取标识后面的字符】
                        $ocrDataSerch = isset($ocrDataArr[1]) && $ocrDataArr[1] != '' ? $ocrDataArr[1] : $ocrData;
                    }
                    unset($ocrDataArr);
                }
                $ocrDataSerch = trim($ocrDataSerch);
                if($ocrDataSerch != ''){
                    //判断否存在不含必有关键词信息【0 否 1 是】
                    $serchInformal = 0;
                    if(!empty($informal_keyword_arr)){
                        foreach($informal_keyword_arr as $informal_keyword){
                            $informal_keyword = trim($informal_keyword);
                            if($informal_keyword != ''){
                                $have_noword_informal = 0;
                                if(!empty($noword_arr)){
                                    foreach($noword_arr as $noword){
                                        if(strpos($ocrDataSerch,$noword.$informal_keyword) !== false){
                                            $have_noword_informal = 1;
                                        }
                                    }
                                }
                                if($have_noword_informal == 0){
                                    if(strpos($ocrDataSerch,$informal_keyword) !== false){
                                        $serch_keyword[] = $informal_keyword;
                                    }else{
                                        $serchInformal = 1;
                                    }
                                }
                                unset($have_noword_informal);
                            }
                        }
                    }

                    if($serchInformal == 0){
                        //去除字符串中的黑名单数据
                        if(!empty($blackword_arr)){
                            foreach($blackword_arr as $blackword){
                                $blackword = trim($blackword);
                                if($blackword != '' && strpos($ocrDataSerch,$blackword) !== false){
                                    $ocrDataSerch = str_replace($blackword,'',$ocrDataSerch);
                                }
                            }
                        }

                        //获取中标信息
                        if($ocrDataSerch != '' && !empty($name_keyword_arr)){
                            foreach($name_keyword_arr as $name_keyword){
                                $name_keyword = trim($name_keyword);
                                if($name_keyword != ''){
                                    $have_noword_keyword = 0;
                                    if(!empty($noword_arr)){
                                        foreach($noword_arr as $noword1){
                                            if(strpos($ocrDataSerch,$noword1.$name_keyword) !== false){
                                                $have_noword_keyword = 1;
                                            }
                                        }
                                    }
                                    if($have_noword_keyword == 0 && strpos($ocrDataSerch,$name_keyword) !== false && !in_array($annexOcrData['annex_id'],$serch_annex)){
                                        $serch_annex[] = $annexOcrData['annex_id'];
                                        $serch_keyword[] = $name_keyword;
                                    }
                                    unset($have_noword_keyword);
                                }
                            }
                        }
                    }
                }


                //释放空间
                unset($serchInformal);
                unset($ocrData);
                unset($ocrDataSerch);
                unset($annexOcrDatas[$annexOcrKey]);
            }
        }
        //释放空间
        unset($cut_keyword);
        unset($cut_type);
        unset($informal_keyword_arr);
        unset($name_keyword_arr);
        unset($blackword_arr);
        unset($annexOcrDatas);
        unset($noword_arr);

        return ['serch_annex'=>$serch_annex,'serch_keyword'=>$serch_keyword,'serch_annex_type'=>$serch_annex_type];
    }


}