| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace App\Module\Task\Enums;
- /**
- * 任务状态枚举
- * 定义了任务的各种状态
- */
- enum TASK_STATUS: int {
- case NOT_ACCEPTED = 0; // 未接取
- case IN_PROGRESS = 1; // 进行中
- case COMPLETED = 2; // 已完成
- case REWARDED = 3; // 已领取奖励
- case FAILED = 4; // 已失败
- case EXPIRED = 5; // 已过期
-
- /**
- * 获取所有任务状态的选项,用于表单选择
- *
- * @return array
- */
- public static function getOptions(): array
- {
- $options = [];
- foreach (self::cases() as $case) {
- $options[$case->value] = self::getDescription($case);
- }
- return $options;
- }
-
- /**
- * 获取任务状态的描述
- *
- * @param TASK_STATUS $status
- * @return string
- */
- public static function getDescription(self $status): string
- {
- return match($status) {
- self::NOT_ACCEPTED => '未接取',
- self::IN_PROGRESS => '进行中',
- self::COMPLETED => '已完成',
- self::REWARDED => '已领取奖励',
- self::FAILED => '已失败',
- self::EXPIRED => '已过期',
- };
- }
-
- /**
- * 获取任务状态的颜色
- *
- * @param TASK_STATUS $status
- * @return string
- */
- public static function getColor(self $status): string
- {
- return match($status) {
- self::NOT_ACCEPTED => 'gray',
- self::IN_PROGRESS => 'blue',
- self::COMPLETED => 'green',
- self::REWARDED => 'purple',
- self::FAILED => 'red',
- self::EXPIRED => 'orange',
- };
- }
-
- /**
- * 检查任务是否可以接取
- *
- * @param TASK_STATUS $status
- * @return bool
- */
- public static function canAccept(self $status): bool
- {
- return $status === self::NOT_ACCEPTED;
- }
-
- /**
- * 检查任务是否可以放弃
- *
- * @param TASK_STATUS $status
- * @return bool
- */
- public static function canAbandon(self $status): bool
- {
- return $status === self::IN_PROGRESS;
- }
-
- /**
- * 检查任务是否可以完成
- *
- * @param TASK_STATUS $status
- * @return bool
- */
- public static function canComplete(self $status): bool
- {
- return $status === self::IN_PROGRESS;
- }
-
- /**
- * 检查任务是否可以领取奖励
- *
- * @param TASK_STATUS $status
- * @return bool
- */
- public static function canClaimReward(self $status): bool
- {
- return $status === self::COMPLETED;
- }
- }
|