<?php
declare (strict_types = 1);

namespace app\admin\model\v1;

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

/**
 * 产品分类推荐模型
 * @mixin \think\Model
 */
class ProductCategoryRecommendModel extends ProductCategoryRecommendBaseModel
{
    // 启用软件删除
    use SoftDelete;
    // 软件删除时间字段
    protected $deleteTime = 'deleted_at';

    // 关联语言
    public function language()
    {
        return $this->belongsTo(\app\index\model\LanguageModel::class, 'language_id', 'id');
    }

    // 关联产品分类
    public function category()
    {
        return $this->belongsTo(\app\index\model\ProductCategoryModel::class, 'category_id', 'id');
    }

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

    // 关键词搜索
    public function searchKeywordsAttr($query, string|null $keywords)
    {
        if (is_null($keywords)) {
            return;
        }
        $query->where($this->getTable() . '.title', 'like', "%{$keywords}%")
              ->whereOr($this->getTable() . '.desc', 'like', "%{$keywords}%");
    }
}
