<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\UserShare.
 *
 * @property int                             $id
 * @property int                             $user_id    用户
 * @property int                             $child_id   二级ID
 * @property int                             $order_type 1-充值VIP 2-充值金币
 * @property string                          $order_id   订单号
 * @property string                          $income     收入
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 *
 * @method static \Illuminate\Database\Eloquent\Builder|UserShare newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserShare newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|UserShare query()
 * @method static \Illuminate\Database\Eloquent\Builder|UserShare whereChildId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserShare whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserShare whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserShare whereIncome($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserShare whereOrderId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserShare whereOrderType($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserShare whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|UserShare whereUserId($value)
 *
 * @mixin \Eloquent
 *
 * @property \App\Models\UserRechargeRecord|null $rechargeOrder
 * @property \App\Models\UserVipRecord|null      $vipOrder
 * @property mixed                               $order
 * @property mixed                               $type_name
 */
class UserShare extends Model
{
    use HasDateTimeFormatter;
    protected $table = 'user_share';

    protected $hidden = [
        'updated_at',
    ];

    public function vipOrder()
    {
        return $this->belongsTo(UserVipRecord::class, 'order_id', 'pay_id');
    }

    public function rechargeOrder()
    {
        return $this->belongsTo(UserRechargeRecord::class, 'order_id', 'pay_id');
    }

    public function setTypeNameAttribute($type)
    {
        $config                        = config('global.income_type');
        $this->attributes['type_name'] = $config[$type] ?? '未知';
    }

    public function setOrderAttribute(UserShare $share)
    {
        $order                     = 1 == $share->order_type ? $share->vipOrder : $share->rechargeOrder;
        $this->attributes['order'] = $order;
    }
}
