<?php

namespace app\admin\model;

use support\Db;
use support\Model;

/**
 * 角色模型
 * Class Users
 * @package app\admin\model
 */
class PackageGoods extends Model
{
    const GOODS_PUT_SHELVES = '1';
    const GOODS_LOWER_SHELF = '2';
    const GOODS_STATUS = [
        self::GOODS_PUT_SHELVES => '上架',
        self::GOODS_LOWER_SHELF => '下架'
    ];
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'package_goods';

    public $timestamps = false;

    /**
     * Notes:获取产品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/9/20
     */
    public static function getPackageGoodsList(int $page, int $limit, $keywords)
    {
        $model = new PackageGoods();

        if($limit == 1000){
            $model = $model->where('goods_status',1);
        }
        $list = $model->select('*')
            ->with('Category')
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('goods_name', 'like', '%' . $keywords . '%');
            })
            ->orderBy('goods_update_time','DESC')
            ->forPage($page, $limit)
            ->get();

        $count = $model->when($keywords != '', function ($query) use ($keywords){
                $query->where('goods_name',  $keywords);
            })
            ->count();
        return [$list, $count];
    }
    //测试
    public static function getProductList(int $page, int $limit, string $keywords)
    {
        $list = static::select(['intelligence_products.*','intelligence_category.category_name'])
            ->where(['product_del'=>0])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('product_name', $keywords);
            })
            ->leftJoin('intelligence_category', 'category_id', '=', 'intelligence_products.product__category_id')
            ->orderBy('product_create_time','DESC')
            ->forPage($page, $limit)
            ->get();
        $count = static::where(['product_del'=>0])
            ->when($keywords != '', function ($query) use ($keywords){
                $query->where('product_name',  $keywords);
            })
            ->count();
        return [$list, $count];
    }
    public function Category(){
        return $this->belongsTo(PackageGoodsCategory::class,'goods_category_id','category_id');
    }

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

    /**
     * Notes:获取套餐包包含所有产品
     * @param array $goods_ids
     * @return array
     * User: yym
     * Date: 2022/9/22
     */
    public static function getPackageGoodsListNew(array $goods_ids)
    {
        return static::where(['goods_status' => static::GOODS_PUT_SHELVES])
            ->whereIn('goods_id', $goods_ids)
            ->get()
            ->toArray();
    }
}
