ClaimRewardAction.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace App\Module\Activity\AdminControllers\Actions;
  3. use App\Module\Activity\Enums\PARTICIPATION_STATUS;
  4. use App\Module\Activity\Enums\REWARD_STATUS;
  5. use App\Module\Activity\Models\ActivityParticipation;
  6. use Dcat\Admin\Grid\RowAction;
  7. use Illuminate\Http\Request;
  8. /**
  9. * 标记活动奖励为已领取操作
  10. */
  11. class ClaimRewardAction extends RowAction
  12. {
  13. /**
  14. * 操作按钮标题
  15. *
  16. * @var string
  17. */
  18. protected $title = '<i class="fa fa-gift"></i> 标记已领取';
  19. /**
  20. * 判断是否允许显示此操作
  21. *
  22. * @return bool
  23. */
  24. public function allowed()
  25. {
  26. return $this->row->completion_status === PARTICIPATION_STATUS::COMPLETED
  27. && $this->row->reward_status === REWARD_STATUS::NOT_CLAIMED;
  28. }
  29. /**
  30. * 处理请求
  31. *
  32. * @param Request $request
  33. * @return mixed
  34. */
  35. public function handle(Request $request)
  36. {
  37. try {
  38. $id = $this->getKey();
  39. $participation = ActivityParticipation::findOrFail($id);
  40. if ($participation->completion_status !== PARTICIPATION_STATUS::COMPLETED) {
  41. return $this->response()->error('只有已完成的参与记录才能领取奖励');
  42. }
  43. if ($participation->reward_status !== REWARD_STATUS::NOT_CLAIMED) {
  44. return $this->response()->error('只有未领取的奖励才能标记为已领取');
  45. }
  46. $participation->reward_status = REWARD_STATUS::CLAIMED;
  47. $participation->reward_time = now();
  48. $participation->save();
  49. // 触发奖励领取事件
  50. event(new \App\Module\Activity\Events\ActivityRewardClaimedEvent(
  51. $participation->user_id,
  52. $participation->activity_id
  53. ));
  54. return $this->response()
  55. ->success("参与记录 [{$id}] 的奖励已标记为已领取")
  56. ->refresh();
  57. } catch (\Exception $e) {
  58. return $this->response()
  59. ->error('操作失败: ' . $e->getMessage());
  60. }
  61. }
  62. /**
  63. * 确认信息
  64. *
  65. * @return array|string|void
  66. */
  67. public function confirm()
  68. {
  69. return ['确定要标记此奖励为已领取吗?', '标记已领取后,用户将无法再次领取奖励'];
  70. }
  71. }