TASK_STATUS.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace App\Module\Task\Enums;
  3. /**
  4. * 任务状态枚举
  5. * 定义了任务的各种状态
  6. */
  7. enum TASK_STATUS: int {
  8. case NOT_ACCEPTED = 0; // 未接取
  9. case IN_PROGRESS = 1; // 进行中
  10. case COMPLETED = 2; // 已完成
  11. case REWARDED = 3; // 已领取奖励
  12. case FAILED = 4; // 已失败
  13. case EXPIRED = 5; // 已过期
  14. /**
  15. * 获取所有任务状态的选项,用于表单选择
  16. *
  17. * @return array
  18. */
  19. public static function getOptions(): array
  20. {
  21. $options = [];
  22. foreach (self::cases() as $case) {
  23. $options[$case->value] = self::getDescription($case);
  24. }
  25. return $options;
  26. }
  27. /**
  28. * 获取任务状态的描述
  29. *
  30. * @param TASK_STATUS $status
  31. * @return string
  32. */
  33. public static function getDescription(self $status): string
  34. {
  35. return match($status) {
  36. self::NOT_ACCEPTED => '未接取',
  37. self::IN_PROGRESS => '进行中',
  38. self::COMPLETED => '已完成',
  39. self::REWARDED => '已领取奖励',
  40. self::FAILED => '已失败',
  41. self::EXPIRED => '已过期',
  42. };
  43. }
  44. /**
  45. * 获取任务状态的颜色
  46. *
  47. * @param TASK_STATUS $status
  48. * @return string
  49. */
  50. public static function getColor(self $status): string
  51. {
  52. return match($status) {
  53. self::NOT_ACCEPTED => 'gray',
  54. self::IN_PROGRESS => 'blue',
  55. self::COMPLETED => 'green',
  56. self::REWARDED => 'purple',
  57. self::FAILED => 'red',
  58. self::EXPIRED => 'orange',
  59. };
  60. }
  61. /**
  62. * 检查任务是否可以接取
  63. *
  64. * @param TASK_STATUS $status
  65. * @return bool
  66. */
  67. public static function canAccept(self $status): bool
  68. {
  69. return $status === self::NOT_ACCEPTED;
  70. }
  71. /**
  72. * 检查任务是否可以放弃
  73. *
  74. * @param TASK_STATUS $status
  75. * @return bool
  76. */
  77. public static function canAbandon(self $status): bool
  78. {
  79. return $status === self::IN_PROGRESS;
  80. }
  81. /**
  82. * 检查任务是否可以完成
  83. *
  84. * @param TASK_STATUS $status
  85. * @return bool
  86. */
  87. public static function canComplete(self $status): bool
  88. {
  89. return $status === self::IN_PROGRESS;
  90. }
  91. /**
  92. * 检查任务是否可以领取奖励
  93. *
  94. * @param TASK_STATUS $status
  95. * @return bool
  96. */
  97. public static function canClaimReward(self $status): bool
  98. {
  99. return $status === self::COMPLETED;
  100. }
  101. }