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

namespace Application\Service\DB\Ocr;

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

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


    /**
     * Notes: 获取指定条件下的识别结果数据 与 指标关键字匹配，存入匹配信息表
     * User: lltyy
     * DateTime: 2023/4/25 10:16
     *
     * @param $where
     * @return array
     */
    public function updateAnnexCount($where,$limit=0,$offset=0,&$raw_count_arr,&$medical_count_arr,&$ct_count_arr){
        //获取指定条件下的识别结果数据
        $cond = [
            'where'=>$where,
            'columns'=>['id','annex_type','annex_date','is_lock'],
            'order'=>['id']
        ];
        if(!empty($limit)){
            $cond['limit'] = $limit;
            $cond['offset'] = $offset;
        }
        $datas = $this->fetchAll($cond);
        $last_annex_id = 0;
        if(!empty($datas)){
            foreach($datas as $dataKey=>$data){
                $data_id = $data['id'];
                $last_annex_id = $data_id;
                $mate_count = $act_count = $no_mate_count = $black_count = 0;
                $annex_date = '';
                if($data['annex_type'] == 1){//化验单
                    $mate_count = isset($raw_count_arr['mate_count']) && isset($raw_count_arr['mate_count'][$data_id]) && !empty($raw_count_arr['mate_count'][$data_id]) ? $raw_count_arr['mate_count'][$data_id] : 0;
                    $act_count = isset($raw_count_arr['act_count']) && isset($raw_count_arr['act_count'][$data_id]) && !empty($raw_count_arr['act_count'][$data_id]) ? $raw_count_arr['act_count'][$data_id] : 0;
                    $no_mate_count = isset($raw_count_arr['no_mate_count']) && isset($raw_count_arr['no_mate_count'][$data_id]) && !empty($raw_count_arr['no_mate_count'][$data_id]) ? $raw_count_arr['no_mate_count'][$data_id] : 0;
                    $black_count = isset($raw_count_arr['black_count']) && isset($raw_count_arr['black_count'][$data_id]) && !empty($raw_count_arr['black_count'][$data_id]) ? $raw_count_arr['black_count'][$data_id] : 0;
                    $annex_date = isset($raw_count_arr['annex_date']) && isset($raw_count_arr['annex_date'][$data_id]) && $raw_count_arr['annex_date'][$data_id] != '' ? $raw_count_arr['annex_date'][$data_id] : '';
                    unset($raw_count_arr['mate_count'][$data_id]);
                    unset($raw_count_arr['no_mate_count'][$data_id]);
                    unset($raw_count_arr['black_count'][$data_id]);
                    unset($raw_count_arr['annex_date'][$data_id]);
                }

                if(in_array($data['annex_type'],[2])){//医嘱
                    $mate_count = isset($medical_count_arr['mate_count']) && isset($medical_count_arr['mate_count'][$data_id]) && !empty($medical_count_arr['mate_count'][$data_id]) ? $medical_count_arr['mate_count'][$data_id] : 0;
                    $act_count = isset($medical_count_arr['act_count']) && isset($medical_count_arr['act_count'][$data_id]) && !empty($medical_count_arr['act_count'][$data_id]) ? $medical_count_arr['act_count'][$data_id] : 0;
                    $no_mate_count = isset($medical_count_arr['no_mate_count']) && isset($medical_count_arr['no_mate_count'][$data_id]) && !empty($medical_count_arr['no_mate_count'][$data_id]) ? $medical_count_arr['no_mate_count'][$data_id] : 0;
                    $black_count = isset($medical_count_arr['black_count']) && isset($medical_count_arr['black_count'][$data_id]) && !empty($medical_count_arr['black_count'][$data_id]) ? $medical_count_arr['black_count'][$data_id] : 0;
                    $annex_date = isset($medical_count_arr['annex_date']) && isset($medical_count_arr['annex_date'][$data_id]) && !empty($medical_count_arr['annex_date'][$data_id]) ? implode('、',$medical_count_arr['annex_date'][$data_id]) : '';
                    unset($medical_count_arr['mate_count'][$data_id]);
                    unset($medical_count_arr['no_mate_count'][$data_id]);
                    unset($medical_count_arr['black_count'][$data_id]);
                    unset($medical_count_arr['annex_date'][$data_id]);
                }
                if(in_array($data['annex_type'],[3,16,17,18])){//CT
                    $mate_count = isset($ct_count_arr['mate_count']) && isset($ct_count_arr['mate_count'][$data_id]) && !empty($ct_count_arr['mate_count'][$data_id]) ? $ct_count_arr['mate_count'][$data_id] : 0;
                    $act_count = isset($ct_count_arr['act_count']) && isset($ct_count_arr['act_count'][$data_id]) && !empty($ct_count_arr['act_count'][$data_id]) ? $ct_count_arr['act_count'][$data_id] : 0;
                    $no_mate_count = isset($ct_count_arr['no_mate_count']) && isset($ct_count_arr['no_mate_count'][$data_id]) && !empty($ct_count_arr['no_mate_count'][$data_id]) ? $ct_count_arr['no_mate_count'][$data_id] : 0;
                    $black_count = isset($ct_count_arr['black_count']) && isset($ct_count_arr['black_count'][$data_id]) && !empty($ct_count_arr['black_count'][$data_id]) ? $ct_count_arr['black_count'][$data_id] : 0;
                    $annex_date = isset($ct_count_arr['annex_date']) && isset($ct_count_arr['annex_date'][$data_id]) && $ct_count_arr['annex_date'][$data_id] != '' ? $ct_count_arr['annex_date'][$data_id] : '';
                    unset($ct_count_arr['mate_count'][$data_id]);
                    unset($ct_count_arr['no_mate_count'][$data_id]);
                    unset($ct_count_arr['black_count'][$data_id]);
                    unset($ct_count_arr['annex_date'][$data_id]);
                }
                $save_data = [
                    'id'=>$data_id,
                    'act_count'=>$act_count,
                    'mate_count'=>$mate_count,
                    'no_mate_count'=>$no_mate_count,
                    'black_count'=>$black_count
                ];
                if($data['annex_type'] != 2 && ($data['annex_date'] == '' || is_null($data['annex_date']))){
                    $save_data['annex_date'] = $annex_date;
                }
                //弃用、锁定
                if($data['annex_type'] == 1) {
                    $save_data['is_lock'] = 0;
                    //将弃用的附件下的识别数据变为弃用状态
                    if((empty($mate_count) && empty($black_count) && !empty($no_mate_count)) || $data['is_lock'] == 2) {
                        $save_data['is_lock'] = 2;
                        $lose_where = [
                            'annex_type' => 1,
                            'id' => $data_id,
                        ];
                        $this->loseAnnexData($lose_where,0);
                    }
                    //锁定数据
                    if($data['is_lock'] == 1){
                        $save_data['is_lock'] = 1;
                        $lock_where = [
                            'annex_type' => 1,
                            'id' => $data_id,
                        ];
                        $this->lockAnnexData($lock_where,0);
                    }
                }

                if($data['annex_type'] == 2) {
                    $save_data['is_lock'] = 0;
                    //将弃用的附件下的识别数据变为弃用状态
                    if((empty($mate_count) && (!empty($black_count) || !empty($no_mate_count))) || $data['is_lock'] == 2) {
                        $save_data['is_lock'] = 2;
                        $lose_where = [
                            'annex_type' => 2,
                            'id' => $data_id,
                        ];
                        $this->loseAnnexData($lose_where,0);
                    }
                    //锁定数据
                    if($data['is_lock'] == 1){
                        $save_data['is_lock'] = 1;
                        $lock_where = [
                            'annex_type' => 2,
                            'id' => $data_id,
                        ];
                        $this->lockAnnexData($lock_where,0);
                    }
                }
                $this->save($save_data);
                unset($save_data);
                unset($datas[$dataKey]);
            }
        }
        unset($cond);
        unset($datas);
        return $last_annex_id;
    }

    /**
     * Notes:附件 - 弃用
     * User: lltyy
     * DateTime: 2023/5/25 9:37
     *
     * @param array $where//条件
     * @param int $execute_type//执行类型【0 只执行有传递条件的数据 1 传递条件基础上增加弃用条件】
     * @return int
     */
    public function loseAnnexData(array $where = [],$execute_type=0,$info_type=0){
        $count = 0;
        //获取有匹配数无黑名单数无未匹配数据的附件ID
        if(!empty($execute_type)){
            if($info_type == 1){
                $where = ArrayUtils::merge($where,[
                    'is_del' => 0,
                    'mate_count2' => 0,
                    'black_count2' => 0,
                    new Operator('no_mate_count', Operator::OP_GT, 0)
                ]);
            }else{
                $where = ArrayUtils::merge($where,[
                    'is_del' => 0,
                    'mate_count' => 0,
                    'black_count' => 0,
                    new Operator('no_mate_count', Operator::OP_GT, 0)
                ]);
            }
        }
        if(empty($where)){
            $where = [new Operator('id',Operator::OP_LT,0)];
        }
        $annexCond = [
            'where'=>$where,
            'columns'=>['id','annex_type']
        ];
        $annexDatas = $this->fetchAll($annexCond);
        if(!empty($annexDatas)){
            $count = 1;
            $raw_annex_arr = $medical_annex_arr = $ct_annex_arr = [];
            foreach($annexDatas as $annexData){
                if($annexData['annex_type'] == 1) {
                    $raw_annex_arr[] = $annexData['id'];
                }
                if(in_array($annexData['annex_type'],[2])) {
                    $medical_annex_arr[] = $annexData['id'];
                }
                if(in_array($annexData['annex_type'],[3,16,17,18])) {
                    $ct_annex_arr[] = $annexData['id'];
                }
            }
            //更新附件下的内容数据为弃用状态
            if(!empty($raw_annex_arr)){//化验单
                $this->LocalService()->ocrRawdata->update(['is_lock'=>2],[
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($raw_annex_arr),
                ]);
                $this->LocalService()->ocrRawlock->upDelState([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($raw_annex_arr),
                ]);
            }
            unset($raw_annex_arr);

            if(!empty($medical_annex_arr)){//医嘱
                $this->LocalService()->ocrMedical->update(['is_lock'=>2],[
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($medical_annex_arr),
                    'info_type' => $info_type,
                ]);
                $this->LocalService()->ocrMedicallock->upDelState([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($medical_annex_arr),
                    'info_type' => $info_type,
                ]);
            }
            unset($medical_annex_arr);

            if(!empty($ct_annex_arr)){//CT
                $this->LocalService()->ocrCt->update(['is_lock'=>2],[
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($ct_annex_arr),
                ]);
            }
            unset($ct_annex_arr);

            //更新附件状态为弃用
            $up_annex = [
                'is_lock'=>2,
                'template_id'=>0,
                'change_type'=>0
            ];
            if($info_type == 1){
                $up_annex = [
                    'is_lock2'=>2
                ];
            }
            $this->update($up_annex,$where);
        }
        unset($execute_type);
        unset($where);
        unset($annexCond);
        unset($annexDatas);

        return $count;
    }

    /**
     * Notes:附件 - 锁定
     * User: lltyy
     * DateTime: 2023/5/25 9:37
     *
     * @param string $where//条件
     * @param int $execute_type//执行类型【0 只执行有传递条件的数据 1 传递条件基础上增加弃用条件】
     * @return int
     */
    public function lockAnnexData(array $where = [],$execute_type=0){
        $count = 0;

        //获取有匹配数无黑名单数无未匹配数据的附件ID
        if(!empty($execute_type)){
            $where = ArrayUtils::merge($where,[
                'is_del' => 0,
                'mate_count' => 0,
                'black_count' => 0,
                new Operator('no_mate_count', Operator::OP_GT, 0)
            ]);
        }

        if(empty($where)){
            $where = [new Operator('id',Operator::OP_LT,0)];
        }
        $annexCond = [
            'where'=>$where,
            'columns'=>['id','annex_type']
        ];
        $annexDatas = $this->fetchAll($annexCond);
        if(!empty($annexDatas)){
            $count = 1;
            $raw_annex_arr = $medical_annex_arr = $ct_annex_arr = [];
            foreach($annexDatas as $annexData){
                if($annexData['annex_type'] == 1) {
                    $raw_annex_arr[] = $annexData['id'];
                }
                if(in_array($annexData['annex_type'],[2])) {
                    $medical_annex_arr[] = $annexData['id'];
                }
                if(in_array($annexData['annex_type'],[3,16,17,18])) {
                    $ct_annex_arr[] = $annexData['id'];
                }
            }

            //更新附件下的内容数据为锁定状态
            if(!empty($raw_annex_arr)){//化验单
                //获取附件下已匹配的内容数据
                $info_where = [
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($raw_annex_arr),
                ];
                $info_cond = [
                    'where'=> ArrayUtils::merge($info_where, [
                        'mate_type' => 1,
                    ]),
                    'columns'=>['id','sig_id','patient_id','annex_id','raw_name','raw_code','raw_date','raw_result','raw_judge'],
                    'order'=>['id']
                ];
                $rawDatas = $this->LocalService()->ocrRawdata->fetchAll($info_cond);
                if(!empty($rawDatas)){
                    //获取已匹配数据
                    $mateDatas = $this->LocalService()->ocrMatedata->fetchRawMatedata(ArrayUtils::merge($info_where, [
                        'is_mate' => 1
                    ]),1);
                    //获取锁定数据
                    $rawLockDatas = $this->LocalService()->ocrRawlock->fetchLockByRaw($info_where,['raw_id','raw_min','raw_max']);
                    if(!empty($mateDatas)){
                        foreach($rawDatas as &$rawData){
                            $raw_id = $rawData['id'];
                            $rawData['raw_id'] = $raw_id;
                            $rawData['raw_category'] = '';
                            //匹配的信息
                            $mateData = isset($mateDatas[$raw_id]) && !empty($mateDatas[$raw_id]) ? $mateDatas[$raw_id] : [];
                            $rawData['raw_unit'] = $rawData['raw_min'] = $rawData['raw_max'] = $rawData['raw_range'] = '';
                            if(!empty($mateData)){
                                $rawData['keyword_id'] = $mateData['keyword_id'];
                                $rawData['raw_name'] = $mateData['keyword_name'];
                                $rawData['raw_code'] = $mateData['keyword_code'];
                                $rawData['raw_category'] = $mateData['category_name'];
                                $rawData['raw_unit'] = isset($mateData['keyword_unit']) && $mateData['keyword_unit'] != '' ? $mateData['keyword_unit'] : '';
                                $rawData['raw_min'] = isset($mateData['keyword_min']) && $mateData['keyword_min'] != '' ? $mateData['keyword_min'] : '';
                                $rawData['raw_max'] = isset($mateData['keyword_max']) && $mateData['keyword_max'] != '' ? $mateData['keyword_max'] : '';
                                $rawData['raw_range'] = isset($mateData['keyword_range']) && $mateData['keyword_range'] != '' ? $mateData['keyword_range'] : '';
                            }
                            if(!isset($rawLockDatas[$raw_id])){
                                $rawData['raw_min'] = isset($rawLockDatas[$raw_id]['raw_min']) && $rawLockDatas[$raw_id]['raw_min'] != '' ? $rawLockDatas[$raw_id]['raw_min'] : '';
                                $rawData['raw_max'] = isset($rawLockDatas[$raw_id]['raw_max']) && $rawLockDatas[$raw_id]['raw_max'] != '' ? $rawLockDatas[$raw_id]['raw_max'] : '';
                                $rawData['raw_range'] = isset($rawLockDatas[$raw_id]['raw_range']) && $rawLockDatas[$raw_id]['raw_range'] != '' ? $rawLockDatas[$raw_id]['raw_range'] : '';
                            }

                            $this->LocalService()->ocrRawlock->save($rawData);
                        }
                    }
                    unset($mateDatas);
                    $this->LocalService()->ocrRawdata->update(['is_lock'=>1],$info_where);
                }
                unset($info_where);
                unset($info_cond);
                unset($rawDatas);
            }
            unset($raw_annex_arr);

            if(!empty($medical_annex_arr)){//医嘱
                //获取附件下已匹配的内容数据
                $detail_where = [
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($medical_annex_arr)
                ];
                $detail_cond = [
                    'where'=> ArrayUtils::merge($detail_where, [
                        'mate_type' => 1,
                        new Operator('drug_id', Operator::OP_GT, 0)
                    ]),
                    'columns'=>['id','sig_id','patient_id','annex_id','medical_name','medical_date','end_date','drug_name','drug_id','dosage_id','dosage_num','dosage_unit','times_id','is_dispensary','medical_type'],
                    'order'=>['id']
                ];
                $medicalDatas = $this->LocalService()->ocrMedical->fetchAll($detail_cond);
                if(!empty($medicalDatas)){
                    //获取已匹配数据
                    $medicalMateDatas = $this->LocalService()->ocrMatewhite->fetchMedicalMatedata($detail_where,0);
                    //获取锁定数据
                    $medicalLockDatas = $this->LocalService()->ocrMedicallock->fetchLockByMedical($detail_where,['id','medical_id']);
                    if(!empty($medicalMateDatas)){
                        foreach($medicalDatas as &$medicalData){
                            $medical_id = $medicalData['id'];
                            unset($medicalData['id']);
                            $medicalData['id'] = isset($medicalLockDatas[$medical_id]) && !empty($medicalLockDatas[$medical_id]) ? $medicalLockDatas[$medical_id]['id'] : 0;
                            $medicalData['medical_id'] = $medical_id;
                            //匹配的信息
                            $medicalMatedata = isset($medicalMateDatas[$medical_id]) && !empty($medicalMateDatas[$medical_id]) ? $medicalMateDatas[$medical_id] : [];
                            $medicalData['mate_name'] = json_encode($medicalMatedata);

                            $this->LocalService()->ocrMedicallock->save($medicalData);
                        }
                    }
                    unset($medicalMateDatas);
                }
                unset($detail_where);
                unset($detail_cond);
                unset($medicalDatas);
            }
            unset($medical_annex_arr);

            if(!empty($ct_annex_arr)) {//CT
                $this->LocalService()->ocrCt->update(['is_lock'=>1,'mate_type'=>1],[
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($ct_annex_arr)
                ]);
                $haveCtAnnex = $this->LocalService()->ocrCt->fetchCol('annex_id',[
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($ct_annex_arr)
                ]);
                $haveCtAnnex = array_values($haveCtAnnex);
                if(!empty($haveCtAnnex)){
                    $this->update(['mate_count'=>1,'act_count'=>1],[
                        'is_del' => 0,
                        'id' => StringHelper::toArray($haveCtAnnex)
                    ]);
                }
            }
            unset($ct_annex_arr);

            //更新附件为锁定
            $this->update(['is_lock'=>1,'template_id'=>0,'change_type'=>0,'black_count'=>0,'no_mate_count'=>0],$where);
        }
        unset($execute_type);
        unset($where);
        unset($annexCond);
        unset($annexDatas);

        return $count;
    }

    /**
     * Notes:附件 - 清空
     * User: lltyy
     * DateTime: 2023/5/25 9:37
     *
     * @param string $where//条件
     * @param int $execute_type//执行类型【0 只执行有传递条件的数据 1 传递条件基础上增加弃用条件】
     * @return int
     */
    public function emptyAnnexData($where=[],$execute_type=0){
        $count = 0;

        //获取有匹配数无黑名单数无未匹配数据的附件ID
        if(!empty($execute_type)){
            $where = ArrayUtils::merge($where, [
               'is_del' => 0,
               'mate_count' => 0,
               'no_mate_count' => 0,
               'black_count' => 0,
               new Operator('is_lock', Operator::OP_NE, 1)
            ]);
        }

        if(empty($where)){
            $where = [new Operator('id',Operator::OP_LT,0)];
        }
        $annexCond = [
            'where'=>$where,
            'columns'=>['id','annex_type']
        ];
        $annexDatas = $this->fetchAll($annexCond);
        if(!empty($annexDatas)){
            $count = 1;
            $raw_annex_arr = $medical_annex_arr = $ct_annex_arr = [];
            foreach($annexDatas as $annexData){
                if($annexData['annex_type'] == 1) {
                    $raw_annex_arr[] = $annexData['id'];
                }
                if(in_array($annexData['annex_type'],[2])) {
                    $medical_annex_arr[] = $annexData['id'];
                }
                if(in_array($annexData['annex_type'],[3,16,17,18])) {
                    $ct_annex_arr[] = $annexData['id'];
                }
            }

            //清空附件下的内容数据
            if(!empty($raw_annex_arr)){//化验单
                $this->LocalService()->ocrRawdata->upDelState([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($raw_annex_arr)
                ],1);
                $this->LocalService()->ocrMatedata->upDelState([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($raw_annex_arr)
                ],1);
                $this->LocalService()->ocrRawblack->upDelState([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($raw_annex_arr)
                ],1);
                $this->LocalService()->ocrRawlock->upDelState([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($raw_annex_arr)
                ],1);
            }
            unset($raw_annex_arr);

            if(!empty($medical_annex_arr)){//医嘱
                $this->LocalService()->ocrMedical->upDelState([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($medical_annex_arr)
                ],1);
                $this->LocalService()->ocrMatewhite->upDelState([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($medical_annex_arr)
                ],1);
                $this->LocalService()->ocrMateblack->upDelState([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($medical_annex_arr)
                ],1);
                $this->LocalService()->ocrMedicallock->upDelState([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($medical_annex_arr)
                ],1);
            }
            unset($medical_annex_arr);

            if(!empty($ct_annex_arr)){//CT
                $this->LocalService()->ocrCt->upDelState([
                    'is_del' => 0,
                    'annex_id' => StringHelper::toArray($ct_annex_arr)
                ],1);
            }
            unset($ct_annex_arr);

            //更新附件信息
            $this->update(['is_lock'=>0,'mate_count'=>0,'act_count'=>0,'no_mate_count'=>0,'black_count'=>0],$where);
        }
        unset($execute_type);
        unset($where);
        unset($annexCond);
        unset($annexDatas);

        return $count;
    }

    /**
     * Notes:获取附件类型数量
     * User: lltyy
     * DateTime: 2023/5/29 11:57
     *
     * @param $where //附件类型数量
     */
    public function fetchAnnexTypeCount($where){
        $countList = [];
        //获取非医嘱信息类型附件
        $annex_where = [
            'where'=>$where,
            'columns'=>['id','annex_type','act_count','mate_count','no_mate_count','black_count','is_lock'],
            'order'=>['id']
        ];
        $annex_datas = $this->fetchAll($annex_where);
        if(!empty($annex_datas)){
            foreach($annex_datas as $annex_data){
                $annex_type = $annex_data['annex_type'];
                $mate_count = $annex_data['mate_count'];
                $no_mate_count = $annex_data['no_mate_count'];
                $black_count = $annex_data['black_count'];
                $is_lock = $annex_data['is_lock'];

                //化验单
                if($annex_type == 1){
                    if($is_lock == 1 || ($is_lock == 0 && ($mate_count > 0 || $no_mate_count > 0) && $black_count == 0)){//保留
                        $countList['1_0'] = isset($countList['1_0']) && !empty($countList['1_0']) ? $countList['1_0'] : 0;
                        $countList['1_0']++;
                        if($is_lock != 1){
                            $countList['1_1'] = isset($countList['1_1']) && !empty($countList['1_1']) ? $countList['1_1'] : 0;
                            $countList['1_1']++;
                        }
                    }

                    if($is_lock == 2 || ($is_lock == 0 && $black_count > 0)){//无效
                        $countList['3_0'] = isset($countList['3_0']) && !empty($countList['3_0']) ? $countList['3_0'] : 0;
                        $countList['3_0']++;
                        if($is_lock != 2){
                            $countList['3_1'] = isset($countList['3_1']) && !empty($countList['3_1']) ? $countList['3_1'] : 0;
                            $countList['3_1']++;
                        }
                    }

                    if(($is_lock == 0 && $mate_count == 0 && $no_mate_count == 0 && $black_count == 0) || $is_lock == 3){//未识别
                        $countList['5_0'] = isset($countList['5_0']) && !empty($countList['5_0']) ? $countList['5_0'] : 0;
                        $countList['5_0']++;
                        if($is_lock != 3){
                            $countList['5_1'] = isset($countList['5_1']) && !empty($countList['5_1']) ? $countList['5_1'] : 0;
                            $countList['5_1']++;
                        }
                    }
                }

                //未识别医嘱
                if(in_array($annex_type,[2])){
                    if($is_lock == 1 || ($is_lock == 0 && $mate_count > 0)){//保留
                        $countList['2_0'] = isset($countList['2_0']) && !empty($countList['2_0']) ? $countList['2_0'] : 0;
                        $countList['2_0']++;
                        if($is_lock != 1){
                            $countList['2_1'] = isset($countList['2_1']) && !empty($countList['2_1']) ? $countList['2_1'] : 0;
                            $countList['2_1']++;
                        }
                    }

                    if($is_lock == 2 || ($is_lock == 0 && empty($mate_count) && (!empty($no_mate_count) || !empty($black_count)))){//无效
                        $countList['4_0'] = isset($countList['4_0']) && !empty($countList['4_0']) ? $countList['4_0'] : 0;
                        $countList['4_0']++;
                        if($is_lock != 2){
                            $countList['4_1'] = isset($countList['4_1']) && !empty($countList['4_1']) ? $countList['4_1'] : 0;
                            $countList['4_1']++;
                        }
                    }

                    if($is_lock == 3 || ($is_lock == 0 && empty($mate_count) && empty($no_mate_count) && empty($black_count))){
                        $countList['6_0'] = isset($countList['6_0']) && !empty($countList['6_0']) ? $countList['6_0'] : 0;
                        $countList['6_0']++;
                        if($is_lock == 3){
                            $countList['6_1'] = isset($countList['6_1']) && !empty($countList['6_1']) ? $countList['6_1'] : 0;
                            $countList['6_1']++;
                        }
                    }
                }

                //CT
                if(in_array($annex_type,[3,17,18,16])){
                    if($mate_count > 0){
                        $mate_annex_arr[] = $annex_data['id'];
                    }
                    if($is_lock == 1 || ($is_lock == 0 && $mate_count > 0)){//保留
                        $countList['7_0'] = isset($countList['7_0']) && !empty($countList['7_0']) ? $countList['7_0'] : 0;
                        $countList['7_0']++;
                        if($is_lock != 1){
                            $countList['7_1'] = isset($countList['7_1']) && !empty($countList['7_1']) ? $countList['7_1'] : 0;
                            $countList['7_1']++;
                        }
                    }

                    if($is_lock == 2){//无效
                        $countList['8_0'] = isset($countList['8_0']) && !empty($countList['8_0']) ? $countList['8_0'] : 0;
                        $countList['8_0']++;
                    }

                    if(($is_lock == 0 && $mate_count == 0) || $is_lock == 3){//未识别
                        $countList['9_0'] = isset($countList['9_0']) && !empty($countList['9_0']) ? $countList['9_0'] : 0;
                        $countList['9_0']++;

                        if($is_lock != 3){
                            $countList['9_1'] = isset($countList['9_1']) && !empty($countList['9_1']) ? $countList['9_1'] : 0;
                            $countList['9_1']++;
                        }
                    }
                }

                //未分类
                if($annex_type == 0){
                    if($is_lock == 2){
                        $countList['-1_0'] = isset($countList['-1_0']) && !empty($countList['-1_0']) ? $countList['-1_0'] : 0;
                        $countList['-1_0']++;
                    }else{
                        $countList['0_0'] = isset($countList['0_0']) && !empty($countList['0_0']) ? $countList['0_0'] : 0;
                        $countList['0_0']++;
                    }
                }

                //病案首页
                if($annex_type == 8){
                    $countList['10_0'] = isset($countList['10_0']) && !empty($countList['10_0']) ? $countList['10_0'] : 0;
                    $countList['10_0']++;
                }

            }
        }

        return $countList;
    }


    /**
     * Notes:获取附件类型数量
     * User: lltyy
     * DateTime: 2023/5/29 11:57
     *
     * @param $where //附件类型数量
     */
    public function fetchAnnexTypeCount2($where){
        $countList = [];
        //获取非医嘱信息类型附件
        $annex_where = [
            'where'=>$where,
            'columns'=>['id','annex_type','act_count'=>'act_count2','mate_count'=>'mate_count2','is_lock2'],
            'order'=>['id']
        ];
        $annex_datas = $this->fetchAll($annex_where);
        if(!empty($annex_datas)){
            foreach($annex_datas as $annex_data){
                $annex_type = $annex_data['annex_type'];
                $mate_count = $annex_data['mate_count'];
                $is_lock2 = $annex_data['is_lock2'];

                //化验单
                if($annex_type == 1){
                    if($mate_count > 0 && in_array($is_lock2,[0,1])){//保留
                        $countList['1_0'] = isset($countList['1_0']) && !empty($countList['1_0']) ? $countList['1_0'] : 0;
                        $countList['1_0']++;
                        if($is_lock2 != 1){
                            $countList['1_1'] = isset($countList['1_1']) && !empty($countList['1_1']) ? $countList['1_1'] : 0;
                            $countList['1_1']++;
                        }
                    }else{
                        $countList['3_0'] = isset($countList['3_0']) && !empty($countList['3_0']) ? $countList['3_0'] : 0;
                        $countList['3_0']++;
                    }
                }

                //未识别医嘱
                if(in_array($annex_type,[2])){
                    if($mate_count > 0 && in_array($is_lock2,[0,1])){//保留
                        $countList['2_0'] = isset($countList['2_0']) && !empty($countList['2_0']) ? $countList['2_0'] : 0;
                        $countList['2_0']++;
                        if($is_lock2 != 1){
                            $countList['2_1'] = isset($countList['2_1']) && !empty($countList['2_1']) ? $countList['2_1'] : 0;
                            $countList['2_1']++;
                        }
                    }else{
                        $countList['4_0'] = isset($countList['4_0']) && !empty($countList['4_0']) ? $countList['4_0'] : 0;
                        $countList['4_0']++;
                    }
                }
            }
        }
        return $countList;
    }

    /**
     * Notes:附件更新
     * User: lltyy
     * DateTime: 2023/5/30 11:00
     *
     * @param $annex_type//附件类型【1 化验单 2 医嘱单 3 CT】
     */
    public function scanAnnex($annex_type=0,$sig_id=0,$annex_id=0){
        //匹配黑白名单、药物
        $sig_id_arr = [];
        if(!empty($sig_id)){
            $sig_id_arr = [$sig_id];
        }
        //更新附件信息
        $raw_annex_where = $medical_annex_where = $ct_annex_where = 'id < 0';
        if(in_array($annex_type,[0,1])){//化验单
            $raw_annex_where = [
                'is_del' => 0,
                'is_lock' => 0,
            ];
        }

        if(in_array($annex_type,[0,2])){//医嘱单
            $medical_annex_where = [
                'is_del' => 0,
                'is_lock' => 0,
            ];
        }

        if(in_array($annex_type,[0,3,16,17,18])){//CT单
            $ct_annex_where = [
                'is_del' => 0,
                'is_lock' => 0,
            ];
        }
        if(!empty($sig_id_arr)){
            $raw_annex_where['sig_id'] = $medical_annex_where['sig_id'] = $ct_annex_where['sig_id'] = StringHelper::toArray($sig_id_arr);
        }
        $annex_arr = [];
        if(!empty($annex_id)){
            $annex_arr = [$annex_id];
        }
        if(!empty($annex_arr)) {
            $raw_annex_where['annex_id'] = $medical_annex_where['annex_id'] = $ct_annex_where['annex_id'] = StringHelper::toArray($annex_arr);
        }
        $raw_annex_count_arr = $this->LocalService()->ocrRawdata->fetchAnnexData($raw_annex_where);
        $medical_annex_arr = $this->LocalService()->ocrMedical->fetchAnnexData($medical_annex_where);
        $ct_annex_count_arr = $this->LocalService()->ocrCt->fetchAnnexData($ct_annex_where);
        unset($raw_annex_count_arr['mate_id']);
        unset($raw_annex_count_arr['no_mate_id']);
        unset($raw_annex_count_arr['black_id']);
        unset($medical_annex_arr['mate_id']);
        unset($medical_annex_arr['no_mate_id']);
        unset($medical_annex_arr['black_id']);
        unset($ct_annex_count_arr['mate_id']);
        unset($ct_annex_count_arr['no_mate_id']);
        unset($ct_annex_count_arr['black_id']);

        $annex_where = [
            'is_del' => 0,
            'is_lock' => [0, 3]
        ];
        if(!empty($annex_type)){
            $annex_where['annex_type'] = $annex_type;
            if(in_array($annex_type,[3,16,17,18])){
                $annex_where['annex_type'] = [3, 16, 17, 18];
            }
        }else{
            $annex_where['annex_type'] = [1,2,3,16,17,18,19,20];
        }
        if(!empty($sig_id_arr)){
            $annex_where['sig_id'] = StringHelper::toArray($sig_id_arr);
        }
        if(!empty($annex_arr)) {
            $annex_where['id'] = StringHelper::toArray($annex_arr);
        }
        $annex_bool = true;
        $annex_page = 0;
        $annex_limit = 100;
        $last_annex_id = 0;
        while ($annex_bool) {
            $annex_page++;
            echo "AnnexPAGE {$annex_page}：" . PHP_EOL;

            $last_annex_id = $this->updateAnnexCount(ArrayUtils::merge($annex_where, [
                new Operator('id', Operator::OP_GT, $last_annex_id)
            ]),$annex_limit,0,$raw_annex_count_arr,$medical_annex_arr,$ct_annex_count_arr);
            if(!$last_annex_id) $annex_bool = false;
        }
        unset($annex_arr);
        unset($raw_annex_count_arr);
        unset($medical_annex_arr);
        unset($ct_annex_count_arr);
    }

    public function loseAnnexHaveName($where='',array $lose_datas =[]){
        $count = 0;
        $where = 'id < 0';
        $lose_name_arr = [];
        if(!empty($lose_datas)){
            if(empty($where)){
                $where = [
                    'is_del' => 0,
                    'is_lock' => 0,
                    'annex_type' => 0,
                ];
            }
            $lose_where_arr = [];
            foreach($lose_datas as $lose_data){
                $lose_annex_type = $lose_data['annex_type'];
                $lose_where_arr[$lose_annex_type]['or'][] = new Like('annex_name', "%{$lose_data['lose_name']}%");
                $lose_name_arr[$lose_annex_type][] = $lose_data['lose_name'];
            }
            $where_arr = [];
            if(!empty($lose_where_arr)){
                foreach($lose_where_arr as $lose_type=>$lose_where){
                    $where_arr['or'][] = [
                        'annex_type' => [0, $lose_type],
                        [
                            'or' => $lose_where,
                        ]
                    ];
                }
            }
            unset($lose_where_arr);
            if(!empty($where_arr)){
                $where[] = $where_arr;
            }
            unset($where_arr);
        }
        ksort($lose_name_arr);
        $cond = [
            'where'=>$where,
            'columns'=>['id','annex_name','annex_type'],
            'order'=>['id']
        ];
        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            $count = 1;
            $lose_annex_arr = [];
            $lose_rawname_arr = isset($lose_name_arr[1]) && !empty($lose_name_arr[1]) ? $lose_name_arr[1] : [];
            $lose_medicalname_arr = isset($lose_name_arr[2]) && !empty($lose_name_arr[2]) ? $lose_name_arr[2] : [];
            $lose_ctname_arr = isset($lose_name_arr[3]) && !empty($lose_name_arr[3]) ? $lose_name_arr[3] : [];
            foreach($datas as $data){
                $data_id = $data['id'];
                $annex_type = $data['annex_type'];
                if(!empty($annex_type)){
                    $annexName = trim($data['annex_name']);
                    //切割附件名称
                    $annexName = "{$annexName}";
                    $annexName_arr =  !empty($annexName) ? Com::cutIntoSlices($annexName) : [];
                    $annexName_arr = !empty($annexName_arr) ? array_values($annexName_arr) : [];

                    $update_annex_type = 0;
                    //判断是否和化验单批量弃用文件名匹配
                    $mate_lose_arr = !empty($lose_rawname_arr) ? array_uintersect($lose_rawname_arr,$annexName_arr,"strcasecmp") : [];
                    $mate_lose_arr = isset($mate_lose_arr) && !empty($mate_lose_arr) ? array_values($mate_lose_arr) : [];
                    if(!empty($mate_lose_arr)){
                        $update_annex_type = 1;
                    }else{
                        $mate_lose_arr = !empty($lose_medicalname_arr) ? array_uintersect($lose_medicalname_arr,$annexName_arr,"strcasecmp") : [];
                        $mate_lose_arr = isset($mate_lose_arr) && !empty($mate_lose_arr) ? array_values($mate_lose_arr) : [];
                        if(!empty($mate_lose_arr)){
                            $update_annex_type = 2;
                        }else{
                            $mate_lose_arr = !empty($lose_ctname_arr) ? array_uintersect($lose_ctname_arr,$annexName_arr,"strcasecmp") : [];
                            $mate_lose_arr = isset($mate_lose_arr) && !empty($mate_lose_arr) ? array_values($mate_lose_arr) : [];
                            if(!empty($mate_lose_arr)){
                                $update_annex_type = 3;
                            }
                        }
                    }
                    //判断是否和化验单批量弃用文件名匹配
                    if(empty($update_annex_type)){
                        $this->update(['annex_type'=>$update_annex_type],'id = '.$data_id);
                    }
                    unset($annexName_arr);
                    unset($mate_lose_arr);
                }
                $lose_annex_arr[] = $data_id;
            }
            unset($lose_rawname_arr);
            unset($lose_medicalname_arr);
            unset($lose_ctname_arr);

            if(!empty($lose_annex_arr)){
                $this->loseAnnexData([
                    'is_del' => 0,
                    'is_lock' => 0,
                    'id' => StringHelper::toArray($lose_annex_arr)
                ],0);
            }
        }
        unset($where);
        unset($cond);
        unset($datas);
        unset($lose_name_arr);

        return $count;
    }

    /**
     * Notes:
     * User: lltyy
     * DateTime: 2023/6/6 8:52
     *
     */
    public function updateDate($where,$limit=0,$offset=0){
        $cond = [
            'where'=>$where,
            'columns'=>['id','annex_date','annex_type','is_lock'],
            'order'=>['id']
        ];
        if(!empty($limit) || !empty($offset)){
            $cond['limit'] = $limit;
            $cond['offset'] = $offset;
        }
        $datas = $this->fetchAll($cond);
        $count = 0;
        if(!empty($datas)){
            $count = 1;
            foreach($datas as $data){
                $annex_date = trim($data['annex_date']);
                $annex_date = $this->LocalService()->ocrRawdata->replaceRawdata($annex_date,1);
                $annex_date = $annex_date != '' ? $annex_date : '';
                $annex_date = $annex_date != '' ? $annex_date : '';
                if($annex_date != ''){
                    if($data['is_lock'] == 1){
                        $annex_date = substr($annex_date,0,8);
                    }
                }
                $this->update(['annex_date'=>$annex_date],['id'=>$data['id']]);
                if($data['annex_type'] == 1){
                    $this->LocalService()->ocrRawdata->update(['raw_date'=>$annex_date],[
                        'is_del' => 0,
                        'annex_id' => $data['id']
                    ]);
                    if($data['is_lock'] == 1){
                        $this->LocalService()->ocrRawlock->update(['raw_date'=>$annex_date],[
                            'is_del' => 0,
                            'annex_id' => $data['id']
                        ]);
                    }
                }
                if(in_array($data['annex_type'],[2])){
                    $this->LocalService()->ocrMedical->update(['medical_date'=>$annex_date],[
                        'is_del' => 0,
                        'annex_id' => $data['id']
                    ]);
                }

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

    /**
     * Notes:
     * User: lltyy
     * DateTime: 2023/6/6 8:52
     *
     */
    public function scanDate($where){
        $bool = true;
        $page = 0;
        $limit = 1000;
        while ($bool) {
            $page++;
            echo "DatePage {$page}：" . PHP_EOL;
            $offset = intval(($page - 1) * $limit);
            $dataCount = $this->updateDate($where,$limit,$offset);
            if(!$dataCount) $bool = false;
        }
    }

    /**
     * Notes: 验证日期
     * User: lltyy
     * DateTime: 2023/6/25 9:31
     *
     * @param string $annex_date
     * @return array
     */
    public function validAnnexDate($annex_date=''){
        $code = 200;
        $msg = '';
        //截取日期前8位数字
        $annex_date = $this->LocalService()->ocrRawdata->replaceRawdata($annex_date,2);
        $annex_date = substr($annex_date,0,8);

        //判断报告时间是否符合要求
        if(empty($annex_date)){
            $code = 60010;
            $msg = '采集时间不能为空！';
        }else{
            $annex_date_year = substr($annex_date,0,4);
            $annex_date_month = substr($annex_date,4,2);
            $annex_date_day = substr($annex_date,6,2);
            $annex_date_str = $annex_date_year.'-'.$annex_date_month.'-'.$annex_date_day;
            $date_reg = "/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/";
            //匹配日期格式
            if (preg_match ($date_reg, $annex_date_str, $parts)) {
                //检测是否为日期,checkdate为月日年
                if (checkdate($parts[2], $parts[3], $parts[1])) {
                    $code = 200;
                }else{
                    $code = 60011;
                    $msg = '采集时间格式不正确，【正确格式如：20210607】！';
                }
            }else{
                $code = 60011;
                $msg = '采集时间格式不正确，【正确格式如：20210607】！';
            }
            if($code == 200){
                $nowYear = date('Y');
                if(intval($annex_date_year) > $nowYear){
                    $code = 60011;
                    $msg = '采集时间所属年份【'.$annex_date_year.'】不能超过当前年份【'.$nowYear.'】！';
                }else{
                    if(intval($annex_date_year) < 2020){
                        $code = 60012;
                        $msg = '采集时间所属年份【'.$annex_date_year.'】不在【2020~~'.$nowYear.'】之间！';
                    }
                }
            }
        }
        return ['code'=>$code,'msg'=>$msg,'date'=>$annex_date];
    }

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


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

    /**
     * Notes:获取默认日期
     * User: lltyy
     * DateTime: 2023/7/5 9:36
     *
     * @param $annex_type //附件类型【1 化验单 2 医嘱 3 CT】
     * @param $patient_id //受试者ID
     * @param $sig_id //中心ID
     * 获取规则如下：
     *      1》获取 该患者下  最新已锁定的 日期
     *      2》无1》 获取 该患者下  未识别里最新设置的日期
     *      3》无2》 获取 该患者 设置的 入组日期
     *      4》无3》 获取 该中心下最新已锁定的 日期
     */
    public function getDefaultAnnexDate($annex_type,$patient_id,$sig_id,$info_type=0){
        $annex_date = '';
        $annex_type_arr = [$annex_type];
        if(in_array($annex_type,[3,16,17,18])){
            $annex_type_arr = [3,16,17,18];
        }
        //获取该患者下最新锁定的日期
        $preCond = [
            'where'=> [
                'is_del' => 0,
                'patient_id' => $patient_id,
                new Operator('raw_date', Operator::OP_NE, ''),
                new IsNotNull('raw_date'),
                'info_type' => $info_type
            ],
            'columns'=>['raw_date'],
            'order'=>['update_time desc','create_time desc']
        ];
        $preData = $this->LocalService()->ocrRawlock->fetchOne($preCond);
        if(!empty($preData) && isset($preData['raw_date']) && $preData['raw_date'] != ''){
            $annex_date = $preData['raw_date'];
        }else{
            //获取该患者下未识别里最新设置的日期
            $prewhere = [
                'is_del' => 0,
                'is_lock' => 0,
                'annex_type' => StringHelper::toArray($annex_type_arr),
                'patient_id' => $patient_id,
                new Operator('annex_date', Operator::OP_NE, ''),
                new IsNotNull('annex_date'),
                'mate_count' => 0,
                'no_mate_count' => 0,
                'black_count' => 0,
                [
                    'or' => [
                        new Operator('template_id', Operator::OP_GT, 0),
                        new Operator('change_type', Operator::OP_LT, 0),
                    ]
                ]
            ];
            $columns = ['annex_date'];
            if($info_type == 1){
                $prewhere = [
                    'is_del' => 0,
                    'is_lock2' => 0,
                    'annex_type' => StringHelper::toArray($annex_type_arr),
                    'patient_id' => $patient_id,
                    new Operator('annex_date2', Operator::OP_NE, ''),
                    new IsNotNull('annex_date2'),
                    'mate_count2' => 0,
                    'no_mate_count2' => 0,
                    'black_count2' => 0,
                ];
                $columns = ['annex_date'=>'annex_date2'];
            }
            $preCond = [
                'where'=>$prewhere,
                'columns'=>$columns,
                'order'=>['update_time desc']
            ];
            $preData = $this->fetchOne($preCond);
            if(!empty($preData) && isset($preData['annex_date']) && $preData['annex_date'] != ''){
                $annex_date = $preData['annex_date'];
            }else {
                //获取该患者的入组日期
                $group_date = $this->LocalService()->ocrSigpatient->getOneFieldVal('group_date',[
                    'id' => $patient_id
                ]);
                $group_date_str = $this->LocalService()->ocrSigpatient->getTrueGroupDate($group_date);
                if(!empty($group_date_str)){
                    $annex_date = str_replace('-','',$group_date_str);
                }else{
                    //获取 该中心下最新已锁定的 日期
                    $preCond = [
                        'where' => [
                            'is_del' => 0,
                            new Operator('raw_date', Operator::OP_NE, ''),
                            new IsNotNull('raw_date'),
                            'sig_id' => $sig_id,
                            'info_type' => $info_type,
                        ],
                        'columns' => ['raw_date'],
                        'order' => ['update_time desc','create_time desc']
                    ];
                    $preData = $this->LocalService()->ocrRawlock->fetchOne($preCond);
                    if (!empty($preData) && isset($preData['raw_date']) && $preData['raw_date'] != '') {
                        $annex_date = $preData['raw_date'];
                    }
                }
            }
        }
        return $annex_date;
    }

    /**
     * Notes:
     * User: lltyy
     * DateTime: 2023/8/17 9:48
     *
     * @param array $info
     * @return array
     */
    public function fetchAnnexNum(array $info = []){
        $select_type = isset($info['select_type']) && !empty($info['select_type']) ? $info['select_type'] : 0;//查询类型【0 所有数据 1 第一批数据 2 第二批数据】
        $return_type = isset($info['return_type']) && !empty($info['return_type']) ? $info['return_type'] : 0;//中心化验单情况
        $data_type = isset($info['data_type']) && !empty($info['data_type']) ? $info['data_type'] : 0;//数据信息【0 所有 1 化验单 2 CT】
        $sig_id = isset($info['sig_id']) && !empty($info['sig_id']) ? $info['sig_id'] : 0;//中心ID
        $enter_type = isset($info['enter_type']) && !empty($info['enter_type']) ? $info['enter_type'] : 0;//入口【0 正常查询 2 第二批首页入口】

        $arr = [];

        //获取中心化验单情况
        if (empty($return_type)) {
            $arr = [
                'have_raw' => [],//有保留化验单或者有未识别化验单的 人数
                'have_nolock' => [],//保留化验单有未锁定的 人数
                'have_noocr' => [],//未识别化验单待识别的 人数
                'have_ct' => [],//有保留CT或者有未识别CT的 人数
                'havect_nolock' => [],//保留CT有未锁定的 人数
                'havect_noocr' => [],//未识别CT待识别的 人数
                'have_medical' => [],//有保留医嘱或者有未识别医嘱的 人数
                'havemedical_nolock' => [],//保留医嘱有未锁定的 人数
                'havemedical_lock' => [],//保留医嘱有锁定的 人数
                'havemedical_noocr' => [],//未识别医嘱待识别的 人数
            ];
            //获取附件信息
            $where = [
                'is_del' => 0,
            ];
            if($data_type == 0){
                $where[] = [
                    'or'=>[
                        [
                           'annex_type'=>1,
                            'is_lock'=>[0,1,3]
                        ],
                        [
                           'annex_type'=>[3,16,17,18],
                            'is_lock'=>[0,1,3]
                        ],
                        [
                           'annex_type'=>2,
                            'is_lock'=>[0,1,3]
                        ]
                    ]
                ];
            }
            if($data_type == 1){
                $where = ArrayUtils::merge($where, [
                    'annex_type' => 1,
                    'is_lock' => [0, 1, 3],
                ]);
            }
            if($data_type == 2){
                $where = ArrayUtils::merge($where, [
                    'annex_type' => [3, 16, 17, 18],
                    'is_lock' => [0, 1, 3],
                ]);
            }
            if($data_type == 3){
                $where = ArrayUtils::merge($where, [
                    'annex_type' => 2,
                    'is_lock' => [0, 1, 3],
                ]);
            }
            if($data_type == 4){
                $where = ArrayUtils::merge($where, [
                    'annex_type' => 2,
                    new Operator('mate_count2', Operator::OP_GT, 0),
                    'is_lock2' => [0, 1],
                ]);
            }
            //获取未删除的患者
            $patient_where = [
                'is_del' => 0,
                new Operator('pid', Operator::OP_GT, 0),
                new Operator('have_status', Operator::OP_LTE, 1),
            ];
            if (in_array($select_type,[2,3])) {
                $patient_where['select_type'] = $select_type;
            }else{
                $patient_where[] = new Operator('select_type', Operator::OP_NE, 3);;
            }
            if($enter_type == 2){
                $patient_where[] = new Operator('csae_status', Operator::OP_GT, 0);
            }
            $have_patient_arr = $this->LocalService()->ocrSigpatient->fetchCol('id', $patient_where);
            $have_patient_arr = array_values($have_patient_arr);
            if (!empty($have_patient_arr)) {
                $where['patient_id'] = StringHelper::toArray($have_patient_arr);
            } else {
                $where = [new Operator('id',Operator::OP_LT,0)];
            }
            $cond = [
                'where' => $where,
                'columns' => ['annex_type', 'patient_id', 'sig_id', 'mate_count', 'no_mate_count', 'black_count', 'is_lock']
            ];
            if($data_type == 4){
                $cond = [
                    'where' => $where,
                    'columns' => ['annex_type', 'patient_id', 'sig_id', 'mate_count'=>'mate_count2', 'no_mate_count'=>'no_mate_count2', 'black_count'=>'black_count2', 'is_lock'=>'is_lock2']
                ];
            }
            $annexs = $this->fetchAll($cond);
            unset($where);
            unset($cond);
            if (!empty($annexs)) {
                foreach ($annexs as $annexKey => $annex) {
                    $annex_sig = $annex['sig_id'];
                    $annex_patient = $annex['patient_id'];
                    $data_annex_type = $annex['annex_type'];

                    //化验单
                    if ($data_annex_type == 1) {
                        $arr['have_raw'][$annex_sig] = isset($arr['have_raw'][$annex_sig]) && !empty($arr['have_raw'][$annex_sig]) ? $arr['have_raw'][$annex_sig] : [];
                        if(!in_array($annex_patient,$arr['have_raw'][$annex_sig])){
                            $arr['have_raw'][$annex_sig][] = $annex_patient;
                        }
                        //保留化验单未锁定
                        if ($annex['is_lock'] == 0 && (!empty($annex['mate_count']) || !empty($annex['no_mate_count'])) && empty($annex['black_count'])) {//未锁定数据
                            $arr['have_nolock'][$annex_sig] = isset($arr['have_nolock'][$annex_sig]) && !empty($arr['have_nolock'][$annex_sig]) ? $arr['have_nolock'][$annex_sig] : [];
                            if(!in_array($annex_patient,$arr['have_nolock'][$annex_sig])){
                                $arr['have_nolock'][$annex_sig][] = $annex_patient;
                            }
                        }
                        //未识别化验单待识别的
                        if ($annex['is_lock'] == 3) {
                            $arr['have_noocr'][$annex_sig] = isset($arr['have_noocr'][$annex_sig]) && !empty($arr['have_noocr'][$annex_sig]) ? $arr['have_noocr'][$annex_sig] : [];
                            if(!in_array($annex_patient,$arr['have_noocr'][$annex_sig])){
                                $arr['have_noocr'][$annex_sig][] = $annex_patient;
                            }
                        }
                    }

                    //CT
                    if (in_array($data_annex_type, [3, 16, 17, 18])) {
                        $arr['have_ct'][$annex_sig] = isset($arr['have_ct'][$annex_sig]) && !empty($arr['have_ct'][$annex_sig]) ? $arr['have_ct'][$annex_sig] : [];
                        if(!in_array($annex_patient,$arr['have_ct'][$annex_sig])){
                            $arr['have_ct'][$annex_sig][] = $annex_patient;
                        }

                        //保留CT未锁定
                        if ($annex['is_lock'] == 0 && !empty($annex['mate_count'])) {//未锁定数据
                            $arr['havect_nolock'][$annex_sig] = isset($arr['havect_nolock'][$annex_sig]) && !empty($arr['havect_nolock'][$annex_sig]) ? $arr['havect_nolock'][$annex_sig] : [];
                            if(!in_array($annex_patient,$arr['havect_nolock'][$annex_sig])){
                                $arr['havect_nolock'][$annex_sig][] = $annex_patient;
                            }
                        }
                        if(($annex['is_lock'] == 0 && empty($annex['mate_count'])) || $annex['is_lock'] == 3){
                            $arr['havect_noocr'][$annex_sig] = isset($arr['havect_noocr'][$annex_sig]) && !empty($arr['havect_noocr'][$annex_sig]) ? $arr['havect_noocr'][$annex_sig] : [];
                            if(!in_array($annex_patient,$arr['havect_noocr'][$annex_sig])){
                                $arr['havect_noocr'][$annex_sig][] = $annex_patient;
                            }
                        }
                    }

                    //医嘱
                    if (in_array($data_annex_type, [2])) {
                        $arr['have_medical'][$annex_sig] = isset($arr['have_medical'][$annex_sig]) && !empty($arr['have_medical'][$annex_sig]) ? $arr['have_medical'][$annex_sig] : [];
                        if(!in_array($annex_patient,$arr['have_medical'][$annex_sig])){
                            $arr['have_medical'][$annex_sig][] = $annex_patient;
                        }

                        //保留医嘱未锁定
                        if ($annex['is_lock'] == 0 && !empty($annex['mate_count'])) {//未锁定数据
                            $arr['havemedical_nolock'][$annex_sig] = isset($arr['havemedical_nolock'][$annex_sig]) && !empty($arr['havemedical_nolock'][$annex_sig]) ? $arr['havemedical_nolock'][$annex_sig] : [];
                            if(!in_array($annex_patient,$arr['havemedical_nolock'][$annex_sig])){
                                $arr['havemedical_nolock'][$annex_sig][] = $annex_patient;
                            }
                        }

                        //保留医嘱未锁定
                        if ($annex['is_lock'] == 1 && !empty($annex['mate_count'])) {//未锁定数据
                            $arr['havemedical_lock'][$annex_sig] = isset($arr['havemedical_lock'][$annex_sig]) && !empty($arr['havemedical_lock'][$annex_sig]) ? $arr['havemedical_lock'][$annex_sig] : [];
                            if(!in_array($annex_patient,$arr['havemedical_lock'][$annex_sig])){
                                $arr['havemedical_lock'][$annex_sig][] = $annex_patient;
                            }
                        }
                        //未识别医嘱待识别的
                        if ($annex['is_lock'] == 3 || ($annex['is_lock'] == 0 && empty($annex['mate_count']) && empty($annex['no_mate_count']) && empty($annex['black_count']))) {
                            $arr['havemedical_noocr'][$annex_sig] = isset($arr['havemedical_noocr'][$annex_sig]) && !empty($arr['havemedical_noocr'][$annex_sig]) ? $arr['havemedical_noocr'][$annex_sig] : [];
                            if(!in_array($annex_patient,$arr['havemedical_noocr'][$annex_sig])){
                                $arr['havemedical_noocr'][$annex_sig][] = $annex_patient;
                            }
                        }
                    }

                    unset($annexs[$annexKey]);
                }
            }
            unset($annexs);
        }

        if ($return_type == 1) {
            //获取化验单信息
            $arr = [
                'lock' => [],//未锁定保留化验单数据
                'lose' => [],//未弃用无效化验单数据
                'ocr' => [],//未标注（待识别）未识别的化验单数据
                'wait_ocr' => [],//标注（待识别）未识别的化验单数据
                'have_annex' => [],//有已锁定的患者
                'no_handle_cat' => [],//未分类未弃用附件
                'lock_ct' => [],//保留CT已锁定数据
                'noocr_ct' => [],//未标注（待识别）未识别的CT数据
                'waitocr_ct' => [],//标注（待识别）未识别的CT数据
                'lock_medical' => [],//未锁定用药数据
            ];
            $where = [
                'is_del' => 0,
                'sig_id' => $sig_id,
                'annex_type' => [0,1,2,3,16,17,18],
                new Operator('is_lock', Operator::OP_NE, 2)
            ];
            if($data_type == 4){
                $where = [
                    'is_del' => 0,
                    'sig_id' => $sig_id,
                    'annex_type' => 2,
                    new Operator('mate_count2', Operator::OP_GT, 0),
                    'is_lock2' => [0, 1],
                ];
            }
            //获取未删除的患者
            $patient_where = [
                'is_del' => 0,
                'pid' => $sig_id,
                new Operator('pid', Operator::OP_GT, 0),
                new Operator('have_status', Operator::OP_LTE, 1)
            ];
            if (in_array($select_type,[2,3])) {
                $patient_where['select_type'] = $select_type;
            }else{
                $patient_where[] = new Operator('select_type', Operator::OP_NE, 3);
            }
            $have_patient_arr = $this->LocalService()->ocrSigpatient->fetchCol('id', $patient_where);
            $have_patient_arr = array_values($have_patient_arr);
            if (!empty($have_patient_arr)) {
                $where['patient_id'] = StringHelper::toArray($have_patient_arr);
            } else {
                $where = [new Operator('id',Operator::OP_LT,0)];
            }

            //获取有附件的中心ID
            $cond = [
                'where' => $where,
                'columns' => ['patient_id', 'annex_type', 'mate_count', 'no_mate_count', 'black_count', 'is_lock']
            ];
            if($data_type == 4){
                $cond = [
                    'where' => $where,
                    'columns' => ['patient_id', 'annex_type', 'mate_count'=>'mate_count2', 'no_mate_count'=>'no_mate_count2', 'black_count'=>'black_count2', 'is_lock'=>'is_lock2']
                ];
            }
            $annexs = $this->fetchAll($cond);
            unset($annex_cond);
            if (!empty($annexs)) {
                foreach ($annexs as $annex_key => $annex) {
                    $annex_patient = $annex['patient_id'];

                    //化验单
                    if ($annex['annex_type'] == 1) {
                        if ($annex['is_lock'] == 0) {//未锁定、未弃用
                            //未锁定保留化验单
                            if ((!empty($annex['mate_count']) || !empty($annex['no_mate_count'])) && empty($annex['black_count'])) {
                                $arr['lock'][$annex_patient] = isset($arr['lock'][$annex_patient]) && !empty($arr['lock'][$annex_patient]) ? $arr['lock'][$annex_patient] : 0;
                                $arr['lock'][$annex_patient] += 1;
                            }

                            //未弃用无效化验单
                            if (!empty($annex['black_count'])) {
                                $arr['lose'][$annex_patient] = isset($arr['lose'][$annex_patient]) && !empty($arr['lose'][$annex_patient]) ? $arr['lose'][$annex_patient] : 0;
                                $arr['lose'][$annex_patient] += 1;
                            }

                            //未标注（待识别）未识别的化验单数据
                            if (empty($annex['mate_count']) && empty($annex['no_mate_count']) && empty($annex['black_count'])) {
                                $arr['ocr'][$annex_patient] = isset($arr['ocr'][$annex_patient]) && !empty($arr['ocr'][$annex_patient]) ? $arr['ocr'][$annex_patient] : 0;
                                $arr['ocr'][$annex_patient] += 1;
                            }
                        }

                        if ($annex['is_lock'] == 3) {
                            $arr['wait_ocr'][$annex_patient] = isset($arr['wait_ocr'][$annex_patient]) && !empty($arr['wait_ocr'][$annex_patient]) ? $arr['wait_ocr'][$annex_patient] : 0;
                            $arr['wait_ocr'][$annex_patient] += 1;
                        }

                        if ($annex['is_lock'] == 1 && !in_array($annex_patient, $arr['have_annex'])) {
                            $arr['have_annex'][] = $annex_patient;
                        }
                    }

                    //未分类
                    if ($annex['annex_type'] == 0 && !in_array($annex_patient, $arr['no_handle_cat'])) {//未分类
                        $arr['no_handle_cat'][] = $annex_patient;
                    }

                    //CT
                    if (in_array($annex['annex_type'], [3, 16, 17, 18])) {
                        if ($annex['is_lock'] == 1 || ($annex['is_lock'] == 0 && !empty($annex['mate_count']))) {
                            //未锁定保留CT
                            if ($annex['is_lock'] == 0 && !empty($annex['mate_count'])) {
                                $arr['nolock_ct'][$annex_patient] = isset($arr['nolock_ct'][$annex_patient]) && !empty($arr['nolock_ct'][$annex_patient]) ? $arr['nolock_ct'][$annex_patient] : 0;
                                $arr['nolock_ct'][$annex_patient] += 1;
                            }

                            //已锁定保留CT
                            if ($annex['is_lock'] == 1) {
                                $arr['lock_ct'][$annex_patient] = isset($arr['lock_ct'][$annex_patient]) && !empty($arr['lock_ct'][$annex_patient]) ? $arr['lock_ct'][$annex_patient] : 0;
                                $arr['lock_ct'][$annex_patient] += 1;
                            }
                        }

                        if ($annex['is_lock'] == 0 && empty($annex['mate_count'])) {//未标注（待识别）未识别的CT数据
                            $arr['noocr_ct'][$annex_patient] = isset($arr['noocr_ct'][$annex_patient]) && !empty($arr['noocr_ct'][$annex_patient]) ? $arr['noocr_ct'][$annex_patient] : 0;
                            $arr['noocr_ct'][$annex_patient] += 1;
                        }

                        if ($annex['is_lock'] == 3) {//标注（待识别）未识别的CT数据
                            $arr['waitocr_ct'][$annex_patient] = isset($arr['waitocr_ct'][$annex_patient]) && !empty($arr['waitocr_ct'][$annex_patient]) ? $arr['waitocr_ct'][$annex_patient] : 0;
                            $arr['waitocr_ct'][$annex_patient] += 1;
                        }

                    }


                    //医嘱
                    if (in_array($annex['annex_type'], [2])) {
                        if ($annex['is_lock'] == 1 || ($annex['is_lock'] == 0 && !empty($annex['mate_count']))) {
                            //未锁定保留医嘱
                            if ($annex['is_lock'] == 0 && !empty($annex['mate_count'])) {
                                $arr['nolock_medical'][$annex_patient] = isset($arr['nolock_medical'][$annex_patient]) && !empty($arr['nolock_medical'][$annex_patient]) ? $arr['nolock_medical'][$annex_patient] : 0;
                                $arr['nolock_medical'][$annex_patient] += 1;
                            }

                            //已锁定保留医嘱
                            if ($annex['is_lock'] == 1) {
                                $arr['lock_medical'][$annex_patient] = isset($arr['lock_medical'][$annex_patient]) && !empty($arr['lock_medical'][$annex_patient]) ? $arr['lock_medical'][$annex_patient] : 0;
                                $arr['lock_medical'][$annex_patient] += 1;
                            }
                        }

                        if ($annex['is_lock'] == 0 && empty($annex['mate_count'])) {//未标注（待识别）未识别的医嘱数据
                            $arr['noocr_medical'][$annex_patient] = isset($arr['noocr_medical'][$annex_patient]) && !empty($arr['noocr_medical'][$annex_patient]) ? $arr['noocr_medical'][$annex_patient] : 0;
                            $arr['noocr_medical'][$annex_patient] += 1;
                        }

                        if ($annex['is_lock'] == 3) {//标注（待识别）未识别的医嘱数据
                            $arr['waitocr_medical'][$annex_patient] = isset($arr['waitocr_ct'][$annex_patient]) && !empty($arr['waitocr_medical'][$annex_patient]) ? $arr['waitocr_medical'][$annex_patient] : 0;
                            $arr['waitocr_medical'][$annex_patient] += 1;
                        }
                    }

                    unset($annexs[$annex_key]);
                }
            }
            unset($annexs);
        }


        //获取中心住院及自费单据数量
        if ($return_type == 2) {
            //获取附件信息
            $where = [
                'is_del' => 0,
                'annex_type' => 8,
                'sig_id' => $sig_id,
            ];
            //获取未删除的患者
            $patient_where = [
                'is_del' => 0,
                new Operator('pid', Operator::OP_GT, 0),
                new Operator('have_status', Operator::OP_LTE, 1),
                'pid' => $sig_id,
            ];
            if (in_array($select_type,[2,3])) {
                $patient_where['select_type'] = $select_type;
            }else{
                $patient_where[] = new Operator('select_type', Operator::OP_NE, 3);
            }
            $have_patient_arr = $this->LocalService()->ocrSigpatient->fetchCol('id', $patient_where);
            $have_patient_arr = array_values($have_patient_arr);
            if (!empty($have_patient_arr)) {
                $where['patient_id'] = StringHelper::toArray($have_patient_arr);
            } else {
                $where = [new Operator('id',Operator::OP_LT,0)];
            }
            $cond = [
                'where' => $where,
                'columns' => ['patient_id', 'id']
            ];
            $annexs = $this->fetchAll($cond);
            unset($where);
            unset($cond);
            if (!empty($annexs)) {
                foreach ($annexs as $annexKey => $annex) {
                    $annex_patient = $annex['patient_id'];
                    $arr[$annex_patient] = isset($arr[$annex_patient]) && !empty($arr[$annex_patient]) ? $arr[$annex_patient] : 0;
                    $arr[$annex_patient] += 1;
                    unset($annexs[$annexKey]);
                }
            }
            unset($annexs);
        }


        return $arr;
    }
}