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

namespace Application\Service\DB\Ocr;

use Application\Service\Extension\Helper\ArrayHelper;
use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\ContainerInterface;
use Application\Common\Com;
use Laminas\Db\Sql\Predicate\IsNotNull;
use Laminas\Db\Sql\Predicate\Like;
use Laminas\Db\Sql\Predicate\NotIn;
use Laminas\Db\Sql\Predicate\Operator;
use Laminas\Stdlib\ArrayUtils;

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

    /**
     * Notes:CT原始数据拆分
     * User: lltyy
     * DateTime: 2023/9/19 10:27
     *
     * @param $where
     * @param int $limit
     * @param int $offset
     * @param $keyword_arr
     * @return int
     */
    public function mateCtdata($where,$limit=0,$offset=0,&$keyword_arr){
        //获取指定条件下的识别原始数据
        $ctCond = [
            'where'=>$where,
            'columns'=>['id','annex_id','annex_type','sig_id','patient_id','content','ocr_data'],
            'order'=>['id']
        ];
        if(!empty($limit) || !empty($offset)){
            $ctCond['limit'] = $limit;
            $ctCond['offset'] = $offset;
        }
        $ctDatas = $this->LocalService()->ocrCtcase->fetchAll($ctCond);
        $last_ct_id = 0;
        if(!empty($ctDatas)){
            $delete_mate_ctannex = [];
            foreach($ctDatas as $ctKey=>$ct_info){
                $last_ct_id = $ct_info['id'];
                $delete_mate_ctannex[] = $ct_info['annex_id'];
            }

            //删除原拆分数据
            $this->upDelState([
                'is_del' => 0,
                'annex_id' => StringHelper::toArray($delete_mate_ctannex),
            ],1);

            //获取附件日期
            $annex_where = [
                'is_del' => 0,
                'id' => StringHelper::toArray($delete_mate_ctannex),
            ];
            $annex_data_arr = $this->LocalService()->ocrOcrannex->fetchCol('annex_date',ArrayUtils::merge($annex_where, [
                new IsNotNull('annex_date'),
                new Operator('annex_date', Operator::OP_NE, '')
            ]));
            $no_ocr_annex_arr = $this->LocalService()->ocrOcrannex->fetchCol('id',ArrayUtils::merge($annex_where, [
                'is_lock' => 3,
            ]));//获取待识别附件ID
            $no_ocr_annex_arr = array_values($no_ocr_annex_arr);
            unset($delete_mate_ctannex);

            //数据拆分
            foreach($ctDatas as $ctKey=>$ctData){
                $last_ct_id = $ctData['id'];
                $annex_type = $ctData['annex_type'];
                $annex_id = $ctData['annex_id'];
                $sig_id = $ctData['sig_id'];
                $patient_id = $ctData['patient_id'];
                $ocr_data = $ctData['ocr_data'];
                $ct_content_str = $ocr_data;

                //日期拆分
                $ct_date = '';
                foreach($keyword_arr['date'] as $date_keyword){
                    if(strpos($ct_content_str,$date_keyword) !== false){
                        $date_split_arr = explode($date_keyword,$ct_content_str);
                        unset($date_split_arr[0]);
                        $split_date_arr = array_filter($date_split_arr);
                        $split_date_arr = array_values($split_date_arr);
                        $split_date = implode('',$split_date_arr);
                        $split_date = $this->LocalService()->ocrRawdata->replaceRawdata($split_date,2);
                        $ct_date = substr($split_date,0,8);
                        if(!empty($ct_date)){
                            break;
                        }
                    }
                }

                if(strpos($ct_content_str,'报告医师') !== false){
                    $split_ctContent_arr = explode('报告医师', $ct_content_str);
                    $ct_content_str = $split_ctContent_arr[0];
                }

                if(strpos($ct_content_str,'报告医生') !== false){
                    $split_ctContent_arr = explode('报告医生', $ct_content_str);
                    $ct_content_str = $split_ctContent_arr[0];
                }

                //结果、内容拆分
                $ctContent = '';
                $ctResult = '';
                $split_arr = $this->splitCtdata($ct_content_str,$keyword_arr['content'],$keyword_arr['result'],1);
                $ctContent = $split_arr['ctContent'];
                $ctResult = $split_arr['ctResult'];
                if($ctContent == '' && $ctResult == ''){
                    $split_arr = $this->splitCtdata($ct_content_str,$keyword_arr['result'],$keyword_arr['content'],2);
                    $ctContent = $split_arr['ctContent'];
                    $ctResult = $split_arr['ctResult'];
                }

                if (!empty($ctContent) || !empty($ctResult)) {
                    if (empty($ctContent) && !empty($ctResult)) {
                        $end_str_arr = explode($ctResult, $ct_content_str);
                        $end_str_arr = array_filter($end_str_arr);
                        $end_str_arr = array_values($end_str_arr);
                        $ctContent = end($end_str_arr);
                        if (empty($ctContent)) {
                            $ctContent = isset($end_str_arr[0]) && !empty($end_str_arr[0]) ? $end_str_arr[0] : '';
                        }
                    }
                    if (!empty($ctContent) && empty($ctResult)) {
                        $end_str_arr = explode($ctContent, $ct_content_str);
                        $end_str_arr = array_filter($end_str_arr);
                        $end_str_arr = array_values($end_str_arr);
                        $ctResult = end($end_str_arr);
                        if (empty($ctResult)) {
                            $ctResult = isset($end_str_arr[0]) && !empty($end_str_arr[0]) ? $end_str_arr[0] : '';
                        }
                    }
                }

                if($ctContent == '' && $ctResult == ''){
                    $ctContent = $ct_content_str;
                }

                if(isset($annex_data_arr[$annex_id]) && !empty($annex_data_arr[$annex_id])){
                    $ct_date = $annex_data_arr[$annex_id];
                }

                $save_data = [
                    'annex_type'=>$annex_type,
                    'ocr_data'=>$ocr_data,
                    'category_name'=>'',
                    'annex_id'=>$annex_id,
                    'sig_id'=>$sig_id,
                    'patient_id'=>$patient_id,
                    'ct_date'=>$ct_date,
                    'ct_result'=>$ctResult,
                    'ct_content'=>$ctContent,
                    'mate_type'=>1
                ];
                $this->save($save_data);

                $this->LocalService()->ocrCtcase->update(['is_split'=>1],[
                    'id' => $ctData['id']
                ]);

                unset($ct_content_str);
                unset($ctDatas[$ctKey]);
            }
        }
        unset($ctCond);
        unset($ctDatas);

        return $last_ct_id;
    }

    /**
     * Notes:内容拆分
     * User: lltyy
     * DateTime: 2023/9/21 22:19
     *
     * @param $content_str //拆分内容信息
     * @param $keywordArr //首先拆分数据关键字
     * @param $otherKeywordArr //然后拆分数据关键字
     * @param $split_type //首先拆分数据【1 先拆分内容 2 先拆分结果】
     * @return array
     */
    public function splitCtdata($content_str,$keywordArr,$otherKeywordArr,$split_type=1){
        $ctContent = '';//内容
        $ctResult = '';//结果
        foreach($keywordArr as $keyword){
            if(strpos($content_str,$keyword) !== false){
                $split_arr = explode($keyword,$content_str);
                $split_one = $split_arr[0];
                unset($split_arr[0]);
                $split_two_arr = array_filter($split_arr);
                $split_two_arr = array_values($split_two_arr);
                $split_two = implode('',$split_two_arr);
                $split_arr = [
                    0=>$split_one,
                    1=>$split_two
                ];
                if($split_type == 1){//先拆分内容
                    $ctContent = $split_arr[1];
                }
                if($split_type == 2){//先拆分结果
                    $ctResult = $split_arr[1];
                }
                foreach($otherKeywordArr as $otherKeyword) {
                    if(strpos($split_arr[0],$otherKeyword) !== false) {
                        $otherDatas = explode($otherKeyword, $split_arr[0]);
                        unset($otherDatas[0]);
                        $otherDatas = array_filter($otherDatas);
                        $otherDatas = array_values($otherDatas);
                        if($split_type == 1){//先拆分内容
                            $ctResult = implode('',$otherDatas);
                        }
                        if($split_type == 2){//先拆分结果
                            $ctContent = implode('',$otherDatas);
                        }
                        break;
                    }else {
                        if(strpos($split_arr[1],$otherKeyword) !== false) {
                            $otherDatas = explode($otherKeyword, $split_arr[1]);
                            if($split_type == 1){//先拆分内容
                                $ctContent = $otherDatas[0];
                                unset($otherDatas[0]);
                                $otherDatas = array_filter($otherDatas);
                                $otherDatas = array_values($otherDatas);
                                $ctResult = implode('',$otherDatas);
                            }
                            if($split_type == 2){//先拆分结果
                                $ctResult = $otherDatas[0];
                                unset($otherDatas[0]);
                                $otherDatas = array_filter($otherDatas);
                                $otherDatas = array_values($otherDatas);
                                $ctContent = implode('',$otherDatas);
                            }

                            break;
                        }
                    }
                }

                break;
            }
        }
        return ['ctContent'=>$ctContent,'ctResult'=>$ctResult];
    }

    /**
     * Notes: CT自动拆分
     * User: lltyy
     * DateTime: 2023/9/19 9:25
     *
     * @param array $where
     */
    public function scanCt(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);
        }
        //$where .= ' and id > 101938';
        //$where .= ' and id > 160614';
        //yikeen_ocr_ct = id > 94729
        //yikeen_ocr_ct = id <= 94729 and annex_id > 552431
        $where .= ' and id in (161641,161640,161639,161638,161637,161636)';
        $keyword_arr = [
            'date'=>['检查日期','诊断时间','检查时间','报告日期'],
            'result'=>['检测结果','检查结果','超声提示','诊断意见','检查提示','会诊意见','影像学诊断','影像诊断','初步诊断','检查结论','确定诊断','入院诊断','诊新意见','参考意见','检度结论','声提示','断意见','查提示','床诊断','查结论','豫诊断','临床诊断'],
            'content'=>['超声描述','影像描述','检查描述','超声所见','影像所见','彩像所见','影像表现','影像学表现','检查所见','检查所视','检变所见','影像分析','图像所见','扫描所见','病历摘要','声描述','像描述','影像描','像表现','声所见','查所视','像分析','像所见','查所见','描所见','描述'],
        ];


        $bool = true;
        $page = 0;
        $limit = 1000;
        $last_ct_id = 0;
        while ($bool) {
            $page++;
            echo "ctPAGE {$page}：" . PHP_EOL;

            $last_ct_id = $this->mateCtdata($where. ' and id > ' . $last_ct_id, $limit,0,$keyword_arr);
            if(!$last_ct_id) $bool = false;
        }
    }

    /**
     * Notes:CT原始数据拆分
     * User: lltyy
     * DateTime: 2023/9/19 10:27
     *
     * @param $where
     * @param int $limit
     * @param int $offset
     * @param $keyword_arr
     * @return int
     */
    public function mateCtAnnex($where,$limit=0,$offset=0,&$annex_type_arr){
        //获取指定条件下的识别原始数据
        $ctCond = [
            'where'=>$where,
            'columns'=>['id','annex_id','sig_id','patient_id','content'],
            'order'=>['id']
        ];
        if(!empty($limit) || !empty($offset)){
            $ctCond['limit'] = $limit;
            $ctCond['offset'] = $offset;
        }
        $ctDatas = $this->LocalService()->ocrCtcase->fetchAll($ctCond);
        unset($ctCond);
        $last_ct_id = 0;
        if(!empty($ctDatas)){
            //数据拆分
            foreach($ctDatas as $ctKey=>$ctData){
                $last_ct_id = $ctData['id'];
                $annex_id = $ctData['annex_id'];
                $ct_content_arr = json_decode($ctData['content'],true);
                $ct_content_str = implode('',$ct_content_arr);

                $end_annex_type = 16;
                foreach($annex_type_arr[3] as $ctKeyword){
                    if(strpos($ct_content_str,$ctKeyword) !== false){
                        $end_annex_type = 3;
                        break;
                    }
                }

                if($end_annex_type == 16){
                    foreach($annex_type_arr[17] as $mrKeyword){
                        if(strpos($ct_content_str,$mrKeyword) !== false){
                            $end_annex_type = 17;
                            break;
                        }
                    }
                }

                if($end_annex_type == 16){
                    foreach($annex_type_arr[18] as $csKeyword){
                        if(strpos($ct_content_str,$csKeyword) !== false){
                            $end_annex_type = 18;
                            break;
                        }
                    }
                }

                $this->LocalService()->ocrCtcase->update(['annex_type'=>$end_annex_type],[
                    'id' => $ctData['id']
                ]);
                unset($ct_content_arr);
                unset($ctDatas[$ctKey]);
            }
        }
        unset($ctDatas);

        return $last_ct_id;
    }


    /**
     * Notes: CT自动拆分
     * User: lltyy
     * DateTime: 2023/9/19 9:25
     *
     * @param string $where
     */
    public function scanCtAnnex(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);
        }

        $annex_type_arr = [
            3=>['CT','放射检查','DR'],
            17=>['MR','MRI','头颅MR'],
            18=>['超声','多普勒']
        ];

        $bool = true;
        $page = 0;
        $limit = 1000;
        $last_ct_id = 0;
        while ($bool) {
            $page++;
            echo "ctAnnexPAGE {$page}：" . PHP_EOL;

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


    /**
     * Notes: 更新识别内容
     * User: lltyy
     * DateTime: 2023/5/24 9:39
     *
     * @param $content //更新内容
     * @return int
     */
    public function saveCtData($content){
        $ocrContentId = empty($content['id']) ? 0 : $content['id'];
        $save_data = [
            'id'=>$ocrContentId,
            'annex_id'=>$content['annex_id'],
            'sig_id'=>$content['sig_id'],
            'patient_id'=>$content['patient_id'],
            'ct_date'=>$content['annex_date'],
            'ct_result'=>empty($content['ct_result']) ? '' : trim($content['ct_result']),
            'ct_content'=>empty($content['ct_content']) ? '' : trim($content['ct_content'])
        ];
        $result = $this->save($save_data);
        if($result){
            $ocrContentId = empty($ocrContentId) ? $result : $ocrContentId;
        }
        unset($save_data);
        unset($content);

        return $ocrContentId;
    }

    /**
     * Notes:获取附件下指定条件的数据
     * User: lltyy
     * DateTime: 2023/5/26 8:21
     *
     * @param $where//数据条件
     * @param int $return_type//返回数据类型【0 附件下未匹配、已匹配、黑名单信息数据 1 附件下匹配上黑名单的名称】
     * @return array[]
     */
    public function fetchAnnexData($where,$return_type=0){
        $return_type = empty($return_type) ? 0 : $return_type;
        $arr = [
            'mate_id'=>[],//已匹配附件ID
            'mate_count'=>[],//附件下的已匹配数量
            'act_count'=>[],//附件下的已匹配有效数量
            'no_mate_id'=>[],//未匹配附件ID
            'no_mate_count'=>[],//附件下的未匹配数量
            'black_id'=>[],//黑名单附件ID
            'black_count'=>[],//附件下的黑名单数量
            'annex_date'=>[]//附件时间
        ];
        $columns = ['annex_id','mate_type','ct_date'];
        if(!empty($return_type)){
            $arr = [];
            if($return_type == 1){//附件下匹配上黑名单的名称
                $columns = ['annex_id','mate_type','ct_name'];
            }
        }
        $cond = [
            'where'=>$where,
            'columns'=>$columns
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach($datas as $data){
                $mate_type = $data['mate_type'];
                $annex_id = $data['annex_id'];

                //附件时间
                if(empty($return_type)){
                    $arr['annex_date'][$annex_id] = isset($arr['annex_date'][$annex_id]) && $arr['annex_date'][$annex_id] != '' ? $arr['annex_date'][$annex_id] : '';
                    if($arr['annex_date'][$annex_id] == '' && $data['ct_date'] != ''){
                        $arr['annex_date'][$annex_id] = $data['ct_date'];
                    }
                }

                //未匹配
                if(empty($mate_type) && empty($return_type)){//未匹配数
                    $arr['no_mate_id'][$annex_id] = $annex_id;
                    $arr['no_mate_count'][$annex_id] = isset($arr['no_mate_count'][$annex_id]) && !empty($arr['no_mate_count'][$annex_id]) ? $arr['no_mate_count'][$annex_id] : 0;
                    $arr['no_mate_count'][$annex_id] += 1;
                }
                //已匹配
                if($mate_type == 1 && empty($return_type)){
                    $arr['mate_id'][$annex_id] = $annex_id;
                    $arr['mate_count'][$annex_id] = isset($arr['mate_count'][$annex_id]) && !empty($arr['mate_count'][$annex_id]) ? $arr['mate_count'][$annex_id] : 0;
                    $arr['mate_count'][$annex_id] += 1;
                    $arr['act_count'][$annex_id] = isset($arr['act_count'][$annex_id]) && !empty($arr['act_count'][$annex_id]) ? $arr['act_count'][$annex_id] : 0;
                    $arr['act_count'][$annex_id] += 1;
                }
                //黑名单
                if($mate_type == 2){
                    if(empty($return_type)) {//黑名单数
                        $arr['black_id'][$annex_id] = $annex_id;
                        $arr['black_count'][$annex_id] = isset($arr['black_count'][$annex_id]) && !empty($arr['black_count'][$annex_id]) ? $arr['black_count'][$annex_id] : 0;
                        $arr['black_count'][$annex_id] += 1;
                    }

                    if($return_type == 1){//已匹配上黑名单名称
                        $arr[$annex_id] = isset($arr[$annex_id]) && !empty($arr[$annex_id]) ? $arr[$annex_id] : [];
                        $arr[$annex_id][] = $data['ct_name'];
                    }
                }
            }
        }
        if(empty(empty($return_type))){
            if(!empty($arr['black_id'])){
                foreach($arr['black_id'] as $black_annex){
                    unset($arr['mate_id'][$black_annex]);//去除已匹配数据中黑名单附件
                    unset($arr['no_mate_id'][$black_annex]);//去除未匹配数据中黑名单附件
                }
            }

            if(!empty($arr['no_mate_id'])){
                foreach($arr['no_mate_id'] as $no_mate_annex){
                    unset($arr['mate_id'][$no_mate_annex]);//去除已匹配数据中未匹配附件
                }
            }
            $arr['no_mate_id'] = array_values($arr['no_mate_id']);
            $arr['mate_id'] = array_values($arr['mate_id']);
            $arr['black_id'] = array_values($arr['black_id']);
        }

        return $arr;
    }


    /**
     * Notes: 批量删除无效识别项
     * User: lltyy
     * DateTime: 2023/5/25 16:07
     *
     * @param $where//数据条件
     * @param array $delete_name_arr//要删除的识别内容数组
     * @param int $update_annex//是否更新附件信息【0 否 1 是】
     * @return int
     */
    public function deleteDataHaveName($where,array $delete_name_arr=[],$update_annex = 0){
        $count = 0;
        if(!empty($delete_name_arr)){
            $count = 1;
            //查询要删除的数据ID以及对应的附件ID
            $annex_arr = $delete_arr = [];
            $where_arr = [];
            foreach($delete_name_arr as $delete_name){
                $where_arr['or'][] = new Like('ct_name', "%{$delete_name}%");
            }
            $where[] = $where_arr;
            $cond = [
                'where'=>$where,
                'columns'=>['id','annex_id']
            ];
            $datas = $this->fetchAll($cond);
            if(!empty($datas)){
                foreach($datas as $data){
                    $data_annex = $data['annex_id'];
                    $annex_arr[$data_annex] = $data_annex;
                    $delete_arr[] = $data['id'];
                }
            }
            unset($where_arr);
            unset($where);
            unset($cond);
            unset($datas);

            //删除符合要求的识别内容
            if(!empty($delete_arr)){
                $this->upDelState([
                    'is_del' => 0,
                    'id' => StringHelper::toArray($delete_arr)
                ],1);
            }
            unset($delete_arr);

            //更新附件信息
            if($update_annex == 1 && !empty($annex_arr)){
                $annex_arr = array_values($annex_arr);
                $annex_count_arr = $this->fetchAnnexData([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($annex_arr)
                ]);
                $raw_annex_count_arr = $this->LocalService()->ocrRawdata->fetchAnnexData('id < 0');
                $medical_annex_count_arr = $this->LocalService()->ocrMedical->fetchAnnexData('id < 0');
                $this->LocalService()->ocrOcrannex->updateAnnexCount([
                    'id' => StringHelper::toArray($annex_arr)
                ],0,0,$raw_annex_count_arr,$medical_annex_count_arr,$annex_count_arr);
            }
            unset($annex_arr);
        }

        return $count;
    }


    /**
     * Notes: 批量替换识别信息
     * User: lltyy
     * DateTime: 2023/5/25 16:07
     *
     * @param $where//数据条件
     * @param array $replace_datas//要替换的数组
     * @param int $update_ocr//是否重新匹配【0 否 1 是】
     * @return int
     */
    public function replaceDataHaveInfo($where,array $replace_datas=[])
    {
        $count = 0;
        if (!empty($replace_datas)) {
            $count = 1;

            //查询要替换的数据ID
            $where_arr = [];
            foreach ($replace_datas as $replace) {
                $where_arr['or'][] = new Like('ct_name', "%{$replace['replace_pre']}%");
            }
            unset($replace_arr);

            $where[] = $where_arr;
            $cond = [
                'where' => $where,
                'columns' => ['id', 'annex_id', 'ct_name']
            ];
            $datas = $this->fetchAll($cond);
            if (!empty($datas)) {
                foreach ($datas as $data) {
                    $ct_id = $data['id'];
                    $ct_name = $data['ct_name'];

                    foreach ($replace_datas as $replace_info) {
                        $ct_name = str_replace($replace_info['replace_pre'], $replace_info['replace_after'], $ct_name);
                    }
                    $update_data = [];
                    if ($ct_name != $data['ct_name']) {
                        $update_data['ct_name'] = $ct_name;
                    }
                    if (!empty($update_data)) {
                        $this->update($update_data, 'id = ' . $ct_id);
                    }
                    unset($update_data);

                }
            }
            unset($where_arr);
            unset($cond);
            unset($datas);
            unset($replace_datas);
        }
        return $count;
    }

    /**
     * Notes:导出影像数据
     * User: lltyy
     * DateTime: 2023/10/23 8:28
     *
     * @param $sig_id
     * @return array
     */
    public function exportData($sig_id){
        $datas = [];
        $where = [
            'is_del' => 0,
            'sig_id' => $sig_id,
            new NotIn('is_lock', [2]),
        ];
        //获取患者信息
        $patient_arr = $this->LocalService()->ocrSigpatient->fetchCol('name',[
            'is_del' => 0,
            'pid' => $sig_id,
            new Operator('have_status', Operator::OP_LTE, 1),
        ]);
        if(!empty($patient_arr)){
            $patient_id_arr = array_keys($patient_arr);
            $patient_id_arr = array_values($patient_id_arr);
            $where['patient_id'] = StringHelper::toArray($patient_id_arr);
        }else{
            $where = [
                'id' => 0,
            ];
        }
        $cond = [
            'where'=>$where,
            'columns'=>['annex_id','patient_id','ct_date','ct_result','ct_content','ocr_data','is_lock'],
            'order'=>['patient_id','id']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            //获取附件信息
            $annex_where = [
                'is_del' => 0,
                'annex_type' => [3, 16, 17, 18],
                'sig_id' => $sig_id,
                new NotIn('is_lock', [2]),
            ];
            $annexDatas = $this->LocalService()->ocrOcrannex->fetchDataById($annex_where,['id','annex_type','annex_name']);
            //获取附件类型
            $typeNameArr = $this->LocalService()->ocrAnnextype->fetchDataByCode('is_del = 0 and code in ("3","16","17","18")',['code','name']);
            foreach($datas as &$data){
                //患者信息
                $data_patient = $data['patient_id'];
                $data['patient_name'] = isset($patient_arr[$data_patient]) ? $patient_arr[$data_patient] : '';

                //附件信息
                $data_lock = $data['is_lock'];
                $data_annex = $data['annex_id'];
                $data_annex_type = isset($annexDatas[$data_annex]) && isset($annexDatas[$data_annex]['annex_type']) ? $annexDatas[$data_annex]['annex_type'] : 0;
                $data['annex_name'] = isset($annexDatas[$data_annex]) && isset($annexDatas[$data_annex]['annex_name']) ? $annexDatas[$data_annex]['annex_name'] : '';
                $data['type_name'] = isset($typeNameArr[$data_annex_type]) && isset($typeNameArr[$data_annex_type]['name']) ? $typeNameArr[$data_annex_type]['name'] : '';
                $data['lock_state'] = $data_lock == 1 ? '已锁定' : '未锁定';
            }
        }

        return $datas;
    }
}