<?php

namespace Application\Service\Extension\Helper;

use Application\Service\Extension\Laminas;
use Exception;
use Laminas\Db\Sql\Predicate\NotIn;

/**
 * 受试者进度管理checkcontent数组生成
 * Class progressHelper
 * @package Application\Service\Extension\Helper
 */
class ProgressHelper
{
    //是否是计划内检查点，默认计划内检查点
    protected bool $scheduled = false;

    protected $formDatas = [];

    //设置项目
    public function isPlanned($scheduled)
    {
        $this->scheduled = $scheduled == 0 ? true : false;
        return $this;
    }

    public function checktime($item_id,$patient_id,$checktime_id)
    {
        //查询检查点-表单关联表
        if($this->scheduled==true){
            $content_table = 'itemCheckcontent';
            $map['where'] = ['is_del' => 0, 'check_id' => $checktime_id];
            $map['order'] = ['order'];
        }else{
            $content_table='itemPatientcheckcontent';
            $map['where'] = ['is_del' => 0, 'patientchecktime_id' => $checktime_id];
        }
        $map['columns'] = ['id','form_id','order'];
        $CheckcontentDatas = Laminas::$serviceManager->$content_table->fetchAll($map);

        //获取被关联表单单独显示的表单
        $associated_ishide = [];
        foreach($CheckcontentDatas as $k=>$v){
            if($v['associated_ishide'] == 1){
                $associated_ishide[] = $v['form_id'];
            }
        }
        //获取表单信息
        $formId = array_column($CheckcontentDatas,'form_id');
        $formwhere['where'] = [
            'is_del' => 0,
            'id' => $formId
        ];
        $formwhere['columns'] = ['id','group_id','name','type','can_patient_write','is_patient','except_checktime'];
        $form = Laminas::$serviceManager->itemForm->fetchAll($formwhere);
        $this->formDatas = ArrayHelper::index($form,'id');
        unset($form);

        //查询表单关联表【查询当前表单是不是为二级 如果是 查询一级表单是否做过 未做过不显示】
        $FormRelationwhere['where'] = [
            'id' => 0,
            'relation_form_id' => $formId
        ];
        if(!empty($associated_ishide)){
            $FormRelationwhere['where'][] = new NotIn('relation_form_id', StringHelper::toArray($associated_ishide));
        }
        $FormRelationwhere['columns'] = ['form_id','relation_form_id'];
        $FormRelationDatas = Laminas::$serviceManager->itemFormRelation->fetchAll($FormRelationwhere);
        unset($associated_ishide,$formId);

        //关联表单已填写数据
        $PatientformInfo = Laminas::$serviceManager->itemPatientChecktimeForm->fetchCol('form_id', [
            'is_del' => 0,
            'item_id' => $item_id,
            'patient_id' => $patient_id,
            'checktime_id' => $checktime_id,
        ]);


        //表单关系快速匹配数组
        $quickFormRelationDatas = [];
        if(!empty($FormRelationDatas)) {
            //被关联表单  未填写
            $form_id_arr=[];
            foreach ($FormRelationDatas as $k=>$v){
                if(empty($this->formDatas[$v['form_id']])) continue;
                $v['arrayKey'] = $k;
                $quickFormRelationDatas[$v['form_id']] = $v;
                if(!in_array($v['relation_form_id'],$PatientformInfo)){
                    $form_id_arr[$v['relation_form_id']]=$v['relation_form_id'];
                }
            }
        }
        unset($PatientformInfo,$FormRelationDatas);
        $RelationDatas=[];
        foreach($CheckcontentDatas as $kk=>&$vv){
            //关联表单未填写，隐藏

            $vv['is_show'] = true;
            //被关联表单不展示
            if(!empty($form_id_arr) && isset($form_id_arr[$vv['form_id']])) {
//                    unset($CheckcontentDatas[$kk]);
//                    continue;
                $vv['is_show'] = false;
            }
            $relation_v = $quickFormRelationDatas[$vv['form_id']] ?? [];
            if(!empty($relation_v)){
                $RelationDatas[$relation_v['arrayKey']]['order']=$vv['order'];
                $RelationDatas[$relation_v['arrayKey']]['relation_form_id']=$relation_v['relation_form_id'];
            }

            if(!isset($this->formDatas[$vv['form_id']])) {
                unset($CheckcontentDatas[$kk]);
                continue;
            }
            //贯穿全程不在哪个检查点显示
            $val = $this->formDatas[$vv['form_id']];
            if($val['except_checktime'] == null) $val['except_checktime'] = '';
            if(!empty($val['except_checktime']) && $this->scheduled == true && $val['group_id'] == 4){
                if(in_array($checktime_id,explode(',',$val['except_checktime']))){
                    unset($CheckcontentDatas[$kk]);
                    continue;
                }
            }
        }
        foreach ($CheckcontentDatas as $Check_k => &$Check_v) {
            foreach ($RelationDatas as $r_k=>$r_v){
                if($Check_v['form_id']==$r_v['relation_form_id']){
                    $Check_v['order']=strval($r_v['order']+1);
                }
            }
        }
        unset($form_id_arr,$form);
        //按照分类、表单排序进行 排序
        $last_names = array_column($CheckcontentDatas,'order');
        array_multisort($last_names,SORT_ASC,$CheckcontentDatas);
        return [array_values($CheckcontentDatas ),$this->formDatas];
    }
}