<?php

namespace Application\Command;

use Application\Service\DB\Dictionary\FormGroup;
use Application\Service\Extension\Laminas;
use Application\Service\Extension\Queue\jobs\SyncPatientFormJob;
use Application\Service\Extension\Queue\QueueApplication;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Ark 同步脚本
 * http://42.192.180.144/arkedc/login.jsp
 * username:TEST01 pwd:12345
 *
 */
class ArkCommand extends BasicCommand
{
    private ?int $itemId = null;

    private ?int $itemSignId = null;

    private ?int $patientId = null;

    private ?int $formId = null;

    /**
     * 共同页表单类型
     */
    const SHARE_PAGE_FORM = [FormGroup::SAE, FormGroup::AE, FormGroup::THROUGH_PROCESS, FormGroup::TEST_SUMMARY];

    public function execute(InputInterface $input, OutputInterface $output)
    {
        // 要同步的项目
        $syncItem = [
            // 项目id
            101 => [ // 中心id
                272
            ]
        ];

        foreach ($syncItem as $itemId => $signArray) {
            $this->itemId = $itemId;

            foreach ($signArray as $signId) {
                $this->itemSignId = $signId;

                $patients = $this->getPatients();
                foreach ($patients as $patient) {
                    $this->patientId = $patient;
                    // 当前受试者所有检查点
                    $checkTimes = Laminas::$serviceManager->itemPatientchecktime->fetchAll([
                        'columns' => ['checktime_id'],
                        'where' => [
                            'patient_id' => $this->patientId
                        ]
                    ]);
                    // 共同页
                    $allCheckTime[] = [
                        'checktime_id' => 99
                    ];

                    foreach ($checkTimes as $checkTime) {
                        if ($checkTime['checktime_id'] == 99) { // 是共同页
                            $allFormId = Laminas::$serviceManager->itemForm->fetchCol('id', [
                                'item_id' => $this->itemId,
                                'group_id' => self::SHARE_PAGE_FORM,
                                'is_del' => 0
                            ]);
                        } else {
                            $allFormId = Laminas::$serviceManager->patientForm->fetchCol('form_id', ['patient_id' => $this->patientId, 'is_del' => 0, 'checktime_id' => $checkTime['checktime_id']]);
                        }

                        $allFormId = array_unique(array_values($allFormId));

                        foreach ($allFormId as $formId) {
                            $this->formId = $formId;

                            $form = Laminas::$serviceManager->itemForm->fetchOne([
                                'where' => ['id' => $this->formId]
                            ]);

                            if ($checkTime['checktime_id'] != 99) {
                                if (in_array($form['group_id'], self::SHARE_PAGE_FORM)) {
                                    continue;
                                }
                            }
//                            $model = new SyncPatientFormJob();
//                            $model->patientId = intval($this->patientId);
//                            $model->itemSignId = $query['itemsig_id'];
//                            $model->itemId = intval($query['item_id']);
//                            $model->formId = $formId; //747 717;
//                            $model->checkTimeId = intval($checkTime['checktime_id']); //747 717;
//                            echo "受试者: [{$query['id']}]. 检查点: [{$checkTime['checktime_id']}]. 表单 [{$formId}]. " . PHP_EOL;
                            $this->push([
                                'itemId' => $itemId,
                                'itemSignId' => $signId,
                                'patientId' => $this->patientId,
                                'checkTimeId' => $checkTime['checktime_id'],
                                'formId' => $this->formId,
                            ]);
                        }
                    }
                }
            }
        }

        parent::execute($input, $output); // TODO: Change the autogenerated stub
    }


    /**
     * 获取所有受试者id
     * @return array
     */
    private function getPatients(): array
    {
        $query = Laminas::$serviceManager->patient->fetchCol('id', [
            'item_id' => $this->itemId,
            'itemsig_id' => $this->itemSignId,
            'is_del' => 0
        ]);

        return array_values($query) ?: [];
    }

    /**
     * 投递任务
     */
    private function push($params = [])
    {
        $this->LocalService()->swTaskClient->send([
            'svName' => 'itemExport',
            'methodName' => 'execute',
            'params' => $params
        ]);
    }
}