PromotionAdminService.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Services;
  3. use App\Models\PromotionActivity;
  4. use App\Support\PromotionRules;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Schema;
  7. use Illuminate\Support\Facades\Validator;
  8. use Illuminate\Validation\ValidationException;
  9. class PromotionAdminService
  10. {
  11. public function options(): array
  12. {
  13. $types = []; foreach (PromotionRules::types() as $value => $label) $types[] = compact('value', 'label');
  14. return ['types' => $types, 'create_types' => [['value' => PromotionRules::FIRST_DEPOSIT, 'label' => PromotionRules::types()[PromotionRules::FIRST_DEPOSIT]]],
  15. 'defaults' => [7 => PromotionRules::defaults(7), 8 => PromotionRules::defaults(8), 9 => PromotionRules::defaults(9)],
  16. 'prizes' => PromotionRules::PRIZES, 'tasks' => PromotionRules::TASKS];
  17. }
  18. public function listing(array $input): array
  19. {
  20. $input = Validator::make($input, ['page' => ['sometimes', 'integer', 'min:1'], 'limit' => ['sometimes', 'integer', 'between:1,100'],
  21. 'type' => ['sometimes', 'integer', 'in:7,8,9'], 'status' => ['sometimes', 'integer', 'in:0,1'], 'title' => ['sometimes', 'string', 'max:140']])->validate();
  22. $q = PromotionActivity::query();
  23. foreach (['type', 'status'] as $key) if (isset($input[$key])) $q->where($key, $input[$key]);
  24. if (!empty($input['title'])) $q->where('title', 'like', '%' . $input['title'] . '%');
  25. return ['total' => $q->count(), 'data' => $q->orderByDesc('sort')->orderByDesc('id')->forPage($input['page'] ?? 1, $input['limit'] ?? 20)->get()->map(fn ($r) => $this->format($r))->all()];
  26. }
  27. public function detail(int $id): array
  28. {
  29. $row = PromotionActivity::find($id);
  30. if (!$row) throw ValidationException::withMessages(['id' => '独立活动不存在']);
  31. return $this->format($row);
  32. }
  33. private function format(PromotionActivity $row): array
  34. {
  35. return array_replace($row->toArray(), ['params' => json_decode($row->params, true),
  36. 'start_time' => date('Y-m-d H:i:s', (int)$row->start_time), 'end_time' => date('Y-m-d H:i:s', (int)$row->end_time),
  37. 'first_published_at' => DB::table('promotion_campaigns')->where('activity_id', $row->id)->value('first_published_at')]);
  38. }
  39. public function save(array $input): array
  40. {
  41. $money = ['nullable', 'numeric', 'min:0', 'regex:/^\d{1,12}(\.\d{1,2})?$/'];
  42. $data = Validator::make($input, ['id' => ['sometimes', 'integer', 'min:1'], 'title' => ['required', 'string', 'max:140'],
  43. 'sub_title' => ['sometimes', 'nullable', 'string', 'max:200'], 'type' => ['required', 'integer', 'in:7,8,9'], 'status' => ['required', 'integer', 'in:0,1'],
  44. 'start_time' => ['required', 'date'], 'end_time' => ['required', 'date', 'after:start_time'], 'lang' => ['sometimes', 'string', 'max:10'],
  45. 'sort' => ['sometimes', 'integer', 'between:0,4294967295'], 'params' => ['sometimes', 'array'], 'content' => ['sometimes', 'nullable', 'string', 'max:100000'],
  46. 'image' => ['sometimes', 'nullable', 'string', 'max:2048'], 'publish_platforms' => ['sometimes', 'array'], 'publish_platforms.*' => ['string', 'distinct', 'in:PC,H5,APP'],
  47. 'register_time_limited' => ['sometimes', 'boolean'], 'register_start_time' => ['nullable', 'required_if:register_time_limited,1', 'date'],
  48. 'register_end_time' => ['nullable', 'required_if:register_time_limited,1', 'date', 'after_or_equal:register_start_time'],
  49. 'register_platforms' => ['sometimes', 'array'], 'register_platforms.*' => ['string', 'distinct', 'in:PC,H5,APP'], 'deposit_limited' => ['sometimes', 'boolean'],
  50. 'deposit_min' => [...$money, 'required_if:deposit_limited,1'], 'deposit_max' => [...$money, 'required_if:deposit_limited,1', 'gte:deposit_min'],
  51. 'show_ineligible' => ['sometimes', 'boolean'], 'reward_quota' => ['sometimes', 'nullable', 'integer', 'between:0,2147483647'], 'budget' => ['sometimes', ...$money]])->validate();
  52. if (!isset($data['id']) && in_array((int) $data['type'], [PromotionRules::LOSS, PromotionRules::WHEEL], true)) {
  53. throw ValidationException::withMessages(['id' => '净亏和转盘活动不支持新增,请编辑现有活动']);
  54. }
  55. return DB::transaction(function () use ($data) {
  56. $row = isset($data['id']) ? PromotionActivity::where('id', $data['id'])->lockForUpdate()->first() : new PromotionActivity();
  57. if (!$row) throw ValidationException::withMessages(['id' => '独立活动不存在']);
  58. if ($row->exists && (int)$row->type !== (int)$data['type']) throw ValidationException::withMessages(['type' => '创建后不能变更活动类型']);
  59. $before = $row->exists ? $row->toArray() : [];
  60. $params = array_replace($row->params ? json_decode($row->params, true) : [], $data['params'] ?? []);
  61. $values = $data; unset($values['id']);
  62. $values['params'] = json_encode(PromotionRules::normalize((int)$data['type'], $params));
  63. $values['start_time'] = strtotime($data['start_time']); $values['end_time'] = strtotime($data['end_time']);
  64. if (!$row->exists) $values += ['lang' => 'zh', 'sort' => 0, 'publish_platforms' => [], 'register_platforms' => []];
  65. $row->fill($values); $row->save(); PromotionService::publish($row);
  66. if (Schema::hasTable('operation_audits')) OperationAuditService::record('promotion', $row->id, $before ? 'update' : 'create', $before, $row->toArray());
  67. return $this->format($row);
  68. }, 3);
  69. }
  70. public function status(int $id, int $status): array
  71. {
  72. return DB::transaction(function () use ($id, $status) {
  73. $row = PromotionActivity::where('id', $id)->lockForUpdate()->first();
  74. if (!$row) throw ValidationException::withMessages(['id' => '独立活动不存在']);
  75. $before = $row->toArray(); $row->status = $status; $row->save(); PromotionService::publish($row);
  76. if (Schema::hasTable('operation_audits')) OperationAuditService::record('promotion', $id, 'status', $before, $row->toArray());
  77. return $this->format($row);
  78. }, 3);
  79. }
  80. public function records(array $input): array
  81. {
  82. $input = Validator::make($input, ['activity_id' => ['required', 'integer', 'min:1'], 'user_id' => ['sometimes', 'integer', 'min:1'],
  83. 'kind' => ['required', 'in:accounts,rounds,tasks,spins,rewards'], 'page' => ['sometimes', 'integer', 'min:1'], 'limit' => ['sometimes', 'integer', 'between:1,100']])->validate();
  84. $q = DB::table('promotion_accounts as a')->where('a.activity_id', $input['activity_id']);
  85. if (isset($input['user_id'])) $q->where('a.user_id', $input['user_id']);
  86. if ($input['kind'] === 'accounts') $q->select('a.*')->orderByDesc('a.id');
  87. else {
  88. $table = match ($input['kind']) { 'rounds' => 'promotion_rounds', 'tasks' => 'promotion_task_grants', 'spins' => 'promotion_spins', 'rewards' => 'promotion_rewards' };
  89. $q->join($table . ' as r', 'r.account_id', '=', 'a.id')->select(['r.*', 'a.user_id', 'a.activity_id'])->orderByDesc('r.id');
  90. }
  91. return ['total' => $q->count(), 'data' => $q->forPage($input['page'] ?? 1, $input['limit'] ?? 20)->get()];
  92. }
  93. }