<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 店铺
 * Class MerchantShop
 * @package app\admin\model
 */
class MerchantShop extends Model
{
    const DEL_NO  = 0;
    const DEL_YES = 1;
    const SHOP_DEL = [
        self::DEL_NO  => '未删除',
        self::DEL_YES => '已删除'
    ];

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'merchant_shop';

    public $timestamps = false;

    /**
     * Notes:获取产品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/9/23
     */
    public static function getShopList(int $page, int $limit,$keywords)
    {
        $list = static::select('*')
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('shop_name', 'like', '%' . $keywords . '%');
            })
            ->where('shop_del',0)
            ->orderBy('shop_create_time','DESC')
            ->forPage($page, $limit)
            ->get();

        $count = static::when($keywords != '', function ($query) use ($keywords){
            $query->where('shop_name', 'like', '%' . $keywords . '%');
        }) ->where('shop_del',0)->count();
        return [$list, $count];
    }
    /**
     * Notes:获取健康店铺列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/11/08
     */
    public static function getHealthyShopList()
    {
        $list = static::select('*')
            ->where(['shop_del'=>0,'shop_type'=>1])
            ->orderBy('shop_create_time','DESC')
            ->get();
        return [$list];
    }
    public function getShopCreateTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
    //获取器修改产品标签
    public function getShopLabelAttribute($value)
    {
        return explode('|', $value);
    }

    /**
     * Notes:根据分类获取店铺列表
     * @param int $category_id
     * @return array
     * User: QJF
     * Date: 2022/9/23
     */
    public static function getCategoryShop($category_id)
    {
        $list = static::select(['shop_id','shop_name'])
            ->when($category_id != '', function ($query) use ($category_id){
                $query->where('shop_category_id', $category_id);
            })
            ->where('shop_del',0)
            ->orderBy('shop_create_time','DESC')
            ->get();
        return $list;
    }

    /**
     * Notes:店铺子集列表
     * @param int $category_id
     * @return array
     * User: QJF
     * Date: 2022/9/23
     */
    public static function getCategorysShop()
    {
        $list = static::select(['shop_id','shop_name'])
            ->with('children')
            ->where('shop_del',0)
            ->orderBy('shop_create_time','DESC')
            ->get()
            ->toArray();
        return $list;
    }

    /**
     * Notes:子集模型关联
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     * User: ZQ
     * Date: 2022/11/10
     */
    public function children()
    {
        return $this->hasMany(MerchantShopCategory::class, 'category_shop_id', 'shop_id')
            ->where('category_del',0);
    }

}
