Category.php 2.5 KB
<?php

namespace app\api\model;

use think\Db;
use think\Model;

class Category extends Model
{
    // 表名
    protected $name = 'category';

    public function getImageAttr($value)
    {
        if($value) $value = cdnurl($value);
        return $value;
    }

    public function selectData($where, $limit=null,$lang)
    {
        $where['status'] = ['eq', '1'];

        if ($lang == 'ch') $field = 'id,name,image';
        else $field = 'id,en_name name,image';

        $data = $this->where($where)->field($field)->order('weigh desc')->limit($limit)->select();
        return $data;
    }

    public function getExtend($where,$lang,$whereOr=null)
    {
        $goodsModel = new Goods();
        $ids = $goodsModel->alias('g')
//            ->join('fa_category cate1', 'g.category_one_id=cate1.id')
//            ->join('fa_category cate2', 'g.category_two_id=cate2.id')
//            ->join('fa_category cate3', 'g.category_three_id=cate3.id')
            ->where($where)
            ->whereOr($whereOr)
            ->field('country_id,brand_id,level_id,part_id,category2_id')
            ->select();

        $countryids = [];
        $brandids = [];
        $levelids = [];
        $partids = [];
        $category2ids = [];
        foreach ($ids as $k=>$v){
            $countryids[$k] = $v['country_id'];
            $brandids[$k] = $v['brand_id'];
            $levelids[$k] = $v['level_id'];
            $partids[$k] = $v['part_id'];
            $category2ids[$k] = $v['category2_id'];
        }

        if ($lang == 'ch') $field = 'id,ch_name name';
        else $field = 'id,en_name name';

        $country = Db::name('country')->where(['id'=>['in',array_unique($countryids)]])->field($field)->select();
        $brand = Db::name('brand')->where(['id'=>['in',array_unique($brandids)]])->field($field)->select();
        $level = Db::name('level')->where(['id'=>['in',array_unique($levelids)]])->field($field)->select();
        $part = Db::name('part')->where(['id'=>['in',array_unique($partids)]])->field($field)->select();
        $category2 = Db::name('category2')->where(['id'=>['in',array_unique($category2ids)]])->field($field)->select();
        $data = [];
        if ($country) $data[] = ['title' => '国家','list'=>$country];
        if ($brand) $data[] = ['title' => '品牌','list'=>$brand];
        if ($level) $data[] = ['title' => '等级','list'=>$level];
        if ($part) $data[] = ['title' => '部位','list'=>$part];
        if ($category2) $data[] = ['title' => '品类','list'=>$category2];

        return $data;
    }
}