<?php

namespace Application\Service\Extension\Middleware\components;

use Application\Form\item\patient\PatientFormModel;
use Application\Service\Extension\Laminas;
use Application\Service\Extension\Middleware\BaseMiddlewareComponent;

/**
 * 自动更新CM表单受试者药物信息
 */
class HandleCmFormPatientDrug extends BaseMiddlewareComponent
{
    /** @var PatientFormModel $owner */
    public $owner;
    public int $operate_type;
    public function __construct(array $params = [])
    {
        parent::__construct($params);

        $this->owner = &$params['owner'];
    }

    function execute()
    {
        //判断该表单是否为CM表单
        $is_cm_form = Laminas::$serviceManager->itemForm->getCount(['id' =>$this->owner->formId,'is_del' => 0,'type' => 1,'var_name' => 'CM']);
        if(!empty($is_cm_form)){
            //获取该Cm表单对应的药物ID
            $cm_form_drug = Laminas::$serviceManager->patientFormContent->getOneFieldVal('drug_id',['id'=>$this->owner->contentPrimaryKey]);
            if(!empty($cm_form_drug)){
                Laminas::$serviceManager->swTaskClient->send([
                    'svName' => 'collectPatientdrug',
                    'methodName' => 'savePatientdrugData',
                    'params' => [
                        'item_id' => $this->owner->itemId,
                        'itemsig_id' => $this->owner->itemsigId,
                        'patient_id' => $this->owner->patientId,
                        'drug_id' => [$cm_form_drug],
                        'user_id' => Laminas::$serviceManager->identity->getId()
                    ]
                ]);
            }
            unset($cm_form_drug);
        }
        unset($is_cm_form);
    }
}