<?php
declare (strict_types = 1);

namespace app\openapi\model;

use app\common\model\ProductCategoryBaseModel;
use think\facade\Db;

/**
 * 产品分类模型
 * @mixin \think\Model
 */
class ProductCategoryModel extends ProductCategoryBaseModel
{
    // 所属语言
    public function scopeLanguage($query, $language)
    {
        $query->whereExists(function($subquery) use($language) {
            $lang_model = new LanguageModel;
            $subquery->model($lang_model)
                ->name($lang_model->getName())
                ->where('id', '=', Db::Raw($this->getTable() . '.language_id'))
                ->where('code', '=', $language);
        });
    }

    // 所属上级范围查询
    public function scopeParent($query, $parent_id)
    {
        if (is_null($parent_id)) return;
        $query->where('pid', '=', $parent_id);
    }
}
