<?php

namespace App\Models;

use App\Helper\SerialNumber;
use App\Helper\UniPlatform\BaseUniPlatform;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

/**
 * App\Models\Pay.
 *
 * @property int                        $pay_id        订单ID
 * @property int                        $user_id       用户ID
 * @property int                        $store_id      店铺ID
 * @property int                        $pay_type      支付类型
 * @property int                        $status        订单状态
 * @property string                     $pay_dt        支付时间
 * @property string                     $order_fee     订单价格
 * @property string                     $discount_fee  优惠价格
 * @property string                     $handling_fee  手续费
 * @property string                     $prepay_id     微信支付预付单ID
 * @property string|null                $serial_number 支付流水
 * @property string|null                $pay_error     支付错误记录
 * @property \Illuminate\Support\Carbon $updated_at    删除时间
 * @property string                     $deleted_at    删除时间
 * @property \Illuminate\Support\Carbon $created_at    创建时间
 *
 * @method static \Illuminate\Database\Eloquent\Builder|Pay newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Pay newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Pay query()
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereDeletedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereDiscountFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereHandlingFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereOrderFee($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay wherePayDt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay wherePayError($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay wherePayId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay wherePayType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay wherePrepayId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereSerialNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereStoreId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereUserId($value)
 *
 * @mixin \Eloquent
 *
 * @property int $source 来源 1-购买金币 2-购买会员
 *
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereSource($value)
 *
 * @property string|null $token 支付token
 *
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereToken($value)
 *
 * @property \App\Models\User|null $user
 * @property int|null              $is_settle 是否结算
 * @property string|null           $settle_no 结算ID
 *
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereIsSettle($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Pay whereSettleNo($value)
 */
class Pay extends Model
{
    use HasFactory;

    public const SOURCE_RECHARGE = 1;

    public const SOURCE_BUY_VIP = 2;

    protected $table = 'pay';

    protected $primaryKey = 'pay_id';

    protected $casts = [
        'pay_id' => 'string',
    ];

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

    /**
     * @return array|mixed
     */
    public function create(BaseUniPlatform $uniPlatform, float $price, int $source)
    {
        $payId = SerialNumber::createOrderId(\user()->id);
        // 字节跳动下单
        $res           = $uniPlatform->createOrder($payId, $price, \user()->open_id);
        $res['pay_id'] = $payId;

        $pay            = new Pay();
        $pay->pay_id    = $payId;
        $pay->user_id   = \user()->id;
        $pay->order_fee = $price;
        $pay->source    = $source;
        $pay->prepay_id = $res['order_id'];
        $pay->token     = $res['order_token'];
        $pay->save();

        return $res;
    }
}
