WaterOptions.php 851 B

1234567891011121314151617181920
  1. <?php
  2. declare(strict_types=1);
  3. namespace plugin\saiadmin\app\service;
  4. use plugin\saiadmin\app\model\water\WaterModel;
  5. use plugin\saiadmin\exception\ApiException;
  6. final class WaterOptions
  7. {
  8. public function listing(array $input): array
  9. {
  10. $type = (string) ($input['type'] ?? 'enterprise');
  11. if (!in_array($type, ['enterprise', 'sku', 'package'], true)) throw new ApiException('选项类型不支持');
  12. $query = WaterModel::forKind($type)->where('status', 1);
  13. $keyword = trim((string) ($input['keyword'] ?? ''));
  14. if ($keyword !== '') $query->matching('name', '%' . mb_substr($keyword, 0, 120) . '%');
  15. $fields = $type === 'package' ? ['id', 'name', 'sku_id', 'buckets', 'price_cents'] : ['id', 'name'];
  16. return $query->select($fields)->orderByDesc('id')->limit(50)->get()->toArray();
  17. }
  18. }