<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 店铺f分类
 * Class MerchantShopCategory
 * @package app\admin\model
 */
class MerchantShopCategory extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'merchant_shop_category';

    public $timestamps = false;

    /**
     * Notes:获取店铺分类列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/11/03
     */
    public static function getShopCategoryList(int $page, int $limit,$keywords)
    {
        $list = static::select('*')
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('category_name', 'like', '%' . $keywords . '%');
            })
            ->where('category_del',0)
            ->orderBy('category_sort','DESC')
            ->forPage($page, $limit)
            ->get();

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


    /**
     * Notes:根据店铺获取分类列表
     * @param int $category_shop_id
     * @return array
     * User: ZQ
     * Date: 2022/11/08
     */
    public static function getCategoryList($category_shop_id)
    {
        $list = static::select(['category_id','category_name'])
            ->when($category_shop_id != '', function ($query) use ($category_shop_id){
                $query->where('category_shop_id', $category_shop_id);
            })
            ->where('category_del',0)
            ->orderBy('category_sort','DESC')
            ->get();
        return $list;
    }


    public function getCategoryCreateTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function Shop(){
        return $this->belongsTo(MerchantShop::class,'category_shop_id','shop_id');
    }
}
