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

namespace Application\Service\DB\Item;

use Application\Service\DB\Dictionary\FormGroup;
use Application\Service\Extension\ErrorHandler;
use Application\Service\Extension\Helper\ArrayHelper;
use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\ContainerInterface;
use Laminas\Db\Sql\Predicate\Operator;
use mysql_xdevapi\CollectionModify;

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

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

    /**
     * 新增检查点, 同步贯穿全程类表单数据
     * User: lltyy
     * DateTime: 2022/9/8 9:15
     *
     * @param array $formId 贯穿全程类表单id
     * @param int $itemId 项目id
     * @param int $patientId 受试者id
     * @param int $checkTimeId 检查点id
     * @return bool
     */
    public function syncProcess(array $formId, int $itemId, int $patientId, int $checkTimeId): bool
    {
        ErrorHandler::log2txt(([$formId, $itemId, $patientId, $checkTimeId]));
        foreach ($formId as $item) {
            $currentProcessFormData = self::fetchOne([
                'where' => [
                    'form_id' => $item,
                    'item_id' => $itemId,
                    'patient_id' => $patientId,
                    'is_del' => 0
                ]
            ]);
            if ($currentProcessFormData) {
                unset($currentProcessFormData['id']);
                self::insert(ArrayHelper::merge($currentProcessFormData, ['checktime_id' => $checkTimeId]));
            }
        }
        return true;
    }

    /**
     * Notes: 获取受试者表单数据
     * User: lltyy
     * DateTime: 2022/9/6 14:49
     *
     * @param array $data
     *          item_id 副项目ID
     *          itemsig_id 项目中心ID
     *          patient_id_arr 受试者ID数组
     *          form_code_arr  表单类型Code数组
     *          patientform_array_key  受试者表单返回键值类型【0 受试者ID 1 受试者检查点ID】
     * @return array
     */
    public function getFormCountByPatient($data=[]){
        $item_id = isset($data['item_id']) ? $data['item_id'] : 0;//副项目ID
        $itemsig_id = isset($data['itemsig_id']) ? $data['itemsig_id'] : 0;//项目中心ID
        $patient_id_arr = isset($data['patient_id_arr']) ? $data['patient_id_arr'] : [];//受试者ID数组
        $form_code_arr = isset($data['form_code_arr']) ? $data['form_code_arr'] : [];//表单类型数组
        $patientform_array_key = isset($data['patientform_array_key']) ? $data['patientform_array_key'] : 0;//返回数组键值【0 受试者 1 受试者、检查点】

        $patient_form_count = [
            'over'=>[],
            'over_form'=>[],
            'form'=>[]
        ];
        //获取随机分组、贯穿全程类、常规类、检查类（OCR识别）表单ID
        $item_form_where = [
            new Operator('id', Operator::OP_GT, 0),
        ];
        if(!empty($item_id)){
            $item_form_where['item_id'] = $item_id;
        }

        $group_form_where = [
            new Operator('id', Operator::OP_GT, 0),
        ];
        if(!empty($form_code_arr)){
            $group_form_where['code'] = $form_code_arr;
        }
        $item_form_arr = $this->LocalService()->itemForm->getGroupForm($item_form_where,$group_form_where);
        if(!empty($item_form_arr)) {
            $patient_form_where = [
                'is_del' => 0,
                'form_id' => $item_form_arr,
                new Operator('patientchecktime_id', Operator::OP_GT, 0),
            ];
            if (!empty($item_id)) {
                $patient_form_where['item_id'] = $item_id;
            }
            if (!empty($itemsig_id)) {
                $patient_form_where['itemsig_id'] = $itemsig_id;
            }
            if (!empty($patient_id_arr)) {
                $patient_form_where['patient_id'] = StringHelper::toArray($patient_id_arr);
            }
            $patient_form_where_arr = [
                'where' => $patient_form_where,
                'columns' => ['id','form_id', 'patient_id','patientchecktime_id','is_complete','checktime_id']
            ];
            $patient_form_datas = $this->fetchAll($patient_form_where_arr);
            if (!empty($patient_form_datas)) {
                foreach ($patient_form_datas as $patient_form_data) {
                    $patient_form_patient = $patient_form_data['patient_id'];
                    $patient_form_patientchectime = $patient_form_data['patientchecktime_id'];
                    $patient_form_checktime = $patient_form_data['checktime_id'];
                    $patient_form_form = $patient_form_data['form_id'];
                    $key = $patient_form_patient;
                    if($patientform_array_key == 1){
                        $key = $patient_form_patientchectime;
                    }
                    if($patient_form_data['is_complete'] == 1){
                        $patient_form_count['over'][$key] = isset($patient_form_count['over'][$key]) && !empty($patient_form_count['over'][$key]) ? $patient_form_count['over'][$key] : 0;
                        $patient_form_count['over'][$key] += 1;
                        $patient_form_count['over_form'][$patient_form_patientchectime] = isset($patient_form_count['over_form'][$patient_form_patientchectime]) && !empty($patient_form_count['over_form'][$patient_form_patientchectime]) ? $patient_form_count['over_form'][$patient_form_patientchectime] : [];
                        $patient_form_count['over_form'][$patient_form_patientchectime][$patient_form_form] = $patient_form_form;
                        if($patientform_array_key == 2){
                            $patient_form_count[$patient_form_patient][$patient_form_checktime]['over'] = isset($patient_form_count[$patient_form_patient][$patient_form_checktime]['over']) && !empty($patient_form_count[$patient_form_patient][$patient_form_checktime]['over']) ? $patient_form_count[$patient_form_patient][$patient_form_checktime]['over'] : 0;
                            $patient_form_count[$patient_form_patient][$patient_form_checktime]['over'] += 1;
                            $patient_form_count[$patient_form_patient][$patient_form_checktime]['over_form']= isset($patient_form_count[$patient_form_patient][$patient_form_checktime]['over_form']) && !empty($patient_form_count[$patient_form_patient][$patient_form_checktime]['over_form']) ? $patient_form_count[$patient_form_patient][$patient_form_checktime]['over_form'] : [];
                            $patient_form_count[$patient_form_patient][$patient_form_checktime]['over_form'][]= $patient_form_form;
                        }
                    }

                    $patient_form_count['form'][$patient_form_patientchectime] = isset($patient_form_count['form'][$patient_form_patientchectime]) && !empty($patient_form_count['form'][$patient_form_patientchectime]) ? $patient_form_count['form'][$patient_form_patientchectime] : [];
                    $patient_form_count['form'][$patient_form_patientchectime][$patient_form_form] = $patient_form_form;
                    if($patientform_array_key == 2){
                        $patient_form_count[$patient_form_patient][$patient_form_checktime]['form'] = isset($patient_form_count[$patient_form_patient][$patient_form_checktime]['form']) && !empty($patient_form_count[$patient_form_patient][$patient_form_checktime]['form']) ? $patient_form_count[$patient_form_patient][$patient_form_checktime]['form'] : [];
                        $patient_form_count[$patient_form_patient][$patient_form_checktime]['form'] = $patient_form_form;
                    }
                }
            }
        }
        return $patient_form_count;
    }

    /**
     * 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,$order=['id'],$key='id',$is_more = 0){
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>$columns,
            'order'=>$order
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach($infoDatas as $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[$data_key]);
            }
        }
        unset($cond);
        unset($infoDatas);

        return $datas;
    }

    /**
     * Notes:根据受试者、检查点查数据
     * @param
     * @return
     * @author haojinhua
     * @date 2024-12-12
     */
    public function getvisitinfos($where){
        $where = $where ?:'is_del = 0';
        $datas = [];
        $cond = [
            'where'=>$where,
            'columns'=>['id','patient_id','checktime_id'],
            'order'=>['id']
        ];
        $infoDatas = $this->fetchAll($cond);
        if(!empty($infoDatas)){
            foreach ($infoDatas as $infoData){
                $datas[$infoData['checktime_id']][$infoData['patient_id']][] = $infoData['id'];
            }
        }

        return $datas;
    }

}
