<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

/**
 * Class ProductBuy
 *
 * @property int $id
 * @property int $auth_user_id
 * @property int $product_id
 * @property int $num
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 * @property Product $product
 * @package App\Models
 * @property-read \App\User $auth_user
 * @property-read \App\User $user
 * @method static \Illuminate\Database\Eloquent\Builder|ProductBuy newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|ProductBuy newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|ProductBuy query()
 * @method static \Illuminate\Database\Eloquent\Builder|ProductBuy whereAuthUserId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ProductBuy whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ProductBuy whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ProductBuy whereNum($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ProductBuy whereProductId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|ProductBuy whereUpdatedAt($value)
 * @mixin \Eloquent
 */
class ProductBuy extends Model
{
	protected $table = 'product_buys';

	protected $casts = [
		'auth_user_id' => 'int',
		'product_id' => 'int',
		'num' => 'int'
	];

	protected $fillable = [
		'auth_user_id',
		'product_id',
		'num'
	];

	public function product()
	{
		return $this->belongsTo(Product::class);
	}

    public function user()
	{
        return $this->belongsTo('App\User','auth_user_id');
	}

    public function auth_user()
    {
        return $this->belongsTo('App\User','auth_user_id');
    }

	public static function getNotShowProductIDs($user_id){
		$ret = [];
		$ProductBuys = ProductBuy::where('auth_user_id', $user_id)->get();
		if($ProductBuys){
			foreach($ProductBuys as $ProductBuy){
				if($ProductBuy->product){
					if($ProductBuy->product->max_buy && $ProductBuy->num >= $ProductBuy->product->max_buy_num){
						$ret[] = $ProductBuy->product->id;
					}
				}
			}
		}
		return $ret;
	}

}
