<?php

namespace Application\Service\DB\Item;

use Application\Service\Extension\Helper\StringHelper;
use Application\Service\Extension\Laminas;
use Interop\Container\Containerinterface;
use Laminas\Db\Sql\Predicate\Operator;

class PatientFormLogicError extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        if (!$this->tableName) {
            $this->tableName = 'yikeen_item_patient_form_logic_error';
        }

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

    /**
     * Notes: 查询质疑是否关闭（以后可能会新加别的查询字段）
     * @param
     * @return
     * @author haojinhua
     * @date 2024-09-04
     */
    public function getErrorInfo($where = ''){
        $res = [];
        $where  = !empty($where) ? $where : [new Operator('id',Operator::OP_GT,0)];
        $cond = [
            'where' => $where,
            'columns' => ['id','item_id','form_id','checktime_id','patient_id','is_closed','sign_id','expression_note','create_user_id','create_time','update_time','update_user_id','submit_role'],
        ];

        $datas = $this->fetchAll($cond);
        if(!empty($datas)){
            foreach ($datas as $data){
                $res[$data['id']] = $data;
            }
        }

        return $res;
    }

    /**
     * Notes:查询对应质疑信息
     * @param
     * @return
     * @author haojinhua
     * @date 2024-10-11
     */
    public function fetchErrorInfo($info = []){
        $res = [
            'patient_form_checktime'=>[]
        ];
        $item_id    = isset($info['item_id']) && !empty($info['item_id']) ? $info['item_id'] : [];
        $itemsig_id = isset($info['itemsig_id']) && !empty($info['itemsig_id']) ? $info['itemsig_id'] : [];
        $type       = isset($info['type']) && !empty($info['type']) ? $info['type'] : 0;

        //图片质疑-----》后台质疑沟通   【1 未质疑 2 已质疑 3 质疑关闭】
        $edc_question_where = [
            'is_report' => 1,
            'is_report_special' => 1,
        ];  //时间展示入口只查询化验单类型的质疑、 未查，自动发的质疑不统计
        if(!empty($item_id)){
            $edc_question_where['item_id'] = StringHelper::toArray($item_id);
        }
        if(!empty($itemsig_id)){
            $edc_question_where['sign_id'] = StringHelper::toArray($itemsig_id);
        }


        if($type == 2){
            $edc_question_where['is_closed'] = 0;
        }elseif($type == 3){
            //质疑关闭 查询所有关闭的 化验单类型质疑信息
            $edc_question_where['is_closed'] = 1;
        }

        //查询edc质疑
        $edcquestionDatas = Laminas::$serviceManager->itemPatientFormLogicError->fetchAll([
            'where'=>$edc_question_where,
            'columns'=>['id','patient_id','checktime_id','form_id','is_closed']
        ]);

        if(!empty($edcquestionDatas)){
            $edcquestion_statusArr = [0=>'未关闭',1 =>'已关闭'];
            foreach ($edcquestionDatas as $edcquestionDatas){
                $edcquestion_status  = isset($edcquestion_statusArr[$edcquestionDatas['is_closed']])&& !empty($edcquestion_statusArr[$edcquestionDatas['is_closed']])?$edcquestion_statusArr[$edcquestionDatas['is_closed']] : '';
                $res[$edcquestionDatas['form_id']][$edcquestionDatas['checktime_id']][$edcquestionDatas['patient_id']]['question_status'] = $edcquestion_status;
                $edc_patient_form_checktime_key = $edcquestionDatas['patient_id'].'_'.$edcquestionDatas['form_id'].'_'.$edcquestionDatas['checktime_id'];
                if(!in_array($edc_patient_form_checktime_key,$res['patient_form_checktime'])){
                    $res['patient_form_checktime'][] = $edc_patient_form_checktime_key;
                }
            }
        }

        unset($edcquestionDatas);
        return $res;
    }

    /**
     * Notes: 获取待处理的EDC质疑数
     * User: lltyy
     * DateTime: 2024/11/18 8:17
     *
     * @param array $info
     * @return array
     */
    public function fetchNoreplyQuestion(array $info = []){
        $return_type = isset($info['return_type']) && !empty($info['return_type']) ? $info['return_type'] : 0;//返回类型 0 中心项目信息
        $item_id = isset($info['item_id']) && !empty($info['item_id']) ? $info['item_id'] : 0;//副项目ID
        $itemsig_id = isset($info['itemsig_id']) && !empty($info['itemsig_id']) ? $info['itemsig_id'] : 0;//项目中心ID

        //获取患者检查点信息
        $arr = [];
        $where = [
            'is_del' => 0,
            'is_closed' => 0,
            'is_reply' => 0,
        ];
        if(!empty($item_id)){
            $where['item_id'] = $item_id;
        }
        if(!empty($itemsig_id)){
            $where['sign_id'] = $itemsig_id;
        }
        $datas = $this->fetchAll([
            'where'=>$where,
            'columns'=>['item_id','sign_id'],
            'order'=>['id']
        ]);
        unset($where);
        if(!empty($datas)){
            foreach($datas as $key=>$data){
                $data_item = $data['item_id'];
                $data_itemsig = $data['sign_id'];
                if($return_type == 0){
                    //每个中心的待处理质疑数
                    $arr['noreplay_question_count'][$data_itemsig][$data_item] = isset($arr['noreplay_question_count'][$data_itemsig][$data_item]) && !empty($arr['noreplay_question_count'][$data_itemsig][$data_item]) ? $arr['noreplay_question_count'][$data_itemsig][$data_item] : 0;
                    $arr['noreplay_question_count'][$data_itemsig][$data_item] += 1;

                    //需要质疑发信的中心
                    $arr['send_mes_sig'] = isset($arr['send_mes_sig']) && !empty($arr['send_mes_sig']) ? $arr['send_mes_sig'] : [];
                    if(!in_array($data_itemsig,$arr['send_mes_sig'])){
                        $arr['send_mes_sig'][] = $data_itemsig;
                    }

                    //需要质疑发信的项目
                    $arr['send_mes_item'] = isset($arr['send_mes_item']) && !empty($arr['send_mes_item']) ? $arr['send_mes_item'] : [];
                    if(!in_array($data_item,$arr['send_mes_item'])){
                        $arr['send_mes_item'][] = $data_item;
                    }
                }

                //释放空间
                unset($data_item);
                unset($data_itemsig);
                unset($datas[$key]);
            }
        }

        return $arr;
    }


}