| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace App\Services;
- use App\Models\Banner;
- use Illuminate\Support\Facades\DB;
- class BannerVisibilityService
- {
- /** Positive numeric links are activity IDs; empty/zero and other links are unbound. */
- public static function activityId($link): ?string
- {
- $link = trim((string) $link);
- if ($link === '' || !ctype_digit($link)) {
- return null;
- }
- $id = ltrim($link, '0');
- return $id === '' ? null : $id;
- }
- /** Compute display state without overwriting the operator's manual setting. */
- public static function decorate(iterable $banners, ?int $now = null): array
- {
- $now = $now ?? time();
- $items = [];
- $activityIds = [];
- foreach ($banners as $banner) {
- $item = $banner instanceof Banner ? $banner->toArray() : $banner;
- $item['status'] = (int) $item['status'];
- $item['activity_id'] = self::activityId($item['link'] ?? null);
- if ($item['activity_id'] !== null) {
- $activityIds[$item['activity_id']] = $item['activity_id'];
- }
- $items[] = $item;
- }
- // Read raw timestamps; ActivityReward's date accessors discard the time of day.
- $activities = empty($activityIds) ? collect() : DB::table('activity_rewards')
- ->whereIn('id', array_values($activityIds))
- ->get(['id', 'status', 'start_time', 'end_time'])->keyBy('id');
- foreach ($items as &$item) {
- $activity = $item['activity_id'] === null ? null : $activities->get($item['activity_id']);
- $reason = null;
- if ($item['status'] !== 1) {
- $reason = 'manual';
- } elseif ($item['activity_id'] !== null) {
- if ($activity === null) {
- $reason = 'activity_missing';
- } elseif ((int) $activity->status !== 1) {
- $reason = 'activity_offline';
- } elseif ((int) $activity->start_time > $now) {
- $reason = 'activity_not_started';
- } elseif ((int) $activity->end_time <= $now) {
- $reason = 'activity_ended';
- }
- }
- $item['effective_status'] = $reason === null ? 1 : 0;
- $item['offline_reason'] = $reason;
- }
- unset($item);
- return $items;
- }
- }
|