ContentPolicy.php 1.3 KB

123456789101112131415161718192021222324252627282930
  1. <?php
  2. declare(strict_types=1);
  3. namespace plugin\saiadmin\app\service\content;
  4. use Illuminate\Database\Eloquent\Builder;
  5. /** One status rule for management lists and audience previews, using Shanghai wall time. */
  6. final class ContentPolicy
  7. {
  8. public const LABELS = ['active' => '投放中', 'scheduled' => '待上线', 'expired' => '已结束', 'disabled' => '已停用'];
  9. public static function status(array $row, string $now): string
  10. {
  11. if ((int)$row['status'] !== 1) return 'disabled';
  12. if ($row['starts_at'] && $row['starts_at'] > $now) return 'scheduled';
  13. if ($row['ends_at'] && $row['ends_at'] < $now) return 'expired';
  14. return 'active';
  15. }
  16. public static function filter(Builder $query, string $status, string $now): Builder
  17. {
  18. if ($status === '') return $query;
  19. if ($status === 'disabled') return $query->where('status', '<>', 1);
  20. $query->where('status', 1);
  21. if ($status === 'scheduled') return $query->where('starts_at', '>', $now);
  22. $query->where(fn ($q) => $q->whereNull('starts_at')->orWhere('starts_at', '<=', $now));
  23. if ($status === 'expired') return $query->where('ends_at', '<', $now);
  24. return $query->where(fn ($q) => $q->whereNull('ends_at')->orWhere('ends_at', '>=', $now));
  25. }
  26. }