<?php

namespace Application\Form;

use Application\Service\Extension\Export\BaseExport;
use Application\Service\Extension\Helper\DataHelper;
use Application\Service\Extension\Helper\StringHelper;
use Application\Service\Extension\Laminas;
use Laminas\Db\Sql\Predicate\Operator;


/**
 * 导出
 */
class ExportModel extends BaseExport
{
    /**
     * 表格内容
     * @var array
     */
    protected array $tableData = [];

    /**
     * Notes:修改查询方法
     * @param
     * @return
     * @author haojinhua
     * @date 2025-08-29
     */
    public function exportLogicError($data)
    {
        $itemId      = isset($data['item_id']) && !empty($data['item_id']) ? $data['item_id'] : 0;
        $itemsig_ids = isset($data['itemsig_ids'])&&!empty($data['itemsig_ids']) ? $data['itemsig_ids'] : 0;
        $patient_ids = isset($data['patient_ids'])&&!empty($data['patient_ids']) ? $data['patient_ids'] : 0;

        //查询质疑条件拼接
        $quer_where =  [
            'item_id' => $itemId,
            'is_del' => 0,
        ];
        //查询图片质疑条件拼接
        $question_where = [
            'item_id' => $itemId,
            'question_type' => 1,
            'is_del' => 0,
        ];
        if(!empty($itemsig_ids)){
            $question_where['item_sign_id'] = StringHelper::toArray($itemsig_ids);
            $quer_where['sign_id'] = StringHelper::toArray($itemsig_ids);
        }

        if(!empty($patient_ids)){
            $question_where['patient_id'] = StringHelper::toArray($patient_ids);
            $quer_where['patient_id'] = StringHelper::toArray($patient_ids);
        }

        //优化   查询逻辑核查质疑信息都放一起
        $allerror_infos = Laminas::$serviceManager->itemPatientFormLogicError->fetchAll([
            'where' => $quer_where,
            'columns' => ['id','is_closed','form_id','checktime_id','patient_id','is_closed','sign_id','expression_note','create_user_id','create_time','update_time','update_user_id','submit_role'],
        ]);
        $error_ids = []; //存储所有用到的error_id
        $closed_error_ids = []; //所有关闭质疑的ID
        $error_infos = [];  //质疑表信息 根据ID找到质疑所有信息
        if (!empty($allerror_infos)) {
            foreach ($allerror_infos as $error_key => $error_info) {
                $error_ids[] = $error_info['id'];
                if ($error_info['is_closed'] == 1) {
                    //所有关闭质疑的ID
                    $closed_error_ids[] = $error_info['id'];
                }
                $error_infos[$error_info['id']] = $error_info;
                unset($allerror_infos[$error_key]);
            }
            unset($allerror_infos);
        }

        $pointer_where = ['is_del' => 0];
        if (!empty($error_ids)) {
            $pointer_where['error_id'] = StringHelper::toArray($error_ids);
        }

        //查询质疑回复信息
        $all_error_replays = Laminas::$serviceManager->itemPatientFormLogicErrorQuery->fetchAll([
            'where'   => $pointer_where,
            'columns' => ['id','content','error_id','pointer_id','create_user_id','create_time','is_reply','is_auditor','platform'],
        ]);

        $error_replays = [];  //存储质疑回复信息
        $error_closes  = []; //存储质疑关闭信息
        if(!empty($all_error_replays)){
            foreach ($all_error_replays as $all_error_repkey=>$all_error_replay){
                if(in_array($all_error_replay['error_id'],$closed_error_ids)){
                    $error_closes[$all_error_replay['error_id']]['is_auditor'] = $all_error_replay['is_auditor'];
                    $error_closes[$all_error_replay['error_id']]['close_time'] = $all_error_replay['create_time'];
                    $error_closes[$all_error_replay['error_id']]['close_user'] = $all_error_replay['create_user_id'];
                }
                if($all_error_replay['is_reply'] == 1 && !in_array($all_error_replay['content'],['化验单数据更新锁定','常规识别数据更新锁定','化验单数据解锁更新','常规识别数据解锁更新','关闭质疑'])){
                    if($all_error_replay['content'] == '数据更新' && empty($all_error_replay['create_user_id'])){
                        //系统关闭了质疑 如常规识别的补充数据后自动关闭质疑会记录数据更新
                        continue;
                    }else{
                        $error_replays[$all_error_replay['error_id']][$all_error_replay['pointer_id']]['infos'][] = $all_error_replay;
                    }
                }
                unset($all_error_replays[$all_error_repkey]);
            }
            unset($all_error_replays);
        }

        // 调用
        $totalCount = Laminas::$serviceManager->itemPatientFormLogicErrorPointer->getCount($pointer_where);
        $pageSize = 10000;
        $totalPages = ceil($totalCount / $pageSize);

        if ($totalPages > 0) {
            $yeildLogDatas = Laminas::$serviceManager->itemExport->yieldData($totalPages,Laminas::$serviceManager->itemPatientFormLogicErrorPointer,$pointer_where,['id','multi_id','create_time','error_id','prop'],['id']);
        }

        $com_wheres = [
            'item_id' => $itemId,
            'is_del ' => 0
        ];

        //查询大项目ID
        $fiditem_id     = Laminas::$serviceManager->itemInfo->getOneFieldVal('fid', [
            'id' => $itemId,
        ]);
        //项目编号信息
        $itemNumber     = Laminas::$serviceManager->itemInfo->getOneFieldVal('number', [
            'id' => $itemId,
        ]);

        //中心编号信息
        $sigNumbers     = Laminas::$serviceManager->itemSignatory->fetchCol('number',['is_del = 0','item_id'=>$fiditem_id]);
        //受试者编号
        $patientNumbers = Laminas::$serviceManager->patient->fetchCol('patient_number',$com_wheres);
        //表单名称
        $formNames      = Laminas::$serviceManager->itemForm->fetchCol('name',$com_wheres);
        //检查点名称
        $checkNames      = Laminas::$serviceManager->itemChecktime->fetchCol('check_name', $com_wheres);
        //重新查询角色IDs
        $siguser_role_arr = Laminas::$serviceManager->roleSignatoryRelation->fetchDataByKey(['is_del ' =>0],['role_id','admin_user_id'],'id','admin_user_id',1);
        //人员姓名
        $userNameArr = Laminas::$serviceManager->adminUser->fetchCol('realname', ['is_del' => 0]);
        //角色数组
        $roleNameArr = Laminas::$serviceManager->realRole->fetchCol('role_name', ['is_del' => 0]);
        $form_filed_arr = Laminas::$serviceManager->itemFormField->fetchCol('name',$com_wheres);
        $result_filed_arr = Laminas::$serviceManager->itemIdentificationresultchange->fetchCol('item_name',['item_id'=>$itemId]);

        $ocrFormField = [
            'collect_date' => '检查时间',
            'item_name' => '指标名称',
            'unit' => '单位',
            'result' => '结果',
            'range' => '取值范围',
            'normal' => '正常',
            'cs' => 'CS',
            'ncs' => 'NCS',
            'is_identification' => '',
            'sdtx' => '手动填写'
        ];

        foreach ($yeildLogDatas as $pointer_datas){
            if(!empty($pointer_datas)){
                foreach ($pointer_datas as $pointer_key=>$pointer_data) {
                    $errorreplays = isset($error_replays[$pointer_data['error_id']][$pointer_data['id']]['infos']) && !empty($error_replays[$pointer_data['error_id']][$pointer_data['id']]['infos']) ? $error_replays[$pointer_data['error_id']][$pointer_data['id']]['infos'] : [];
                    $errorclose   = isset($error_closes[$pointer_data['error_id']]) && !empty($error_closes[$pointer_data['error_id']]) ? $error_closes[$pointer_data['error_id']] : [];
                    $errorinfos   = isset($error_infos[$pointer_data['error_id']]) && !empty($error_infos[$pointer_data['error_id']]) ? $error_infos[$pointer_data['error_id']] : [];

                    $itemsig_id   = !empty($errorinfos['sign_id']) ? $errorinfos['sign_id'] : 0;
                    $patient_id   = !empty($errorinfos['patient_id']) ? $errorinfos['patient_id'] : 0;
                    $form_id      = !empty($errorinfos['form_id']) ? $errorinfos['form_id'] : 0;
                    $checktime_id = !empty($errorinfos['checktime_id']) ? $errorinfos['checktime_id'] : 0;
                    $submitRole   = $errorinfos['submit_role'];
                    $userId       = $errorinfos['create_user_id'];
                    $prop         = $pointer_data['prop'];
                    $sign_id      = isset($sigNumbers[$itemsig_id]) && !empty($sigNumbers[$itemsig_id]) ? $sigNumbers[$itemsig_id] : '';

                    // 处理化验单的字段
                    if (!is_numeric($prop) && stripos($prop, '-') === false) {
                        if (in_array($prop, ['sdtx', 'collect_date'])) {
                            $prop_filed = $ocrFormField[$prop] ?? '';
                        }else{
                            if (!is_numeric($prop)) {
                                $prefix = ($ocrFormField[$prop] ?? '');
                            }

                            $change_name = isset($result_filed_arr[$pointer_data['multi_id']]) & !empty($result_filed_arr[$pointer_data['multi_id']]) ? $result_filed_arr[$pointer_data['multi_id']] : '';
                            $prop_filed = ($prefix) . "[" . $change_name. ']';
                        }
                    }else{
                        $fieldId = DataHelper::getFieldId($prop);
                        //改为直接查询字段表
                        $fieldId_name  = isset($form_filed_arr[$fieldId]) & !empty($form_filed_arr[$fieldId]) ? $form_filed_arr[$fieldId] : '';
                        $prop_filed = $fieldId_name ?? "未知的字段名称";
                    }

                    /*=====================================质疑提出人员 角色=====================================*/
                    if (!$submitRole) {
                        //查询用户角色
                        $role_ids    = isset($siguser_role_arr[$userId]) && !empty($siguser_role_arr[$userId]) ? array_values(array_unique(array_column($siguser_role_arr[$userId],'role_id')))  : [];
                        if(!empty($role_ids)){
                            foreach ($role_ids as $role_id){
                                $submitRole .= isset($roleNameArr[$role_id]) && !empty($roleNameArr[$role_id]) ? $roleNameArr[$role_id].'|' : '';
                            }
                        }
                        unset($role_ids);
                    }
                    if ($userId == 0) {
                        $create_user_id   = 'System';
                    }else{
                        $create_user_id   = isset($userNameArr[$userId]) & !empty($userNameArr[$userId]) ? $userNameArr[$userId] . '[' . $submitRole . ']' : '';
                    }

                    /*=====================================质疑关闭人员 角色=====================================*/
                    $close_user_id = isset($errorclose['close_user']) ? $errorclose['close_user'] : null;

                    if ($close_user_id === null) {
                        $close_user = '';
                        $closed_user_id   = $close_user;
                    }elseif($close_user_id == 0) {
                        $close_user = 'System';
                        $closed_user_id   = $close_user;
                    } else {
                        //关闭 查询的是edc的角色 是单角色  手动质疑只有提出质疑的人可以关 系统质疑可以自动关或者DM关  系统质疑的话 提质疑角色是空的
                        $close_role = $errorinfos['submit_role'];
                        if(!empty($close_role)){
                            $closeRole = $close_role;
                        }else{
                            $closeRole = 'DM';
                        }
                        $close_user = isset($userNameArr[$close_user_id]) & !empty($userNameArr[$close_user_id]) ? $userNameArr[$close_user_id]:'';
                        $closed_user_id    = $close_user. '[' . $closeRole . ']';
                    }

                    if (!empty($errorreplays)) {
                        $errorIdCount = []; // 初始化计数数组
                        //有回复信息
                        foreach ($errorreplays as $errorreplays_key=>$errorreplay) {
                            $error_id = $pointer_data['error_id']; // 获取当前error_id

                            // 计数逻辑（核心部分）
                            $occurrence = isset($errorIdCount[$error_id])
                                ? ++$errorIdCount[$error_id]  // 已存在：计数+1
                                : ($errorIdCount[$error_id] = 1); // 新ID：初始化计数为1

                            $closed_time    = !empty($errorclose['close_time']) ? $errorclose['close_time'] : null;

                            /*=====================================质疑回复人员 角色=====================================*/
                            if ($errorreplay['create_user_id'] === null) {
                                $reply_user_id   = '';
                            }elseif($errorreplay['create_user_id'] == 0) {
                                $reply_user_id  = 'System';
                            } else {
                                //查询用户角色
                                $rolereply_ids    = isset($siguser_role_arr[$errorreplay['create_user_id']]) && !empty($siguser_role_arr[$errorreplay['create_user_id']]) ? array_values(array_unique(array_column($siguser_role_arr[$errorreplay['create_user_id']],'role_id')))  : [];

                                if(!empty($rolereply_ids)){
                                    foreach ($rolereply_ids as $rolereply_id){
                                        $replyRole = isset($roleNameArr[$rolereply_id]) && !empty($roleNameArr[$rolereply_id]) ? $roleNameArr[$rolereply_id].'|' : '';
                                    }
                                }
                                $reply_user = isset($userNameArr[$errorreplay['create_user_id']]) & !empty($userNameArr[$errorreplay['create_user_id']]) ? $userNameArr[$errorreplay['create_user_id']]:'';
                                if($errorclose['is_auditor'] == 1){
                                    $replyRole = '图片审核员';
                                }
                                unset($rolereply_ids);
                                $reply_user_id  = $reply_user. '[' . $replyRole . ']';
                            }

                            $this->tableData[0][]     = [
                                'item_id'         => $itemNumber,
                                'sign_id'         => $sign_id,
                                'patient_id'      => isset($patientNumbers[$patient_id]) && !empty($patientNumbers[$patient_id]) ? $patientNumbers[$patient_id] : '',
                                'checktime_id'    => isset($checkNames[$checktime_id]) && !empty($checkNames[$checktime_id]) ? $checkNames[$checktime_id] : '',
                                'form_id'         => isset($formNames[$form_id]) && !empty($formNames[$form_id]) ? $formNames[$form_id] : '',
                                'prop'            => $prop_filed,
                                'error_number'    => $occurrence,
                                'expression_note' => !empty($errorinfos['expression_note']) ? $errorinfos['expression_note'] : 0,
                                'create_time'     => !empty($pointer_data['create_time']) ? date('Y-m-d H:i:s', $pointer_data['create_time']) : '',
                                'create_user_id'  => $create_user_id,
                                'reply_content'   => $errorreplay['content'],
                                'reply_time'      => !empty($errorreplay['create_time']) ? date('Y-m-d H:i:s', $errorreplay['create_time']) : '',
                                'reply_user_id'   => $reply_user_id,
                                'closed_time'     => !empty($closed_time) ? date('Y-m-d H:i:s', $closed_time) : '',
                                'closed_user_id'  => $closed_user_id
                            ];
                            unset($errorreplays[$errorreplays_key]);
                        }
                        unset($errorIdCount);
                    } else {
                        //无回复信息
                        $this->tableData[0][] = [
                            'item_id'         => $itemNumber,
                            'sign_id'         => $sign_id,
                            'patient_id'      => isset($patientNumbers[$patient_id]) && !empty($patientNumbers[$patient_id]) ? $patientNumbers[$patient_id] : '',
                            'checktime_id'    => isset($checkNames[$checktime_id]) && !empty($checkNames[$checktime_id]) ? $checkNames[$checktime_id] : '',
                            'form_id'         => isset($formNames[$form_id]) && !empty($formNames[$form_id]) ? $formNames[$form_id] : '',
                            'prop'            => $prop_filed,
                            'error_number'    => 1,
                            'expression_note' => !empty($errorinfos['expression_note']) ? $errorinfos['expression_note'] : 0,
                            'create_time'     => !empty($pointer_data['create_time']) ? date('Y-m-d H:i:s', $pointer_data['create_time']) : '',
                            'create_user_id'  => $create_user_id,
                            'reply_content'   => null,
                            'reply_time'      => null,
                            'reply_user_id'   => null,
                            'closed_time'     => !empty($closed_time) ? date('Y-m-d H:i:s', $closed_time) : '',
                            'closed_user_id'  => $closed_user_id
                        ];
                    }
                    unset($errorinfos);
                    unset($errorclose);
                    unset($errorreplays);
                    unset($pointer_datas[$pointer_key]);
                }
                unset($pointer_datas);
            }
        }

        //查询图片质疑
        $question_cond     = [
            'where'   => $question_where,
            'columns' => ['id','item_sign_id','item_id','form_id','checktime_id','patient_id','content','question','create_time','create_user_id','question_status'],
            'order'   => ['patient_id','id'],
        ];
        $question_infos = Laminas::$serviceManager->itemQuestion->fetchAll($question_cond);
        $question_ids = array_column($question_infos,'id');
        if(!empty($question_infos)){
            $question_replays = Laminas::$serviceManager->itemQuestion->getreplayinfo([
                'is_del' => 0,
                'question_id' => StringHelper::toArray($question_ids),
                new Operator('reply_status', Operator::OP_NE, 2)
            ]);
            $question_closes = Laminas::$serviceManager->itemQuestion->getreplayinfo([
                'is_del' => 0,
                'question_id' => StringHelper::toArray($question_ids),
                'reply_status' => 2,
            ]);
            foreach ($question_infos as &$question_info){
                $questionreplays = isset($question_replays[$question_info['id']]['replays']) && !empty($question_replays[$question_info['id']]['replays']) ? $question_replays[$question_info['id']]['replays'] : [];
                $questionclosed = isset($question_closes[$question_info['id']]['colsed']) && !empty($question_closes[$question_info['id']]['colsed']) ? $question_closes[$question_info['id']]['colsed'] : [];
                $question_info['closed_time']  = !empty($questionclosed) ? $questionclosed['create_time'] : null;
                $question_info['closed_user_id']  = !empty($questionclosed) ? $questionclosed['create_user_id'] : null;
                $closed_time = !empty($question_info['closed_time']) ? date('Y-m-d H:i:s', $question_info['closed_time']) : '';
                /*=====================================质疑提出人员=====================================*/
                $create_user_id  = isset($userNameArr[$question_info['create_user_id']]) & !empty($userNameArr[$question_info['create_user_id']]) ? $userNameArr[$question_info['create_user_id']] : '';
                /*=====================================质疑关闭人员 角色=====================================*/
                if ($question_info['closed_user_id']  === null) {
                    $question_info['closed_user_id'] = '';
                }elseif($question_info['closed_user_id']  == 0) {
                    $question_info['closed_user_id'] = 'System';
                } else {
                    $closeRole = '';
                    //查询用户角色
                    $closerole_ids    = isset($siguser_role_arr[$question_info['closed_user_id']]) && !empty($siguser_role_arr[$question_info['closed_user_id']]) ? array_values(array_unique(array_column($siguser_role_arr[$question_info['closed_user_id']],'role_id'))) : [];
                    if(!empty($closerole_ids)){
                        foreach ($closerole_ids as $closerole_id){
                            $closeRole .= isset($roleNameArr[$closerole_id]) && !empty($roleNameArr[$closerole_id]) ? $roleNameArr[$closerole_id].'|' : '';
                        }
                        $closeRole =  rtrim($closeRole,'|');
                    }

                    $close_user = isset($userNameArr[$question_info['closed_user_id'] ]) && !empty($userNameArr[$question_info['closed_user_id'] ]) ? $userNameArr[$question_info['closed_user_id'] ]:'';
                    $question_info['closed_user_id']   = !empty($closeRole) ? $close_user. '[' . $closeRole . ']' : $close_user;
                }
                $prop = json_decode($question_info['content'],true);
                // 处理化验单的字段
                if (is_array($prop)) {
                    $prop = current(array_keys($prop));
                }
                //改为直接查询字段表
                $fieldId_name  = isset($form_filed_arr[$prop]) & !empty($form_filed_arr[$prop]) ? $form_filed_arr[$prop] : '';
                $prop_filed = $fieldId_name ?? "未知的字段名称";


                if(!empty($questionreplays)){
                    $errorIdCount = [];
                    foreach ($questionreplays as $questionreplay){

                        /*=====================================质疑回复人员 角色=====================================*/
                        if (!$questionreplay['create_user_id']) {
                            $reply_users = '';
                        }else{
                            $reply_user_id = $questionreplay['create_user_id'];
                            if ($reply_user_id === null) {
                                $reply_users   = '';
                            }elseif($reply_user_id == 0) {
                                $reply_users = 'System';
                            } else {
                                //查询用户角色
                                $rolereply_ids    = isset($siguser_role_arr[$reply_user_id]) && !empty($siguser_role_arr[$reply_user_id]) ? array_values(array_unique(array_column($siguser_role_arr[$reply_user_id],'role_id'))) : [];
                                if(!empty($rolereply_ids)){
                                    foreach ($rolereply_ids as $rolereply_id){
                                        $replyRole = isset($roleNameArr[$rolereply_id]) && !empty($roleNameArr[$rolereply_id]) ? $roleNameArr[$rolereply_id].'|' : '';
                                    }
                                }
                                $reply_user = isset($userNameArr[$reply_user_id]) & !empty($userNameArr[$reply_user_id]) ? $userNameArr[$reply_user_id]:'';
                                $reply_users   =!empty($replyRole) ?  $reply_user. '[' . $replyRole . ']' : $reply_user;
                            }
                        }

                        $v = json_decode($questionreplay['remarks'], true);
                        if ($v['remarks'] === null) {
                            $reply_content = '';
                        }elseif (!isset($v['remarks'])) {
                            if ($questionreplay['remarks']) {
                                $reply_content = trim($questionreplay['remarks'], '"');
                            }else{
                                $reply_content = '';
                            }
                        }else{
                            $reply_content =  trim($v['remarks'], '"');
                        }

                        $error_id = $question_info['id']; // 获取当前error_id

                        // 计数逻辑（核心部分）
                        $occurrence = isset($errorIdCount[$error_id])
                            ? ++$errorIdCount[$error_id]  // 已存在：计数+1
                            : ($errorIdCount[$error_id] = 1); // 新ID：初始化计数为1
                        $this->tableData[1][]     = [
                            'item_id'         => $itemNumber,
                            'sign_id'         => isset($sigNumbers[$question_info['item_sign_id']]) && !empty($sigNumbers[$question_info['item_sign_id']]) ? $sigNumbers[$question_info['item_sign_id']] : '',
                            'patient_id'      => isset($patientNumbers[$question_info['patient_id']]) && !empty($patientNumbers[$question_info['patient_id']]) ? $patientNumbers[$question_info['patient_id']] : '',
                            'checktime_id'    => isset($checkNames[$question_info['checktime_id']]) && !empty($checkNames[$question_info['checktime_id']]) ? $checkNames[$question_info['checktime_id']] : '',
                            'form_id'         => isset($formNames[$question_info['form_id']]) && !empty($formNames[$question_info['form_id']]) ? $formNames[$question_info['form_id']] : '',
                            'prop'            => $prop_filed,
                            'error_number'    => $occurrence,
                            'expression_note' => $question_info['question'],
                            'create_time'     => !empty($question_info['create_time']) ? date('Y-m-d H:i:s', $question_info['create_time']) : '',
                            'create_user_id'  => $create_user_id,
                            'reply_content'   => $reply_content,
                            'reply_time'      => !empty($questionreplay['create_time']) ? date('Y-m-d H:i:s', $questionreplay['create_time']) : '',
                            'reply_user_id'   => $reply_users,
                            'closed_time'     => $closed_time,
                            'closed_user_id'  => $question_info['closed_user_id']
                        ];

                    }
                    unset($questionreplays);
                    unset($errorIdCount);
                }else{
                    $this->tableData[1][]     = [
                        'item_id'         => $itemNumber,
                        'sign_id'         => isset($sigNumbers[$question_info['item_sign_id']]) && !empty($sigNumbers[$question_info['item_sign_id']]) ? $sigNumbers[$question_info['item_sign_id']] : '',
                        'patient_id'      => isset($patientNumbers[$question_info['patient_id']]) && !empty($patientNumbers[$question_info['patient_id']]) ? $patientNumbers[$question_info['patient_id']] : '',
                        'checktime_id'    => isset($checkNames[$question_info['checktime_id']]) && !empty($checkNames[$question_info['checktime_id']]) ? $checkNames[$question_info['checktime_id']] : '',
                        'form_id'         => isset($formNames[$question_info['form_id']]) && !empty($formNames[$question_info['form_id']]) ? $formNames[$question_info['form_id']] : '',
                        'prop'            => $prop_filed,
                        'error_number'    => 1,
                        'expression_note' => $question_info['question'],
                        'create_time'     => !empty($question_info['create_time']) ? date('Y-m-d H:i:s', $question_info['create_time']) : '',
                        'create_user_id'  => $create_user_id,
                        'reply_content'   => null,
                        'reply_time'      => null,
                        'reply_user_id'   => null,
                        'closed_time'     => null,
                        'closed_user_id'  => null
                    ];
                }
                unset($questionclosed);
            }

            unset($question_infos);
            unset($question_replays);
            unset($question_ids);
        }

        //print_r($questionQuery);die;
        unset($sigNumbers);
        unset($itemNumber);
        unset($patientNumbers);
        unset($formNames);
        unset($checkNames);
        unset($sig_user_arr);
        unset($user_role_arr);
        unset($siguser_role_arr);
        unset($itemsig_arr);
        unset($userNameArr);
        unset($roleNameArr);
        unset($ocrFormField);
        unset($form_filed_arr);
        return $this->tableData;
    }
}