ConfigHelper.php 7.23 KB
<?php

namespace App\Helpers;

use App\Models\Activity;
use App\Models\SystemConfig;
use Arr;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection as SupportCollection;
use Str;

class ConfigHelper
{
    public const DEMO_TAG_KEY = 'CtGly-jsLjAu5yJvLrN7L';

    public const DEMO_USER_KEY = 'n3HCbruqL_DemfX9MHj-K';

    public const LISTEN_USER_KEY = '-BIGBAR9rPOMwCrw4hIuQ';

    public const SING_USER_KEY = 'Jy-zgPbcSXSGnvHIx8moq';

    public const SINGER_TAG_KEY = 'auth_singer_tag';

    public const BUSINESS_SINGER_LIMIT = 'svMC9TNFz2Acx8ccPdAIH';

    public const INFO_SCORE_LIMIT_KEY = 'iymo-wZcV2F0UWFUKJR5m';

    public const INFO_SCORE_TEXT_KEY = 'YmSNBXwMB6cUo-zRT26s-';

    public const INFO_SCORE_CONTENT_KEY = 'TeZnnaB-Pfwyz8IADs0SM';


    /**
     * @param string $identifier
     * @param string $column
     * @return ?string
     */
    public static function getContent(string $identifier, string $column = 'content'): ?string
    {
        return SystemConfig::withoutGlobalScope('show')->where('status', 1)->where('identifier', $identifier)->value($column);
    }

    /**
     * @param string|array $identifier
     * @param string       $column
     * @return \Illuminate\Support\Collection
     */
    public static function getList(string|array $identifier, string $column = 'content'): SupportCollection
    {
        return SystemConfig::withoutGlobalScope('show')->where('status', 1)->whereIn('identifier', Arr::wrap($identifier))->pluck($column, 'identifier');
    }

    /**
     * @param string|array $identifier
     * @param string       $column
     * @return \Illuminate\Support\Collection
     */
    public static function getChildrenList(string|array $identifier, string $column = 'content'): SupportCollection
    {
        return SystemConfig::withoutGlobalScope('show')->parentKey($identifier)->where('status', 1)->pluck($column, 'identifier');
    }

    /**
     * 获取歌手标签Ids
     * @return array
     */
    public static function getSingerTagIds(): array
    {
        return array_filter(explode(',', self::getContent(self::SINGER_TAG_KEY)));
    }

    /**
     * 获取Demo标签Ids
     * @return array
     */
    public static function getDemoTagIds(): array
    {
        return array_filter(explode(',', self::getContent(self::DEMO_TAG_KEY)));
    }

    /**
     * 获取Demo白名单用户Ids
     * @return array
     */
    public static function getDemoUserIds(): array
    {
        return array_filter(explode(',', self::getContent(self::DEMO_USER_KEY)));
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public static function getDemoList(): SupportCollection
    {
        return self::getList([self::DEMO_TAG_KEY, self::DEMO_USER_KEY])?->map(fn($item) => array_filter(explode(',', $item)));
    }

    /**
     * 获取试听权限白名单用户Ids
     * @return array
     */
    public static function getListenUserIds(): array
    {
        return array_filter(explode(',', self::getContent(self::LISTEN_USER_KEY)));
    }

    /**
     * 获取试听&试唱权限白名单用户Ids
     * @return array
     */
    public static function getSingUserIds(): array
    {
        return array_filter(explode(',', self::getContent(self::SING_USER_KEY)));
    }

    /**
     * 获取试听与试唱白名单Ids
     * @return \Illuminate\Support\Collection
     */
    public static function getListenAndSingUserList(): SupportCollection
    {
        return self::getList([self::LISTEN_USER_KEY, self::SING_USER_KEY])?->map(fn($item) => array_filter(explode(',', $item)));

    }

    /**
     * @return int
     */
    public static function getProjectLimitDay(): int
    {
        return intval(self::getContent('i9V0wD_FitcGXR3HxUK5C')) ?? 7;
    }

    /**
     * @return int
     */
    public static function getBusinessSingerLimit(): int
    {
        return intval(self::getContent(self::BUSINESS_SINGER_LIMIT)) ?? 3;
    }

    /**
     * @return array
     */
    public static function getTelescopeIpList(): array
    {
        return array_filter(explode(',', self::getContent('telescope_ip')));
    }

    /**
     * @param array|int  $tagIds
     * @param array|null $singerTagIds
     * @return bool
     */
    public static function hasSingerTag(array|int $tagIds, array $singerTagIds = NULL): bool
    {
        return filled(array_intersect(Arr::wrap($tagIds), $singerTagIds ?? self::getSingerTagIds()));
    }

    /**
     * @param array|int  $tagIds
     * @param array|NULL $demoTagIds
     * @return bool
     */
    public static function hasDemoTag(array|int $tagIds, array $demoTagIds = NULL): bool
    {
        return filled(array_intersect(Arr::wrap($tagIds), $demoTagIds ?? self::getDemoTagIds()));
    }

    /**
     * @return \Illuminate\Support\Collection
     */
    public static function getInfoScore(): SupportCollection
    {
        return self::getList([self::INFO_SCORE_LIMIT_KEY, self::INFO_SCORE_TEXT_KEY])->map(fn($item, $key) => match ($key) {
            self::INFO_SCORE_LIMIT_KEY => (int)$item,
            self::INFO_SCORE_TEXT_KEY => Str::isJson($item) ? json_decode($item, false, 512, JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE) : [],
            default => $item,
        })->put(self::INFO_SCORE_CONTENT_KEY, self::getChildrenList(self::INFO_SCORE_CONTENT_KEY));
    }

    /**
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public static function getActivityDictionary(): EloquentCollection
    {
        return SystemConfig::parentKey(['activity_mark', 'activity_lang', 'activity_speed', 'activity_sex'])->where('status', 1)->get(['name', 'identifier', 'content']);
    }

    /**
     * @param \App\Models\Activity                          $activity
     * @param \Illuminate\Database\Eloquent\Collection|null $dictionary
     * @return \App\Models\Activity
     */
    public static function formatActivityDictionary(Activity $activity, EloquentCollection $dictionary = NULL): Activity
    {
        $dictionary = $dictionary ?? self::getActivityDictionary();
        $activity->loadMissing('linkArranger:id,nick_name');
        $activity->setAttribute('sex', $dictionary->where('identifier', $activity->getAttribute('sex'))->value('name', ''));
        $activity->setAttribute('speed', $dictionary->where('identifier', $activity->getAttribute('speed'))->value('name', ''));
        $activity->setAttribute('mark', $dictionary->where('identifier', $activity->getAttribute('mark'))->value('content', ''));
        $activity->setAttribute('lang', $dictionary->whereIn('identifier', $activity->getAttribute('lang'))->pluck('name'));
        $activity->setAttribute('arranger', ['in_side' => $activity->getAttribute('linkArranger'), 'out_side' => data_get($activity->getAttribute('expand'), 'arranger.supplement', [])]);

        return $activity;
    }

    /**
     * @param \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection $collection
     * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Support\Collection
     */
    public static function formatManyActivityDictionary(EloquentCollection|SupportCollection $collection): EloquentCollection|SupportCollection
    {
        $dictionary = self::getActivityDictionary();
        return $collection->each(fn($item) => self::formatActivityDictionary($item, $dictionary));
    }
}