<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 商品模型
 * Class Users
 * @package app\admin\model
 */
class LifeGoods extends Model
{
    const ROLE_STATUS = 1;

    const IS_DEL_YES = 1;
    const IS_DEL_NO  = 0;
    const GOODS_IS_SHOW = [
        self::IS_DEL_YES => '已删除',
        self::IS_DEL_NO  => '未删除'
    ];
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'life_market_goods';

    public $timestamps = false;

    /**
     * Notes:获取商品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getGoodsList(int $page, int $limit, string $keywords)
    {
        $list = static::select(['life_market_goods.*','merchant_shop.shop_name','category.category_name'])
            ->where(['goods_is_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('goods_name', 'like', '%' . $keywords . '%');
            })
            ->leftJoin('merchant_shop','shop_id','=','life_market_goods.goods_shop_id')
            ->leftJoin('category','category_id','=','life_market_goods.goods_category_id')
            ->orderBy('goods_create_time','DESC')
            ->forPage($page, $limit)
            ->get();
        $count = static::where(['goods_is_del'=>static::IS_DEL_NO])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('goods_name', 'like', '%' . $keywords . '%');
            })
            ->count();
        return [$list, $count];
    }

    /**
     * Notes:获取商品列表
     * @return array
     * User: ZQ
     * Date: 2022/9/19`
     */
    public static function getGoodsAll()
    {
        $list = static::select(['goods_id','goods_name'])
            ->where(['goods_status'=>static::ROLE_STATUS])
            ->orderBy('goods_create_time','DESC')
            ->get();
        return $list;
    }

    /**
     * Notes:获取商品名称
     * @param string $account
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     * User: ZQ
     * Date: 2022/9/19
     */
    public static function goodsMation($admin_goodss)
    {
        $where = [];
        $where['goods_is_del'] = static::IS_DEL_NO;
        $mation = static::where($where)
            ->when($admin_goodss != '', function ($query) use ($admin_goodss){
                $query->whereIn('goods_id', $admin_goodss);
            })
            ->get('goods_name');
        return $mation;
    }

}
