<?php

namespace App\Models;

use App\Scopes\UserScope;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\UserConsumeRecord.
 *
 * @property int                             $id
 * @property int                             $user_id    user_id
 * @property int|null                        $type       类型
 * @property string                          $before     消费后记录
 * @property string                          $change     消费金币
 * @property string                          $current    消费后金币
 * @property string                          $remark     备注
 * @property int                             $order_id   订单
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property string|null                     $deleted_at
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \App\Models\User|null           $user
 *
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord query()
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord whereBefore($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord whereChange($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord whereCurrent($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord whereOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord whereRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord whereType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord whereUserId($value)
 *
 * @mixin \Eloquent
 *
 * @method static \Illuminate\Database\Eloquent\Builder|UserConsumeRecord filterUser()
 */
class UserConsumeRecord extends Model
{
    use HasFactory;
    use UserScope;

    public const TYPE_RECHARGE = 1;
    public const TYPE_CONSUME  = 2;
    public const TYPE_SIGN     = 3;

    protected $casts = [
        'before'  => 'integer',
        'change'  => 'integer',
        'current' => 'integer',
    ];

    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function record($userId, $type, $change, $remark)
    {
        $record          = new UserConsumeRecord();
        $user            = UserInfo::find($userId);
        $record->user_id = $user->user_id;
        $record->type    = $type;
        $record->before  = $user->integral;
        $record->change  = $change;
        $record->current = $user->integral + $record->change;
        $record->remark  = $remark;
        $record->save();
    }
}
