ActivityReward.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace App\Http\Controllers\admin;
  3. use App\Constants\HttpStatus;
  4. use App\Http\Controllers\Controller;
  5. use App\Services\ActivityRewardService;
  6. use App\Services\RoleService;
  7. use Illuminate\Http\JsonResponse;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Validation\ValidationException;
  10. use Exception;
  11. use App\Models\ActivityReward as ActivityRewardModel;
  12. class ActivityReward extends Controller
  13. {
  14. public function index(): JsonResponse
  15. {
  16. try {
  17. $params = request()->validate([
  18. 'page' => ['required', 'integer', 'min:1'],
  19. 'limit' => ['required', 'integer', 'min:1'],
  20. 'title' => ['nullable', 'string'],
  21. ]);
  22. $page = request()->input('page', 1);
  23. $limit = request()->input('limit', 10);
  24. $where = ActivityRewardService::getWhere($params);
  25. $query = ActivityRewardModel::where($where);
  26. $count = $query->count();
  27. $list = $query->orderByDesc('id')
  28. ->forPage($page, $limit)->get()->toArray();
  29. $result = ['total' => $count, 'data' => $list];
  30. } catch (ValidationException $e) {
  31. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  32. } catch (Exception $e) {
  33. return $this->error($e->getCode(), $e->getMessage());
  34. }
  35. return $this->success($result);
  36. }
  37. public function store()
  38. {
  39. DB::beginTransaction();
  40. try {
  41. $params = request()->validate([
  42. 'id' => ['nullable', 'integer', 'min:1'],
  43. 'title' => ['required', 'string', 'min:1', 'max:140'],
  44. 'gift_amount' => ['required', 'numeric', 'min:0.01', 'regex:/^\d+(\.\d{1,2})?$/'],
  45. 'flow_amount' => ['required', 'numeric', 'min:0', 'regex:/^\d+(\.\d{1,2})?$/'],
  46. 'participant_count' => ['required', 'integer', 'min:0'],
  47. 'activity_total' => ['required', 'integer', 'min:-1'],
  48. 'maximum_participation' => ['required', 'integer', 'min:0'],
  49. 'start_time' => ['required', 'date', 'date_format:Y-m-d'],
  50. 'end_time' => ['required', 'date', 'date_format:Y-m-d'],
  51. ]);
  52. ActivityRewardService::submit($params);
  53. DB::commit();
  54. } catch (ValidationException $e) {
  55. DB::rollBack();
  56. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  57. } catch (Exception $e) {
  58. DB::rollBack();
  59. return $this->error($e->getCode(), $e->getMessage());
  60. }
  61. return $this->success();
  62. }
  63. public function destroy()
  64. {
  65. try {
  66. request()->validate([
  67. 'id' => ['required', 'integer', 'min:1'],
  68. ]);
  69. $id = request()->input('id');
  70. ActivityRewardService::deleteAll(['id' => $id]);
  71. DB::commit();
  72. } catch (ValidationException $e) {
  73. DB::rollBack();
  74. return $this->error(HttpStatus::CUSTOM_ERROR, $e->validator->errors()->first());
  75. } catch (Exception $e) {
  76. DB::rollBack();
  77. return $this->error($e->getCode(), $e->getMessage());
  78. }
  79. return $this->success([], '删除成功');
  80. }
  81. }