236 lines
6.3 KiB
PHP
236 lines
6.3 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\UserPerf;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Overtrue\LaravelFollow\Traits\CanFavorite;
|
|
use RuLong\Area\Traits\UserHasAddress;
|
|
use RuLong\Identity\Traits\UserHasIdentity;
|
|
use RuLong\Order\Models\Order;
|
|
use RuLong\UserAccount\Models\UserAccountLog;
|
|
use RuLong\UserAccount\Traits\UserHasAccount;
|
|
use RuLong\UserRelation\Traits\UserHasRelations;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use Notifiable, SoftDeletes, UserHasAccount, UserHasRelations, UserHasIdentity, UserHasAddress, CanFavorite;
|
|
|
|
protected $guarded = [];
|
|
|
|
protected $info = [];
|
|
|
|
protected $dates = [
|
|
'activationd_at',
|
|
];
|
|
|
|
public static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
self::saved(function ($model) {
|
|
if (is_array($model->info) && !empty($model->info)) {
|
|
$model->info()->updateOrCreate(['user_id' => $model->id], $model->info);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 修改用户资料
|
|
* @Author:<C.Jason>
|
|
* @Date:2018-11-07T13:30:14+0800
|
|
* @param array $info UserInfo fields
|
|
*/
|
|
protected function setInfoAttribute($info)
|
|
{
|
|
$this->info = $info;
|
|
}
|
|
|
|
/**
|
|
* 修改密码,留空则不修改
|
|
* @Author:<C.Jason>
|
|
* @Date:2018-10-31T15:04:59+0800
|
|
* @param void
|
|
*/
|
|
protected function setPasswordAttribute($password)
|
|
{
|
|
if ($password) {
|
|
$this->attributes['password'] = bcrypt($password);
|
|
}
|
|
}
|
|
|
|
public function info()
|
|
{
|
|
return $this->hasOne(UserInfo::class);
|
|
}
|
|
|
|
public function accountLogs()
|
|
{
|
|
return $this->hasMany(UserAccountLog::class);
|
|
}
|
|
|
|
public function cashAccount()
|
|
{
|
|
return $this->hasOne(CashAccount::class);
|
|
}
|
|
|
|
public function cashouts()
|
|
{
|
|
return $this->hasMany(Cashout::class);
|
|
}
|
|
|
|
/**
|
|
* 我的订单
|
|
* @return [type] [description]
|
|
*/
|
|
public function orders()
|
|
{
|
|
return $this->hasMany(Order::class);
|
|
}
|
|
|
|
public function cart()
|
|
{
|
|
return $this->hasMany(Cart::class);
|
|
}
|
|
|
|
//我的提货卡
|
|
public function mycard()
|
|
{
|
|
return $this->hasMany(Card::class, 'user_id', 'id');
|
|
}
|
|
|
|
//使用的提货卡信息
|
|
public function card()
|
|
{
|
|
return $this->hasOne(Card::class, 'active_id', 'id');
|
|
}
|
|
|
|
//自己业绩
|
|
public function selfearnings($type = 'all')
|
|
{
|
|
$activationd_at = $this->activationd_at;
|
|
if (!$activationd_at) {
|
|
return 0;
|
|
}
|
|
switch ($type) {
|
|
case 'all':
|
|
return UserPerf::where('user_id', $this->id)->where('from_id', $this->id)->where('created_at', '>=', $activationd_at)->sum('perf') ?? 0;
|
|
break;
|
|
case 'month':
|
|
$between = [
|
|
now()->startOfMonth(),
|
|
now()->endOfMonth(),
|
|
];
|
|
|
|
if ($between[0] < $activationd_at) {
|
|
$between[0] = $activationd_at;
|
|
}
|
|
|
|
return UserPerf::where('user_id', $this->id)->where('from_id', $this->id)->whereBetween('created_at', $between)->sum('perf') ?? 0;
|
|
break;
|
|
case 'day':
|
|
$between = [
|
|
now()->startOfDay(),
|
|
now()->endOfDay(),
|
|
];
|
|
|
|
if ($between[0] < $activationd_at) {
|
|
$between[0] = $activationd_at;
|
|
}
|
|
|
|
return UserPerf::where('user_id', $this->id)->where('from_id', $this->id)->whereBetween('created_at', $between)->sum('perf') ?? 0;
|
|
break;
|
|
default:
|
|
return 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//团队收益
|
|
public function teamearnings($type = 'all')
|
|
{
|
|
$activationd_at = $this->activationd_at;
|
|
if (!$activationd_at) {
|
|
return 0;
|
|
}
|
|
|
|
switch ($type) {
|
|
case 'all':
|
|
return UserPerf::where('user_id', $this->id)->where('from_id', '!=', $this->id)->where('created_at', '>=', $activationd_at)->sum('perf') ?? 0;
|
|
break;
|
|
case 'month':
|
|
$between = [
|
|
now()->startOfMonth(),
|
|
now()->endOfMonth(),
|
|
];
|
|
if ($between[0] < $activationd_at) {
|
|
$between[0] = $activationd_at;
|
|
}
|
|
return UserPerf::where('user_id', $this->id)->where('from_id', '!=', $this->id)->whereBetween('created_at', $between)->sum('perf') ?? 0;
|
|
break;
|
|
default:
|
|
return 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//团队收益
|
|
public function allearnings($type = 'all')
|
|
{
|
|
$activationd_at = $this->activationd_at;
|
|
if (!$activationd_at) {
|
|
return 0;
|
|
}
|
|
|
|
switch ($type) {
|
|
case 'all':
|
|
return UserPerf::where('user_id', $this->id)->where('created_at', '>=', $activationd_at)->sum('perf') ?? 0;
|
|
break;
|
|
case 'month':
|
|
$between = [
|
|
now()->startOfMonth(),
|
|
now()->endOfMonth(),
|
|
];
|
|
|
|
if ($between[0] < $activationd_at) {
|
|
$between[0] = $activationd_at;
|
|
}
|
|
return UserPerf::where('user_id', $this->id)->whereBetween('created_at', $between)->sum('perf') ?? 0;
|
|
break;
|
|
default:
|
|
return 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
//获取升级金额
|
|
public function getIdentityPrice($identity_id)
|
|
{
|
|
switch ($identity_id) {
|
|
case 1:
|
|
return config('vip_price', 399);
|
|
break;
|
|
case 2:
|
|
return config('agent_price', 10000);
|
|
break;
|
|
case 3:
|
|
return config('city_price', 150000);
|
|
break;
|
|
case 4:
|
|
return config('shareholder_price', 300000);
|
|
break;
|
|
default:
|
|
return 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function getTeamCountAttribute()
|
|
{
|
|
return $this->children()->count();
|
|
}
|
|
|
|
}
|