$label) $types[] = compact('value', 'label'); return ['types' => $types, 'languages' => [['value' => 'zh', 'label' => '中文'], ['value' => 'en', 'label' => '英文'], ['value' => 'vi', 'label' => '越南文']], '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 { $data = array_replace($row->toArray(), ['translations' => PromotionTranslations::all($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')]); unset($data['sub_title']); if (in_array((int) $row->type, [PromotionRules::LOSS, PromotionRules::WHEEL], true)) unset($data['image']); return $data; } public function save(array $input): array { unset($input['sub_title']); if (isset($input['translations']) && is_array($input['translations'])) { foreach ($input['translations'] as &$text) if (is_array($text)) unset($text['sub_title']); unset($text); } if (in_array($input['type'] ?? null, [7, 9, '7', '9'], true)) unset($input['image']); $money = ['nullable', 'numeric', 'min:0', 'regex:/^\d{1,12}(\.\d{1,2})?$/']; $textRules = ['translations' => ['required', 'array:zh,en,vi']]; $messages = ['translations.required' => '请完整填写中文、英文、越南文的标题和正文']; foreach (['zh' => '中文', 'en' => '英文', 'vi' => '越南文'] as $lang => $label) { $textRules['translations.' . $lang] = ['required', 'array:title,content']; $messages['translations.' . $lang . '.required'] = '请完整填写' . $label . '文本'; foreach (['title' => ['标题', 140], 'content' => ['正文', 100000]] as $field => [$name, $max]) { $key = 'translations.' . $lang . '.' . $field; $textRules[$key] = ['required', 'string', 'max:' . $max]; $messages[$key . '.required'] = $label . $name . '不能为空'; } } $data = Validator::make($input, $textRules + ['id' => ['sometimes', 'integer', 'min:1'], 'type' => ['required', 'integer', 'in:7,8,9'], 'status' => ['required', 'integer', 'in:0,1'], 'start_time' => ['required', 'date'], 'end_time' => ['required', 'date', 'after:start_time'], 'sort' => ['sometimes', 'integer', 'between:0,4294967295'], 'params' => ['sometimes', 'array'], '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]], $messages)->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']); $texts = $this->completeTexts($data['translations']); $values = array_replace($values, $texts['zh'], ['translations' => $texts, 'lang' => 'zh']); $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); } private function completeTexts(array $texts): array { foreach (['zh' => '中文', 'en' => '英文', 'vi' => '越南文'] as $lang => $label) { foreach (['title' => '标题', 'content' => '正文'] as $field => $name) { $value = html_entity_decode(strip_tags($texts[$lang][$field]), ENT_QUOTES | ENT_HTML5, 'UTF-8'); if (preg_replace('/[\s\p{Z}\x{200B}\x{FEFF}]+/u', '', $value) === '') { throw ValidationException::withMessages(['translations.' . $lang . '.' . $field => $label . $name . '不能为空']); } if ($field !== 'content') $texts[$lang][$field] = trim($texts[$lang][$field]); } } return $texts; } 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' => '独立活动不存在']); if ($status === 1) $this->completeTexts(PromotionTranslations::all($row->toArray())); $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()]; } }