<?php

namespace Application\Service\Extension\Helper\components;

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

class ItemHelper extends BaseHelper
{
    protected ?int $itemId = null;

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

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

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

    private function _getItemCacheData()
    {
        if (!$this->getCache()) {
            $this->setCache(Laminas::$serviceManager->itemInfo->fetchOne(['where' => ['id' => $this->itemId]]));
        }

        return $this->getCache();
    }

    /**
     * 是否开启edc   0、否；1、是
     * @param int|null $itemId
     * @throws \Exception
     */
    public function getIsEdc(int $itemId = null)
    {
        if ($itemId) {
            $this->itemId = $itemId;
        }

        return $this->getProperty($this->_getItemCacheData(), 'is_edc');
    }

    /**
     * 所属项目ID
     * @param int|null $itemId
     * @throws \Exception
     */
    public function getFid(int $itemId = null)
    {
        if ($itemId) {
            $this->itemId = $itemId;
        }
$val = $this->_getItemCacheData();
        return $this->getProperty($this->_getItemCacheData(), 'fid');
    }

    public function getName(int $itemId = null)
    {
        if ($itemId) {
            $this->itemId = $itemId;
        }
        $val = $this->_getItemCacheData();
        return $this->getProperty($val, 'name');
    }

    public function getNumber(int $itemId = null)
    {
        if ($itemId) {
            $this->itemId = $itemId;
        }
        $val = $this->_getItemCacheData();
        return $this->getProperty($val, 'number');
    }
}