| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace App\Services;
- use App\Models\PromotionActivity;
- use App\Support\PromotionRules;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Schema;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Validation\ValidationException;
- class PromotionAdminService
- {
- public function options(): array
- {
- $types = []; foreach (PromotionRules::types() as $value => $label) $types[] = compact('value', 'label');
- return ['types' => $types, 'create_types' => [['value' => PromotionRules::FIRST_DEPOSIT, 'label' => PromotionRules::types()[PromotionRules::FIRST_DEPOSIT]]],
- 'defaults' => [7 => PromotionRules::defaults(7), 8 => PromotionRules::defaults(8), 9 => PromotionRules::defaults(9)],
- 'prizes' => PromotionRules::PRIZES, 'tasks' => PromotionRules::TASKS];
- }
- public function listing(array $input): array
- {
- $input = Validator::make($input, ['page' => ['sometimes', 'integer', 'min:1'], 'limit' => ['sometimes', 'integer', 'between:1,100'],
- 'type' => ['sometimes', 'integer', 'in:7,8,9'], 'status' => ['sometimes', 'integer', 'in:0,1'], 'title' => ['sometimes', 'string', 'max:140']])->validate();
- $q = PromotionActivity::query();
- foreach (['type', 'status'] as $key) if (isset($input[$key])) $q->where($key, $input[$key]);
- if (!empty($input['title'])) $q->where('title', 'like', '%' . $input['title'] . '%');
- 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()];
- }
- public function detail(int $id): array
- {
- $row = PromotionActivity::find($id);
- if (!$row) throw ValidationException::withMessages(['id' => '独立活动不存在']);
- return $this->format($row);
- }
- private function format(PromotionActivity $row): array
- {
- return array_replace($row->toArray(), ['params' => json_decode($row->params, true),
- '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),
- 'first_published_at' => DB::table('promotion_campaigns')->where('activity_id', $row->id)->value('first_published_at')]);
- }
- public function save(array $input): array
- {
- $money = ['nullable', 'numeric', 'min:0', 'regex:/^\d{1,12}(\.\d{1,2})?$/'];
- $data = Validator::make($input, ['id' => ['sometimes', 'integer', 'min:1'], 'title' => ['required', 'string', 'max:140'],
- 'sub_title' => ['sometimes', 'nullable', 'string', 'max:200'], 'type' => ['required', 'integer', 'in:7,8,9'], 'status' => ['required', 'integer', 'in:0,1'],
- 'start_time' => ['required', 'date'], 'end_time' => ['required', 'date', 'after:start_time'], 'lang' => ['sometimes', 'string', 'max:10'],
- 'sort' => ['sometimes', 'integer', 'between:0,4294967295'], 'params' => ['sometimes', 'array'], 'content' => ['sometimes', 'nullable', 'string', 'max:100000'],
- 'image' => ['sometimes', 'nullable', 'string', 'max:2048'], 'publish_platforms' => ['sometimes', 'array'], 'publish_platforms.*' => ['string', 'distinct', 'in:PC,H5,APP'],
- 'register_time_limited' => ['sometimes', 'boolean'], 'register_start_time' => ['nullable', 'required_if:register_time_limited,1', 'date'],
- 'register_end_time' => ['nullable', 'required_if:register_time_limited,1', 'date', 'after_or_equal:register_start_time'],
- 'register_platforms' => ['sometimes', 'array'], 'register_platforms.*' => ['string', 'distinct', 'in:PC,H5,APP'], 'deposit_limited' => ['sometimes', 'boolean'],
- 'deposit_min' => [...$money, 'required_if:deposit_limited,1'], 'deposit_max' => [...$money, 'required_if:deposit_limited,1', 'gte:deposit_min'],
- 'show_ineligible' => ['sometimes', 'boolean'], 'reward_quota' => ['sometimes', 'nullable', 'integer', 'between:0,2147483647'], 'budget' => ['sometimes', ...$money]])->validate();
- if (!isset($data['id']) && in_array((int) $data['type'], [PromotionRules::LOSS, PromotionRules::WHEEL], true)) {
- throw ValidationException::withMessages(['id' => '净亏和转盘活动不支持新增,请编辑现有活动']);
- }
- return DB::transaction(function () use ($data) {
- $row = isset($data['id']) ? PromotionActivity::where('id', $data['id'])->lockForUpdate()->first() : new PromotionActivity();
- if (!$row) throw ValidationException::withMessages(['id' => '独立活动不存在']);
- if ($row->exists && (int)$row->type !== (int)$data['type']) throw ValidationException::withMessages(['type' => '创建后不能变更活动类型']);
- $before = $row->exists ? $row->toArray() : [];
- $params = array_replace($row->params ? json_decode($row->params, true) : [], $data['params'] ?? []);
- $values = $data; unset($values['id']);
- $values['params'] = json_encode(PromotionRules::normalize((int)$data['type'], $params));
- $values['start_time'] = strtotime($data['start_time']); $values['end_time'] = strtotime($data['end_time']);
- if (!$row->exists) $values += ['lang' => 'zh', 'sort' => 0, 'publish_platforms' => [], 'register_platforms' => []];
- $row->fill($values); $row->save(); PromotionService::publish($row);
- if (Schema::hasTable('operation_audits')) OperationAuditService::record('promotion', $row->id, $before ? 'update' : 'create', $before, $row->toArray());
- return $this->format($row);
- }, 3);
- }
- public function status(int $id, int $status): array
- {
- return DB::transaction(function () use ($id, $status) {
- $row = PromotionActivity::where('id', $id)->lockForUpdate()->first();
- if (!$row) throw ValidationException::withMessages(['id' => '独立活动不存在']);
- $before = $row->toArray(); $row->status = $status; $row->save(); PromotionService::publish($row);
- if (Schema::hasTable('operation_audits')) OperationAuditService::record('promotion', $id, 'status', $before, $row->toArray());
- return $this->format($row);
- }, 3);
- }
- public function records(array $input): array
- {
- $input = Validator::make($input, ['activity_id' => ['required', 'integer', 'min:1'], 'user_id' => ['sometimes', 'integer', 'min:1'],
- 'kind' => ['required', 'in:accounts,rounds,tasks,spins,rewards'], 'page' => ['sometimes', 'integer', 'min:1'], 'limit' => ['sometimes', 'integer', 'between:1,100']])->validate();
- $q = DB::table('promotion_accounts as a')->where('a.activity_id', $input['activity_id']);
- if (isset($input['user_id'])) $q->where('a.user_id', $input['user_id']);
- if ($input['kind'] === 'accounts') $q->select('a.*')->orderByDesc('a.id');
- else {
- $table = match ($input['kind']) { 'rounds' => 'promotion_rounds', 'tasks' => 'promotion_task_grants', 'spins' => 'promotion_spins', 'rewards' => 'promotion_rewards' };
- $q->join($table . ' as r', 'r.account_id', '=', 'a.id')->select(['r.*', 'a.user_id', 'a.activity_id'])->orderByDesc('r.id');
- }
- return ['total' => $q->count(), 'data' => $q->forPage($input['page'] ?? 1, $input['limit'] ?? 20)->get()];
- }
- }
|