349 lines
9.6 KiB
PHP
Executable File
349 lines
9.6 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Model\Customer;
|
|
|
|
use App\Enums\BoolEnum;
|
|
use App\Enums\ClientType;
|
|
use App\Enums\LottState;
|
|
use App\Enums\OrderType;
|
|
use App\Exceptions\JingCaiException;
|
|
use App\Model\Config;
|
|
use App\Model\Order;
|
|
use App\Model\Seller\Shop;
|
|
use App\Model\Traits\PasswordTrait;
|
|
use App\Model\Traits\ScopeTrait;
|
|
use App\Model\Traits\TableFieldTrait;
|
|
use App\Model\Traits\TableTrait;
|
|
use App\Utils\Helps;
|
|
use App\Utils\ThrowException;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Tymon\JWTAuth\Contracts\JWTSubject;
|
|
|
|
class Customer extends Authenticatable implements JWTSubject
|
|
{
|
|
use SoftDeletes;
|
|
use TableFieldTrait;
|
|
use TableTrait;
|
|
use ScopeTrait;
|
|
use Notifiable;
|
|
use PasswordTrait;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'name', 'email', 'password'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for arrays.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $hidden = [
|
|
'password', 'remember_token', 'password_pay'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast to native types.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
'email_verified_at' => 'datetime',
|
|
];
|
|
|
|
protected $appends = [
|
|
'level_name',
|
|
'level_score_group',
|
|
'lottery_state_name',
|
|
'client_type_name',
|
|
'hide_name',
|
|
'agent_days',
|
|
'avatar_url',
|
|
];
|
|
|
|
// Rest omitted for brevity
|
|
|
|
/**
|
|
* Get the identifier that will be stored in the subject claim of the JWT.
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function getJWTIdentifier()
|
|
{
|
|
return $this->getKey();
|
|
}
|
|
|
|
/**
|
|
* Return a key value array, containing any custom claims to be added to the JWT.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getJWTCustomClaims()
|
|
{
|
|
return [];
|
|
}
|
|
|
|
public function shop()
|
|
{
|
|
return $this->hasOne(Shop::class, 'id', 'shop_id');
|
|
}
|
|
|
|
public function agentor()
|
|
{
|
|
return $this->hasOne(Customer::class, 'id', 'agent_id');
|
|
}
|
|
|
|
public function getAvatarUrlAttribute()
|
|
{
|
|
$avatar = $this->avatar;
|
|
if (!$avatar) {
|
|
return '';
|
|
}
|
|
return Helps::fullUrl($avatar);
|
|
}
|
|
|
|
public function getAvatarAttribute()
|
|
{
|
|
if (!$this->attributes) {
|
|
return '';
|
|
}
|
|
$avatar = @$this->attributes['avatar'];
|
|
if (!$avatar) {
|
|
return '';
|
|
}
|
|
return Helps::resizeImage($avatar);
|
|
|
|
}
|
|
|
|
// 代理的时长
|
|
public function getAgentDaysAttribute()
|
|
{
|
|
if (!$this->agent_at) {
|
|
return '未知';
|
|
}
|
|
$m = time() - strtotime($this->agent_at);
|
|
|
|
return ceil($m / (24 * 60 * 60));
|
|
}
|
|
|
|
|
|
// 客户端类型
|
|
public function getClientTypeNameAttribute()
|
|
{
|
|
if (!$this->client_type) {
|
|
return '未知';
|
|
}
|
|
return ClientType::getDescription($this->client_type);
|
|
}
|
|
|
|
// 账号等级
|
|
public function getLevelNameAttribute()
|
|
{
|
|
$score = $this->level_score;
|
|
if ($score < 5000) return '储备组长';
|
|
if ($score < 20000) return '组长';
|
|
if ($score < 50000) return '主管';
|
|
if ($score < 200000) return '部门经理';
|
|
if ($score < 500000) return '区域经理';
|
|
if ($score < 1000000) return '副总经理';
|
|
if ($score < 2000000) return '行政总监';
|
|
if ($score < 5000000) return '总监';
|
|
if ($score < 10000000) return '副总裁';
|
|
if ($score < 20000000) return '总裁';
|
|
return '董事长';
|
|
}
|
|
|
|
// 账号等级级别
|
|
public function getLevelScoreGroupAttribute()
|
|
{
|
|
$score = $this->level_score;
|
|
if ($score < 5000) return '5000';
|
|
if ($score < 20000) return '20000';
|
|
if ($score < 50000) return '50000';
|
|
if ($score < 200000) return '200000';
|
|
if ($score < 500000) return '500000';
|
|
if ($score < 1000000) return '1000000';
|
|
if ($score < 2000000) return '2000000';
|
|
if ($score < 5000000) return '5000000';
|
|
if ($score < 10000000) return '10000000';
|
|
if ($score < 20000000) return '20000000';
|
|
return '20000000';
|
|
}
|
|
|
|
public function getLevelList()
|
|
{
|
|
return [
|
|
['score' => '注册登录', 'name' => '储备组长'],
|
|
['score' => '5000', 'name' => '组长'],
|
|
['score' => '20000', 'name' => '主管'],
|
|
['score' => '50000', 'name' => '部门经理'],
|
|
['score' => '200000', 'name' => '区域经理'],
|
|
['score' => '500000', 'name' => '副总经理'],
|
|
['score' => '1000000', 'name' => '行政总监'],
|
|
['score' => '2000000', 'name' => '总监'],
|
|
['score' => '5000000', 'name' => '副总裁'],
|
|
['score' => '10000000', 'name' => '总裁'],
|
|
['score' => '20000000', 'name' => '董事长'],
|
|
];
|
|
}
|
|
|
|
public function getLotteryStateNameAttribute()
|
|
{
|
|
if (!$this->lottery_state || $this->lottery_state == LottState::NONE) {
|
|
return '';
|
|
}
|
|
if ($this->lottery_state == LottState::PENDING || $this->lottery_state == LottState::DRAFT) {
|
|
return '出票中';
|
|
}
|
|
if ($this->lottery_state == LottState::REVOKE) {
|
|
return '已撤单';
|
|
}
|
|
return '已出票';
|
|
}
|
|
|
|
public function getHideNameAttribute()
|
|
{
|
|
$name = '****' . substr($this->phone, -2);
|
|
if ($this->nickname) {
|
|
$name = mb_substr($this->nickname, 0, 1) . '**';
|
|
}
|
|
return $name;
|
|
}
|
|
|
|
public function shopWithHideContacts()
|
|
{
|
|
$shop = $this->shop;
|
|
if ($shop->keep_secret == BoolEnum::YES) {
|
|
$shop->seller_name = '******';
|
|
$shop->seller_phone = '******';
|
|
$shop->addr = '******';
|
|
}
|
|
return $shop;
|
|
}
|
|
|
|
public function balanceActive($decimals = 2)
|
|
{
|
|
return Helps::floatFormat($this->balance_withdraw + $this->balance_cash, $decimals);
|
|
}
|
|
|
|
public static function levelScoreIncr($customerId, $money)
|
|
{
|
|
$score = intval($money);
|
|
return Customer::where('id', $customerId)->update([
|
|
'level_score' => DB::raw("level_score + {$score}")
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 增加余额
|
|
* @param $customerId
|
|
* @param $withdraw 可提现
|
|
* @param $cash 不可提现
|
|
* @return mixed
|
|
*/
|
|
public static function balanceIncr($customerId, $withdraw, $cash)
|
|
{
|
|
return Customer::where('id', $customerId)->update([
|
|
'balance_withdraw' => DB::raw("balance_withdraw + {$withdraw}"),
|
|
'balance_cash' => DB::raw("balance_cash + {$cash}")
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 减余额
|
|
* @param $money
|
|
* @return mixed
|
|
*/
|
|
public static function balanceReduce($customerId, $money)
|
|
{
|
|
DB::beginTransaction();
|
|
try {
|
|
/** @var Customer $customer */
|
|
$customer = Customer::query()->lockForUpdate()->find($customerId);
|
|
|
|
ThrowException::isTrue($customer->balanceActive(4) < $money, '用户账户余额不足');
|
|
|
|
// 先扣可提现,再扣不可提现
|
|
$withdraw = 0;
|
|
$cash = 0;
|
|
if ($customer->balance_withdraw >= $money) {
|
|
$withdraw = $money;
|
|
} else {
|
|
$withdraw = $customer->balance_withdraw;
|
|
$cash = $money - $customer->balance_withdraw;
|
|
}
|
|
$customer->balance_withdraw -= $withdraw;
|
|
$customer->balance_cash -= $cash;
|
|
$customer->save();
|
|
DB::commit();
|
|
} catch (JingCaiException $exception) {
|
|
DB::rollBack();
|
|
throw $exception;
|
|
} catch (\Exception $exception) {
|
|
DB::rollBack();
|
|
throw $exception;
|
|
}
|
|
}
|
|
|
|
public function refreshFollowerFans()
|
|
{
|
|
$this->follower_num = CustomerFollow::where('follower_id', $this->id)->count();
|
|
$this->fans_num = CustomerFollow::where('customer_id', $this->id)->count();
|
|
$this->save();
|
|
}
|
|
|
|
public function shops()
|
|
{
|
|
return Customer::with('shop:id,name')
|
|
->select('id', 'phone', 'shop_id')
|
|
->where('phone', $this->phone)->get();
|
|
}
|
|
|
|
/**
|
|
* 7日盈利率
|
|
* @return float
|
|
*/
|
|
public function sevenWinRate() {
|
|
$orders = Order::where('customer_id', $this->id)
|
|
->whereIn('lottery_state', [LottState::WIN, LottState::SEND])
|
|
->where('type', OrderType::FADAN)
|
|
->orderBy('id', 'desc')
|
|
->limit(7)
|
|
->get();
|
|
$rate = 0;
|
|
foreach ($orders as $order) {
|
|
$rate += $order->lottery_prize / $order->money;
|
|
}
|
|
return Helps::floatFormat(Helps::floatFormat($rate / 7, 4) * 100);
|
|
}
|
|
|
|
/**
|
|
* 7日命中
|
|
* @return float
|
|
*/
|
|
public function sevenHit() {
|
|
$orders = Order::where('customer_id', $this->id)
|
|
->whereIn('lottery_state', [LottState::WIN, LottState::LOSS, LottState::SEND])
|
|
->where('type', OrderType::FADAN)
|
|
->orderBy('id', 'desc')
|
|
->limit(7)
|
|
->get();
|
|
$winCount = $orders->whereIn('lottery_state', [LottState::WIN, LottState::SEND])->count();
|
|
return count($orders) . '中' . $winCount;
|
|
// if ($winCount == 0) {
|
|
// return 0;
|
|
// }
|
|
//
|
|
// return Helps::floatFormat($winCount / 7, 4) * 100;
|
|
}
|
|
}
|