<?php
declare (strict_types = 1);

namespace app\index\model;

use app\common\model\ArticleBaseModel;
use think\model\concern\SoftDelete;

/**
 * 文章模型
 * @mixin \think\Model
 */
class ArticleModel extends ArticleBaseModel
{
    // 启用软删除
    use SoftDelete;
    // 软删除字段
    protected $deleteTime = 'deleted_at';

    // 语言范围查询
    public function scopeLanguage($query, $language)
    {
        $query->where('language_id', '=', $language);
    }

    // 首页推荐状态范围查询
    public function scopeRecommend($query, bool $stat = true)
    {
        $query->where('recommend', '=', (int)$stat);
    }

    // 文章分类范围查询
    public function scopeCategory($query, $category)
    {
        if (is_null($category)) return;
        if (is_array($category)) {
            $query->where('category_id', 'in', $category);
            return;
        }
        $query->where('category_id', '=', $category);
    }

    // 文章标题搜索器
    public function searchTitleAttr($query, $value)
    {
        if (is_null($value)) return;
        $query->where('title', 'like', "%$value%");
    }
}
