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

namespace Application\Service\DB\Item;

use Application\Service\Extension\Helper\StringHelper;
use Interop\Container\ContainerInterface;

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

    /**
     * Notes: 获取计划外检查点下的表单数据
     * User: lltyy
     * DateTime: 2022/9/8 9:15
     *
     * @param array $data
     *          item_id 副项目ID
     *          itemsig_id 项目中心ID
     *          patient_id_arr 受试者ID数组
     */
    public function getFormByPatientchecktime($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_id_arr = isset($data['form_id_arr']) ? $data['form_id_arr'] : [];//表单ID数组
        $where = [
            'is_del' => 0,
        ];
        if(!empty($item_id)){
            $where['item_id'] = $item_id;
        }
        if(!empty($itemsig_id)){
            $where['itemsig_id'] = $itemsig_id;
        }
        if(!empty($patient_id_arr)){
            $where['patient_id'] = StringHelper::toArray($patient_id_arr);
        }
        if(!empty($form_id_arr)){
            $where['form_id'] = StringHelper::toArray($form_id_arr);
        }
        $where_arr = [
            'where'=>$where,
            'columns'=>['patientchecktime_id','form_id','is_choose']
        ];
        $info_datas = $this->fetchAll($where_arr);
        $arr = [
            'all'=>[],//所有表单
            'choose'=>[],//选做表单
            'select'=>[]//必做表单
        ];
        if(!empty($info_datas)){
            foreach($info_datas as $info_data){
                $check_id = $info_data['patientchecktime_id'];
                $form_id = $info_data['form_id'];
                $arr['all'][$check_id][$form_id] = $form_id;
                if($info_data['is_choose'] == 1){
                    $arr['select'][$check_id][$form_id] = $form_id;
                }else{
                    $arr['choose'][$check_id][$form_id] = $form_id;
                }
            }
        }
        return $arr;
    }
}