<?php

namespace Modules\User\Models\Traits;


use Illuminate\Database\Eloquent\Relations\HasMany;
use Modules\User\Models\Order;

trait HasVipOrders
{

    /**
     * Notes   : 用户的订单列表
     *
     * @Date   : 2021/4/19 10:28 上午
     * @Author : < Jason.C >
     * @return HasMany
     */
    public function vipOrders(): HasMany
    {
        return $this->hasMany(Order::class);
    }

    /**
     * Notes: 开通会员缴费
     *
     * @Author: 玄尘
     * @Date: 2022/9/7 13:59
     */
    public function getOpenVipPrices($type = 'all')
    {
        return $this->vipOrders
            ->where('state', Order::STATE_SUCCESS)
            ->where('price', '>', 0)
            ->pluck('price');
    }

    /**
     * Notes: 创建订单
     *
     * @Author: 玄尘
     * @Date: 2022/9/7 16:55
     */
    public function createOrder($identity_id, $year, $price, $stock, $source = null)
    {
        $data = [
            'user_id'     => $this->id,
            'identity_id' => $identity_id,
            'year'        => $year,
            'type'        => 1,
            'name'        => '',
            'card_no'     => '',
            'cover'       => '',
            'stock'       => $stock,
            'state'       => Order::STATE_INIT,
            'price'       => $price,
            'source'      => $source,
        ];

        $order = Order::create($data);
        $order->pay();
        return $order;
    }

}