<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 智能产品分类模型
 * Class Users
 * @package app\admin\model
 */
class IntelligenceCategory extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'intelligence_category';

    public $timestamps = false;

    /**
     * Notes:获取智能产品分类列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getCategoryList(int $page, int $limit, string $keywords, int $pid)
    {
        if (!empty($keywords)){
            $list = static::select('*')
                ->where(['category_del'=>0])
                ->when($keywords != '', function ($query) use ($keywords){
                    $query->where('category_name', 'like', '%' . $keywords . '%');
                })
                ->orderBy('category_create_time','DESC')
                ->forPage($page, $limit)
                ->get();
        }else{
            $list = static::select('*')
                ->where(['category_del'=>0, 'category_pid'=>$pid])
                ->when($keywords != '', function ($query) use ($keywords){
                    $query->where('category_name', 'like', '%' . $keywords . '%');
                })
                ->orderBy('category_create_time','DESC')
                ->forPage($page, $limit)
                ->get();
        }

        $count = static::where(['category_del'=>0])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('category_name',  'like', '%' . $keywords . '%');
            })
            ->count();
        return [$list, $count];
    }

    /**
     * Notes:获取分类列表
     * @return array
     * User: ZQ
     * Date: 2022/9/19`
     */
    public static function categoryAll($pid)
    {
        $list = static::select(['category_id as value','category_name as label'])
            ->where(['category_del'=>0,'category_pid'=>$pid])
            ->orderBy('category_create_time','DESC')
            ->get();
        return $list;
    }

}
