<?php
namespace Application\Service\DB\Admin;

use Application\Service\Extension\Identity\Identity;
use Interop\Container\ContainerInterface;
use Laminas\Cache\Exception\ExceptionInterface;
use Laminas\Json\Json;

class Userrolerelation extends \Application\Service\DB\AbstractDb
{
    public function __construct($dbAdapter, ContainerInterface $container)
    {
        $this->tableName = 'yikeen_admin_userrolerelation';
        parent::__construct($dbAdapter, $container);
    }

    public function getAdminUserRoleData($whereData=[],$type = 0,$field=''){
        $data = [];
        switch ($type){
            case 1:
                $data = $this->LocalService()->userRoleRelation->fetchAll($whereData);
                break;
            case 2:
                $data = $this->LocalService()->userRoleRelation->fetchOne($whereData);
                break;
            case 3:
                $data = $this->LocalService()->userRoleRelation->fetchCol($field,$whereData);
                break;
            default:
                $data['data'] = $this->LocalService()->userRoleRelation->fetchAll($whereData);
                $data['count'] = $this->LocalService()->userRoleRelation->getCount($whereData['where']);
                break;
        }
        return $data ;
    }

    /**
     * @note 缓存脱敏字段管理员
     * @return void
     * @throws ExceptionInterface
     */
    public function syncMaskingAdminCache()
    {
        $maskingAdminRoleId = $this->LocalService()->adminRole->getOneFieldVal('id', [
            'role_code' => Role::MASKING_ADMIN_CODE,
            'is_del' => 0,
            'is_module_role' => 0
        ]);
        $maskingAdminUserIdList = [];
        if ($maskingAdminRoleId) {
            $maskingAdminUserIdList = $this->fetchCol('user_id', [
                'role_id' => $maskingAdminRoleId,
                'status' => 0,
            ]);
        }
		
		$redisConfig = $this->LocalService()->config['caches']['Redis']['adapter']['options'];
	
        $this->LocalService()->redisExtend->setNamespace($redisConfig['namespace'])->setItem(Identity::MASKING_ADMIN_USER_CACHE_KEY, Json::encode($maskingAdminUserIdList));
    }
}