| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace App\Module\ThirdParty\Enums;
- /**
- * 第三方服务状态枚举
- */
- enum SERVICE_STATUS: string
- {
- case ACTIVE = 'ACTIVE'; // 激活状态
- case INACTIVE = 'INACTIVE'; // 未激活状态
- case DISABLED = 'DISABLED'; // 禁用状态
- case MAINTENANCE = 'MAINTENANCE'; // 维护状态
- case ERROR = 'ERROR'; // 错误状态
- case TESTING = 'TESTING'; // 测试状态
- case EXPIRED = 'EXPIRED'; // 已过期状态
- /**
- * 获取状态的中文描述
- *
- * @return string
- */
- public function getLabel(): string
- {
- return match ($this) {
- self::ACTIVE => '激活',
- self::INACTIVE => '未激活',
- self::DISABLED => '禁用',
- self::MAINTENANCE => '维护中',
- self::ERROR => '错误',
- self::TESTING => '测试中',
- self::EXPIRED => '已过期',
- };
- }
- /**
- * 获取状态的详细描述
- *
- * @return string
- */
- public function getDescription(): string
- {
- return match ($this) {
- self::ACTIVE => '服务正常运行,可以正常调用API',
- self::INACTIVE => '服务未激活,暂时无法使用',
- self::DISABLED => '服务已被禁用,不允许调用API',
- self::MAINTENANCE => '服务正在维护中,暂时不可用',
- self::ERROR => '服务出现错误,需要检查配置',
- self::TESTING => '服务处于测试状态,仅限测试使用',
- self::EXPIRED => '服务已过期,需要续费或重新配置',
- };
- }
- /**
- * 获取状态的颜色
- *
- * @return string
- */
- public function getColor(): string
- {
- return match ($this) {
- self::ACTIVE => 'success',
- self::INACTIVE => 'secondary',
- self::DISABLED => 'danger',
- self::MAINTENANCE => 'warning',
- self::ERROR => 'danger',
- self::TESTING => 'info',
- self::EXPIRED => 'dark',
- };
- }
- /**
- * 获取状态的图标
- *
- * @return string
- */
- public function getIcon(): string
- {
- return match ($this) {
- self::ACTIVE => 'fa-check-circle',
- self::INACTIVE => 'fa-pause-circle',
- self::DISABLED => 'fa-ban',
- self::MAINTENANCE => 'fa-tools',
- self::ERROR => 'fa-exclamation-triangle',
- self::TESTING => 'fa-flask',
- self::EXPIRED => 'fa-clock',
- };
- }
- /**
- * 检查状态是否可用
- *
- * @return bool
- */
- public function isAvailable(): bool
- {
- return in_array($this, [self::ACTIVE, self::TESTING]);
- }
- /**
- * 检查状态是否需要关注
- *
- * @return bool
- */
- public function needsAttention(): bool
- {
- return in_array($this, [self::ERROR, self::EXPIRED, self::MAINTENANCE]);
- }
- /**
- * 检查状态是否可以调用API
- *
- * @return bool
- */
- public function canCallApi(): bool
- {
- return $this === self::ACTIVE;
- }
- /**
- * 检查状态是否可以测试
- *
- * @return bool
- */
- public function canTest(): bool
- {
- return in_array($this, [self::ACTIVE, self::TESTING]);
- }
- /**
- * 获取优先级(数字越小优先级越高)
- *
- * @return int
- */
- public function getPriority(): int
- {
- return match ($this) {
- self::ACTIVE => 1,
- self::TESTING => 2,
- self::INACTIVE => 3,
- self::MAINTENANCE => 4,
- self::DISABLED => 5,
- self::ERROR => 6,
- self::EXPIRED => 7,
- };
- }
- /**
- * 获取状态的严重程度
- *
- * @return string
- */
- public function getSeverity(): string
- {
- return match ($this) {
- self::ACTIVE => 'INFO',
- self::INACTIVE => 'WARNING',
- self::DISABLED => 'ERROR',
- self::MAINTENANCE => 'WARNING',
- self::ERROR => 'CRITICAL',
- self::TESTING => 'INFO',
- self::EXPIRED => 'ERROR',
- };
- }
- /**
- * 获取可以转换到的状态
- *
- * @return array
- */
- public function getAllowedTransitions(): array
- {
- return match ($this) {
- self::ACTIVE => [self::INACTIVE, self::DISABLED, self::MAINTENANCE, self::TESTING],
- self::INACTIVE => [self::ACTIVE, self::DISABLED, self::TESTING],
- self::DISABLED => [self::INACTIVE, self::TESTING],
- self::MAINTENANCE => [self::ACTIVE, self::INACTIVE, self::DISABLED],
- self::ERROR => [self::ACTIVE, self::INACTIVE, self::DISABLED, self::TESTING],
- self::TESTING => [self::ACTIVE, self::INACTIVE, self::DISABLED],
- self::EXPIRED => [self::INACTIVE, self::TESTING],
- };
- }
- /**
- * 检查是否可以转换到指定状态
- *
- * @param SERVICE_STATUS $targetStatus
- * @return bool
- */
- public function canTransitionTo(SERVICE_STATUS $targetStatus): bool
- {
- return in_array($targetStatus, $this->getAllowedTransitions());
- }
- /**
- * 获取所有状态的选项数组
- *
- * @return array
- */
- public static function getOptions(): array
- {
- $options = [];
- foreach (self::cases() as $case) {
- $options[$case->value] = $case->getLabel();
- }
- return $options;
- }
- /**
- * 获取可用状态
- *
- * @return array
- */
- public static function getAvailableStatuses(): array
- {
- return array_filter(self::cases(), function ($case) {
- return $case->isAvailable();
- });
- }
- /**
- * 获取需要关注的状态
- *
- * @return array
- */
- public static function getAttentionStatuses(): array
- {
- return array_filter(self::cases(), function ($case) {
- return $case->needsAttention();
- });
- }
- /**
- * 按优先级排序状态
- *
- * @return array
- */
- public static function getSortedByPriority(): array
- {
- $statuses = self::cases();
- usort($statuses, function ($a, $b) {
- return $a->getPriority() <=> $b->getPriority();
- });
- return $statuses;
- }
- }
|