From c090157c816fc12180998cb1cf213158314a417d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=9D=99?= Date: Thu, 30 Dec 2021 21:22:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=90=86=E6=99=8B=E5=8D=87=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Merchant/AgentController.php | 158 ++++++++++++++++++ .../Merchant/AgentStatisticController.php | 41 ----- app/Endpoint/Api/routes.php | 4 +- app/Models/User.php | 2 +- app/Models/UserInfo.php | 31 ++-- config/agent.php | 47 ++++++ 6 files changed, 228 insertions(+), 55 deletions(-) create mode 100644 app/Endpoint/Api/Http/Controllers/Merchant/AgentController.php delete mode 100644 app/Endpoint/Api/Http/Controllers/Merchant/AgentStatisticController.php create mode 100644 config/agent.php diff --git a/app/Endpoint/Api/Http/Controllers/Merchant/AgentController.php b/app/Endpoint/Api/Http/Controllers/Merchant/AgentController.php new file mode 100644 index 00000000..10a0eb39 --- /dev/null +++ b/app/Endpoint/Api/Http/Controllers/Merchant/AgentController.php @@ -0,0 +1,158 @@ +user()->userInfo; + + // 升级规则 + $rules = config('agent.upgrade_rules'); + + switch ($userInfo->agent_level) { + case UserInfo::AGENT_LEVEL_VIP: + $data = [ + // 是否显示 + 'display' => true, + // 业绩 + 'team_sales_value' => [ + 'label' => '业绩', + 'current_team_sales_value' => $userInfo->team_sales_value, + 'upgrade_team_sales_value' => '-1', + ], + // 代理 + 'agents_count' => [ + 'label' => '店铺', + 'current_agents_count' => $userInfo->getVipAgentsCount(), + 'upgrade_agents_count' => $rules['community']['vips_agents_count'], + ], + ]; + break; + + case UserInfo::AGENT_LEVEL_COMMUNITY: + $data = [ + 'display' => true, + 'team_sales_value' => [ + 'label' => '业绩', + 'current_team_sales_value' => $userInfo->team_sales_value, + 'upgrade_team_sales_value' => (string) $rules['district']['team_sales_value'], + ], + 'agents_count' => [ + 'label' => '店铺', + 'current_agents_count' => $userInfo->getVipAgentsCount(), + 'upgrade_agents_count' => $rules['district']['vips_agents_count'], + ], + ]; + break; + + case UserInfo::AGENT_LEVEL_DISTRICT: + $data = [ + 'display' => true, + 'team_sales_value' => [ + 'label' => '业绩', + 'current_team_sales_value' => $userInfo->team_sales_value, + 'upgrade_team_sales_value' => (string) $rules['city']['team_sales_value'], + ], + 'agents_count' => [ + 'label' => '区代', + 'current_agents_count' => $userInfo->getDistrictAgentsCountOnDifferentLines(), + 'upgrade_agents_count' => $rules['city']['district_agents_count'], + ], + ]; + break; + + case UserInfo::AGENT_LEVEL_CITY: + $data = [ + 'display' => true, + 'team_sales_value' => [ + 'label' => '市代', + 'current_team_sales_value' => $userInfo->team_sales_value, + 'upgrade_team_sales_value' => (string) $rules['province']['team_sales_value'], + ], + 'agents_count' => [ + 'label' => '店铺', + 'current_agents_count' => $userInfo->getCityAgentsCountOnDifferentLines(), + 'upgrade_agents_count' => $rules['province']['city_agents_count'], + ], + ]; + break; + + case UserInfo::AGENT_LEVEL_PROVINCE: + $data = [ + 'display' => true, + 'team_sales_value' => [ + 'label' => '业绩', + 'current_team_sales_value' => $userInfo->team_sales_value, + 'upgrade_team_sales_value' => (string) $rules['branch']['team_sales_value'], + ], + 'agents_count' => [ + 'label' => '省代', + 'current_agents_count' => $userInfo->getProvinceAgentsCountOnDifferentLines(), + 'upgrade_agents_count' => $rules['branch']['province_agents_count'], + ], + ]; + break; + + default: + $data = [ + 'display' => false, + 'team_sales_value' => [ + 'label' => '', + 'current_team_sales_value' => '0', + 'upgrade_team_sales_value' => '-1', + ], + 'agents_count' => [ + 'label' => '', + 'current_agents_count' => 0, + 'upgrade_agents_count' => -1, + ], + ]; + break; + } + + return response()->json($data); + } + + /** + * 代理统计 + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\JsonResponse + */ + public function statistics(Request $request) + { + $path = $request->user()->userInfo->full_path; + + $statistics = UserInfo::selectRaw('count(1) as aggregate, agent_level') + ->where('agent_level', '>=', 0) + ->where('path', 'like', "{$path}%") + ->groupBy('agent_level') + ->pluck('aggregate', 'agent_level'); + $data = []; + + foreach ([ + UserInfo::AGENT_LEVEL_VIP => 'vip', + UserInfo::AGENT_LEVEL_COMMUNITY => 'community', + UserInfo::AGENT_LEVEL_DISTRICT => 'district', + UserInfo::AGENT_LEVEL_CITY => 'city', + UserInfo::AGENT_LEVEL_PROVINCE => 'province', + UserInfo::AGENT_LEVEL_BRANCH => 'branch', + ] as $k => $v) { + $data[$v] = $statistics[$k] ?? 0; + } + + return response()->json($data); + } +} diff --git a/app/Endpoint/Api/Http/Controllers/Merchant/AgentStatisticController.php b/app/Endpoint/Api/Http/Controllers/Merchant/AgentStatisticController.php deleted file mode 100644 index 757d814f..00000000 --- a/app/Endpoint/Api/Http/Controllers/Merchant/AgentStatisticController.php +++ /dev/null @@ -1,41 +0,0 @@ -user(); - - $statistics = UserInfo::selectRaw('count(1) as aggregate, agent_level') - ->where('agent_level', '>=', 0) - ->where('path', 'like', "{$user->userInfo->full_path}%") - ->groupBy('agent_level') - ->pluck('aggregate', 'agent_level'); - $data = []; - - foreach ([ - UserInfo::AGENT_LEVEL_VIP => 'vip', - UserInfo::AGENT_LEVEL_COMMUNITY => 'community', - UserInfo::AGENT_LEVEL_DISTRICT => 'district', - UserInfo::AGENT_LEVEL_CITY => 'city', - UserInfo::AGENT_LEVEL_PROVINCE => 'province', - UserInfo::AGENT_LEVEL_BRANCH => 'branch', - ] as $k => $v) { - $data[$v] = $statistics[$k] ?? 0; - } - - return response()->json($data); - } -} diff --git a/app/Endpoint/Api/routes.php b/app/Endpoint/Api/routes.php index dac39fa1..8901c1f3 100644 --- a/app/Endpoint/Api/routes.php +++ b/app/Endpoint/Api/routes.php @@ -196,7 +196,9 @@ Route::group([ Route::get('sales-value-logs', [Merchant\SalesValueLogController::class, 'index']); // 团队销售值日志 Route::get('team-sales-value-logs', [Merchant\TeamSalesValueLogController::class, 'index']); + // 代理晋升条件 + Route::get('agent-upgrade-conditions', [Merchant\AgentController::class, 'upgradeConditions']); // 代理统计 - Route::get('agent-statistics', Merchant\AgentStatisticController::class); + Route::get('agent-statistics', [Merchant\AgentController::class, 'statistics']); }); }); diff --git a/app/Models/User.php b/app/Models/User.php index 7b12e7f3..d703d361 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -133,7 +133,7 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac */ public function isVip(): bool { - return $this->userInfo?->growth_value >= 650; + return $this->userInfo?->growth_value >= config('agent.upgrade_rules.vip.sales_value'); } /** diff --git a/app/Models/UserInfo.php b/app/Models/UserInfo.php index 7fd75a7d..c2b48186 100644 --- a/app/Models/UserInfo.php +++ b/app/Models/UserInfo.php @@ -274,8 +274,10 @@ class UserInfo extends Model return; } + $rules = config('agent.upgrade_rules'); + // 如果成长值不足650时,不能升级 - if (bccomp($this->growth_value, '650') < 0) { + if (bccomp($this->growth_value, $rules['vip']['sales_value']) < 0) { return; } @@ -284,6 +286,7 @@ class UserInfo extends Model $lvl = static::AGENT_LEVEL_VIP; } + // 团队销售值 $salesValue = $this->team_sales_value; if ($lvl === static::AGENT_LEVEL_VIP) { @@ -291,31 +294,34 @@ class UserInfo extends Model // 如果直推店铺人数>=6,并且团队销售值>=65000,则可升级为区级代理 // 或者直推店铺人数>=4,则可升级为社区 - if ($vipsCount >= 6 && bccomp($salesValue, '65000') >= 0) { + if ( + $vipsCount >= $rules['district']['vips_agents_count'] + && bccomp($salesValue, $rules['district']['team_sales_value']) >= 0 + ) { $lvl = static::AGENT_LEVEL_DISTRICT; - } elseif ($vipsCount >= 4) { + } elseif ($vipsCount >= $rules['community']['vips_agents_count']) { $lvl = static::AGENT_LEVEL_COMMUNITY; } - } elseif ($lvl === static::AGENT_LEVEL_COMMUNITY && bccomp($salesValue, '65000') >= 0) { - if ($this->getVipAgentsCount() >= 6) { + } elseif ($lvl === static::AGENT_LEVEL_COMMUNITY && bccomp($salesValue, $rules['district']['team_sales_value']) >= 0) { + if ($this->getVipAgentsCount() >= $rules['district']['vips_agents_count']) { $lvl = static::AGENT_LEVEL_DISTRICT; } } - if ($lvl === static::AGENT_LEVEL_DISTRICT && bccomp($salesValue, '780000') >= 0) { - if ($this->getDistrictAgentsCountOnDifferentLines() >= 3) { + if ($lvl === static::AGENT_LEVEL_DISTRICT && bccomp($salesValue, $rules['city']['team_sales_value']) >= 0) { + if ($this->getDistrictAgentsCountOnDifferentLines() >= $rules['city']['district_agents_count']) { $lvl = static::AGENT_LEVEL_CITY; } } - if ($lvl === static::AGENT_LEVEL_CITY && bccomp($salesValue, '7800000') >= 0) { - if ($this->getCityAgentsCountOnDifferentLines() >= 2) { + if ($lvl === static::AGENT_LEVEL_CITY && bccomp($salesValue, $rules['province']['team_sales_value']) >= 0) { + if ($this->getCityAgentsCountOnDifferentLines() >= $rules['province']['city_agents_count']) { $lvl = static::AGENT_LEVEL_PROVINCE; } } - if ($lvl === static::AGENT_LEVEL_PROVINCE && bccomp($salesValue, '52000000') >= 0) { - if ($this->getProvinceAgentsCountOnDifferentLines() >= 2) { + if ($lvl === static::AGENT_LEVEL_PROVINCE && bccomp($salesValue, $rules['branch']['team_sales_value']) >= 0) { + if ($this->getProvinceAgentsCountOnDifferentLines() >= $rules['branch']['province_agents_count']) { $lvl = static::AGENT_LEVEL_BRANCH; } } @@ -349,7 +355,8 @@ class UserInfo extends Model // 增加预收益的成长值 $this->increment('pre_growth_value', $growthValue); - $compared = bccomp(bcadd($this->growth_value, $this->pre_growth_value, 2), '650', 2); + $totalGrowthValue = bcadd($this->growth_value, $this->pre_growth_value, 2); + $compared = bccomp($totalGrowthValue, config('agent.upgrade_rules.vip.sales_value'), 2); if ($this->agent_level === static::AGENT_LEVEL_VIP && $compared < 0) { $this->update([ diff --git a/config/agent.php b/config/agent.php new file mode 100644 index 00000000..a105c969 --- /dev/null +++ b/config/agent.php @@ -0,0 +1,47 @@ + [ + // 升级到店铺 + 'vip' => [ + // 个人销售值 650 + 'sales_value' => 650, + ], + // 升级到社区 + 'community' => [ + // 个人销售值 + 'sales_value' => 650, + // 直推店铺数 + 'vips_agents_count' => 4, + ], + // 升级到区级 + 'district' => [ + // 团队销售值 + 'team_sales_value' => 65000, + // 直推店铺数 + 'vips_agents_count' => 6, + ], + // 升级到市级 + 'city' => [ + // 团队销售值 + 'team_sales_value' => 780000, + // 不同线上的区代 + 'district_agents_count' => 3, + ], + // 升级到省级 + 'province' => [ + // 团队销售值 + 'team_sales_value' => 7800000, + // 不同线上的市代 + 'city_agents_count' => 2, + ], + // 升级到分公司 + 'branch' => [ + // 团队销售值 + 'team_sales_value' => 52000000, + // 不同线上的省代 + 'province_agents_count' => 2, + ], + ], +];