6
0
Fork 0
jiqu-library-server/app/Endpoint/Api/Http/Controllers/Merchant/AgentStatisticController.php

42 lines
1.3 KiB
PHP

<?php
namespace App\Endpoint\Api\Http\Controllers\Merchant;
use App\Endpoint\Api\Http\Controllers\Controller;
use App\Models\UserInfo;
use Illuminate\Http\Request;
class AgentStatisticController extends Controller
{
/**
* 代理统计
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function __invoke(Request $request)
{
$user = $request->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);
}
}