<?php
declare (strict_types = 1);

namespace app\admin\model\v1;

use app\common\model\SysNavigationItemBaseModel;

/**
 * 导航数据模型
 * @mixin \think\Model
 */
class SysNavigationItemModel extends SysNavigationItemBaseModel
{
    // 关联导航
    public function navigation()
    {
        return $this->belongsTo(SysNavigationModel::class, 'nav_id', 'id');
    }

    // 名称搜索
    public function searchNameAttr($query, $value, $data) 
    {
        if (empty($value)) return;
        $query->where('name', 'like', "%{$value}%");
    }

    // 新增时间搜索
    public function searchCreatedAtAttr($query, $value, $data)
    {
        if (empty($value)) return;
        if (is_array($value)) {
            if (count($value) > 1) {
                $query->whereBetweenTime('created_at', $value[0], $value[1]);
            } else {
                $query->whereTime('created_at', '>=', $value[0]);
            }
        }
    }

    // 导航查询
    public function scopeNavId($query, $value)
    {
        if (empty($value)) return;
        $query->where('nav_id', '=', $value);
    }
}
