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

namespace Application\Service\DB\Project;
use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\ContainerInterface;
use Application\Common\Ocr;
use Application\Service\Extension\Uploader\adapter\Oss;
use Application\Service\Extension\Uploader\ImageUploader;
use Application\Service\Extension\Laminas;
use Laminas\Db\Sql\Expression;
use Laminas\Db\Sql\Predicate\NotIn;
use Laminas\Db\Sql\Predicate\Operator;
use Laminas\Db\Sql\Sql;
use Laminas\Db\Sql\Where;
use Laminas\Db\ResultSet\ResultSet;
use Laminas\Db\Adapter\Adapter;

class Patientworkannex extends \Application\Service\DB\AbstractDb {
    private $ossPath = 'http://yikeen.oss-cn-beijing.aliyuncs.com';
    public function __construct($dbAdapter, ContainerInterface $container) {
        $this->tableName = 'yikeen_item_patientworkannex';
        parent::__construct($dbAdapter, $container);
    }
    //批量识别数据
    public function manyReidentification()
    {
        $twoTime = strtotime('-1 day ',strtotime(date('Y-m-d')));
        //附件===未锁定
        $whereItemPatientworkannex['where'] = [
            'lock_state' => 0,
            'ocr_state' => 0,
            'confirm_state' => 0,
            'change_type' => 0,
            'annex_type' => [1, 5],
            new Operator('ocr_id', Operator::OP_NE, 0),
            new Operator('up_annex_date', Operator::OP_GTE, $twoTime)
        ];
        $itemPatientworkannexData = $this->LocalService()->itemPatientworkannex->fetchAll($whereItemPatientworkannex);
        if (empty($itemPatientworkannexData)){
            return false;
        }
        foreach ($itemPatientworkannexData as $itemPatientworkannexDataKey=>$itemPatientworkannexDataValue) {
            $cacheOcrId = $this->LocalService()->itemIdentificationresult->getOcrTemplateCache($itemPatientworkannexDataValue);
            !empty($cacheOcrId) && $itemPatientworkannexDataValue['ocr_id'] = $cacheOcrId;
            $result = $this->reidentification($itemPatientworkannexDataValue['id'],$itemPatientworkannexDataValue['ocr_id']);
            if (empty($result)){
                continue;
            }
        }
        return $result;
        echo $result;die;

        $itemPatientworkannexIdS= implode(',',array_column($itemPatientworkannexData,'id'));
        $itemPatientworkannexPatientFormIdS= implode(',',array_column($itemPatientworkannexData,'patient_form_id'));
//
        $itemPatientworkannexOcrIdData= array_filter(array_column($itemPatientworkannexData,'ocr_id'));
        if (empty($itemPatientworkannexOcrIdData)) return false;

        //cs
        $whereItemIdentificationresultchange = 'is_del=0 and patient_form_id in('.$itemPatientworkannexPatientFormIdS.')';
        $itemIdentificationresultchangeData = $this->LocalService()->itemIdentificationresultchange->fetchCol('patient_form_id',$whereItemIdentificationresultchange);

        //识别数据
        $whereItemIdentificationresult = 'is_del=0 and workannex_id in('.$itemPatientworkannexIdS.')';//['where']
        $itemIdentificationresultData = $this->LocalService()->itemIdentificationresult->fetchCol('workannex_id',$whereItemIdentificationresult);

        //识别模板设置数据
        $whereDictionaryOcr['where'] = [
            'is_del' => 0,
            'id' => $itemPatientworkannexOcrIdData,
        ];

        $whereDictionaryOcr['columns'] = ['id','name','key_val','separator'];
        $dictionaryOcrData = $this->LocalService()->dictionaryOcr->fetchAll($whereDictionaryOcr);

        if (empty($dictionaryOcrData)) return false;
        $dictionarySetocrfieldData = $this->LocalService()->dictionarySetocrfield->getAllGroupType();
        foreach ($itemPatientworkannexData as $itemPatientworkannexDataKey=>$itemPatientworkannexDataValue) {
            if (!empty($itemIdentificationresultchangeData)){
                $itemIdentificationresultchangeData = array_reverse($itemIdentificationresultchangeData);
                if (isset($itemIdentificationresultchangeData[$itemPatientworkannexDataValue['patient_form_id']])){
                    continue;
                }
            }

            if (empty($itemPatientworkannexDataValue['annex_path'])){
                continue;
            }
            $imgPath = $this->ossPath.$itemPatientworkannexDataValue['annex_path'];
            foreach ($dictionaryOcrData as $dictionaryOcrDataKey=>$dictionaryOcrDataValue){
                if ($itemPatientworkannexDataValue['ocr_id'] == $dictionaryOcrDataValue['id']){
                    if (empty($dictionaryOcrDataValue['key_val']) || empty($dictionaryOcrDataValue['separator'])){
                        continue;
                    }
                    $result = Ocr::ocr_result($imgPath, $dictionaryOcrDataValue['key_val'], $dictionaryOcrDataValue['separator'], $dictionarySetocrfieldData);
                    if ($result['error_code'] != 0) {
                        continue;
                    }

                    $upWorkannex['ocr_state'] = 1;
                    $upWorkannex['is_result'] = 1;
//                    $upWorkannex['confirm_state'] = 0;
                    $upWorkannex['id'] = $itemPatientworkannexDataValue['id'];
                    $resultItemPatientworkannex = $this->LocalService()->itemPatientworkannex->save($upWorkannex);
                    $arr['content'] = json_encode($result);
                    $arr['workannex_id'] = $itemPatientworkannexDataValue['id'];
                    empty($itemIdentificationresultData) && $itemIdentificationresultData = array_reverse($itemIdentificationresultData);
                    if (isset($itemIdentificationresultData[$dictionaryOcrDataValue['id']])){
                        $resultId = $arr['id'] = $itemIdentificationresultData[$dictionaryOcrDataValue['id']];
                        $resultItemIdentificationresult = $this->LocalService()->itemIdentificationresult->save($arr);
                    }else{
                        $resultId = $this->LocalService()->itemIdentificationresult->save($arr);
                    }
                }
            }


        }
        return true;

    }

    //重新识别
    public function reidentification($id,$ocr_id)
    {
        if (empty($id) || empty($ocr_id)) {

            return false;
        }
        $whereItemPatientworkannex['where'] = [
            'is_del' => 0,
            'id' => $id
        ];
        $itemPatientworkannexInfo = $this->LocalService()->itemPatientworkannex->fetchOne($whereItemPatientworkannex);
        $whereItemIdentificationresult['where'] = [
            'is_del' => 0,
            'workannex_id' => $id
        ];
        $itemIdentificationresultInfo = $this->LocalService()->itemIdentificationresult->fetchOne($whereItemIdentificationresult);

        //识别模板设置数据
        $whereDictionaryOcr['where'] = [
            'is_del' => 0,
            'id' => $ocr_id,
        ];
        $whereDictionaryOcr['columns'] = ['id','name','key_val','separator'];
        $dictionaryOcrInfo = $this->LocalService()->dictionaryOcr->fetchOne($whereDictionaryOcr);
        if (empty($dictionaryOcrInfo)){
            return false;
//            return $this->RenderApiJson()->Error(StatusCode::E_FIELD_VALIDATOR, '识别模板不存在');
        }
        if (empty($dictionaryOcrInfo['key_val'])){
//            return $this->RenderApiJson()->Error(StatusCode::E_FIELD_VALIDATOR, '识别模板Key值不存在');
        }
        if (empty($dictionaryOcrInfo['separator'])){
            return false;
//            return $this->RenderApiJson()->Error(StatusCode::E_FIELD_VALIDATOR, '识别模板分割符不存在');
        }

        $dictionarySetocrfieldData = $this->LocalService()->dictionarySetocrfield->getAllGroupType();
        $fileUpLoader = new Oss(Laminas::$app->getConfig()['image_uploader']);
        $imgPath =  $fileUpLoader->getSignUrl($itemPatientworkannexInfo['annex_path']);//.$itemPatientworkannexInfo['annex_path'];

        $result = Ocr::ocr_result($imgPath, $dictionaryOcrInfo['key_val'], $dictionaryOcrInfo['separator'], $dictionarySetocrfieldData);
        if ($result['error_code'] != 0) {
            return false;
//            return $this->RenderApiJson()->Error(StatusCode::E_FIELD_VALIDATOR, '识别错误，请检查模板和文件');
        }


        $upWorkannex['ocr_state'] = 1;
        $upWorkannex['is_result'] = 1;
//        $upWorkannex['change_type'] = 1;
        $upWorkannex['ocr_id'] = $ocr_id;
        $upWorkannex['id'] = $id;
        $resultItemPatientworkannex = $this->LocalService()->itemPatientworkannex->save($upWorkannex);
        $arr['content'] = json_encode($result);
        $arr['workannex_id'] = $id;
        if (!empty($itemIdentificationresultInfo)){
            $id = $arr['id'] = $itemIdentificationresultInfo['id'];
            $resultItemIdentificationresult = $this->LocalService()->itemIdentificationresult->save($arr);
            if (empty($resultItemIdentificationresult)){
                return true;
//                return $this->RenderApiJson()->Success(200,'识别结果已一致');
            }else{
                return true;
//                return $this->RenderApiJson()->Success();
            }

        }else{
            $id = $this->LocalService()->itemIdentificationresult->save($arr);
            if (!empty($id)){
                return true;
//                return $this->RenderApiJson()->Success();
            }
        }


    }

    //批量识别数据
    public function manyReidentificaBacktion()
    {
        //附件===未锁定
        $whereItemPatientworkannex['where'] = [
            'lock_state' => 0,
            'orc_state' => 0,
            'confirm_state' => 0,
            'is_result' => 0,
            'annex_type' => 1,
            new Operator('ocr_id', Operator::OP_NE, 0)
        ];
        $itemPatientworkannexData = $this->LocalService()->itemPatientworkannex->fetchAll($whereItemPatientworkannex);
        if (empty($itemPatientworkannexData)){
            return false;
        }
        $itemPatientworkannexIdS= implode(',',array_column($itemPatientworkannexData,'id'));
        $itemPatientworkannexPatientFormIdS= implode(',',array_column($itemPatientworkannexData,'patient_form_id'));
//
        $itemPatientworkannexOcrIdData= array_filter(array_column($itemPatientworkannexData,'ocr_id'));
        if (empty($itemPatientworkannexOcrIdData)) return false;

        //cs
        $whereItemIdentificationresultchange = 'is_del=0 and patient_form_id in('.$itemPatientworkannexPatientFormIdS.')';
        $itemIdentificationresultchangeData = $this->LocalService()->itemIdentificationresultchange->fetchCol('patient_form_id',$whereItemIdentificationresultchange);

        //识别数据
        $whereItemIdentificationresult = 'is_del=0 and workannex_id in('.$itemPatientworkannexIdS.')';//['where']
        $itemIdentificationresultData = $this->LocalService()->itemIdentificationresult->fetchCol('workannex_id',$whereItemIdentificationresult);

        //识别模板设置数据
        $whereDictionaryOcr['where'] = [
            'is_del' => 0,
            'id' => $itemPatientworkannexOcrIdData
        ];

        $whereDictionaryOcr['columns'] = ['id','name','key_val','separator'];
        $dictionaryOcrData = $this->LocalService()->dictionaryOcr->fetchAll($whereDictionaryOcr);

        if (empty($dictionaryOcrData)) return false;
        $dictionarySetocrfieldData = $this->LocalService()->dictionarySetocrfield->getAllGroupType();
        foreach ($itemPatientworkannexData as $itemPatientworkannexDataKey=>$itemPatientworkannexDataValue) {
            if (!empty($itemIdentificationresultchangeData)){
                $itemIdentificationresultchangeData = array_reverse($itemIdentificationresultchangeData);
                if (isset($itemIdentificationresultchangeData[$itemPatientworkannexDataValue['patient_form_id']])){
                    continue;
                }
            }


//            //识别数据
//            $whereItemIdentificationresult['where'] = 'is_del=0 and workannex_id = '.$itemPatientworkannexDataValue['id'];
//            $itemIdentificationresultInfo = $this->LocalService()->itemIdentificationresult->fetchOne($whereItemIdentificationresult);
//            //识别模板设置数据
//            $whereDictionaryOcr['where'] = 'is_del=0 and id='.$itemPatientworkannexDataValue['ocr_id'];
//            $whereDictionaryOcr['columns'] = ['id','name','key_val','separator'];
//            $dictionaryOcrInfo = $this->LocalService()->dictionaryOcr->fetchOne($whereDictionaryOcr);
//            if (empty($dictionaryOcrInfo) || empty($dictionaryOcrInfo['key_val']) || empty($dictionaryOcrInfo['separator']) || empty($itemPatientworkannexDataValue['annex_path'])){
//                continue;
//            }
//            $imgPath = $this->ossPath.$itemPatientworkannexDataValue['annex_path'];
//            $result = Ocr::ocr_result($imgPath, $dictionaryOcrInfo['key_val'], $dictionaryOcrInfo['separator'], $dictionarySetocrfieldData);
            if (empty($itemPatientworkannexDataValue['annex_path'])){
                continue;
            }
            $imgPath = $this->ossPath.$itemPatientworkannexDataValue['annex_path'];
            foreach ($dictionaryOcrData as $dictionaryOcrDataKey=>$dictionaryOcrDataValue){
                if ($itemPatientworkannexDataValue['ocr_id'] == $dictionaryOcrDataValue['id']){
                    if (empty($dictionaryOcrDataValue['key_val']) || empty($dictionaryOcrDataValue['separator'])){
                        continue;
                    }
                    $result = Ocr::ocr_result($imgPath, $dictionaryOcrDataValue['key_val'], $dictionaryOcrDataValue['separator'], $dictionarySetocrfieldData);
                    if ($result['error_code'] != 0) {
                        continue;
                    }

                    $upWorkannex['ocr_state'] = 1;
                    $upWorkannex['is_result'] = 1;
//                    $upWorkannex['confirm_state'] = 0;
                    $upWorkannex['id'] = $itemPatientworkannexDataValue['id'];
                    $resultItemPatientworkannex = $this->LocalService()->itemPatientworkannex->save($upWorkannex);
                    $arr['content'] = json_encode($result);
                    $arr['workannex_id'] = $itemPatientworkannexDataValue['id'];
                    empty($itemIdentificationresultData) && $itemIdentificationresultData = array_reverse($itemIdentificationresultData);
                    if (isset($itemIdentificationresultData[$dictionaryOcrDataValue['id']])){
                        $resultId = $arr['id'] = $itemIdentificationresultData[$dictionaryOcrDataValue['id']];
                        $resultItemIdentificationresult = $this->LocalService()->itemIdentificationresult->save($arr);
                    }else{
                        $resultId = $this->LocalService()->itemIdentificationresult->save($arr);
                    }
                }
            }


        }
        return true;

    }

    /**
     * Notes: 获取信息
     * User: lltyy
     * DateTime: 2024/8/15 10:26
     *
     * @param array $arr
     * @return array
     */
    public function countByStatus($arr = []){
//        $startTime = microtime(true);
        $item_id = isset($arr['item_id']) && !empty($arr['item_id']) ? trim($arr['item_id'],',') : 0;//项目ID 字符串

        $info = [
            'cancelNum'=>0,//作废
            'supplementNum'=>0,//补图
            'unlockNoOperateNum'=>0,//申请解锁/解锁未更新
            'unlockOperateNum'=>0,//解锁已更新
//            'child_item'=>[]//副项目IDS
        ];

        $where = [
            'is_del' => 0,
            'item_id' => $item_id,
            new Operator('annex_type', Operator::OP_NE, 2),
            new Operator('category_id', Operator::OP_NE, 0),
        ];

        $datas = $this->fetchAll([
            'where'=>$where,
            'columns'=>['id','item_id','checktime_id','patient_form_id','lock_state','is_unlock','unlock_id','is_question_date','is_lock_operate','annex_type','change_type'],
            'group'=>['checktime_id','patient_form_id','form_id'],
        ]);

        unset($where);
        if(!empty($datas)){
            //识别结果信息
            $patientChangeArr = [];
            $checktime_id_arr = array_values(array_unique(array_column($datas,'checktime_id')));
            $patientChangeDatas = $this->LocalService()->itemIdentificationresultchange->fetchAll([
                'where'=> [
                    'is_del' => 0,
                    'checktime_id' => StringHelper::toArray($checktime_id_arr),
                    new Operator('is_annul', Operator::OP_GT, 0),
                ],
                'columns'=>['id','is_annul','patient_form_id','checktime_id'],
            ]);


            if(!empty($patientChangeDatas)){
                foreach($patientChangeDatas as $patientChangeDataKey=>$patientChangeData){
                    $patientChange_key = $patientChangeData['patient_form_id'].'_'.$patientChangeData['checktime_id'];
                    $patientChangeArr[$patientChange_key][] = $patientChangeData;
                    unset($patientChange_key);
                }
            }
            unset($patientChangeDatas);
//            annex_type = 4 change_type = 0


            //申请解锁信息
            $unlock_id_arr = $patientFormUnlockArr = [];
            foreach($datas as $dataInfo){
                if (!empty($dataInfo['is_unlock'])){
                    $unlock_id_arr[] = $dataInfo['is_unlock'];
                }
                if (!empty($dataInfo['unlock_id'])){
                    $unlock_id_arr[] = $dataInfo['unlock_id'];
                }
            }
            if(!empty($unlock_id_arr)){
                $patientFormUnlockDatas = $this->LocalService()->patientFormUnlock->fetchAll([
                    'where'=> [
                        'is_del' => 0,
                        'id' => StringHelper::toArray($unlock_id_arr),
                    ],
                    'columns'=>['id','unlock_note']
                ]);
            }
            unset($unlock_id_arr);
            if(!empty($patientFormUnlockDatas)){
                foreach($patientFormUnlockDatas as $patientFormUnlockDataKey=>$patientFormUnlockData){
                    $patientFormUnlockArr[$patientFormUnlockData['id']] = $patientFormUnlockData;
                    unset($patientFormUnlockDatas[$patientFormUnlockDataKey]);
                }
            }
            unset($patientFormUnlockDatas);
            foreach($datas as $key=>$data){
                $patientFormId_ChecktimeId =  $data['patient_form_id'].'_'.$data['checktime_id'];

                //补单状态【0 全部 4 补单】
                $is_annex_type = 0;

                if($data['annex_type'] == 4 && $data['change_type'] == 0){
                    $is_annex_type = 4;
                }
//                if(in_array($patientFormId_ChecktimeId,$patientworkAnnexTypeArr)) {
//                    $is_annex_type = 4;
//                }

                //作废状态【0 全部 1 作废】
                $is_annul = 0;
                if(!empty($data['lock_state']) && isset($patientChangeArr[$patientFormId_ChecktimeId]) && !empty($patientChangeArr[$patientFormId_ChecktimeId])){
                    $is_annul = 1;
                }
                
//                $patientChangeAnnulData = !empty($data['lock_state']) && isset($patientChangeArr[$patientFormId_ChecktimeId]) && !empty($patientChangeArr[$patientFormId_ChecktimeId]) ? array_values(array_column($patientChangeArr[$patientFormId_ChecktimeId],'is_annul')) : [];
//                if(!empty($patientChangeAnnulData) && in_array(1,$patientChangeAnnulData)) {
//                    $is_annul = 1;
//                }
                unset($patientChangeAnnulData);
                unset($patientFormId_ChecktimeId);

                //申请解锁
                $is_unlock = $data['is_unlock'];
                $unlock_id = $data['unlock_id'];
                if (!empty($is_unlock)){
                    $is_unlock = isset($patientFormUnlockArr[$is_unlock]) && !empty($patientFormUnlockArr[$is_unlock]) ? $patientFormUnlockArr[$is_unlock]['id'] : 0;
                }

                $is_noover = 0;
                //作废
                if($is_annul != 0 || $data['is_question_date'] > 0){
                    $info['cancelNum'] += 1;
                    $is_noover = 1;
                }else{
                    //补图
                    if($is_annex_type != 0){
                        $info['supplementNum'] += 1;
                        $is_noover = 1;
                    }else{
                        //申请解锁/解锁未更新
                        if($is_unlock != 0 || ($unlock_id != 0 && empty($data['is_lock_operate']))){
                            $info['unlockNoOperateNum'] += 1;
                            $is_noover = 1;
                        }else{
                            //解锁已更新
                            if($data['is_lock_operate'] == 1){
                                $info['unlockOperateNum'] += 1;
                                $is_noover = 1;
                            }
                        }
                    }
                }

//                if(!in_array($data['item_id'],$info['child_item_id']) && (empty($data['lock_state']) || $is_noover == 1)){
//                    $info['child_item'][] = $data['item_id'];
//                }

                unset($is_annul);
                unset($is_unlock);
                unset($unlock_id);
                unset($is_annex_type);
                unset($is_noover);
                unset($datas[$key]);
            }
            //释放空间
            unset($patientChangeArr);
//            unset($patientworkAnnexTypeArr);
            unset($patientFormUnlockArr);
        }
        unset($datas);
//                    $endTime = microtime(true);
//            $executionTime = $endTime - $startTime;
//            echo "程序运行时间：{$executionTime} 秒";die;
        return $info;
    }


    public function fetchDataNum($arr = []){
        $noitem_id = isset($arr['noitem_id']) && !empty($arr['noitem_id']) ? trim($arr['noitem_id'],',') : 0;//去除项目ID 字符串

        $item_id = isset($arr['item_id']) && !empty($arr['item_id']) ? trim($arr['item_id'],',') : 0;//项目ID 字符串

        $itemsig_id = isset($arr['itemsig_id']) && !empty($arr['itemsig_id']) ? trim($arr['itemsig_id'],',') : 0;//项目中心ID 字符串


        $info = [
            'cancelNum'=>0,//作废

            'supplementNum'=>0,//补图

            'unlockNoOperateNum'=>0,//申请解锁/解锁未更新

            'unlockOperateNum'=>0,//解锁已更新

            'child_item'=>[]//副项目IDS

        ];

        $where = [
            'is_del' => 0,
            new Operator('annex_type', Operator::OP_NE, 2),
            new Operator('category_id', Operator::OP_NE, 0),
            new Operator('item_id', Operator::OP_GT, 0),
        ];
        if(!empty($item_id)){
            $where['item_id'] = StringHelper::toArray($item_id);
        }
        if(!empty($noitem_id)){
            $where[] = new NotIn('item_id', StringHelper::toArray($noitem_id));
        }
        if(!empty($itemsig_id)){
            $where['itemsig_id'] = $itemsig_id;
        }
        $datas = $this->fetchAll([
            'where'=>$where,
            'columns'=>['item_id','checktime_id','patient_form_id','lock_state','is_unlock','unlock_id','is_question_date','is_lock_operate'],
            'group'=>['checktime_id','patient_form_id','form_id'],
            'order'=>['id desc']
        ]);
        unset($where);
        if(!empty($datas)){
            //识别结果信息

            $patientChangeArr = [];
            $patient_form_id_arr = array_values(array_unique(array_column($datas,'patient_form_id')));
            $checktime_id_arr = array_values(array_unique(array_column($datas,'checktime_id')));

            $patientChangeDatas = $this->LocalService()->itemIdentificationresultchange->fetchAll([
                'where'=> [
                    'is_del' => 0,
                    'patient_form_id' => StringHelper::toArray($patient_form_id_arr),
                    'checktime_id' => StringHelper::toArray($checktime_id_arr),
                ],
                'columns'=>['id','item_id','itemsig_id','patient_id','checktime_id','patient_form_id','report_type','lock_state','is_annul','item_name']
            ]);
            if(!empty($patientChangeDatas)){
                foreach($patientChangeDatas as $patientChangeDataKey=>$patientChangeData){
                    $patientChange_key = $patientChangeData['patient_form_id'].'_'.$patientChangeData['checktime_id'];
                    $patientChangeArr[$patientChange_key][] = $patientChangeData;
                    unset($patientChangeDatas[$patientChangeDataKey]);
                    unset($patientChange_key);
                }
            }
            unset($patientChangeDatas);

            //附件类型

            $patientworkAnnexTypeArr = [];
            $patientworkAnnexTypeDatas = $this->fetchAll([
                'where'=> [
                    'is_del' => 0,
                    'change_type' => 0,
                    'patient_form_id' => StringHelper::toArray($patient_form_id_arr),
                    'checktime_id' => StringHelper::toArray($checktime_id_arr),
                    'annex_type' => 4,
                ],
                'columns'=>['patient_form_id','checktime_id']
            ]);
            if(!empty($patientworkAnnexTypeDatas)){
                foreach($patientworkAnnexTypeDatas as $patientworkAnnexTypeDataKey=>$patientworkAnnexTypeData){
                    $patientworkAnnexTypeArr[] = $patientworkAnnexTypeData['patient_form_id'].'_'.$patientworkAnnexTypeData['checktime_id'];
                    unset($patientworkAnnexTypeDatas[$patientworkAnnexTypeDataKey]);
                    unset($patientworkAnnexType_key);
                }
            }
            unset($patientworkAnnexTypeDatas);
            unset($patient_form_id_arr);
            unset($checktime_id_arr);

            //申请解锁信息

            $unlock_id_arr = $patientFormUnlockArr = [];
            foreach($datas as $dataInfo){
                if (!empty($dataInfo['is_unlock'])){
                    $unlock_id_arr[] = $dataInfo['is_unlock'];
                }
                if (!empty($dataInfo['unlock_id'])){
                    $unlock_id_arr[] = $dataInfo['unlock_id'];
                }
            }
            if(!empty($unlock_id_arr)){
                $patientFormUnlockDatas = $this->LocalService()->patientFormUnlock->fetchAll([
                    'where'=> [
                        'is_del' => 0,
                        'id' => $unlock_id_arr,
                    ],
                    'columns'=>['id','unlock_note']
                ]);
            }
            unset($unlock_id_arr);
            if(!empty($patientFormUnlockDatas)){
                foreach($patientFormUnlockDatas as $patientFormUnlockDataKey=>$patientFormUnlockData){
                    $patientFormUnlockArr[$patientFormUnlockData['id']] = $patientFormUnlockData;
                    unset($patientFormUnlockDatas[$patientFormUnlockDataKey]);
                }
            }
            unset($patientFormUnlockDatas);
            foreach($datas as $key=>$data){
                $patientFormId_ChecktimeId =  $data['patient_form_id'].'_'.$data['checktime_id'];

                //补单状态【0 全部 4 补单】

                $is_annex_type = 0;
                if(in_array($patientFormId_ChecktimeId,$patientworkAnnexTypeArr)) {
                    $is_annex_type = 4;
                }

                //作废状态【0 全部 1 作废】

                $is_annul = 0;
                $patientChangeAnnulData = !empty($data['lock_state']) && isset($patientChangeArr[$patientFormId_ChecktimeId]) && !empty($patientChangeArr[$patientFormId_ChecktimeId]) ? array_values(array_column($patientChangeArr[$patientFormId_ChecktimeId],'is_annul')) : [];
                if(!empty($patientChangeAnnulData) && in_array(1,$patientChangeAnnulData)) {
                    $is_annul = 1;
                }
                unset($patientChangeAnnulData);
                unset($patientFormId_ChecktimeId);

                //申请解锁

                $is_unlock = $data['is_unlock'];
                $unlock_id = $data['unlock_id'];
                if (!empty($is_unlock)){
                    $is_unlock = isset($patientFormUnlockArr[$is_unlock]) && !empty($patientFormUnlockArr[$is_unlock]) ? $patientFormUnlockArr[$is_unlock]['id'] : 0;
                }

                $is_noover = 0;
                //作废

                if($is_annul != 0 || $data['is_question_date'] > 0){
                    $info['cancelNum'] += 1;
                    $is_noover = 1;
                }else{
                    //补图

                    if($is_annex_type != 0){
                        $info['supplementNum'] += 1;
                        $is_noover = 1;
                    }else{
                        //申请解锁/解锁未更新

                        if($is_unlock != 0 || ($unlock_id != 0 && empty($data['is_lock_operate']))){
                            $info['unlockNoOperateNum'] += 1;
                            $is_noover = 1;
                        }else{
                            //解锁已更新

                            if($data['is_lock_operate'] == 1){
                                $info['unlockOperateNum'] += 1;
                                $is_noover = 1;
                            }
                        }
                    }
                }

                if(!in_array($data['item_id'],$info['child_item_id']) && (empty($data['lock_state']) || $is_noover == 1)){
                    $info['child_item'][] = $data['item_id'];
                }

                unset($is_annul);
                unset($is_unlock);
                unset($unlock_id);
                unset($is_annex_type);
                unset($is_noover);
                unset($datas[$key]);
            }
            //释放空间

            unset($patientChangeArr);
            unset($patientworkAnnexTypeArr);
            unset($patientFormUnlockArr);
        }
        unset($datas);

        return $info;
    }


    /**
     * Notes: 获取指定条件下的数据
     * User: lltyy
     * DateTime: 2023/6/2 7:50
     *
     * @param $where 条件
     * @param $columns 信息
     * @return array
     */
    public function fetchDataByKey($where,$columns,$order=['id'],$key='id',$is_more=0){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>$order
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $infoData){
                $key_arr = explode('/',$key);
                $key_str_arr = [];
                foreach ($key_arr as $key_info){
                    $key_str_arr[] = $infoData[$key_info];
                }
                unset($key_arr);
                $key_str = implode('_',$key_str_arr);
                unset($key_str_arr);
                if($is_more == 1){
                    $datas[$key_str][] = $infoData;
                }
                if($is_more == 0){
                    $datas[$key_str] = $infoData;
                }
            }
        }
        unset($cond);
        unset($infoDatas);
        return $datas;
    }

}