<?php

namespace Modules\User\Models;


use App\Models\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class UserStockLog extends Model
{
    const  TYPE_INIT = 'init';
    const  TYPE_IN   = 'in';
    const  TYPE_OUT  = 'out';

    const  TYPES = [
        self::TYPE_INIT => '开通会员',
        self::TYPE_IN   => '续费',
        self::TYPE_OUT  => '提货',
    ];

    public function identity()
    {
        return $this->belongsTo(Identity::class);
    }

    public function userStock(): BelongsTo
    {
        return $this->belongsTo(UserStock::class);
    }

    public function getTypeTextAttribute(): string
    {
        return self::TYPES[$this->type];
    }

    /**
     * Notes: description
     *
     * @Author: 玄尘
     * @Date: 2022/8/3 10:44
     * @param  Builder  $query
     * @param  User  $user
     * @return Builder
     */
    public function scopeByUser(Builder $query, User $user): Builder
    {
        return $query->whereHas('userStock', function ($q) use ($user) {
            return $q->ByUser($user);
        });
    }

}
