<?php

namespace App\Models;

use App\Auth;
use Carbon\Carbon;
use Dcat\Admin\Traits\HasDateTimeFormatter;
use Illuminate\Foundation\Auth\User as Authenticatable;
use PHPOpenSourceSaver\JWTAuth\Contracts\JWTSubject;

class User extends Authenticatable implements JWTSubject
{
    use HasDateTimeFormatter;

    protected $fillable = ['name', 'open_id', 'avatar', 'email', 'mobile', 'status', 'online', 'last_login_ip', 'register_ip', 'last_login_time', 'created_at', 'updated_at', 'deleted_at',
        'is_black', 'sessionKey', 'share_pid', 'diamond', 'is_share', 'share_name', 'share_phone', 'share_pid', 'share_date', 'income','qr_code'];

    public function getJWTIdentifier()
    {
        return $this->getKey(); // TODO: Implement getJWTIdentifier() method.
    }

    public function getJWTCustomClaims()
    {
        return [
            'role' => 'user',
        ];  // TODO: Implement getJWTCustomClaims() method.
    }

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

    /**
     * 设置密码加密.
     *
     * @return string
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = bcrypt($value);
    }

    public function getAmountSumAmountAttribute($value)
    {
        return (float) ($value / 100);
    }

    public function getIncomeAttribute($value)
    {
        return (float) ($value / 100);
    }

    public function setIncomeAttribute($value)
    {
        return $value * 100;
    }

    public function getTotalIncomeAttribute($value)
    {
        return (float) ($value / 100);
    }

    public function parent()
    {
        return $this->belongsTo(User::class, 'parent_id');
    }

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

    public function childOrder()
    {
        return $this->hasMany(UserShare::class, 'child_id', 'id');
    }

    public function team()
    {
        return $this->hasMany(User::class, 'share_pid', Auth::$userId);
    }

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

    public function consumeRecords()
    {
        return $this->hasMany(UserConsumeRecord::class, 'user_id', 'id');
    }

    public function rechargeRecords()
    {
        return $this->hasMany(UserRechargeRecord::class, 'user_id', 'id');
    }
    public function creation()
    {
        return $this->hasMany(TaskList::class, 'user_id', 'id');
    }
    public function invite_data()
    {
        return $this->hasMany(User::class, 'share_pid', 'id');
    }
    public function userShare(){
        return $this->hasOne(User::class, 'id', 'share_pid');
    }
    public function vipRecords()
    {
        return $this->hasMany(UserVipRecord::class, 'user_id', 'id');
    }

    public function amount()
    {
        return $this->hasMany(Order::class, 'user_id', 'id')->where('state', 1);
    }

    public function order()
    {
        return $this->hasMany(Order::class, 'user_id', 'id')->where('state', 1);
    }

    public function beComeShare($id)
    {
        $user = self::find($id);
        if (!$user->is_share) {
            $config   = ShareConfig::first();
            $isBecome = false;
            if (1 == $config->become_type) {// 无条件
                $isBecome = true;
            } elseif (2 == $config->become_type) { // 成为对应的会员
                $vipRecords = UserVipRecord::where('combo_id', $config->become_vip_id)
                    ->where('user_id', $user->id)
                    ->where('status', 1)
                    ->count();
                if ($vipRecords) {
                    $isBecome = true;
                }
            } else { // 充值一定金币
                $RechargeGold = UserRechargeRecord::where('user_id', $user->id)
                    ->where('status', 1)
                    ->sum('gold');
                if ($RechargeGold >= $config->become_gold) {
                    $isBecome = true;
                }
            }

            if ($isBecome) {
                $user->is_share        = 1;
                $user->become_share_at = Carbon::now()->toDateTimeString();
                $user->save();
            }
        }
    }
}
