| 123456789101112131415161718192021222324252627282930 |
- <?php
- declare(strict_types=1);
- namespace plugin\saiadmin\app\service\content;
- use Illuminate\Database\Eloquent\Builder;
- /** One status rule for management lists and audience previews, using Shanghai wall time. */
- final class ContentPolicy
- {
- public const LABELS = ['active' => '投放中', 'scheduled' => '待上线', 'expired' => '已结束', 'disabled' => '已停用'];
- public static function status(array $row, string $now): string
- {
- if ((int)$row['status'] !== 1) return 'disabled';
- if ($row['starts_at'] && $row['starts_at'] > $now) return 'scheduled';
- if ($row['ends_at'] && $row['ends_at'] < $now) return 'expired';
- return 'active';
- }
- public static function filter(Builder $query, string $status, string $now): Builder
- {
- if ($status === '') return $query;
- if ($status === 'disabled') return $query->where('status', '<>', 1);
- $query->where('status', 1);
- if ($status === 'scheduled') return $query->where('starts_at', '>', $now);
- $query->where(fn ($q) => $q->whereNull('starts_at')->orWhere('starts_at', '<=', $now));
- if ($status === 'expired') return $query->where('ends_at', '<', $now);
- return $query->where(fn ($q) => $q->whereNull('ends_at')->orWhere('ends_at', '>=', $now));
- }
- }
|