<?php
namespace Application\Service\DB\Item;
use Interop\Container\ContainerInterface;
use Laminas\Stdlib\ArrayUtils;

class Randomdetails extends \Application\Service\DB\AbstractDb
{
	private $randGroupId = 'randgroup_id';
	public function __construct($dbAdapter, ContainerInterface $container)
	{
		$this->tableName = 'yikeen_item_randomdetails';
		parent::__construct($dbAdapter, $container);
	}
	/**
	 * Notes: 获取随机号
	 * @param int $item_id 项目id(小项目id)
	 * @param int $itemsig_id 中心id
	 * @param int $patient_id 受试者id
	 * @param int $random_group_id 研究分层id
	 * @return array
	 */
	public function getRandomdetailsNumberPop($item_id,$itemsig_id=0,$patient_id=0, $random_group_id = 0){
		$whereS = [
			'is_annul' => 0,
			'is_del' => 0,
			'is_use' => 0,
			'item_id' => $item_id,
		];
		!empty($itemsig_id) && $whereS['item_sign_id'] = $itemsig_id;//or item_sign_id=0)
		$wherePatient = [
			'is_del' => 0,
			'item_id' => $item_id,
		];
		!empty($itemsig_id) && $wherePatient['itemsig_id'] = $itemsig_id;

		//该项目下有没有自定义分层=======受试者有没有研究分层属性，都给他分配随机号
		$wherePatientattrs = [
			'is_del' => 0,
			'item_id' => $item_id,
		];
		$patientattrsField = $this->LocalService()->patientattrs->fetchCol('attr_field', ArrayUtils::merge($wherePatientattrs, [
			'attr_field' => $this->randGroupId,
		]));

		$itemFid = $this->LocalService()->itemInfo->getOneFieldVal('fid',[
			'id' => $item_id,
		]);
		$itemBlindMethod = 0;
		if (!empty($itemFid)){
			$itemBlindMethod = $this->LocalService()->itemInfo->getOneFieldVal('blind_method',[
				'id' => $itemFid,
			]);
		}
		//方案
		$itemRandomNameArr = $this->LocalService()->itemRandom->fetchCol('random_name', [
			'is_del' => 0,
			'item_id' => $item_id,
		]);

		//药物分组
		$medicationFlag = 0;
		$itemMedicationDatas = [];
		$whereItemMedication['where'] = [
			'is_del' => 0,
			'item_id' => $item_id,
		];
		$whereItemMedication['columns'] = ['id', 'medication_name', 'medication_message'];
		$itemMedicationData = $this->LocalService()->itemMedication->fetchAll($whereItemMedication);
		if (!empty($itemMedicationData)){
			foreach ($itemMedicationData as $itemMedicationDataKey=>$itemMedicationDataValue) {
				$itemMedicationDatas[$itemMedicationDataValue['id']] = $itemMedicationDataValue;
			}
		}

		//受试者
		$wherePatientArr['where'] =  ArrayUtils::merge($wherePatient, [
			'id' => $patient_id,
		]);
		$wherePatientArr['columns'] =['id','patient_name','randomdetails_name'=>'random_number','random_number_id','random_id','randgroup_id'] ;
		$patientInfo = $this->LocalService()->patient->fetchOne($wherePatientArr);

		if (!empty($patientInfo['randgroup_id'])){//!empty($patientInfo['randgroup_id'])去掉研究分层===不验证研究分层
			if (!empty($patientInfo['random_number_id'])){
				//受试者已添加分层
				$whereitemRandomdetails['where'] = [
					'id' => $patientInfo['random_number_id'],
				];
			}else{
				//表单添加分层
				$whereitemRandomdetails['where'] = ArrayUtils::merge($whereS, [
					'randgroup_id' => $patientInfo['randgroup_id'],
				]);
				$itemRandomdetailsInfo = $this->LocalService()->itemRandomdetails->fetchOne($whereitemRandomdetails);
				if (empty($itemRandomdetailsInfo)){
					$whereitemRandomdetails['where'] = [
						'is_annul' => 0,
						'is_del' => 0,
						'is_use' => 0,
						'item_id' => $item_id,
						'randgroup_id' => $patientInfo['randgroup_id'],
					];
				}
			}

			$whereitemRandomdetails['columns'] = ['id','item_id','item_sign_id','randomdetails_name','area_code','randgroup_id','programme_id','medication_id'];
			$itemRandomdetailsInfo = $this->LocalService()->itemRandomdetails->fetchOne($whereitemRandomdetails);
			if (!empty($itemRandomdetailsInfo)){
				if (!empty($itemRandomNameArr)){
					$itemRandomName = isset($itemRandomNameArr[$itemRandomdetailsInfo['programme_id']])?$itemRandomNameArr[$itemRandomdetailsInfo['programme_id']]:'';
					$medicationName = isset($itemMedicationDatas[$itemRandomdetailsInfo['medication_id']]['medication_name'])?$itemMedicationDatas[$itemRandomdetailsInfo['medication_id']]['medication_name']:'';
					$medicationMessage = isset($itemMedicationDatas[$itemRandomdetailsInfo['medication_id']]['medication_message'])?$itemMedicationDatas[$itemRandomdetailsInfo['medication_id']]['medication_message']:'';
					$itemRandomdetailsInfo['medicationName'] = $medicationName;
					$itemRandomdetailsInfo['medicationMessage'] = $medicationMessage;
					$itemRandomdetailsInfo['itemBlindMethod'] = $itemBlindMethod;
					if (isset($itemMedicationDatas[$itemRandomdetailsInfo['medication_id']])){
						$itemRandomdetailsInfo['medicationFlag'] = 1;
					}else{
						$itemRandomdetailsInfo['medicationFlag'] = $medicationFlag;
					}
					if (!empty($itemRandomName)){
						$itemRandomdetailsInfo['randomdetails_name'] = $itemRandomdetailsInfo['randomdetails_name'].'/'.$itemRandomName;
					}
				}
			}else{
				$itemRandomdetailsInfo = ['id'=>0,'randomdetails_name'=>''];
			}

		}else{
			$itemRandomdetailsInfo = ['id'=>0,'randomdetails_name'=>''];
		}

		$whereItemRandGroup['where'] = [
			'is_del' => 0,
			'item_id' => $item_id,
		];
		if($random_group_id) $whereItemRandGroup['where']['id'] = $random_group_id;
		$whereItemRandGroup['columns'] = ['id','group_name'];
		$itemRandGroupData = $this->LocalService()->itemRandgroup->fetchAll($whereItemRandGroup);
		if (empty($itemRandGroupData)){
			return [['id'=>0,'group_name'=>'暂无分组','rand_number'=>['id'=>0,'randomdetails_name'=>'']]];
		}
		$randgroupIdS = implode(',', array_column($itemRandGroupData,'id'));

		//中心下的随机号
		$whereItemRandomDetails['where'] = ArrayUtils::merge($whereS, [
			'randgroup_id' => array_column($itemRandGroupData,'id')
		]);
		$whereItemRandomDetails['order'] = 'randomdetails_order';
		$whereItemRandomDetails['columns'] = ['id','item_id','item_sign_id','randomdetails_name','area_code','randgroup_id','programme_id','medication_id'];
		$itemRandomDetailsData = $this->LocalService()->itemRandomdetails->fetchAll($whereItemRandomDetails);

		//项目下的随机号并且中心为空的随机号
		if (empty($itemRandomDetailsData)){
			$whereItemRandomDetails['where'] = [
				'is_annul' => 0,
				'is_del' => 0,
				'is_use' => 0,
				'item_sign_id' => 0,
				'item_id' => $item_id,
				'randgroup_id' => array_column($itemRandGroupData,'id')
			];
			$itemRandomDetailsData = $this->LocalService()->itemRandomdetails->fetchAll($whereItemRandomDetails);
		}

		if (!empty($itemRandomDetailsData)){
			foreach ($itemRandGroupData as $itemRandGroupDataKey=>&$itemRandGroupDataValue) {
				$itemRandGroupDataValue['rand_number'] = ['id'=>0,'randomdetails_name'=>''];
				//老数据没有研究分层，匹配不上。。。
				if (!empty($patientInfo['id']) && ($itemRandGroupDataValue['id'] == $patientInfo['randgroup_id'])){
					$itemRandGroupDataValue['rand_number'] = $itemRandomdetailsInfo;
					continue;
				}
				foreach ($itemRandomDetailsData as $itemRandomDetailsDataKey=>$itemRandomDetailsDataValue){
					if ($itemRandomDetailsDataValue['randgroup_id'] == $itemRandGroupDataValue['id'] ){
						if (!empty($itemRandomNameArr)){
							$itemRandomName = isset($itemRandomNameArr[$itemRandomDetailsDataValue['programme_id']])?$itemRandomNameArr[$itemRandomDetailsDataValue['programme_id']]:'';
							if (!empty($itemRandomName)){
								$itemRandomDetailsDataValue['randomdetails_name'] = $itemRandomDetailsDataValue['randomdetails_name'].'/'.$itemRandomName;
							}
						}
						$medicationName = isset($itemMedicationDatas[$itemRandomDetailsDataValue['medication_id']]['medication_name'])?$itemMedicationDatas[$itemRandomDetailsDataValue['medication_id']]['medication_name']:'';
						$medicationMessage = isset($itemMedicationDatas[$itemRandomDetailsDataValue['medication_id']]['medication_message'])?$itemMedicationDatas[$itemRandomDetailsDataValue['medication_id']]['medication_message']:'';
						$itemRandomDetailsDataValue['medicationName'] = $medicationName;
						$itemRandomDetailsDataValue['medicationMessage'] = $medicationMessage;
						$itemRandomDetailsDataValue['itemBlindMethod'] = $itemBlindMethod;
						if (isset($itemMedicationDatas[$itemRandomdetailsInfo['medication_id']])){
							$itemRandomdetailsInfo['medicationFlag'] = 1;
						}else{
							$itemRandomdetailsInfo['medicationFlag'] = $medicationFlag;
						}

						$itemRandGroupDataValue['rand_number'] =  $itemRandomDetailsDataValue;
						break;
					}
				}
				if (empty($itemRandGroupDataValue['rand_number']['id'])){
					$whereItemRandomDetails['where'] = [
						'is_annul' => 0,
						'is_del' => 0,
						'is_use' => 0,
						'item_sign_id' => 0,
						'item_id' => $item_id,
						'randgroup_id' => $itemRandGroupDataValue['id'],
					];
					$itemRandomDetailsInfo = $this->LocalService()->itemRandomdetails->fetchOne($whereItemRandomDetails);
					if (!empty($itemRandomNameArr) && !empty($itemRandomDetailsInfo)){
						$itemRandomName = isset($itemRandomNameArr[$itemRandomDetailsInfo['programme_id']])?$itemRandomNameArr[$itemRandomDetailsInfo['programme_id']]:'';
						if (!empty($itemRandomName)){
							$itemRandomDetailsInfo['randomdetails_name'] = $itemRandomDetailsInfo['randomdetails_name'].'/'.$itemRandomName;
						}
						$medicationName = isset($itemMedicationDatas[$itemRandomDetailsInfo['medication_id']]['medication_name'])?$itemMedicationDatas[$itemRandomDetailsInfo['medication_id']]['medication_name']:'';
						$medicationMessage = isset($itemMedicationDatas[$itemRandomDetailsInfo['medication_id']]['medication_message'])?$itemMedicationDatas[$itemRandomDetailsInfo['medication_id']]['medication_message']:'';
						$itemRandomDetailsInfo['medicationName'] = $medicationName;
						$itemRandomDetailsInfo['medicationMessage'] = $medicationMessage;
						$itemRandomDetailsInfo['itemBlindMethod'] = $itemBlindMethod;
						if (isset($itemMedicationDatas[$itemRandomdetailsInfo['medication_id']])){
							$itemRandomdetailsInfo['medicationFlag'] = 1;
						}else{
							$itemRandomdetailsInfo['medicationFlag'] = $medicationFlag;
						}

						$itemRandGroupDataValue['rand_number'] = $itemRandomDetailsInfo;
					}
				}
			}
		}else{
			foreach ($itemRandGroupData as $itemRandGroupDataKey=>&$itemRandGroupDataValue) {
				$itemRandGroupDataValue['rand_number'] = ['id'=>0,'randomdetails_name'=>''];
			}
		}

		return $itemRandGroupData;
	}

	/**
	 * Notes: 更新随机号及中心
	 * @param int $randomdetailsId 随机号id
	 * @param int $itemsig_id 中心id
	 * @param int $patient_id 受试者id
	 * @return bool(true|false)
	 */
	public function setRandomdetailsNumber($randomdetailsId,$itemsig_id=0,$patient_id=0){
		//受试者
		$wherePatientArr['where'] = [
			'id' => $patient_id,
		];
		$wherePatientArr['columns'] =['id','patient_name','randomdetails_name'=>'random_number','random_number_id','random_id','randgroup_id','patient_content','item_id'] ;
		$patientInfo = $this->LocalService()->patient->fetchOne($wherePatientArr);
		if (empty($patientInfo)){
			return false;
		}
		$whereS = [
			'is_annul' => 0,
			'is_del' => 0,
			'id' => $randomdetailsId
		];
		$whereItemRandomDetails['where'] = $whereS;
		$itemRandomDetailsInfoUse= $this->LocalService()->itemRandomdetails->fetchOne($whereItemRandomDetails);
		$flag = 0;
		if (!empty($patientInfo['randgroup_id'])){
			if (($patientInfo['randgroup_id'] == $itemRandomDetailsInfoUse['randgroup_id']) && ($patientInfo['random_number_id'] == $itemRandomDetailsInfoUse['id'])){
				return true;
			}
			$flag = 1;
		}

		$whereS = [
			'is_annul' => 0,
			'is_del' => 0,
			'is_use' => 0,
			'id' => $randomdetailsId,
		];
		$whereItemRandomDetails['where'] = $whereS;
		$itemRandomDetailsInfo= $this->LocalService()->itemRandomdetails->fetchOne($whereItemRandomDetails);
		if(empty($itemRandomDetailsInfo)){
			return false;
		}
		$whereRDS = [
			'is_annul' => 0,
			'is_del' => 0,
			'is_use' => 0,
			'item_id' => $itemRandomDetailsInfo['item_id']
		];
		if (!empty($itemRandomDetailsInfo['area_code'])){
			//把作废的一组区组号修改中心===方案下的区号
//			$whereRDS = 'is_annul=0 and is_use=0 and item_id=' . $itemRandomDetailsInfo['item_id'].' and programme_id=' . $itemRandomDetailsInfo['programme_id'].' and area_code='.$itemRandomDetailsInfo['area_code'];
			//新逻辑，要把不同的方案下并且是同一个区组号的所有中心修改==11-03
			//新逻辑，有分层只更新当前分层下的数据，没有分层更新所有的数据，
			if(!empty($itemRandomDetailsInfo['randgroup_id'])){
				$whereRDS = [
					'is_annul' => 0,
					'item_id' => $itemRandomDetailsInfo['item_id'],
					'is_use' => 0,
					'area_code' => $itemRandomDetailsInfo['area_code'],
					'randgroup_id' => $itemRandomDetailsInfo['randgroup_id'],
				];
			}else{
				$whereRDS = [
					'is_annul' => 0,
					'item_id' => $itemRandomDetailsInfo['item_id'],
					'is_use' => 0,
					'area_code' => $itemRandomDetailsInfo['area_code'],
				];
			}
		}else{
			$whereRDS['id'] = $randomdetailsId;
		}
		$itemRandomdetailsIdArr = $this->LocalService()->itemRandomdetails->fetchCol('id', $whereRDS);
		//更新中心
		if (!empty($itemsig_id)){
			if (!empty($itemRandomdetailsIdArr)){
				$upArr = ['item_sign_id'=>$itemsig_id];
				$result=$this->LocalService()->itemRandomdetails->update($upArr,[
					'id' => $itemRandomdetailsIdArr
				]);
			}
		}
		//该项目下有没有自定义分层==没有设置返回false====没有分层分配随机号
		$wherePatientattrs['where'] = [
			'is_del' => 0,
			'item_id' => $itemRandomDetailsInfo['item_id'],
			'attr_field' => $this->randGroupId,
		];
		$patientattrsInfo = $this->LocalService()->patientattrs->fetchOne($wherePatientattrs);

		$newPatientContent = '';
		if(!empty($patientattrsInfo)){
			if (!empty($patientInfo['patient_content'])){
				$arr['id'] = $patientattrsInfo['id'];
				$arr['field'] = $this->randGroupId;
				$arr['value'] = $itemRandomDetailsInfo['randgroup_id'];
				$patientContent = json_decode($patientInfo['patient_content'],true);
				$status = 0;
				foreach ($patientContent as $patientContentKey=>$patientContentValue) {
					if ($patientContentValue['field'] == 'randgroup_id'){
						$patientContent[$patientContentKey]['value'] = $itemRandomDetailsInfo['randgroup_id'];
						$status = 1;
						break;
					}
				}
				if (empty($status)){
					array_push($patientContent,$arr);
				}
				$newPatientContent = json_encode($patientContent);
			}else{
				$arr['id'] = $patientattrsInfo['id'];
				$arr['field'] = $this->randGroupId;
				$arr['value'] = $itemRandomDetailsInfo['randgroup_id'];
				$newPatientContent = json_encode($arr);
			}
		}


		//更新受试者
		$newLogArr = $upPatientData = [
			'id'=>$patient_id,
			'randgroup_id'=>$itemRandomDetailsInfo['randgroup_id'],
			'random_id'=>$itemRandomDetailsInfo['programme_id'],
			'random_number_id'=>$itemRandomDetailsInfo['id'],
			'random_number'=>$itemRandomDetailsInfo['randomdetails_name'],
		];

		if (!empty($newPatientContent)){
			$upPatientData['patient_content'] = $newPatientContent;
		}
		$resultPatient = $this->LocalService()->patient->save($upPatientData);

		if(empty($resultPatient)) return false;

		if (!empty($flag) && !empty($patientInfo['random_number_id'])){
			//删除随机号====//新逻辑，切换研究分层时把原来的更改为未调用
//			$upDelData = ['id'=>$patientInfo['random_number_id'],'is_use'=>0];//,'is_del'=>1
			//删除随机号====//新逻辑，切换研究分层时把原来的更改为已作废====
			$upDelData = ['id'=>$patientInfo['random_number_id'],'is_del'=>1];//,'is_del'=>1
			$resultItemRandomdetails = $this->LocalService()->itemRandomdetails->save($upDelData);
			if(empty($resultItemRandomdetails)) return false;
		}

		//更新已调用
		$upData = ['id'=>$itemRandomDetailsInfo['id'],'is_use'=>1];
		$resultItemRandNumberOne = $this->LocalService()->itemRandomdetails->save($upData);
		if(empty($resultItemRandNumberOne)) return false;
//		echo $patientInfo['random_id'] .'!='. $itemRandomDetailsInfo['programme_id'];die;
		//更新受试者与检查点关系表
		if (!empty($itemRandomDetailsInfo['programme_id'])){
			if (!empty($flag) && (!empty($patientInfo['random_id'])) && ($patientInfo['random_id'] != $itemRandomDetailsInfo['programme_id'])){
				$this->LocalService()->itemPatientchecktime->DelChecktime($patientInfo['random_id'],$patient_id,$patientInfo['item_id']);

			}
			if (($patientInfo['random_id'] != $itemRandomDetailsInfo['programme_id'])){
				$this->LocalService()->itemChecktime->insertPatientchecktime($itemRandomDetailsInfo['item_id'],$itemsig_id,$patient_id,$itemRandomDetailsInfo['programme_id']);
			}
		}
		unset($newLogArr['random_number_id']);
		$patientData = $this->patientTemplate($patientInfo);
		$oper_type = 0;
		$log_remark = '（系统分配随机号）随机号添加';
		if (!empty($patientInfo['randgroup_id']) || !empty($patientInfo['programme_id']) || !empty($patientInfo['randomdetails_name'])){// || !empty($patientInfo['random_number_id'])
			$oldData = [
				'randgroup_id'=>$patientInfo['randgroup_id'],
				'random_id'=>$patientInfo['random_id'],
				'random_number'=>$patientInfo['randomdetails_name'],
			];
			$_POST['old_data_str'] = json_encode($oldData);
			$oper_type = 1;
			$log_remark = '（系统分配随机号）随机号修改';
		}
		if (!empty($resultPatient)) $this->LocalService()->log->saveFileLog($patient_id,'patient',$newLogArr,$oper_type,$log_remark,$patientInfo['item_id'],'',['item_id'=>$patientInfo['item_id'],'patientAttrData'=>$patientData]);
		return true;
	}

	public function setRandom($randomId,$itemsig_id=0,$patient_id=0,$random_number){
		//受试者
		$wherePatientArr['where'] = ['id' => $patient_id];
		$wherePatientArr['columns'] =['id','patient_name','randomdetails_name'=>'random_number','random_number_id','random_id','randgroup_id','patient_content','item_id'] ;
		$patientInfo = $this->LocalService()->patient->fetchOne($wherePatientArr);
		if (empty($patientInfo)){
			return false;
		}

		//更新受试者
		$newLogArr = $upPatientData = [
			'id'=>$patient_id,
			'randgroup_id'=>0,
			'random_id'=>$randomId,
			'random_number_id'=>0,
			'random_number'=>$random_number,
		];

		$resultPatient = $this->LocalService()->patient->save($upPatientData);

		if(empty($resultPatient)) return false;

		if (!empty($patientInfo['random_number_id'])){
			//删除随机号====//新逻辑，切换研究分层时把原来的更改为未调用
//			$upDelData = ['id'=>$patientInfo['random_number_id'],'is_use'=>0];//,'is_del'=>1
			//删除随机号====//新逻辑，切换研究分层时把原来的更改为已作废====
			$upDelData = ['id'=>$patientInfo['random_number_id'],'is_del'=>1];//,'is_del'=>1
			$resultItemRandomdetails = $this->LocalService()->itemRandomdetails->save($upDelData);
			if(empty($resultItemRandomdetails)) return false;
		}

		if($patientInfo['random_id'] != $randomId && !empty($patientInfo['random_id'])){
			$this->LocalService()->itemPatientchecktime->DelChecktime($patientInfo['random_id'],$patient_id,$patientInfo['item_id']);
		}

		if($patientInfo['random_id'] != $randomId){
			$this->LocalService()->itemChecktime->insertPatientchecktime($patientInfo['item_id'],$itemsig_id,$patient_id,$randomId);
		}
		unset($newLogArr['random_number_id']);
		$patientData = $this->patientTemplate($patientInfo);
		$oper_type = 0;
		$log_remark = '（手动分配随机号）随机号添加';
		if (!empty($patientInfo['randgroup_id']) || !empty($patientInfo['programme_id']) || !empty($patientInfo['randomdetails_name'])){// || !empty($patientInfo['random_number_id'])
			$oldData = [
				'randgroup_id'=>$patientInfo['randgroup_id'],
				'random_id'=>$patientInfo['random_id'],
				'random_number'=>$patientInfo['randomdetails_name'],
			];
			$_POST['old_data_str'] = json_encode($oldData);
			$oper_type = 1;
			$log_remark = '（手动分配随机号）随机号修改';
		}

		if (!empty($resultPatient)) $this->LocalService()->log->saveFileLog($patient_id,'patient',$newLogArr,$oper_type,$log_remark,$patientInfo['item_id'],'',['item_id'=>$patientInfo['item_id'],'patientAttrData'=>$patientData]);

		return true;
	}

	/**
	 * Notes: 查询中心
	 * @param int $itemsig_id 中心id
	 * @param int $item_id 项目id（小项目id，有项目id时返回给数据结构用）
	 * @return array
	 */
	public function itemSigninfo($item_sign_id =0,$item_id=0) {
		//项目合作方
		if (!empty($item_id)){
			//数据结构调用
			$newItemSignatoryData = [];
			$bigItemId = 0;
			$itemFid = $this->LocalService()->itemInfo->getOneFieldVal('fid',[
				'is_del' => 0,
				'id' => $item_id,
			]);
			if(!empty($itemFid)){
				$bigItemId = $this->LocalService()->itemInfo->getOneFieldVal('id',[
					'is_del' => 0,
					'id' => $itemFid,
				]);
			}
			if (empty($bigItemId) || empty($itemFid)){
				return $newItemSignatoryData;
			}
			$whereItemSignatory['where'] = [
				'is_del' => 0,
				'item_id' => $bigItemId,
			];
		}else{
			$whereItemSignatory['where'] = [
				'is_del' => 0,
				'id' => explode(',', $item_sign_id)
			];
		}
		$whereItemSignatory['columns'] = ['id', 'item_id', 'signatory_id','number'];//, 'department_id'
		$whereItemSignatory['order'] = 'order';
		$itemSignatoryData = $this->LocalService()->itemSignatory->fetchAll($whereItemSignatory);
		if (!empty($itemSignatoryData)) {
			$sign_ids = implode(',',array_filter(array_unique( array_column($itemSignatoryData, 'signatory_id'))));

			//合作方===signatoryInfo
			$whereSignatoryInfo['where'] = [
				'is_del' => 0,
				'id' => array_filter(array_unique( array_column($itemSignatoryData, 'signatory_id'))),
			];
			$whereSignatoryInfo['columns'] = ['id', 'info_name'];
			$sign_datas = $this->LocalService()->signatoryInfo->fetchAll($whereSignatoryInfo);

			//机构==========signatoryDepartment
			foreach ($itemSignatoryData as $key => $val) {

				if ($sign_datas) {
					foreach ($sign_datas as $ks => $vs) {
						if ($vs['id'] == $itemSignatoryData[$key]['signatory_id']) {
							$itemSignatoryData[$key]['info_name'] = $vs['info_name'];
						}
					}
				}
			}
			//数据结构调用
			if (!empty($item_id)){

				foreach ($itemSignatoryData as $key => $val) {
					$label = '';
					if(!empty($val['number'])) $label = '【'.$val['number'].'】';
					$newItemSignatoryData[$key]['label'] = $label.$val['info_name'];
					$newItemSignatoryData[$key]['number'] = $val['number'];
					$newItemSignatoryData[$key]['value'] = $val['id'];
				}
				return $newItemSignatoryData;
			}
		}


		return $itemSignatoryData;
	}

	public function patientTemplate($data = []) {
		$datas = [
			'randgroup_id' => [
				'type' => 'Select',
				'label' => '研究分层',
				'placeholder' => '请选择研究分层',
				'options' => $this->LocalService()->itemRandgroup->fetchAll([
					'where' => [
						'is_del' => 0,
						'item_id' => $data['item_id']
					],
					'columns' => ['value' => 'id', 'label' => 'group_name']
				]),
				'prop' => 'randgroup_id',
				'rules' => [
					[
						'required' => true,
						'message' => '研究分层不能为空',
						'trigger' => 'blur'
					]
				]
			],
//
			'random_id'=>[
		        'type'=>'Radio',
		        'label'=>'方案名称',
		        'placeholder'=>'方案名称',
		        'prop'=>'random_id',
		        'radios'=>$this->LocalService()->itemRandom->fetchAll([
					'where' => [
						'is_del' => 0,
						'item_id' => $data['item_id']
					],
					'columns' => ['value' => 'id', 'label' => 'random_name']
				]),
		    ],

			'random_number'=>[
				'type'=>'Input',
				'label'=>'随机号码',
				'placeholder'=>'请输入随机号码',
				'prop'=>'random_number',
				'rules'=>[
					[
						'required'=>false,
						'message'=>'随机号码不能为空',
						'trigger'=>'blur'
					]
				]
			],
		];
		return $datas;
	}

	/**
	 * Notes: 获取对应的方案分组 药物分组名称
	 * @param
	 * @return
	 * @author haojinhua
	 * @date 2024-11-20
	 */
	public function fetchDataInfos($where){
		$datas = [];
		$cond = [
			'where'=>$where,
			'columns'=>['id','programme_id','medication_id','randomdetails_name']
		];
		$infoDatas = $this->fetchAll($cond);
		if(!empty($infoDatas)){
			$itemRandom_infos = $this->LocalService()->itemRandom->fetchCol('random_name','is_del = 0');
			$randomName_infos = $this->LocalService()->itemMedication->fetchCol('medication_name','is_del = 0');
			foreach($infoDatas as &$infoData){
				$id = $infoData['id'];
				$programme_id = $infoData['programme_id'];
				$medication_id = $infoData['medication_id'];
				$programme_name = isset($itemRandom_infos[$programme_id]) && !empty($itemRandom_infos[$programme_id]) ? $itemRandom_infos[$programme_id] :'';
				$medication_name = isset($randomName_infos[$medication_id]) && !empty($randomName_infos[$medication_id]) ? $randomName_infos[$medication_id] :'';
				$infoData['random_name']= $programme_name;
				$infoData['medication_name']= $medication_name;
				$datas[$id] = $infoData;
			}
		}
		unset($cond);
		unset($infoDatas);
		unset($itemRandom_infos);
		unset($randomName_infos);
		return $datas;
	}

}
?>
