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

namespace Application\Service\DB\Item;

use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\ContainerInterface;
use Application\Service\DB\Dictionary\FormField;
use Laminas\Db\Sql\Predicate\Operator;

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


    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2024/5/20 10:59
     *
     * @param string $where
     * @param string[] $columns
     * @param string[] $order
     * @param string $key
     * @param int $is_more
     * @return array
     */
    public function fetchDataByKey($where='is_del = 0',$columns=['id'],$order=['id'],$key='id',$is_more=0){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>$order
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoData){
                $data_key = $infoData[$key];
                if($is_more == 1){
                    $datas[$data_key][] = $infoData;
                }else{
                    $datas[$data_key] = $infoData;
                }
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }

    /***
     * Notes:常规类识别表单 - 识别内容拆分
     * User: lltyy
     * DateTime: 2024/5/20 15:50
     *
     * @param $annex_id
     * @param $ocr_data
     */
    public function splitImgOcr($ocr_arr=[]){
        $annex_id = isset($ocr_arr['annexId']) && !empty($ocr_arr['annexId']) ? $ocr_arr['annexId'] : 0;
        $save_data = [];
        $collectCategoryCode = '';
        //获取附件信息
        $annexData = $this->LocalService()->patientFormContentImg->fetchOne(['where'=> [
            'id' => $annex_id,
        ],'columns'=>['item_id','itemsig_id','patient_id','checktime_id','form_id','annex_id'=>'id','info_collect_type','gco_pool']]);
        if(!empty($annexData)){
            $gco_pool = !is_null($annexData['gco_pool']) && $annexData['gco_pool'] != '' ? $annexData['gco_pool'] : '';//获取表单共享标识
            $info_collect_type = !is_null($annexData['info_collect_type']) && !empty($annexData['info_collect_type']) ? $annexData['info_collect_type'] : 0;
            unset($annexData['gco_pool']);
            if($gco_pool != '' && !empty($info_collect_type)){
                $collectCategory = $this->LocalService()->collectCategory->fetchOne(['where'=> [
                    'is_del' => 0,
                    'id' => $info_collect_type,
                ],'columns'=>['ocr_type','category_code']]);
                $collectCategoryCode = !empty($collectCategory) ? $collectCategory['category_code'] : '';
                $ocr_type = !empty($collectCategory) ? $collectCategory['ocr_type'] : 0;
                unset($collectCategory);

                if($collectCategoryCode == 'Medical'){//医嘱
                    $this->LocalService()->doctoradviceOriginal->handleOcrData(['annexId'=>$annex_id]);
                }else{
                    $save_data = $annexData;
                    $save_data['is_lock'] = 0;
                    $formArr = $formFiledArr = $sigFiledCutArr = [];//获取共享表单
                    if($gco_pool != ''){
                        $formArr = $this->LocalService()->itemForm->fetchAll(['where'=> [
                            'is_del' => 0,
                            'gco_pool' => $gco_pool,
                            'item_id' => $annexData['item_id'],
                        ],'columns'=>['id','type','var_name']]);
                        $formIdArr = !empty($formArr) ? array_column($formArr,'id') : [];
                        if(!empty($formIdArr)){
                            //获取共享表单字段信息
                            $formFiledArr = $this->LocalService()->itemFormField->fetchDataByKey([
                                'is_del' => 0,
                                'form_id' => $formIdArr,
                                new Operator('type', Operator::OP_NE, FormField::FORM_FIELD_TYPE_UPLOAD_SHARE_IMAGE),
                                new Operator('type', Operator::OP_GT, 0),
                            ],['id','form_id','name','english','var_name','cut_pre','cut_after','cut_start','cut_length'],'form_id',1);

                            //获取中心设置的截取标识
                            $sigFiledCutArr = $this->LocalService()->itemFormrecognition->fetchDataByKey([
                                'is_del' => 0,
                                'pid' => 0,
                                'form_id' => StringHelper::toArray($formIdArr),
                                'itemsig_id' => $annexData['itemsig_id'],
                                'item_id' => $annexData['item_id'],
                            ],['form_field_id','english','cut_pre','cut_after','cut_start','cut_length'],['id'],'form_field_id',0);
                        }
                    }
                    //echo "<pre>";
                    //print_R($formArr);exit;
                    $splitContents = [];
                    if($collectCategoryCode == 'DischargeRecord'){
                        $ocr_info = $this->LocalService()->collectText->getOneFieldVal('content', [
                            'is_del' => 0,
                            'annex_id' => $annex_id,
                        ]);
                        $ocr_content_arr = json_decode($ocr_info, true);
                        $splitContents = $this->splitOcrTextContent(['form' => $formArr, 'formField' => $formFiledArr, 'sigSetFiled' => $sigFiledCutArr, 'ocr_content' => $ocr_content_arr]);
                    }elseif($collectCategoryCode == 'InterventionReport'){
                        $ocr_info = $this->LocalService()->collectCustom->getOneFieldVal('content', [
                            'is_del' => 0,
                            'annex_id' => $annex_id,
                        ]);
                        $ocr_content_arr = json_decode($ocr_info, true);
                        $splitContents = $this->splitOcrMoreContent(['form' => $formArr, 'formField' => $formFiledArr, 'sigSetFiled' => $sigFiledCutArr, 'ocr_content' => $ocr_content_arr]);
                    }else{
                        if($ocr_type == 1) {//全文识别
                            $ocr_info = $this->LocalService()->collectText->getOneFieldVal('content', [
                                'is_del' => 0,
                                'annex_id' => $annex_id,
                            ]);
                            $ocr_content_arr = json_decode($ocr_info, true);
                            $splitContents = $this->splitOcrTextContent(['form' => $formArr, 'formField' => $formFiledArr, 'sigSetFiled' => $sigFiledCutArr, 'ocr_content' => $ocr_content_arr]);
                        }
                        if($ocr_type == 2) {//表格识别
                            $ocr_info = $this->LocalService()->collectTable->getOneFieldVal('content', [
                                'is_del' => 0,
                                'annex_id' => $annex_id,
                            ]);
                            $ocr_content_arr = json_decode($ocr_info, true);
                            $splitContents = $this->splitOcrTableContent(['form' => $formArr, 'formField' => $formFiledArr, 'sigSetFiled' => $sigFiledCutArr, 'ocr_content' => $ocr_content_arr]);
                        }
                        if($ocr_type == 3) {//全文识别+AI提取
                            $ocr_info = $this->LocalService()->collectTable->getOneFieldVal('content', [
                                'is_del' => 0,
                                'annex_id' => $annex_id,
                            ]);
                            $ocr_content_arr = json_decode($ocr_info, true);
                            $splitContents = $this->splitOcrCustomAiContent(['form' => $formArr, 'formField' => $formFiledArr, 'sigSetFiled' => $sigFiledCutArr, 'ocr_content' => $ocr_content_arr]);
                        }
                        if($ocr_type == 4) {//自定义识别
                            $ocr_info = $this->LocalService()->collectCustom->getOneFieldVal('content', [
                                'is_del' => 0,
                                'annex_id' => $annex_id,
                            ]);
                            $ocr_content_arr = json_decode($ocr_info, true);
                            $splitContents = $this->splitOcrCustomContent(['form' => $formArr, 'formField' => $formFiledArr, 'sigSetFiled' => $sigFiledCutArr, 'ocr_content' => $ocr_content_arr]);
                        }
                    }
                    unset($formArr);
                    unset($formFiledArr);
                    unset($sigFiledCutArr);
                    $save_data['content'] = json_encode($splitContents['content'],true);
                    $save_data['table'] = json_encode($splitContents['table'],true);

                    $old_split_id = $this->getOneFieldVal('id','is_del = 0 and annex_id = '.$annex_id);
                    if(!empty($old_split_id)){
                        $this->upDelState('is_del = 0 and annex_id = '.$annex_id.' and id = '.$old_split_id,1);
                    }
                }
            }
        }
        unset($annexData);
        //return $save_data;
        if(!empty($save_data)){
            $this->save($save_data);
            if($collectCategoryCode == 'DischargeRecord'){
                $this->LocalService()->collectText->update(['is_split'=>1], [
                    'is_del' => 0,
                    'annex_id' => $annex_id,
                ]);
            }elseif($collectCategoryCode == 'InterventionReport'){
                $this->LocalService()->collectCustom->update(['is_split'=>1], [
                    'is_del' => 0,
                    'annex_id' => $annex_id,
                ]);
                //调用临时保存数据方法
                $saveMoreModel = new \Application\Form\item\patient\NormalOCRFormModel();
                $saveMoreModel->importMultiRow($annex_id);
            }else{
                if($ocr_type == 1) {//全文识别
                    $this->LocalService()->collectText->update(['is_split'=>1], [
                        'is_del' => 0,
                        'annex_id' => $annex_id,
                    ]);
                    //调用临时保存数据方法
                    $saveMoreModel = new \Application\Form\item\patient\NormalOCRFormModel();
                    $saveMoreModel->importMultiRow($annex_id,1);
                }
                if($ocr_type == 2) {//表格识别
                    $this->LocalService()->collectTable->update(['is_split'=>1], [
                        'is_del' => 0,
                        'annex_id' => $annex_id,
                    ]);
                }
                if($ocr_type == 3) {//全文识别+AI提取
                    $this->LocalService()->collectTable->update(['is_split'=>1], [
                        'is_del' => 0,
                        'annex_id' => $annex_id,
                    ]);
                }
                if($ocr_type == 4) {//自定义识别
                    $this->LocalService()->collectCustom->update(['is_split'=>1], [
                        'is_del' => 0,
                        'annex_id' => $annex_id,
                    ]);
                }
            }

        }
        unset($save_data);
    }

    /**
     * Notes:常规类识别表单 - (全文识别)内容拆分
     * User: lltyy
     * DateTime: 2024/5/21 9:44
     *
     * @param array $info
     * @return array
     */
    public function splitOcrTextContent(array $info=[]){
        $data = [
            'table'=>[],
            'content'=>[]
        ];
        $formArr = isset($info['form']) && !empty($info['form']) ? $info['form'] : [];
        $formFiledArr = isset($info['formField']) && !empty($info['formField']) ? $info['formField'] : [];
        $sigFiledCutArr = isset($info['sigSetFiled']) && !empty($info['sigSetFiled']) ? $info['sigSetFiled'] : [];
        $ocr_content_arr = isset($info['ocr_content']) && !empty($info['ocr_content']) ? $info['ocr_content'] : [];
        if(!empty($formArr) && !empty($ocr_content_arr) && !empty($formFiledArr)){
            //echo "<pre>";
            //print_R($formArr);exit;
            $ocr_content_str = implode('',$ocr_content_arr);
            $delete_str_arr = [":","：","，",",","。",".","·"];
            foreach($formArr as $formInfo){
                $formId = $formInfo['id'];
                if(isset($formFiledArr[$formId]) && !empty($formFiledArr[$formId])){
                    $table_info_arr = [];
                    foreach($formFiledArr[$formId] as $formFiled){
                        $formFiledId = $formFiled['id'];
                        $sigSet = isset($sigFiledCutArr[$formFiledId]) && !empty($sigFiledCutArr[$formFiledId]) ? $sigFiledCutArr[$formFiledId] : [];
                        $english = !is_null($formFiled['english']) && $formFiled['english'] != '' ? trim($formFiled['english']) : '';
                        $cut_pre = !is_null($formFiled['cut_pre']) && $formFiled['cut_pre'] != '' ? trim($formFiled['cut_pre']) : '';
                        $cut_after = !is_null($formFiled['cut_after']) && $formFiled['cut_after'] != '' ? trim($formFiled['cut_after']) : '';
                        $cut_start = !is_null($formFiled['cut_start']) && $formFiled['cut_start'] != '' ? trim($formFiled['cut_start']) : '';
                        $cut_length = !is_null($formFiled['cut_length']) && !empty($formFiled['cut_length']) ? $formFiled['cut_length'] : 0;
                        if (!empty($sigSet)) {
                            $english = !is_null($sigSet['english']) && $sigSet['english'] != '' ? trim($sigSet['english']) : '';
                            $cut_pre = !is_null($sigSet['cut_pre']) && $sigSet['cut_pre'] != '' ? trim($sigSet['cut_pre']) : '';
                            $cut_after = !is_null($sigSet['cut_after']) && $sigSet['cut_after'] != '' ? trim($sigSet['cut_after']) : '';
                            $cut_start = !is_null($sigSet['cut_start']) && $sigSet['cut_start'] != '' ? trim($sigSet['cut_start']) : '';
                            $cut_length = !is_null($sigSet['cut_length']) && !empty($sigSet['cut_length']) ? $sigSet['cut_length'] : 0;
                        }
                        $filed_value = '';
                        $cut_str = '';//该信息截取的字符串
                        //判断是否含前置顶信息
                        if(!empty($cut_start)){
                            if(strpos($ocr_content_str,$cut_start) !== false){
                                $cut_str_arr = explode($cut_start, $ocr_content_str);
                                if(count($cut_str_arr) > 1){
                                    unset($cut_str_arr[0]);
                                    $cut_str_arr = array_values($cut_str_arr);
                                    if(count($cut_str_arr) > 1){
                                        $cut_str = implode($cut_start,$cut_str_arr);
                                    }else{
                                        $cut_str = implode('',$cut_str_arr);
                                    }
                                }else{
                                    $cut_str = implode('',$cut_str_arr);
                                }
                            }else{
                                $cut_str = $ocr_content_str;
                            }
                        }else{
                            $cut_str = $ocr_content_str;
                        }
                        //截取前/后
                        if (($cut_pre != '' && strpos($cut_str, $cut_pre) !== false) || ($cut_after != '' && strpos($cut_str, $cut_after) !== false)) {
                            $filed_value = $cut_str;
                            if ($filed_value != '' && !empty($cut_pre) && strpos($filed_value, $cut_pre) !== false) {
                                $cut_pre_arr = explode($cut_pre, $filed_value);
                                if(count($cut_pre_arr) > 1){
                                    unset($cut_pre_arr[0]);
                                    $cut_pre_arr = array_values($cut_pre_arr);
                                    if(count($cut_pre_arr) > 1){
                                        $filed_value = implode($cut_pre,$cut_pre_arr);
                                    }else{
                                        $filed_value = implode('',$cut_pre_arr);
                                    }
                                }else{
                                    $filed_value = implode('',$cut_pre_arr);
                                }
                            }
                            if ($filed_value != '' && !empty($cut_after) && strpos($filed_value, $cut_after) !== false) {
                                $cut_after_arr = explode($cut_after, $filed_value);
                                $filed_value = $cut_after_arr[0];
                            }
                        }
                        //截取长度
                        if(!empty($cut_length) && strlen($filed_value) > $cut_length){
                            $filed_value = mb_substr($filed_value,0,$cut_length);
                        }

                        if ($filed_value != '') {
                            foreach ($delete_str_arr as $delete_str) {
                                if (mb_substr($filed_value, 0, 1) === $delete_str) {
                                    $filed_value = ltrim($filed_value, $delete_str);
                                }
                                if (mb_substr($filed_value, -1, 1) === $delete_str) {
                                    $filed_value = rtrim($filed_value, $delete_str);
                                }
                            }
                        }
                        if($formInfo['type'] == 0) {//一对一
                            $data['content'][$formId][$formFiledId] = [
                                'id' => $formFiledId,
                                'name' => $formFiled['name'],
                                'english' => $english,
                                'cut_pre' => $cut_pre,
                                'cut_after' => $cut_after,
                                'cut_start' => $cut_start,
                                'cut_length' => $cut_length,
                                'value' => $filed_value
                            ];
                        }else{
                            $table_info_arr[$formFiledId] = [
                                'id' => $formFiledId,
                                'name' => $formFiled['name'],
                                'ocr_name' => $formFiled['name'],
                                'var_name' => $formFiled['var_name'],
                                'english' => $english,
                                'cut_pre' => $cut_pre,
                                'cut_after' => $cut_after,
                                'cut_start' => $cut_start,
                                'cut_length' => $cut_length,
                                'value' => $filed_value
                            ];
                        }
                    }
                    if(!empty($table_info_arr)){
                        $data['table'][$formId][] = $table_info_arr;
                    }
                    unset($table_info_arr);
                }
            }
        }

        return $data;
    }

    /**
     * Notes:常规类识别表单 - (表格识别、全文+AI提取)内容拆分
     * User: lltyy
     * DateTime: 2024/5/21 9:44
     *
     * @param array $info
     * @return array
     */
    public function splitOcrTableContent(array $info=[]){
        $data = [
            'table'=>[],
            'content'=>[]
        ];
        $formArr = isset($info['form']) && !empty($info['form']) ? $info['form'] : [];
        $formFiledArr = isset($info['formField']) && !empty($info['formField']) ? $info['formField'] : [];
        $sigFiledCutArr = isset($info['sigSetFiled']) && !empty($info['sigSetFiled']) ? $info['sigSetFiled'] : [];
        $ocr_content_arr = isset($info['ocr_content']) && !empty($info['ocr_content']) ? $info['ocr_content'] : [];
        if(!empty($formArr) && !empty($ocr_content_arr) && !empty($formFiledArr)){
            $delete_str_arr = [":","：","，",",","。",".","·"];
            foreach($formArr as $formInfo){
                $formId = $formInfo['id'];
                if(isset($formFiledArr[$formId]) && !empty($formFiledArr[$formId])){
                    if($formInfo['type'] == 0) {//一对一
                        foreach ($formFiledArr[$formId] as $formFiled) {
                            $formFiledId = $formFiled['id'];
                            $sigSet = isset($sigFiledCutArr[$formFiledId]) && !empty($sigFiledCutArr[$formFiledId]) ? $sigFiledCutArr[$formFiledId] : [];
                            $english = !is_null($formFiled['english']) && $formFiled['english'] != '' ? trim($formFiled['english']) : '';
                            $cut_pre = !is_null($formFiled['cut_pre']) && $formFiled['cut_pre'] != '' ? trim($formFiled['cut_pre']): '';
                            $cut_after = !is_null($formFiled['cut_after']) && $formFiled['cut_after'] != '' ? trim($formFiled['cut_after']) : '';
                            $cut_start = !is_null($formFiled['cut_start']) && $formFiled['cut_start'] != '' ? trim($formFiled['cut_start']) : '';
                            $cut_length = !is_null($formFiled['cut_length']) && !empty($formFiled['cut_length']) ? $formFiled['cut_length'] : 0;
                            if (!empty($sigSet)) {
                                $english = !is_null($sigSet['english']) && $sigSet['english'] != '' ? trim($sigSet['english']) : '';
                                $cut_pre = !is_null($sigSet['cut_pre']) && $sigSet['cut_pre'] != '' ? trim($sigSet['cut_pre']) : '';
                                $cut_after = !is_null($sigSet['cut_after']) && $sigSet['cut_after'] != '' ? trim($sigSet['cut_after']) : '';
                                $cut_start = !is_null($sigSet['cut_start']) && $sigSet['cut_start'] != '' ? trim($sigSet['cut_start']) : '';
                                $cut_length = !is_null($sigSet['cut_length']) && !empty($sigSet['cut_length']) ? $sigSet['cut_length'] : 0;
                            }
                            $filed_value = isset($ocr_content_arr[$english]) && $ocr_content_arr[$english] != '' ? $ocr_content_arr[$english] : '';
                            if ($filed_value != '') {
                                foreach ($delete_str_arr as $delete_str) {
                                    if (mb_substr($filed_value, 0, 1) === $delete_str) {
                                        $filed_value = ltrim($filed_value, $delete_str);
                                    }
                                    if (mb_substr($filed_value, -1, 1) === $delete_str) {
                                        $filed_value = rtrim($filed_value, $delete_str);
                                    }
                                }
                            }
                            $data['content'][$formId][$formFiledId] = [
                                'id' => $formFiledId,
                                'name' => $formFiled['name'],
                                'english' => $english,
                                'cut_pre' => $cut_pre,
                                'cut_after' => $cut_after,
                                'cut_start' => $cut_start,
                                'cut_length' => $cut_length,
                                'value' => $filed_value
                            ];
                        }
                    }

                    if($formInfo['type'] == 1 && isset($ocr_content_arr['table']) && !empty($ocr_content_arr['table'])) {
                        $formFiledData = [];
                        foreach ($formFiledArr[$formId] as $formFiled1) {
                            $formFiledId1 = $formFiled1['id'];
                            $filedsigSet = isset($sigFiledCutArr[$formFiledId1]) && !empty($sigFiledCutArr[$formFiledId1]) ? $sigFiledCutArr[$formFiledId1] : [];
                            $formFiled1_eng = !is_null($formFiled1['english']) && $formFiled1['english'] != '' ? $formFiled1['english'] : '';
                            if (!empty($filedsigSet)) {
                                $formFiled1_eng = !is_null($filedsigSet['english']) && $filedsigSet['english'] != '' ? $filedsigSet['english'] : '';
                            }
                            $formFiledData[$formFiled1_eng] = $formFiledId1;
                        }
                        $formFiledData_key = array_keys($formFiledData);
                        foreach($ocr_content_arr['table'] as $ocr_content_info){
                            $ocr_content_data = $null_content_datas = [];
                            foreach($ocr_content_info as $ocr_content_key=>$ocr_content_value){
                                if(in_array($ocr_content_key,$formFiledData_key)){
                                    $ocr_content_filedid = $formFiledData[$ocr_content_key];
                                    if($ocr_content_value != '' && $ocr_content_value != '未提及'){
                                        $ocr_content_data[$ocr_content_filedid] = $ocr_content_value != '' ? $ocr_content_value : '';
                                    }else{
                                        $null_content_datas[$ocr_content_filedid] = '';
                                    }
                                }
                            }
                            if(!empty($ocr_content_data)){
                                if(!empty($null_content_datas)){
                                    foreach($null_content_datas as $null_content_key=>$null_content_data){
                                        $ocr_content_data[$null_content_key] = $null_content_data;
                                        unset($null_content_datas[$null_content_key]);
                                    }
                                }
                                $data['table'][$formId][] = $ocr_content_data;
                            }
                            unset($null_content_datas);
                            unset($ocr_content_data);
                        }
                    }
                }
            }
        }

        return $data;
    }

    /**
     * Notes:常规类识别表单 - (介入报告)内容拆分
     * User: lltyy
     * DateTime: 2024/5/21 9:44
     *  介入报告：$table = ['介入部位名称'=>'region','球囊装置名称'=>'sacculus','支架装置名称'=>'bracket'];
     * @param array $info
     * @return array
     */
    public function splitOcrMoreContent(array $info=[]){
        $data = [
            'table'=>[],
            'content'=>[]
        ];
        $formArr = isset($info['form']) && !empty($info['form']) ? $info['form'] : [];
        $formFiledArr = isset($info['formField']) && !empty($info['formField']) ? $info['formField'] : [];
        $sigFiledCutArr = isset($info['sigSetFiled']) && !empty($info['sigSetFiled']) ? $info['sigSetFiled'] : [];
        $ocr_content_arr = isset($info['ocr_content']) && !empty($info['ocr_content']) ? $info['ocr_content'] : [];
        $tableDatas = [
            'JROT'=>[//病变部位信息
                'data'=>isset($ocr_content_arr['JROT']) && !empty($ocr_content_arr['JROT']) ? $ocr_content_arr['JROT'] : [],
                'key'=>['介入部位名称'=>'NAME','CASS位置'=>'CASS','狭窄程度'=>'XZ','病变形态'=>'PAMO','TIMI血流'=>'TIMI','描述'=>'OTH']
            ],
            'INRB'=>[//支架信息信息
                'data'=>isset($ocr_content_arr['INRB']) && !empty($ocr_content_arr['INRB']) ? $ocr_content_arr['INRB'] : [],
                'key'=>['支架装置名称'=>'ZNAM','直径'=>'DIAM','长度'=>'LEN','置入/扩张部位'=>'POS']
            ],
            'BAL'=>[//球囊信息
                'data'=>isset($ocr_content_arr['BAL']) && !empty($ocr_content_arr['BAL']) ? $ocr_content_arr['BAL'] : [],
                'key'=>['球囊装置名称'=>'QNAM','直径'=>'QDIA','长度'=>'QLEN','置入/扩张部位'=>'EXS']
            ]
        ];
        unset($info);
        unset($ocr_content_arr);
        if(!empty($formArr) && !empty($formFiledArr)){
            foreach($formArr as $formInfo){
                $formId = $formInfo['id'];
                $formCode = trim($formInfo['var_name']);
                if($formInfo['type'] == 1 && isset($formFiledArr[$formId]) && !empty($formFiledArr[$formId]) && isset($tableDatas[$formCode]) && !empty($tableDatas[$formCode]['data'])){
                    $formFiledData = $formFiledName = [];
                    foreach ($formFiledArr[$formId] as $formFiled1) {
                        $formFiledId1 = $formFiled1['id'];
                        $formFiled1_eng = !is_null($formFiled1['var_name']) && $formFiled1['var_name'] != '' ? trim($formFiled1['var_name']) : '';
                        $formFiledData[$formFiled1_eng] = $formFiledId1;
                        $formFiledName[$formFiledId1]['name'] = $formFiled1['name'];
                        $formFiledName[$formFiledId1]['var_name'] = $formFiled1_eng;
                    }
                    $ocr_key_arr = [];
                    foreach($tableDatas[$formCode]['data'] as $dataKey=>$ocr_content_info){
                        if($dataKey == 0){
                            foreach($ocr_content_info as $ocr_content_key=>$ocr_content_field){
                                $ocr_content_field_key = isset($tableDatas[$formCode]['key'][$ocr_content_field]) && !empty($tableDatas[$formCode]['key'][$ocr_content_field]) ? $tableDatas[$formCode]['key'][$ocr_content_field] : '';
                                $ocr_content_field_id = $ocr_content_field_key != '' && isset($formFiledData[$ocr_content_field_key]) && !empty($formFiledData[$ocr_content_field_key]) ? $formFiledData[$ocr_content_field_key] : $ocr_content_field;
                                $ocr_key_arr[$ocr_content_key]['id'] = $ocr_content_field_id;
                                $ocr_key_arr[$ocr_content_key]['name'] = $ocr_content_field;
                            }
                        }else{
                            $ocr_content_data = $null_content_datas = [];
                            foreach($ocr_content_info as $ocr_content_key=>$ocr_content_value){
                                $ocr_content_filedid = isset($ocr_key_arr[$ocr_content_key]) && !empty($ocr_key_arr[$ocr_content_key]['id']) ? $ocr_key_arr[$ocr_content_key]['id'] : '';
                                if($ocr_content_value != '' && $ocr_content_value != '未提及'){
                                    $ocr_content_data[$ocr_content_filedid] = [
                                        'id' => $ocr_content_filedid,
                                        'name' => isset($formFiledName[$ocr_content_filedid]) && $formFiledName[$ocr_content_filedid]['name'] != '' ? $formFiledName[$ocr_content_filedid]['name'] : $ocr_content_filedid,
                                        'var_name' => isset($formFiledName[$ocr_content_filedid]) && $formFiledName[$ocr_content_filedid]['var_name'] != '' ? $formFiledName[$ocr_content_filedid]['var_name'] : $ocr_content_filedid,
                                        'ocr_name' => isset($ocr_key_arr[$ocr_content_key]) && $ocr_key_arr[$ocr_content_key]['name'] != '' ? $ocr_key_arr[$ocr_content_key]['name'] : $ocr_content_filedid,
                                        'value' => $ocr_content_value != '' ? $ocr_content_value : ''
                                    ];
                                }else{
                                    $null_content_datas[$ocr_content_filedid] = [
                                        'id' => $ocr_content_filedid,
                                        'name' => isset($formFiledName[$ocr_content_filedid]) && $formFiledName[$ocr_content_filedid]['name'] != '' ? $formFiledName[$ocr_content_filedid]['name'] : $ocr_content_filedid,
                                        'var_name' => isset($formFiledName[$ocr_content_filedid]) && $formFiledName[$ocr_content_filedid]['var_name'] != '' ? $formFiledName[$ocr_content_filedid]['var_name'] : $ocr_content_filedid,
                                        'ocr_name' => isset($ocr_key_arr[$ocr_content_key]) && $ocr_key_arr[$ocr_content_key]['name'] != '' ? $ocr_key_arr[$ocr_content_key]['name'] : $ocr_content_filedid,
                                        'value' => ''
                                    ];
                                }
                            }
                            if(!empty($ocr_content_data)){
                                if(!empty($null_content_datas)){
                                    foreach($null_content_datas as $null_content_key=>$null_content_data){
                                        $ocr_content_data[$null_content_key] = $null_content_data;
                                        unset($null_content_datas[$null_content_key]);
                                    }
                                }
                                $data['table'][$formId][] = $ocr_content_data;
                            }
                            unset($null_content_datas);
                            unset($ocr_content_data);
                        }
                    }
                    unset($ocr_key_arr);
                    unset($formFiledData);
                }
            }
        }
        unset($tableDatas);
        return $data;
    }

    /**
     * Notes:常规类识别表单 - (自定义识别)内容拆分
     * User: lltyy
     * DateTime: 2024/5/21 9:44
     *
     * @param array $info
     * @return array
     */
    public function splitOcrCustomContent(array $info=[]){
        $data = [
            'table'=>[],
            'content'=>[]
        ];
        $formArr = isset($info['form']) && !empty($info['form']) ? $info['form'] : [];
        $formFiledArr = isset($info['formField']) && !empty($info['formField']) ? $info['formField'] : [];
        $sigFiledCutArr = isset($info['sigSetFiled']) && !empty($info['sigSetFiled']) ? $info['sigSetFiled'] : [];
        $ocr_content_arr = isset($info['ocr_content']) && !empty($info['ocr_content']) ? $info['ocr_content'] : [];
        if(!empty($formArr) && !empty($ocr_content_arr) && !empty($formFiledArr)){
            $delete_str_arr = [":","：","，",",","。",".","·"];
            foreach($formArr as $formInfo){
                $formId = $formInfo['id'];
                if(isset($formFiledArr[$formId]) && !empty($formFiledArr[$formId])){
                    if($formInfo['type'] == 0) {//一对一
                        foreach ($formFiledArr[$formId] as $formFiled) {
                            $formFiledId = $formFiled['id'];
                            $sigSet = isset($sigFiledCutArr[$formFiledId]) && !empty($sigFiledCutArr[$formFiledId]) ? $sigFiledCutArr[$formFiledId] : [];
                            $english = !is_null($formFiled['english']) && $formFiled['english'] != '' ? trim($formFiled['english']) : '';
                            $cut_pre = !is_null($formFiled['cut_pre']) && $formFiled['cut_pre'] != '' ? trim($formFiled['cut_pre']) : '';
                            $cut_after = !is_null($formFiled['cut_after']) && $formFiled['cut_after'] != '' ? trim($formFiled['cut_after']) : '';
                            $cut_start = !is_null($formFiled['cut_start']) && $formFiled['cut_start'] != '' ? trim($formFiled['cut_start']) : '';
                            $cut_length = !is_null($formFiled['cut_length']) && !empty($formFiled['cut_length']) ? $formFiled['cut_length'] : 0;
                            if (!empty($sigSet)) {
                                $english = !is_null($sigSet['english']) && $sigSet['english'] != '' ? trim($sigSet['english']) : '';
                                $cut_pre = !is_null($sigSet['cut_pre']) && $sigSet['cut_pre'] != '' ? trim($sigSet['cut_pre']) : '';
                                $cut_after = !is_null($sigSet['cut_after']) && $sigSet['cut_after'] != '' ? trim($sigSet['cut_after']) : '';
                                $cut_start = !is_null($sigSet['cut_start']) && $sigSet['cut_start'] != '' ? trim($sigSet['cut_start']) : '';
                                $cut_length = !is_null($sigSet['cut_length']) && !empty($sigSet['cut_length']) ? $sigSet['cut_length'] : 0;
                            }
                            $filed_value = isset($ocr_content_arr[$english]) && $ocr_content_arr[$english] != '' ? $ocr_content_arr[$english] : '';
                            if ($filed_value != '') {
                                foreach ($delete_str_arr as $delete_str) {
                                    if (mb_substr($filed_value, 0, 1) === $delete_str) {
                                        $filed_value = ltrim($filed_value, $delete_str);
                                    }
                                    if (mb_substr($filed_value, -1, 1) === $delete_str) {
                                        $filed_value = rtrim($filed_value, $delete_str);
                                    }
                                }
                            }
                            $data['content'][$formId][$formFiledId] = [
                                'id' => $formFiledId,
                                'name' => $formFiled['name'],
                                'english' => $english,
                                'cut_pre' => $cut_pre,
                                'cut_after' => $cut_after,
                                'cut_start' => $cut_start,
                                'cut_length' => $cut_length,
                                'value' => $filed_value
                            ];
                        }
                    }
                }
            }
        }

        return $data;
    }

    /**
     * Notes: 重新拆分未锁定的数据
     * User: lltyy
     * DateTime: 2024/7/26 8:08
     *
     */
    public function scanSplitNoLockImg(){

        //获取常规类（识别）表单
        $form_where = 'is_del = 0 and item_id > 0';
        $form_group = $this->LocalService()->dictionaryFormGroup->getOneFieldVal('id','is_del = 0 and code in ("GCO")');
        if(!empty($form_group)){
            $form_where = ' and group_id = '.$form_group;
        }else{
            $form_where = 'id < 0';
        }
        unset($form_group);

        //获取病例收集类型
        $info_collect_type = $ocrTypeArr = $categoryCodeArr = [];
        $collectCategoryDatas = $this->LocalService()->collectCategory->fetchAll(['where'=>'is_del = 0 and category_code not in ("Medical")','columns'=>['id','ocr_type','category_code']]);
        if(!empty($collectCategoryDatas)){
            foreach($collectCategoryDatas as $collectCategoryData){
                $collectCategoryDataId = $collectCategoryData['id'];
                $ocrTypeArr[$collectCategoryDataId] = $collectCategoryData['ocr_type'];
                $categoryCodeArr[$collectCategoryDataId] = $collectCategoryData['category_code'];
                $info_collect_type[] = $collectCategoryDataId;
            }
        }
        unset($collectCategoryDatas);
        if(!empty($info_collect_type)){
            $form_where = ' and info_collect_type in ('.implode(',',$info_collect_type).')';
        }else{
            $form_where = 'id < 0';
        }
        unset($info_collect_type);
        $form_arr = $this->LocalService()->itemForm->fetchCol('id',$form_where);
        $form_arr = !empty($form_arr) ? array_values($form_arr) : [];
        unset($form_where);

        $where = 'is_del = 0 and is_lock != 0';
        if(!empty($form_arr)){
            $where .= ' and form_id in ('.implode(',',$form_arr).')';
        }else{
            $where = 'id < 0';
        }
        unset($form_arr);

        $bool = true;
        $page = 0;
        $limit = 1000;
        $last_id = 0;
        while ($bool) {
            $page++;
            //echo "hospitalPAGE {$page}：" . PHP_EOL;
            $last_id = $this->splitNoLockImg($where.' and id > '.$last_id,$limit,0,$ocrTypeArr,$categoryCodeArr);
            if(!$last_id) $bool = false;
        }
        unset($wordDatas);
        if($bool == false){
            return 1;
        }
    }

    /**
     * Notes: 重新拆分未锁定的数据
     * User: lltyy
     * DateTime: 2024/7/26 8:08
     *
     */
    public function splitNoLockImg($where,$limit=0,$offset=0,&$ocrTypeArr,&$categoryCodeArr){
        //获取指定条件下的医嘱数据
        $cond = [
            'where'=>$where,
            'columns'=>['item_id','itemsig_id','patient_id','checktime_id','form_id','annex_id'=>'id','info_collect_type','gco_pool'],
            'order'=>['id']
        ];
        if(!empty($limit) || !empty($offset)){
            $cond['limit'] = $limit;
            $cond['offset'] = $offset;
        }
        $datas = $this->LocalService()->patientFormContentImg->fetchAll($cond);

        $last_id = 0;
        if(!empty($datas)){
            foreach ($datas as $dataKey=>$annexData) {
                $annex_id = $annexData['annex_id'];
                $last_id = $annex_id;

                $gco_pool = !is_null($annexData['gco_pool']) && $annexData['gco_pool'] != '' ? $annexData['gco_pool'] : '';//获取表单共享标识
                $info_collect_type = !is_null($annexData['info_collect_type']) && !empty($annexData['info_collect_type']) ? $annexData['info_collect_type'] : 0;
                unset($annexData['gco_pool']);
                if($gco_pool != '' && !empty($info_collect_type)) {
                    $ocr_type = isset($ocrTypeArr[$info_collect_type]) && !empty($ocrTypeArr[$info_collect_type]) ? $ocrTypeArr[$info_collect_type] : 0;
                    $collectCategoryCode = isset($categoryCodeArr[$info_collect_type]) && !empty($categoryCodeArr[$info_collect_type]) ? $categoryCodeArr[$info_collect_type] : '';
                    $save_data = $annexData;
                    $save_data['is_lock'] = 0;
                    $formArr = $formFiledArr = $sigFiledCutArr = [];//获取共享表单
                    if ($gco_pool != '') {
                        $formArr = $this->LocalService()->itemForm->fetchAll(['where' => [
                            'is_del' => 0,
                            'gco_pool' => $gco_pool,
                            'item_id' => $annexData['item_id'],
                        ], 'columns' => ['id', 'type']]);
                        $formIdArr = !empty($formArr) ? array_column($formArr, 'id') : [];
                        if (!empty($formIdArr)) {
                            //获取共享表单字段信息
                            $formFiledArr = $this->LocalService()->itemFormField->fetchDataByKey([
                                'is_del' => 0,
                                'form_id' => StringHelper::toArray($formIdArr),
                                new Operator('type', Operator::OP_GT, 0),
                                new Operator('type', Operator::OP_NE, FormField::FORM_FIELD_TYPE_UPLOAD_SHARE_IMAGE),
                            ], ['id', 'form_id', 'name', 'english', 'cut_pre', 'cut_after', 'cut_start', 'cut_length'], 'form_id', 1);

                            //获取中心设置的截取标识
                            $sigFiledCutArr = $this->LocalService()->itemFormrecognition->fetchDataByKey([
                                'is_del' => 0,
                                'pid' => 0,
                                'form_id' => StringHelper::toArray($formIdArr),
                                'itemsig_Id' => $annexData['itemsig_id'],
                                'item_id' => $annexData['item_id'],
                            ], ['form_field_id', 'english', 'cut_pre', 'cut_after', 'cut_start', 'cut_length'], ['id'], 'form_field_id', 0);
                        }
                    }

                    $splitContents = [];
                    if ($collectCategoryCode == 'DischargeRecord') {
                        $ocr_info = $this->LocalService()->collectText->getOneFieldVal('content', [
                            'is_del' => 0,
                            'annex_id' => $annex_id,
                        ]);
                        $ocr_content_arr = json_decode($ocr_info, true);
                        $splitContents = $this->splitOcrTextContent(['form' => $formArr, 'formField' => $formFiledArr, 'sigSetFiled' => $sigFiledCutArr, 'ocr_content' => $ocr_content_arr]);
                    } else {
                        if ($ocr_type == 1) {//全文识别
                            $ocr_info = $this->LocalService()->collectText->getOneFieldVal('content', []);
                            $ocr_content_arr = json_decode($ocr_info, true);
                            $splitContents = $this->splitOcrTextContent(['form' => $formArr, 'formField' => $formFiledArr, 'sigSetFiled' => $sigFiledCutArr, 'ocr_content' => $ocr_content_arr]);
                        }
                        if ($ocr_type == 2) {//表格识别
                            $ocr_info = $this->LocalService()->collectTable->getOneFieldVal('content', [
                                'is_del' => 0,
                                'annex_id' => $annex_id,
                            ]);
                            $ocr_content_arr = json_decode($ocr_info, true);
                            $splitContents = $this->splitOcrTableContent(['form' => $formArr, 'formField' => $formFiledArr, 'sigSetFiled' => $sigFiledCutArr, 'ocr_content' => $ocr_content_arr]);
                        }
                        if ($ocr_type == 3) {//全文识别+AI提取
                            $ocr_info = $this->LocalService()->collectTable->getOneFieldVal('content', [
                                'is_del' => 0,
                                'annex_id' => $annex_id,
                            ]);
                            $ocr_content_arr = json_decode($ocr_info, true);
                            $splitContents = $this->splitOcrCustomAiContent(['form' => $formArr, 'formField' => $formFiledArr, 'sigSetFiled' => $sigFiledCutArr, 'ocr_content' => $ocr_content_arr]);
                        }
                        if ($ocr_type == 4) {//自定义识别
                            $ocr_info = $this->LocalService()->collectCustom->getOneFieldVal('content', [
                                'is_del' => 0,
                                'annex_id' => $annex_id,
                            ]);
                            $ocr_content_arr = json_decode($ocr_info, true);
                            $splitContents = $this->splitOcrCustomContent(['form' => $formArr, 'formField' => $formFiledArr, 'sigSetFiled' => $sigFiledCutArr, 'ocr_content' => $ocr_content_arr]);
                        }
                    }
                    unset($formArr);
                    unset($formFiledArr);
                    unset($sigFiledCutArr);
                    $save_data['content'] = json_encode($splitContents['content'], true);
                    $save_data['table'] = json_encode($splitContents['table'], true);

                    $old_split_id = $this->getOneFieldVal('id', [
                            'is_del' => 0,
                            'annex_id' => $annex_id,
                          ]);
                    if (!empty($old_split_id)) {
                        $this->upDelState([
                            'is_del' => 0,
                            'annex_id' => $annex_id,
                            'id' => $old_split_id,
                          ], 1);
                    }
                    unset($annexData);
                    //return $save_data;
                    $this->save($save_data);
                    if ($ocr_type == 1) {//全文识别
                        $this->LocalService()->collectText->update(['is_split' => 1], [
                            'is_del' => 0,
                            'annex_id' => $annex_id,
                          ]);
                    }
                    if ($ocr_type == 2) {//表格识别
                        $this->LocalService()->collectTable->update(['is_split' => 1], [
                            'is_del' => 0,
                            'annex_id' => $annex_id,
                          ]);
                    }
                    if ($ocr_type == 3) {//全文识别+AI提取
                        $this->LocalService()->collectTable->update(['is_split' => 1], [
                            'is_del' => 0,
                            'annex_id' => $annex_id,
                          ]);
                    }
                    if ($ocr_type == 4) {//自定义识别
                        $this->LocalService()->collectCustom->update(['is_split' => 1], [
                            'is_del' => 0,
                            'annex_id' => $annex_id,
                          ]);
                    }
                    unset($save_data);
                }
                unset($datas[$dataKey]);
            }
        }
        unset($cond);
        unset($datas);

        return $last_id;
    }

}