<?php

namespace Application\Service\Extension\Helper\components;

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

class UserHelper extends BaseHelper
{
    protected ?int $userId = null;

    protected const HELPER_KEY_ROLE_NAME_STRING = 'ROLE_NAME_STRING';

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

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

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

    public function getRoleNameString(int $userId = null, int $itemSignId = null, int $itemId = null): string
    {
        $this->userId = $userId;
        if (!$this->getCache(self::HELPER_KEY_ROLE_NAME_STRING)) {
            $itemId = Laminas::$serviceManager->itemInfo->getOneFieldVal('fid',[
                'is_del' => 0,
                'id' => $itemId ?? 0,
            ]);
            $userId = Laminas::$serviceManager->signatoryUser->findsignatoryUser($userId);
            if (!$userId) {
                return '';
            }
            $roleId = ArrayHelper::getColumn(Laminas::$serviceManager->roleSignatoryRelation->changeUser($itemId, $userId, $itemSignId), 'role_id');

            $this->setCache(implode('|', ArrayHelper::getColumn(Laminas::$serviceManager->realRole->fetchAll([
                'where' => [
                    'id' => $roleId
                ],
                'columns' => ['role_name']
            ]) ?: [], 'role_name')), self::HELPER_KEY_ROLE_NAME_STRING);
        }

        return $this->getCache(self::HELPER_KEY_ROLE_NAME_STRING);
    }

    public function getRealName(int $userId)
    {
        if ($userId) {
            $this->userId = $userId;
        }

        if (!$this->getCache()) {
            $this->setCache(Laminas::$serviceManager->adminUser->fetchOne(['where' => ['id' => $this->userId]]));
        }

        return $this->getCache()['realname'];
    }

    public function getEDCRoleNameString(int $userId, int $itemId, int $signId)
    {
        $paramsStr = implode('', func_get_args());
        if (!isset(self::$cache[__FUNCTION__][$paramsStr])) {
            $container = Laminas::$serviceManager;
            $itemId = $container->itemInfo->getOneFieldVal('fid',[
                'is_del' => 0,
                'id' => $itemId ?? 0,
            ]);
            $userId = $container->signatoryUser->findsignatoryUser($userId);
            $roleId = ArrayHelper::getColumn($container->roleSignatoryRelation->changeUser($itemId, $userId, $signId), 'role_id');
            self::$cache[__FUNCTION__][$paramsStr] = implode('|', ArrayHelper::getColumn(Laminas::$serviceManager->realRole->fetchAll([
                'where' => [
                    'id' => $roleId
                ],
                'columns' => ['role_name']
            ]) ?: [], 'role_name'));
        }

        return self::$cache[__FUNCTION__][$paramsStr];
    }

}