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

use Application\Service\Extension\Laminas;
use Interop\Container\ContainerInterface;

use Laminas\Db\Sql\Predicate\In;
use Laminas\Db\Sql\Predicate\IsNotNull;
use Laminas\Db\Sql\Predicate\NotIn;
use Laminas\Db\Sql\Predicate\Operator;

class ItemFormField extends FormField
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        if (!$this->tableName) {
            $this->tableName = 'yikeen_item_form_field';
        }

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

    /**
     * 获取项目表单单选模板字段表结构
     * @return FormField|mixed
     */
    protected function getFormFieldRadio()
    {
        return Laminas::$serviceManager->itemFormFieldRadio;
    }


    protected function getForm()
    {
        return Laminas::$serviceManager->itemForm;
    }
    
    protected function getFormField()
    {
        return Laminas::$serviceManager->itemFormField;
    }

    /**
     * 获取项目版本数据表结构
     * @return FormField|mixed
     */
    protected function getFormVersion()
    {
        return Laminas::$serviceManager->itemFormVersion;
    }

    /**
     * 获取项目表单文本模板字段表结构
     * @return FormField|mixed
     */
    protected function getFormFieldText()
    {
        return Laminas::$serviceManager->itemFormFieldText;
    }

    /**
     * @note 验证表单字段是否为住院医嘱共享图片字段
     * @return bool
     */
    public function validDrugShareImg(int $fieldId): bool
    {
        $fieldData = $this->fetchOne([
            'where' => ['id' => $fieldId],
            'columns' => ['type', 'form_id']
        ]);

        if($fieldData['type'] !== FormField::FORM_FIELD_TYPE_UPLOAD_SHARE_IMAGE) return false;

        // 验证表单是否为医嘱采集类型
        $info_collect_type = $this->LocalService()->itemForm->getOneFieldVal('info_collect_type', [
            'id' => $fieldData['form_id']
        ]);

        if(!$info_collect_type) return false;

        return in_array($this->LocalService()->collectCategory->getOneFieldVal('category_code', ['id' => $info_collect_type]), [ucfirst('Medical'), 'DischargeRecord']);
    }

    public function generateCollecttypeFieldSelect(): array
    {
        // 获取 出院记录(DischargeRecord) 和 医嘱(Medical) 病例采集类型信息
        $collectCategroyList = $this->LocalService()->collectCategory->fetchAll([
            'where' => [
                new In('category_code', ['Medical', 'DischargeRecord']),
                'is_del' => 0
            ],
            'columns' => ['id', 'name' => 'category_name']
        ]);

        if(empty($collectCategroyList)) return [];

        array_walk($collectCategroyList, function (&$item) {
           $item['name'] .= '/药物名称';
        });
        // 获取 出院记录(DischargeRecord) 和 医嘱(Medical) 病例采集类型信息
        return [
            'id' => '-1',
            'name' => '病例收集类型',
            'children' => $collectCategroyList
        ];
    }


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

        return $datas;
    }

    /**
     * Notes:
     * User: lltyy
     * DateTime: 2024/6/12 15:22
     *
     * @param int $form_id 表单ID
     * @param int $show_type 展示信息【0 详情展示 1 列表展示】
     * @return array
     *
     * $formFieldData 表单结构信息
     * $formFieldNameArr 【键值=>[选项值=>选项名称]】
     * $select_field_arr 【键值数组】
     *
     */
    public function fetchFormFiledData($form_id=0,$show_type=0){
        $formFieldData = $select_field_arr = $formFieldNameArr = [];

        $itemFormFiledSelectDatas = $field_key_arr = $field_relation_arr = [];
        $itemFormFiledDatas = $this->fetchAll(['where'=> [
            'is_del' => 0,
            'form_id' => $form_id,
        ],'columns'=>['prop'=>'id','parent','label'=>'name','is_list','var_name'],'order'=>['parent','order']]);
        if(!empty($itemFormFiledDatas)){
            $itemFormFiledRadioDatas = $this->LocalService()->itemFormFieldRadio->fetchDataByKey([
                'form_id' => $form_id,
            ],['field_id','value','relation_information'],'field_id',0);
            foreach($itemFormFiledDatas as $itemFormFiledDataKey=>&$itemFormFiledData){
                $itemFormFiled_data_var = $itemFormFiledData['var_name'];
                $itemFormFiled_data_id = $itemFormFiledData['prop'];
                $itemFormFiled_data_parent = $itemFormFiledData['parent'];
                unset($itemFormFiledData['var_name']);
                unset($itemFormFiledData['parent']);
                if($itemFormFiled_data_var == 'CMINDC'){//用药原因获取字典项信息
                    $select_field_arr[] = $itemFormFiled_data_id;
                    $itemFormFiledSelectDatas[$itemFormFiled_data_id] = $this->LocalService()->collectMedicaltake->fetchAll(['where'=> [
                        'is_del' => 0,
                        'take_type' => 6,
                    ],'columns'=>['value'=>'id','name'=>'take_name'],'order'=>['take_order']]);
                }
                if(!empty($itemFormFiled_data_parent)){
                    if($itemFormFiled_data_var != 'CMINDC') {
                        $select_field_arr[] = $itemFormFiled_data_parent;
                        $itemFormFiled_data_id = $itemFormFiledData['prop'];
                        $itemFormFiledSelectDatas[$itemFormFiled_data_parent][] = [
                            'id' => $itemFormFiled_data_id,
                            'name' => $itemFormFiledData['label'],
                            'value' => isset($itemFormFiledRadioDatas[$itemFormFiled_data_id]) ? $itemFormFiledRadioDatas[$itemFormFiled_data_id]['value'] : '',
                            'relations' => isset($itemFormFiledRadioDatas[$itemFormFiled_data_id]) ? explode(',', $itemFormFiledRadioDatas[$itemFormFiled_data_id]['relation_information']) : []
                        ];
                    }
                }else{
                    $formFieldData[] = $itemFormFiledData;
                }
                unset($itemFormFiledDatas[$itemFormFiledDataKey]);
            }
            unset($itemFormFiledRadioDatas);
        }
        unset($itemFormFiledDatas);

        //重组选项信息
        if(!empty($itemFormFiledSelectDatas)){
            foreach($itemFormFiledSelectDatas as $itemFormFiled=>$itemFormFiledSelects){
                foreach($itemFormFiledSelects as $itemFormFiledSelectKey=>$itemFormFiledSelect){
                    $select_value = $itemFormFiledSelect['value'];
                    $formFieldNameArr[$itemFormFiled][$select_value] = $itemFormFiledSelect['name'];
                    if(isset($itemFormFiledSelect['relations']) && !empty($itemFormFiledSelect['relations']) && $select_value != ''){
                        foreach($itemFormFiledSelect['relations'] as $filed_relation_information){
                            if(!empty($filed_relation_information)){
                                $field_key_arr[$filed_relation_information] = $filed_relation_information.'-'.$itemFormFiled.'-'.$select_value;
                                $field_relation_arr[] = $filed_relation_information;
                            }
                        }
                    }
                    unset($itemFormFiledSelects[$itemFormFiledSelectKey]);
                    unset($itemFormFiledSelectDatas[$itemFormFiled][$itemFormFiledSelectKey]);
                }
                unset($itemFormFiledSelectDatas[$itemFormFiled]);
            }
        }
        unset($itemFormFiledSelectDatas);

        if(!empty($field_key_arr)){
            foreach($field_key_arr as $field_key_relation=>$field_key_info){
                if(isset($formFieldNameArr[$field_key_relation]) && !empty($formFieldNameArr[$field_key_relation])){
                    $formFieldNameArr[$field_key_info] = $formFieldNameArr[$field_key_relation];
                    $select_field_arr[] = $field_key_info;
                }
                $field_key_info_arr = explode('-',$field_key_info);
                $field_key_info_filed = $field_key_info_arr[1];
                if(isset($field_key_arr[$field_key_info_filed]) && !empty($field_key_arr[$field_key_info_filed])){
                    $parent_str = $field_key_arr[$field_key_info_filed];
                    $field_key_arr[$field_key_relation] = $field_key_info_arr[0].'-'.$parent_str.'-'.$field_key_info_arr[2];
                    if(isset($formFieldNameArr[$field_key_info_filed]) && !empty($formFieldNameArr[$field_key_info_filed])){
                        $formFieldNameArr[$parent_str] = $formFieldNameArr[$field_key_info_filed];
                        $select_field_arr[] = $parent_str;
                    }
                }

            }
        }

        $relationsFormArr = [];
        if(!empty($formFieldData)){
            //获取用药原因字典项信息
            $reasonDatas = [];
            foreach($formFieldData as $formField_Key=>&$formField){
                $formField_is_list = $formField['is_list'];
                $formField_prop = $formField['prop'];
                unset($formField['is_list']);
                $is_show = 0;
                if($show_type == 1){
                    if($formField_is_list == 1){
                        $is_show = 1;
                    }
                }else{
                    $is_show = 1;
                }
                if($is_show == 1){
                    if(in_array($formField_prop,$field_relation_arr)){
                        $formField['prop'] = isset($field_key_arr[$formField_prop]) && $field_key_arr[$formField_prop] != '' ? $field_key_arr[$formField_prop] : $formField_prop;
                        $parent_str = $formField['prop'];
                        $relationsFormArr[$parent_str] = $formField;
                        unset($formFieldData[$formField_Key]);
                    }
                }else{
                    unset($formFieldData[$formField_Key]);
                }
            }
        }

        $formFieldData = !empty($formFieldData) ? array_values($formFieldData) : [];
        unset($field_key_arr);
        unset($field_relation_arr);

        return ['FormFileData'=>$formFieldData,'select_name'=>$formFieldNameArr,'select_id'=>$select_field_arr,'relationsForm'=>$relationsFormArr];
    }


    /**
     * Notes: 获取特殊选项值的name
     * @param
     * @return
     * @author haojinhua
     * @date 2024-06-04
     */
    public function getSonNameArr($where = ''){
        $where = !empty($where) ? $where : 'id > 0';
        $res = [];
        $cond = [
            'where'=>$where,
            'columns'=>['id','parent','type','name','var_name','is_hide'],
            'order'=>['form_id','parent','order']
        ];
        $infoDatas = $this->fetchAll($cond);

        if(!empty($infoDatas)){
            foreach ($infoDatas as $infoData){
                $res['allson'][$infoData['parent']][] = $infoData['id'];  //父子关系对应数组
                if(in_array($infoData['type'],[10,11,12])){
                    $res['type'][] = $infoData['id'];  //特殊字段类型的所有id数据
                }
                $res['allname'][$infoData['id']] = [
                    'name'     => $infoData['name'],
                    'type'     => $infoData['type'],
                    'var_name' => $infoData['var_name'],
                    'is_hide'  => $infoData['is_hide'],
                ];
            }
        }

        //查yikeen_item_form_field_radio  获取选项value值 的 对应field_id 字段
        $filedDatas = $this->LocalService()->itemFormFieldRadio->fetchDataByKey($where,['field_id','value'],'field_id',0);

        $all_res = [];  //存储最终结果数组
        //循环特殊类型
        if(!empty($res['type'])){
            foreach ($res['type'] as $infoid){
                //获取特殊类型的子集
                $son_arr = isset($res['allson'][$infoid]) && !empty($res['allson'][$infoid]) ? $res['allson'][$infoid]: [];
                if(!empty($son_arr)){
                    foreach ($son_arr as $son){
                        $value = isset($filedDatas[$son]) && !empty($filedDatas[$son]) ? $filedDatas[$son]['value']:"";
                        //获取name值
                        $son_name =  isset($res['allname'][$son]) && !empty($res['allname'][$son]) ? $res['allname'][$son]: '';
                        $all_res[$infoid][$value] = $son_name;
                    }
                }
            }

            unset($res['allson']);
            unset($res['type']);
        }
        return [$all_res,$res['allname']];
    }

    /**
     * Notes: 获取指定表单下表单内容设置参考检查项及结果信息
     * User: lltyy
     * DateTime: 2024/8/20 10:06
     *
     * @param array $info
     * @return array
     */
    public function fetchChecknameResultData($info=[]){
        $arr = [];

        $item_id = isset($info['item_id']) && !empty($info['item_id']) ? $info['item_id'] : 0;//副项目ID
        $patient_id = isset($info['patient_id']) && !empty($info['patient_id']) ? $info['patient_id'] : 0;//受试者ID
        $checktime_id = isset($info['checktime_id']) && !empty($info['checktime_id']) ? $info['checktime_id'] : 0;//项目检查点ID
        $form_id = isset($info['form_id']) && !empty($info['form_id']) ? $info['form_id'] : 0;//表单ID
        if(!empty($form_id)){
            //获取该检查点下设置的项目检查项的表单内容
           // $field_checkname_time_arr = [275668=>[201559]];//字段ID【】=》检查点
            $field_checkname_time_arr = [];//字段ID【】=》检查点
            $field_checktime_arr = $field_checkname_arr = [];
            $field_checkname_times = $this->fetchAll([
                'where'=> [
                    'is_del' => 0,
                    'form_id' => $form_id,
                    [
                        'or' =>[
                            [
                                new Operator('checktime_id', Operator::OP_NE, ''),
                                new IsNotNull('checktime_id'),
                            ],
                            [
                                new Operator('checkname_id', Operator::OP_NE, ''),
                                new IsNotNull('checkname_id'),
                            ]
                        ]
                    ]
                ],
                'columns'=>['id','checktime_id','checkname_id']
            ]);

            if(!empty($field_checkname_times)){
                foreach ($field_checkname_times as $field_checkname_time){
                    if(!empty($field_checkname_time['checktime_id'])){
                        $field_checktime_arr[$field_checkname_time['id']] = $field_checkname_time['checktime_id'];
                        $set_checktimeids = explode(',',$field_checkname_time['checktime_id']);
                        if(!empty($set_checktimeids)){
                            foreach ($set_checktimeids as $set_checktimeid){
                                $field_checkname_time_arr[$field_checkname_time['id']][] = $set_checktimeid;
                                $checktime_arr[] = $set_checktimeid;
                            }
                        }
                    }
                    if(!empty($field_checkname_time['checkname_id'])){
                        $field_checkname_arr[$field_checkname_time['id']] = $field_checkname_time['checkname_id'];
                    }
                }
            }
            if(!empty($field_checkname_arr)){
                $lock_category_arr = [];//锁定的分类信息
                //获取项目检查项对应患者检查点下的结果信息
                $checkname_result_arr = [];
                $checkname_arr = array_values($field_checkname_arr);
                //$checktime_arr = array_values($field_checktime_arr);
                $result_change_where = [
                    'is_del' => 0,
                    'lock_state' => 1,
                    'patient_id' => $patient_id,
                    new Operator('checktime_id', Operator::OP_GT, 0),
                    'item_checkname_id' => $checkname_arr,
                ];
                if(!empty($checktime_arr)){
                    $result_change_where['checktime_id'] =  $checktime_arr;
                }

                $checkname_result_datas = $this->LocalService()->itemIdentificationresultchange->fetchAll([
                    'where'=>$result_change_where,
                    'columns'=>['item_checkname_id','result','unit','category_id','checktime_id'],
                    'order'=>['id']
                ]);
                if(!empty($checkname_result_datas)){
                    foreach($checkname_result_datas as $checkname_result_key=>$checkname_result_data){
                        if(!is_null($checkname_result_data['result']) && $checkname_result_data['result'] != ''){
                            $show_result = $checkname_result_data['result'];
                            if(!is_null($checkname_result_data['unit']) && $checkname_result_data['unit'] != '' ){
                                $show_result .= '（'.$checkname_result_data['unit'].'）';
                            }
                            $checkname_result_arr[$checkname_result_data['item_checkname_id']][$checkname_result_data['checktime_id']][] = $show_result;
                        }
                        unset($checkname_result_datas[$checkname_result_key]);
                        $lock_category_arr[$checkname_result_data['category_id']][$checkname_result_data['checktime_id']] = $checkname_result_data['category_id'];
                    }
                }
                unset($checkname_result_datas);
                $lock_category_arr = array_values($lock_category_arr);

                //获取检查项信息
                $item_checkname_arr = $category_checkname_arr = $checkname_category_arr = $field_category_arr = [];
                $itemcheckDatas = $this->LocalService()->itemCheckname->fetchAll([
                    'where'=> [
                        'is_del' => 0,
                        'item_id' => $item_id,
                        'id' => $checkname_arr,
                    ],
                    'columns'=>['id','category_id','name'],
                    'order'=>['category_id','checkname_order']
                ]);
                unset($checkname_arr);
                if(!empty($itemcheckDatas)){
                    $category_name_arr = $this->LocalService()->DictionaryCheckcategory->fetchCol('category_name','is_del = 0');
                    foreach($itemcheckDatas as $itemcheckKey=>$itemcheckData){
                        $itemcheckData_category = isset($category_name_arr[$itemcheckData['category_id']]) && !empty($category_name_arr[$itemcheckData['category_id']]) ? $category_name_arr[$itemcheckData['category_id']] : '';
                        $item_checkname_arr[$itemcheckData['id']] = $itemcheckData_category != '' ? $itemcheckData_category.'-'.$itemcheckData['name'] : $itemcheckData['name'];
                        $checkname_category_arr[$itemcheckData['id']] = $itemcheckData['category_id'];
                        $category_checkname_arr[$itemcheckData['category_id']][] = $itemcheckData['id'];    //$category_checkname_arr 检查项分类id 和检查项ID数组 根据分类获取到所有的检查项ID
                        $field_category_arr[] = $itemcheckData['category_id'];                             //$field_category_arr 检查项分类id数组
                        unset($itemcheckDatas[$itemcheckKey]);
                        unset($itemcheckData_category);
                    }
                    unset($category_name_arr);
                }
                unset($itemcheckDatas);

                //获取分类下的表单
                $checknameTimeArr = [];//检查项对应的阶段检查点下的
                if(!empty($field_category_arr)){   //检查项分类ID不为空
                    //获取表单检查项
                    $check_form_arr = $this->LocalService()->itemForm->fetchCol('checklist_id',[
                        'is_del' => 0,
                        'item_id' => $item_id,
                        'checklist_id' => $field_category_arr,
                    ]);
                    if(!empty($check_form_arr)){
                        $check_form_where = [
                            'is_del' => 0,
                            new Operator('check_id', Operator::OP_GT, 0),
                            'item_id' => $item_id,
                            'form_id' => array_values(array_keys($check_form_arr)),
                        ];
                        if(!empty($checktime_arr)){
                            $check_form_where['check_id'] = $checktime_arr;
                        }
                        //获取表单阶段检查点
                        $checkFormDatas = $this->LocalService()->itemCheckcontent->fetchAll([
                            'where'=>$check_form_where,
                            'columns'=>['check_id','form_id']
                        ]);
                        if(!empty($checkFormDatas)){
                            $checktimeNameArr = [];//获取患者检查点信息
                            //获取患者阶段检查点信息
                            $patient_checktime_where = [
                                'is_del' => 0,
                                'patient_id' => $patient_id,
                            ];
                            if(!empty($checktime_arr)){
                                $patient_checktime_where['checktime_id'] = $checktime_arr;
                            }
                            $patientCheckTimeDatas = $this->LocalService()->itemPatientchecktime->fetchAll([
                                'where'=> $patient_checktime_where,
                                'columns'=>['id','checktime_id','researchstage_id','checktime_name','check_order']
                            ]);
                            if(!empty($patientCheckTimeDatas)){
                                //获取检查点信息
                                $checktimeInfos = [
                                    'check'=>[],
                                    'patient_check'=>[]
                                ];
                                $checktimeDatas = $this->LocalService()->itemChecktime->fetchAll([
                                    'where'=> [
                                        'item_id' => $item_id,
                                        [
                                            'or' => [
                                                'is_del' => 0,
                                                [
                                                    new NotIn('is_del', [0, 1]),
                                                    new Operator('patientchecktime_id', Operator::OP_GT, 0)
                                                ]
                                            ]
                                        ]
                                    ],
                                    'columns'=>['id','patientchecktime_id','check_name']
                                ]);
                                if(!empty($checktimeDatas)){
                                    foreach($checktimeDatas as $checktimeKey=>$checktimeData){
                                        $checktimeInfos['check'][$checktimeData['id']] = $checktimeData;
                                        if(!empty($checktimeData['patientchecktime_id'])){
                                            $checktimeInfos['patient_check'][$checktimeData['patientchecktime_id']] = $checktimeData;
                                        }
                                        unset($checktimeDatas[$checktimeKey]);
                                    }
                                }
                                unset($checktimeDatas);
                                //获取阶段信息
                                $stageNameArr = $this->LocalService()->itemResearchstage->fetchCol('stage_name',[
                                    'is_del' => 0,
                                    'item_id' => $item_id,
                                    'id' => array_values(array_column($patientCheckTimeDatas,'researchstage_id')),
                                ]);
                                foreach($patientCheckTimeDatas as $patientCheckTimeKey=>$patientCheckTimeData){
                                    $patientCheckTimeData_checktime = !empty($patientCheckTimeData['checktime_id']) ? $patientCheckTimeData['checktime_id'] : 0;
                                    $check_name = $patientCheckTimeData['checktime_name'] != '' ? $patientCheckTimeData['checktime_name'] : '';
                                    if(empty($patientCheckTimeData_checktime)){
                                        $patientCheckTimeData_checktime = isset($checktimeInfos['patient_check'][$patientCheckTimeData['id']]) && !empty($checktimeInfos['patient_check'][$patientCheckTimeData['id']]) ? $checktimeInfos['patient_check'][$patientCheckTimeData['id']]['id'] : 0;
                                    }else{
                                        $check_name = isset($checktimeInfos['check'][$patientCheckTimeData_checktime]) && !empty($checktimeInfos['check'][$patientCheckTimeData_checktime]) ? $checktimeInfos['check'][$patientCheckTimeData_checktime]['check_name'] : '';
                                    }
                                    if(!empty($patientCheckTimeData_checktime)){
                                        if(isset($stageNameArr[$patientCheckTimeData['researchstage_id']]) && $stageNameArr[$patientCheckTimeData['researchstage_id']] != ''){
                                            $check_name = $stageNameArr[$patientCheckTimeData['researchstage_id']].'-'.$check_name;
                                        }
                                        $checktimeNameArr[$patientCheckTimeData_checktime] = [
                                            'checktime_id'=>$patientCheckTimeData_checktime,
                                            'check_name' =>$check_name,
                                            'check_order' =>$patientCheckTimeData['check_order']
                                        ];
                                    }
                                    unset($patientCheckTimeData_checktime);
                                    unset($check_name);
                                    unset($patientCheckTimeDatas[$patientCheckTimeKey]);
                                }
                                unset($checktimeInfos);
                                unset($stageNameArr);
                            }
                            unset($patientCheckTimeDatas);
                            //重组检查项检查点
                            foreach($checkFormDatas as $checkFormKey=>$checkFormData){
                                $patientFormCheckData_category = isset($check_form_arr[$checkFormData['form_id']]) && !empty($check_form_arr[$checkFormData['form_id']]) ? $check_form_arr[$checkFormData['form_id']] : 0;
                                if(!empty($patientFormCheckData_category)){
                                    if(isset($category_checkname_arr[$patientFormCheckData_category]) && !empty($category_checkname_arr[$patientFormCheckData_category])){
                                        foreach($category_checkname_arr[$patientFormCheckData_category] as $category_checkname){
                                            if(!isset($checknameTimeArr[$category_checkname]) || !in_array($checkFormData['check_id'],$checknameTimeArr[$category_checkname])){
                                                if(isset($checktimeNameArr[$checkFormData['check_id']]) && !empty($checktimeNameArr[$checkFormData['check_id']])){
                                                    $checknameTimeArr[$category_checkname][] = $checktimeNameArr[$checkFormData['check_id']];
                                                }
                                            }
                                        }
                                    }
                                }
                                unset($checkFormDatas[$checkFormKey]);
                            }
                            unset($checktimeNameArr);
                        }
                        unset($checkFormDatas);
                    }
                    unset($check_form_arr);
                }
                unset($category_checkname_arr);
                unset($field_category_arr);
                if(empty($checktime_arr)){
                    //没设置查询所有的检查点
                    $checktime_arr = $this->LocalService()->itemChecktime->fetchCol('id',[
                        'is_del' => 0,
                        'item_id' => $item_id,
                    ]);
                }
                //重组每个表单内容对应的参考检查项及结果值
                foreach($field_checkname_arr as $field_id=>$field_checknames){
                    $field_checkname_arr = explode(',',$field_checknames);
                    if(empty($field_checkname_time_arr[$field_id])){
                        //没设置 查所有的
                        $field_checkname_time_arr[$field_id] = $checktime_arr;
                    }
                    foreach($field_checkname_arr as $field_checkname){
                        if(isset($item_checkname_arr[$field_checkname]) && !empty($item_checkname_arr[$field_checkname])){
                            $checkname_time_datas = [];
                            if(isset($checknameTimeArr[$field_checkname]) && !empty($checknameTimeArr[$field_checkname])){
                                $checknameReaultArr = isset($checkname_result_arr[$field_checkname]) && !empty($checkname_result_arr[$field_checkname]) ? $checkname_result_arr[$field_checkname] : [];
                                foreach($checknameTimeArr[$field_checkname] as $checkname_time){
                                    $checkname_time_timeid = $checkname_time['checktime_id'];
                                    if(in_array($checkname_time_timeid,$field_checkname_time_arr[$field_id])) {
                                        $key_str = $checkname_time['check_order'].'_'.$checkname_time_timeid;
                                        if(isset($checknameReaultArr[$checkname_time_timeid]) && !empty($checknameReaultArr[$checkname_time_timeid])){
                                            $field_checkname_result = implode('、',array_values(array_unique($checknameReaultArr[$checkname_time_timeid])));
                                            $arr['checktime_id'] = $checkname_time_timeid;
                                            $arr['result'] = $field_checkname_result;
                                            $this->fieldResultDataContainer[$field_checknames][] = $arr;
                                        }else{
                                            $checkname_category = isset($checkname_category_arr[$field_checkname]) && !empty($checkname_category_arr[$field_checkname]) ? $checkname_category_arr[$field_checkname] : 0;
                                            $checktime_lock_categorys = isset($lock_category_arr[$field_checkname]) && isset($lock_category_arr[$field_checkname][$checkname_time_timeid]) && !empty($lock_category_arr[$field_checkname][$checkname_time_timeid]) ? $lock_category_arr[$field_checkname][$checkname_time_timeid] : [];
                                            if(!empty($checkname_category) && in_array($checkname_category,$checktime_lock_categorys)){
                                                $field_checkname_result = '空值';
                                            }else{
                                                $field_checkname_result = '未识别';
                                            }
                                            unset($checkname_category);
                                            unset($checktime_lock_categorys);
                                        }
                                        if($checkname_time_timeid == $checktime_id){
                                            $key_str = '0_'.$checkname_time_timeid;
                                            $checkname_time_datas[$key_str] = '【'.$field_checkname_result.'】';
                                        }else{
                                            $checkname_time_datas[$key_str] = '【'.$checkname_time['check_name'].'：'.$field_checkname_result.'】';
                                        }
                                        unset($checkname_time_timeid);
                                        unset($field_checkname_result);
                                        unset($key_str);
                                    }
                                }
                                unset($checknameReaultArr);
                            }
                            ksort($checkname_time_datas);
                            $arr[$field_id][] = $item_checkname_arr[$field_checkname].'：'.implode('、',$checkname_time_datas);
                            unset($checkname_time_datas);
                        }
                    }
                    unset($field_checkname_arr[$field_id]);
                }
                unset($item_checkname_arr);
                unset($checkname_category_arr);
                unset($lock_category_arr);
                unset($checkname_result_arr);
                unset($checknameTimeArr);
            }
            unset($field_checkname_arr);
        }

        return $arr;
    }

    public array $fieldResultDataContainer = [];

    /**
     * Notes: 获取指定表单下表单内容设置参考表单及结果信息
     * User: lltyy
     * DateTime: 2024/8/20 10:06
     *
     * @param array $info
     * @return array
     */
    public function fetchFormFieldResultData($info=[]){
        $arr = [];

        $item_id = isset($info['item_id']) && !empty($info['item_id']) ? $info['item_id'] : 0;//副项目ID
        $patient_id = isset($info['patient_id']) && !empty($info['patient_id']) ? $info['patient_id'] : 0;//受试者ID
        $checktime_id = isset($info['checktime_id']) && !empty($info['checktime_id']) ? $info['checktime_id'] : 0;//项目检查点ID
        $form_id = isset($info['form_id']) && !empty($info['form_id']) ? $info['form_id'] : 0;//表单ID
        if(!empty($form_id)){
            $item_fid = $this->LocalService()->itemInfo->getOneFieldVal('fid', [
                'is_del' => 0,
                'id' => $item_id,
            ]);//获取项目名称
            $blind_method = $this->LocalService()->itemInfo->getOneFieldVal('blind_method', [
                'is_del' => 0,
                'id' => $item_fid,
            ]);//获取项目名称

            //获取该检查点下设置的参考表单的表单内容
            $form_field_datas = $this->fetchAll([
                'where'=> [
                    'is_del' => 0,
                    'form_id' => $form_id,
                    [
                        'or' => [
                            [
                                new Operator('form_field_id', Operator::OP_NE, ''),
                                new IsNotNull('form_field_id'),
                            ],
                            [
                                new Operator('formchecktime_id', Operator::OP_NE, ''),
                                new IsNotNull('formchecktime_id'),
                            ]
                        ]
                    ]
                ],
                'columns'=>['id','form_field_id','formchecktime_id','default_checktimefield_id']
            ]);
            //$form_fieldid_arr  字段对应的表单检查点   $form_field_arr 字段对应的 表单ID  $form_checktimeid_arr
            $form_fieldid_arr = $form_field_arr = $form_checktimeid_arr =[];
            if(!empty($form_field_datas)){
                foreach ($form_field_datas as $form_field_data){
                    if(!empty($form_field_data['form_field_id'])){
                        $form_field_arr[$form_field_data['id']] = $form_field_data['form_field_id'];
                    }
                    if(!empty($form_field_data['formchecktime_id'])){ //formchecktime_id 参考表单检查点
                        $form_fieldid_arr[$form_field_data['id']] = $form_field_data['formchecktime_id'];
                        $formchecktime_ids = explode(',',$form_field_data['formchecktime_id']);
                        if(!empty($formchecktime_ids)){
                            foreach ($formchecktime_ids as $formchecktime_id){
                                $form_checktimeid_arr[$form_field_data['id']][] = $formchecktime_id;
                                $checktime_id_arr[] = $formchecktime_id;
                            }
                        }
                    }
                }
            }

            if(!empty($form_field_arr)){
                //获取参考表单内容信息
                $select_field_infos = [];
                $field_arr = array_values($form_field_arr);
                //$checktime_id_arr = array_values($form_checktimeid_arr);
                $select_form_field_arr = $this->LocalService()->itemFormField->fetchDataByKey([
                    'is_del' => 0,
                    'id' => $field_arr,
                ], ['id','form_id','type','name','var_name'],'form_id',1);
                if(!empty($select_form_field_arr)){
                    foreach($select_form_field_arr as $select_form_id=>$form_fieldInfo_arr){
                        foreach($form_fieldInfo_arr as $form_fieldInfo){
                            $select_field_infos[$form_fieldInfo['id']] = $form_fieldInfo;
                        }
                    }
                }

                //获取表单名称
                $formNameArr = [];
                if(!empty($select_form_field_arr)){
                    $formNameArr = $this->LocalService()->itemForm->fetchDataByKey([
                        'is_del' => 0,
                        'id' => array_values(array_keys($select_form_field_arr)),
                    ],['id','name','group_id'],['order'],'id',0);
                }

                //获取常规类识别 分组ID
                $ocrGroupId = $this->LocalService()->dictionaryFormGroup->getOneFieldVal('id','is_del = 0 and code = "GCO"');

                $formFieldTimeArr = [];//获取表单内容对应的检查点
                $field_result_arr = [];//获取表单内容对应的检查点结果

                if(!empty($formNameArr)){
                    //获取参考表单检查点 及 参考表单对应患者检查点下的结果信息
                    $form_result_where = [
                        'is_del' => 0,
                        'patient_id' => $patient_id,
                        'form_id' => array_values(array_keys($formNameArr)),
                        new Operator('checktime_id', Operator::OP_GT, 0),
                    ];
                    if(!empty($checktime_id_arr)){
                        $form_result_where['checktime_id'] = $checktime_id_arr;;
                    }
                    $form_result_datas = $this->LocalService()->patientFormContent->fetchAll([
                        'where'=> $form_result_where,
                        'columns'=>['checktime_id','form_id','data','is_lock'],
                        'order'=>['id']
                    ]);
                    if(!empty($form_result_datas)){
                        //获取选项信息
                        $selectFieldValueArr = [];
                        $selectFieldArr = $this->LocalService()->itemFormField->fetchDataByKey([
                            'is_del' => 0,
                            'form_id' => array_values(array_keys($select_form_field_arr)),
                            new Operator('parent', Operator::OP_GT, 0)
                        ], ['id','parent','name'],'parent',1);
                        $selectFieldRadioArr = $this->LocalService()->itemFormFieldRadio->fetchDataByKey([
                            'form_id' => array_values(array_keys($select_form_field_arr)),
                        ], ['value','field_id'],'field_id',0);
                        if(!empty($selectFieldArr)){
                            foreach($selectFieldArr as $selectFieldParent=>$selectFields){
                                foreach($selectFields as $selectFieldId=>$selectField){
                                    if(isset($selectFieldRadioArr[$selectField['id']]) && isset($selectFieldRadioArr[$selectField['id']]['value']) && $selectFieldRadioArr[$selectField['id']]['value'] != ''){
                                        $selectFieldValueArr[$selectFieldParent][$selectFieldRadioArr[$selectField['id']]['value']] = $selectField['name'];
                                    }
                                }
                            }
                        }
                        unset($selectFieldArr);
                        unset($selectFieldRadioArr);
                        foreach($form_result_datas as $form_result_key=>$form_result_data){
//                            if($form_result_key != 2) continue;
                            $form_result_group = isset($formNameArr[$form_result_data['form_id']]) && !empty($formNameArr[$form_result_data['form_id']]['group_id']) ? $formNameArr[$form_result_data['form_id']]['group_id'] : 0;
                            if((!empty($ocrGroupId) && !empty($form_result_group) && $ocrGroupId == $form_result_group && $form_result_data['is_lock'] == 1) || empty($ocrGroupId) || empty($form_result_group) || (!empty($ocrGroupId) && $ocrGroupId != $form_result_group)){
                                $form_result_data_arr = json_decode($form_result_data['data'],true);

                                if(isset($select_form_field_arr[$form_result_data['form_id']]) && !empty($select_form_field_arr[$form_result_data['form_id']])){

                                    foreach ($select_form_field_arr[$form_result_data['form_id']] as $select_form_field){
                                        if(isset($form_result_data_arr[$select_form_field['id']]) && $form_result_data_arr[$select_form_field['id']] != ''){
                                            if(isset($selectFieldValueArr[$select_form_field['id']]) && !empty($selectFieldValueArr[$select_form_field['id']])){
                                                $result_arr = explode(',',$form_result_data_arr[$select_form_field['id']]);
                                                $show_result = [];
                                                if(!empty($result_arr)){
                                                    foreach($result_arr as $result){
                                                        if(isset($selectFieldValueArr[$select_form_field['id']]) && isset($selectFieldValueArr[$select_form_field['id']][$result]) && $selectFieldValueArr[$select_form_field['id']][$result] != ''){
                                                            $show_result[] = $selectFieldValueArr[$select_form_field['id']][$result];
                                                        }
                                                    }
                                                }
                                                if(!empty($show_result)){
                                                    $arr['checktime_id'] = $form_result_data['checktime_id'];
                                                    if($select_form_field['type'] == 10){
                                                        $arr['result'] = isset($result_arr[0]) ? $result_arr[0] : '';
                                                    }elseif($select_form_field['type'] == 11){
                                                        $arr['result'] = $result_arr;
                                                    }else{
                                                        $arr['result'] = $show_result;
                                                    }
                                                    $this->fieldResultDataContainer[$select_form_field['id']][] = $arr;
//                                                    if($form_result_data['checktime_id'] == $checktime_id){
//                                                        $this->fieldResultDataContainer[$select_form_field['id']] = $show_result;
//                                                    }
                                                    $field_result_arr[$select_form_field['id']][$form_result_data['checktime_id']] = implode('、',$show_result);
                                                }
                                                unset($show_result);
                                                unset($result_arr);
                                            }else{
                                                //随机分组表单 -- 系统
                                                if($form_result_group == 2 && ($select_form_field['var_name'] == 'RANDOM_GROUP_SYSTEM' || $select_form_field['var_name'] == 'RANGCA')){
                                                    $patientInfo = $this->LocalService()->patient->fetchOne(['where'=>[
                                                        'id' => $patient_id,
                                                    ],'columns'=>['random_id','random_number']]);

                                                    $randgroupName = $this->LocalService()->itemRandgroup->getOneFieldVal('group_name', [
                                                        'is_del' => 0,
                                                        'id' => $form_result_data_arr[$select_form_field['id']],
                                                    ]);
                                                    $field_result_arr[$select_form_field['id']][$form_result_data['checktime_id']] = '【'.$randgroupName.'】';
                                                    if ($blind_method == 0) {
                                                        $randomName = $this->LocalService()->itemRandom->getOneFieldVal('random_name', [
                                                            'is_del' => 0,
                                                            'id' => $patientInfo['random_id'],
                                                        ]);
                                                        $field_result_arr[$select_form_field['id']][$form_result_data['checktime_id']] .= '【'.$randomName.'】';
                                                    }
                                                    $field_result_arr[$select_form_field['id']][$form_result_data['checktime_id']] .= '【'.$patientInfo['random_number'].'】';
                                                    //随机分组表单 -- 手动
                                                }elseif($form_result_group == 2 && ($select_form_field['var_name'] == 'RANDOM_GROUP_CUSTOM' || $select_form_field['var_name'] == 'RANGC')){
                                                    $patientInfo = $this->LocalService()->patient->fetchOne(['where'=> [
                                                        'id' => $patient_id,
                                                    ],'columns'=>['random_id','random_number']]);
                                                    if ($blind_method == 0) {
                                                        $randomName = $this->LocalService()->itemRandom->getOneFieldVal('random_name', [
                                                            'is_del' => 0,
                                                            'id' => $patientInfo['random_id'],
                                                        ]);
                                                        $field_result_arr[$select_form_field['id']][$form_result_data['checktime_id']] .= '【'.$randomName.'】';
                                                    }
                                                    $field_result_arr[$select_form_field['id']][$form_result_data['checktime_id']] .= '【'.$patientInfo['random_number'].'】';
                                                }else{
                                                    $field_result_arr[$select_form_field['id']][$form_result_data['checktime_id']] = $form_result_data_arr[$select_form_field['id']];
                                                }

                                                $arr['checktime_id'] = $form_result_data['checktime_id'];
                                                $arr['result'] = $field_result_arr[$select_form_field['id']][$form_result_data['checktime_id']];
                                                $this->fieldResultDataContainer[$select_form_field['id']][] = $arr;
                                            }
                                        }else{
                                            if(!empty($ocrGroupId) && !empty($form_result_group) && $ocrGroupId == $form_result_group && $form_result_data['is_lock'] == 1){
                                                $field_result_arr[$select_form_field['id']][$form_result_data['checktime_id']] = '空值';
                                            }
                                        }
                                    }
                                }
                                unset($form_result_data_arr);
                            }
                            unset($form_result_group);
                            unset($form_result_datas[$form_result_key]);
                        }
                        unset($selectFieldValueArr);
                    }
                    unset($form_result_datas);

                    //获取表单阶段检查点
                    $checkFormDatas = $this->LocalService()->itemCheckcontent->fetchAll([
                        'where'=> [
                            'is_del' => 0,
                            new Operator('check_id', Operator::OP_GT, 0),
                            'item_id' => $item_id,
                            'form_id' => array_values(array_keys($formNameArr)),
                        ],
                        'columns'=>['check_id','form_id']
                    ]);
                    if(!empty($checkFormDatas)){
                        $checktimeNameArr = [];//获取患者检查点信息
                        //获取患者阶段检查点信息
                        $patientCheckTimeDatas = $this->LocalService()->itemPatientchecktime->fetchAll([
                            'where'=> [
                                'is_del' => 0,
                                'patient_id' => $patient_id,
                            ],
                            'columns'=>['id','checktime_id','researchstage_id','checktime_name','check_order']
                        ]);
                        if(!empty($patientCheckTimeDatas)){
                            //获取检查点信息
                            $checktimeInfos = [
                                'check'=>[],
                                'patient_check'=>[]
                            ];
                            $checktimeDatas = $this->LocalService()->itemChecktime->fetchAll([
                                'where'=> [
                                    'item_id' => $item_id,
                                    [
                                        'or' => [
                                            'is_del' => 0,
                                            [
                                                new NotIn('is_del', [0, 1]),
                                                new Operator('patientchecktime_id', Operator::OP_GT, 0)
                                            ]
                                        ]
                                    ]
                                ],
                                'columns'=>['id','patientchecktime_id','check_name']
                            ]);
                            if(!empty($checktimeDatas)){
                                foreach($checktimeDatas as $checktimeKey=>$checktimeData){
                                    $checktimeInfos['check'][$checktimeData['id']] = $checktimeData;
                                    if(!empty($checktimeData['patientchecktime_id'])){
                                        $checktimeInfos['patient_check'][$checktimeData['patientchecktime_id']] = $checktimeData;
                                    }
                                    unset($checktimeDatas[$checktimeKey]);
                                }
                            }
                            unset($checktimeDatas);
                            //获取阶段信息
                            $stageNameArr = $this->LocalService()->itemResearchstage->fetchCol('stage_name',[
                                'is_del' => 0,
                                'item_id' => $item_id,
                                'id' => array_values(array_column($patientCheckTimeDatas,'researchstage_id')),
                            ]);
                            foreach($patientCheckTimeDatas as $patientCheckTimeKey=>$patientCheckTimeData){
                                $patientCheckTimeData_checktime = !empty($patientCheckTimeData['checktime_id']) ? $patientCheckTimeData['checktime_id'] : 0;
                                $check_name = $patientCheckTimeData['checktime_name'] != '' ? $patientCheckTimeData['checktime_name'] : '';
                                if(empty($patientCheckTimeData_checktime)){
                                    $patientCheckTimeData_checktime = isset($checktimeInfos['patient_check'][$patientCheckTimeData['id']]) && !empty($checktimeInfos['patient_check'][$patientCheckTimeData['id']]) ? $checktimeInfos['patient_check'][$patientCheckTimeData['id']]['id'] : 0;
                                }else{
                                    $check_name = isset($checktimeInfos['check'][$patientCheckTimeData_checktime]) && !empty($checktimeInfos['check'][$patientCheckTimeData_checktime]) ? $checktimeInfos['check'][$patientCheckTimeData_checktime]['check_name'] : '';
                                }
                                if(!empty($patientCheckTimeData_checktime)){
                                    if(isset($stageNameArr[$patientCheckTimeData['researchstage_id']]) && $stageNameArr[$patientCheckTimeData['researchstage_id']] != ''){
                                        $check_name = $stageNameArr[$patientCheckTimeData['researchstage_id']].'-'.$check_name;
                                    }
                                    $checktimeNameArr[$patientCheckTimeData_checktime] = [
                                        'checktime_id'=>$patientCheckTimeData_checktime,
                                        'check_name' =>$check_name,
                                        'check_order' =>$patientCheckTimeData['check_order']
                                    ];
                                }
                                unset($patientCheckTimeData_checktime);
                                unset($check_name);
                                unset($patientCheckTimeDatas[$patientCheckTimeKey]);
                            }
                            unset($stageNameArr);
                            unset($checktimeInfos);
                        }
                        unset($patientCheckTimeDatas);

                        //重组检查项检查点
                        foreach($checkFormDatas as $checkFormKey=>$checkFormData){
                            if(isset($select_form_field_arr[$checkFormData['form_id']]) && !empty($select_form_field_arr[$checkFormData['form_id']]) && isset($checktimeNameArr[$checkFormData['check_id']]) && !empty($checktimeNameArr[$checkFormData['check_id']])){
                                foreach($select_form_field_arr[$checkFormData['form_id']] as $formFiled_info){
                                    $formFieldTimeArr[$formFiled_info['id']][] = $checktimeNameArr[$checkFormData['check_id']];
                                }
                            }
                            unset($checkFormDatas[$checkFormKey]);
                        }
                        unset($checktimeNameArr);
                    }
                    unset($checkFormDatas);
                }
                //重组每个表单内容对应的参考表单及结果值
                if(empty($checktime_id_arr)){
                    //没设置查询所有的检查点
                    $checktime_id_arr = $this->LocalService()->itemChecktime->fetchCol('id',[
                        'is_del' => 0,
                        'item_id' => $item_id,
                    ]);
                }
                foreach($form_field_arr as $field_id=>$form_field){
                    $form_field_ids = explode(',',$form_field);
                    if(!empty( $form_fieldid_arr[$field_id])){
                        $form_fieldid_arr[$field_id] = explode(',',$form_fieldid_arr[$field_id]);
                    }else{
                        $form_fieldid_arr[$field_id] = $checktime_id_arr;
                    }
                    foreach($form_field_ids as $form_field_id){
                        if(isset($select_field_infos[$form_field_id]) && !empty($select_field_infos[$form_field_id])){
                            $form_field_form = isset($select_field_infos[$form_field_id]) && !empty($select_field_infos[$form_field_id]['form_id']) ? $select_field_infos[$form_field_id]['form_id'] : 0;
                            $form_field_var_name = isset($select_field_infos[$form_field_id]) && !empty($select_field_infos[$form_field_id]['var_name']) ? $select_field_infos[$form_field_id]['var_name'] : '';
                            $form_field_formdata = isset($formNameArr[$form_field_form]) && !empty($formNameArr[$form_field_form]) ? $formNameArr[$form_field_form] : [];
                            if(isset($formFieldTimeArr[$form_field_id]) && !empty($formFieldTimeArr[$form_field_id])){
                                $checktimeFieldDatas = [];
                                foreach($formFieldTimeArr[$form_field_id] as $formFieldTime){
                                    if(in_array($formFieldTime['checktime_id'],$form_fieldid_arr[$field_id])){
                                        $fieldChecktimeResult = '';
                                        if(isset($field_result_arr[$form_field_id]) && isset($field_result_arr[$form_field_id][$formFieldTime['checktime_id']]) && !empty($field_result_arr[$form_field_id][$formFieldTime['checktime_id']])){
                                            $fieldChecktimeResult = $field_result_arr[$form_field_id][$formFieldTime['checktime_id']];
                                        }else{
                                            if(!empty($form_field_formdata) && !empty($ocrGroupId) && !empty($form_field_formdata['group_id']) && $form_field_formdata['group_id'] == $ocrGroupId){
                                                $fieldChecktimeResult = '未识别';
                                            }else{
                                                $fieldChecktimeResult = '未填写';
                                            }
                                        }
                                        $key_str = $formFieldTime['check_order'].'_'.$formFieldTime['checktime_id'];
                                        if($formFieldTime['checktime_id'] == $checktime_id){
                                            $key_str = '0_'.$formFieldTime['checktime_id'];
                                            $checktimeFieldDatas[$key_str] = '【'.$fieldChecktimeResult.'】';
                                        }else{
                                            $checktimeFieldDatas[$key_str] = '【'.$formFieldTime['check_name'].'：'.$fieldChecktimeResult.'】';
                                        }
                                        unset($fieldChecktimeResult);
                                    }

                                }
                                ksort($checktimeFieldDatas);
                                $arr[$field_id][] = $form_field_formdata['name'] != '' ? $form_field_formdata['name'].'-'.$select_field_infos[$form_field_id]['name'].'：'.implode('、',$checktimeFieldDatas) : $select_field_infos[$form_field_id]['name'].'：'.implode('、',$checktimeFieldDatas);
                                unset($checktimeFieldDatas);
                            }
                            unset($form_field_formdata);
                            unset($form_field_form);
                        }
                    }
                    unset($form_field_arr[$field_id]);
                }
                unset($field_result_arr);
                unset($select_form_field_arr);
                unset($formNameArr);
                unset($formFieldTimeArr);
                unset($select_field_infos);
                unset($ocrGroupId);
            }
            unset($form_field_arr);
        }

        return $arr;
    }

    /**
     * Notes: 获取表单内容设置的参考表单、参考检查项信息及结果
     * User: lltyy
     * DateTime: 2024/8/27 9:33
     *
     * @param array $info
     * @return array
     */
    public function fetchFieldVisitData($info=[]){
        $arr = [];

        //获取参考检查项及结果
        $visitCheckDatas = $this->fetchChecknameResultData($info);
        if(!empty($visitCheckDatas)){
            foreach($visitCheckDatas as $visitCheckKey=>$visitCheckData){
                $arr[$visitCheckKey] = $visitCheckData;
                unset($visitCheckDatas[$visitCheckKey]);
            }
        }
        unset($visitCheckDatas);

        //获取参考表单及结果
        $visitFormDatas = $this->fetchFormFieldResultData($info);
        if(!empty($visitFormDatas)){
            foreach($visitFormDatas as $visitFormKey=>$visitFormData){
                if(isset($arr[$visitFormKey]) && !empty($arr[$visitFormKey])){
                    $arr[$visitFormKey] = array_merge($arr[$visitFormKey],$visitFormData);
                }else{
                    $arr[$visitFormKey] = $visitFormData;
                }
                unset($visitFormDatas[$visitFormKey]);
            }
        }
        unset($visitFormDatas);

        return $arr;
    }

}
