<?php

namespace Application\Models\logic\patientForm;

use Application\Models\logic\BaseLogic;
use Application\Service\DB\Dictionary\FormGroup;
use Application\Service\DB\Item\PatientFormContentUpdatedLog;
use Application\Service\Extension\Helper\ArrayHelper;
use Application\Service\Extension\HelperV2\Helper;
use Application\Service\Extension\Laminas;
use Application\Service\Extension\Params\Params;

class PatientFormLogic extends BaseLogic
{
    /**
     * 受试者表单 失活/激活
     * 默认是激活的
     */
    public function inactive()
    {
        $patientFormId = Helper::app()->patient->getPatientForm(Params::app()->must()->patientId, Params::app()->formId, Params::app()->checkTimeId);

        // 是否已经失活了
        $isInactive = array_sum(ArrayHelper::getColumn($patientFormId, 'is_inactive')) > 0;

        $this->getLogger()->event = $isInactive ? PatientFormContentUpdatedLog::EVENT_PATIENT_FORM_ACTIVE : PatientFormContentUpdatedLog::EVENT_PATIENT_FORM_INACTIVE;

        $this->getLogger()->data = json_encode(Params::app()->getValuesToArray());

        Laminas::$serviceManager->patientForm->update(['is_inactive' => intval(!$isInactive)], ['id' => ArrayHelper::getColumn($patientFormId, 'id')]);

        //异步处理访视进度情况信息
        $content_where = [
            'is_del' => 0,
            'patient_id' => Params::app()->must()->patientId,
            'form_id' => Params::app()->formId,
        ];
        if(!in_array(Helper::app()->form->getGroupId(Params::app()->formId),[FormGroup::THROUGH_PROCESS,FormGroup::AE,FormGroup::SAE,FormGroup::TEST_SUMMARY])){
            $content_where['checktime_id'] = Params::app()->checkTimeId;
        }
        $form_content_arr = Laminas::$serviceManager->patientFormContent->fetchCol('id',$content_where);
        $form_content_arr = !empty($form_content_arr) ? array_values($form_content_arr) : [];
        //更新明细为失活状态
        if(!empty($form_content_arr)){
            Laminas::$serviceManager->patientFormContent->update(['is_inactive' => intval(!$isInactive)], ['id' => $form_content_arr]);
        }
        unset($content_where);
        Laminas::$serviceManager->swTaskClient->send([
            'svName' => 'projectPatientworkbatch',
            'methodName' => 'multipleUpdateData',
            'params' => [
                'item_id' => 0,
                'itemsig_id' => 0,
                'patient_id' => Params::app()->must()->patientId,
                'checktime_id' => Params::app()->must()->checktime_id,
                'form_id' => Params::app()->must()->formId,
                'operate_type'=>11,
                'content_id'=>!empty($form_content_arr) ? $form_content_arr : [],
                'user_id' => Laminas::$serviceManager->identity->getId()
            ]
        ]);
        unset($form_content_arr);

    }
}