ActivityReward.php 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Http\Controllers\api;
  3. use App\Constants\HttpStatus;
  4. use App\Models\ActivityReward as ActivityRewardModel;
  5. use App\Models\ActivityUser;
  6. use App\Services\ActivityRewardService;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Validation\ValidationException;
  9. use Exception;
  10. class ActivityReward extends BaseController
  11. {
  12. public function participate(): JsonResponse
  13. {
  14. try {
  15. request()->validate([
  16. 'member_id' => ['required', 'integer', 'exists:users,member_id'],
  17. 'activity_id' => ['required', 'integer'],
  18. ]);
  19. $memberId = request()->input('member_id');
  20. if (ActivityUser::where('member_id', $memberId)->where('status', 0)->exists()) {
  21. throw new Exception('完成活动才可以参与新活动', HttpStatus::CUSTOM_ERROR);
  22. }
  23. $activityId = request()->input('activity_id');
  24. if (ActivityUser::where('member_id', $memberId)->where('activity_id', $activityId)->exists()) {
  25. throw new Exception('已参与过此活动,每个活动仅可参加一次', HttpStatus::CUSTOM_ERROR);
  26. }
  27. $time = time();
  28. $activity = ActivityRewardService::findOne([
  29. 'start_time' => ['<=', $time],
  30. 'end_time' => ['>=', $time],
  31. 'status' => ActivityRewardModel::STATUS_UP,
  32. 'id' => $activityId
  33. ]);
  34. if (!$activity) {
  35. throw new Exception('活动不存在', HttpStatus::CUSTOM_ERROR);
  36. }
  37. ActivityUser::create([
  38. 'activity_id' => $activityId,
  39. 'title' => $activity->title,
  40. 'sub_title' => $activity->sub_title,
  41. 'start_time' => $activity->start_time,
  42. 'end_time' => $activity->end_time,
  43. 'detail_image' => $activity->detail_image,
  44. 'part_in_time' => $time,
  45. 'member_id' => $memberId,
  46. 'status' => 0
  47. ]);
  48. } catch (ValidationException $e) {
  49. return $this->error($e->validator->errors()->first());
  50. } catch (Exception $e) {
  51. return $this->error($e->getMessage());
  52. }
  53. return $this->success($activity);
  54. }
  55. public function index(): JsonResponse
  56. {
  57. try {
  58. request()->validate(['member_id' => ['nullable', 'integer']]);
  59. $memberId = request()->input('member_id');
  60. $time = time();
  61. $query = ActivityRewardModel::where(ActivityRewardService::getWhere([
  62. 'start_time' => ['<=', $time],
  63. 'end_time' => ['>=', $time],
  64. 'status' => ActivityRewardModel::STATUS_UP
  65. ]));
  66. if (!empty($memberId)) {
  67. $query->with(['activityUser' => function ($query1) use ($memberId) {
  68. $query1->where('member_id', $memberId);
  69. }]);
  70. }
  71. $list = $query->orderByDesc('id')->get();
  72. $list->transform(function ($activity) use ($memberId) {
  73. if (!empty($memberId)) {
  74. $activity->is_claimed = $activity->activityUser->isNotEmpty();
  75. } else {
  76. $activity->is_claimed = false;
  77. }
  78. unset($activity->activityUser);
  79. return $activity;
  80. })->toArray();
  81. } catch (ValidationException $e) {
  82. return $this->error($e->validator->errors()->first());
  83. } catch (Exception $e) {
  84. return $this->error($e->getMessage());
  85. }
  86. return $this->success($list);
  87. }
  88. }