<?php

namespace Application\Service\Extension\Helper;

use Application\Service\Extension\Helper\base\BaseHelper;
use Application\Service\Extension\Laminas;

class CheckTimeHelper extends BaseHelper
{
    public ?int $checkTimeId = null;

    public function getCacheKey(): string
    {
        return self::class;
    }


    protected function _getCheckTimeData()
    {
        if (!$this->getCache()) {
            $data = Laminas::$serviceManager->itemChecktime->fetchOne(['where' => ['id' => $this->checkTimeId]]);
            $this->setCache($data);
        }

        return $this->getCache();
    }

    public function getCheckName(int $checkTimeId)
    {
        if (!$this->checkTimeId) {
            $this->checkTimeId = $checkTimeId;
        }
        return $this->getProperty($this->_getCheckTimeData(), 'check_name', "{$checkTimeId} 找不到检查点");
    }

    protected function setCache($value, string $helperKey = self::HELPER_KEY_BASE)
    {
        self::$cache[$this->getCacheKey()][$this->checkTimeId][$helperKey] = $value;
    }

    protected function getCache(string $helperKey = self::HELPER_KEY_BASE)
    {
        return self::$cache[$this->getCacheKey()][$this->checkTimeId][$helperKey] ?? [];
    }

    /**
     * 当前检查点内是否有指定表单。
     * @param int $formId
     * @param int $checkTimeId
     * @return array|mixed
     */
    public function getHasForm(int $formId, int $checkTimeId)
    {
        $cacheKey = __FUNCTION__ . $formId . $checkTimeId;
        if (!$this->getCache($cacheKey)) {
            $this->setCache((bool)Laminas::$serviceManager->itemCheckcontent->fetchOne([
                'where' => ['form_id' => $formId, 'check_id' => $checkTimeId, 'is_del' => 0]
            ]), $cacheKey);
        }

        return $this->getCache($cacheKey);
    }
}