<?php

namespace App\Services;

use App\Traits\ServiceAppAop;
use App\Supplier;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

class SupplierService
{
    use ServiceAppAop;
    protected $modelClass=Supplier::class;

    public function screenSupplierIds(): array
    {
        $user=Auth::user();
        if (($user && $user->isSuperAdmin()) ||Gate::allows('供应商-可见全部')) return Supplier::query()->get()->map(function ($supplier){return $supplier->id;})->toArray();
        $suppliers=Supplier::query()
            ->whereHas('user',function($query)use($user){
                /** @var Builder $query */
                return $query->where('id',$user->id);
            })->get();

        if (isset($suppliers)) return $suppliers->map(function ($supplier){
            return $supplier->id;
        })->toArray();
    }

}
