<?php

namespace App\Domains\Permission\Repositories;

use App\Core\Repository;
use App\Models\Permission\Permission as Model;

class PermissionRepository extends Repository
{
    /**
     * 是否关闭缓存
     *
     * @var boolean
     */
    protected $cacheSkip = false;

    /**
     * @var array
     */
    protected $fieldSearchable = [
        'id'    => '=',
        'name'  => 'like',
        'title' => 'like',
    ];

    public function model()
    {
        return Model::class;
    }

    /**
     * 查询条件
     *
     * @return void
     */
    public function withConditions(array $conditions = [])
    {
        if (isset($conditions['ids'])) {
            $this->model = $this->model->whereIn('id', $conditions['ids']);
        }

        if (isset($conditions['role_ids'])) {
            $this->model = $this->model->whereHas('roles', function ($query) use ($conditions) {
                $query->where('id', $conditions['role_ids']);
            });
        }

        $this->model = $this->model->orderBy('displayorder', 'desc')->orderBy('id', 'asc');

        return $this;
    }
}
