<?php

namespace RuLong\Identity\Models;

use App\Models\IdentityGift;
use App\Models\IdentityPrice;
use Illuminate\Database\Eloquent\Model;

class Identity extends Model
{
    //
    protected $casts = [
        'configs' => 'array',
    ];

    public function prices()
    {
        return $this->hasMany(IdentityPrice::class);
    }

    public function codes()
    {
        return $this->hasMany(IdentityCode::class);
    }

    /**
     * 获取身份指定日期的佣金设定值
     * @Date:2019-09-16T13:53:36+0800
     * @param string $date [Y-m-d H:i:s]
     * @return [type] [description]
     */
    public function getPrice($date = '')
    {
        $date = $date ?: now()->endOfDay()->format('Y-m-d H:i:s');
        $info = IdentityPrice::where('identity_id', $this->id)->where('created_at', '<', $date)->orderBy('created_at', 'desc')->first();
        if ($info) {
            return $info->configs;
        } else {
            return false;
        }
    }

    public function gifts()
    {
        return $this->hasMany(IdentityGift::class);
    }
}
