<?php

namespace Modules\Payment\Models;

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

class Refund extends Model
{

    protected $table = 'payment_refunds';

    protected $dates = [
        'refund_at',
    ];

    public static function boot()
    {
        parent::boot();

        self::creating(function ($model) {
            $time    = explode(' ', microtime());
            $counter = $model->whereDate('created_at', Carbon::today())->count() + 1;
            $len     = config('payment.refund_no_counter_length');
            $prefix  = config('payment.refund_no_counter_prefix');

            $len = $len < 6 ? 6 : $len;
            $len = $len > 16 ? 16 : $len;

            $model->refund_no = $prefix.date('YmdHis').
                sprintf('%06d', $time[0] * 1e6).
                sprintf('%0'.$len.'d', $counter);
        });
    }

    /**
     * Notes   : 支付订单
     *
     * @Date   : 2021/6/1 11:13 上午
     * @Author : < Jason.C >
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
     */
    public function payment(): BelongsTo
    {
        return $this->belongsTo(Payment::class);
    }

}