Commit ef230759 authored by 龚双喜's avatar 龚双喜

更新佣金排行和工艺技术排行接口

parent 7f49f083
...@@ -14,90 +14,71 @@ class Ranking extends Commission ...@@ -14,90 +14,71 @@ class Ranking extends Commission
/** /**
* 佣金排行榜 * 佣金排行榜
* 按用户表佣金字段排序 * 按已结算佣金金额排序
*/ */
public function commission() public function commission()
{ {
$limit = $this->request->param('limit', 20); $limit = $this->request->param('limit', 20);
$timeType = $this->request->param('time_type', 'all'); // all|month|week|day $timeType = $this->request->param('time_type', 'all'); // all|month|week|day
// 从User表直接读取commission字段 $query = RewardModel::field([
$query = UserModel::field([ 'agent_id',
'id as user_id', 'sum(commission) as total_commission',
'nickname', 'count(distinct order_id) as order_count'
'avatar',
'commission as total_commission'
]) ])
->where('commission', '>', 0) ->where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED)
->where('status', 'normal'); ->group('agent_id');
// 时间筛选(根据佣金记录计算时间范围内的佣金)
if ($timeType !== 'all') {
// 查询在时间范围内有佣金记录的用户
$rewardQuery = RewardModel::field('agent_id')
->where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
switch ($timeType) {
case 'month':
$rewardQuery->whereTime('commission_time', 'month');
break;
case 'week':
$rewardQuery->whereTime('commission_time', 'week');
break;
case 'day':
$rewardQuery->whereTime('commission_time', 'today');
break;
}
$agentIds = $rewardQuery->group('agent_id')->column('agent_id'); // 时间筛选
$query->whereIn('id', $agentIds); switch ($timeType) {
case 'month':
$query->whereTime('commission_time', 'month');
break;
case 'week':
$query->whereTime('commission_time', 'week');
break;
case 'day':
$query->whereTime('commission_time', 'today');
break;
} }
$rankings = $query->order('commission', 'desc') $rankings = $query->order('total_commission', 'desc')
->limit($limit) ->limit($limit)
->select(); ->select();
if (empty($rankings)) { if (empty($rankings)) {
$this->success('获取成功', [ $this->success('获取成功', []);
'list' => [], }
'current_user_rank' => null
]); $agentIds = array_column($rankings, 'agent_id');
// 获取用户信息(排除已删除用户)
$users = UserModel::whereIn('id', $agentIds)
->where('status', 'normal')
->field('id, nickname, avatar')
->select();
$userMap = [];
if (is_array($users)) {
$userMap = array_column($users, null, 'id');
} else {
$userMap = $users->column(null, 'id');
} }
$result = []; $result = [];
foreach ($rankings as $index => $item) { foreach ($rankings as $index => $item) {
// 查询订单数量 $user = $userMap[$item->agent_id] ?? null;
$orderCount = 0; // 跳过已删除的用户
if ($timeType !== 'all') { if (!$user) {
$orderQuery = RewardModel::where('agent_id', $item['user_id']) continue;
->where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
switch ($timeType) {
case 'month':
$orderQuery->whereTime('commission_time', 'month');
break;
case 'week':
$orderQuery->whereTime('commission_time', 'week');
break;
case 'day':
$orderQuery->whereTime('commission_time', 'today');
break;
}
$orderCount = $orderQuery->count('distinct order_id');
} else {
$orderCount = RewardModel::where('agent_id', $item['user_id'])
->where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED)
->count('distinct order_id');
} }
$result[] = [ $result[] = [
'rank' => $index + 1, 'rank' => count($result) + 1,
'user_id' => $item['user_id'], 'user_id' => $item->agent_id,
'nickname' => $item['nickname'] ?: '匿名用户', 'nickname' => $user ? $user->nickname : '匿名用户',
'avatar' => $item['avatar'] ?: '', 'avatar' => $user ? $user->avatar : '',
'total_commission' => round($item['total_commission'], 2), 'total_commission' => round($item->total_commission, 2),
'order_count' => $orderCount 'order_count' => $item->order_count
]; ];
} }
...@@ -203,63 +184,65 @@ class Ranking extends Commission ...@@ -203,63 +184,65 @@ class Ranking extends Commission
} }
} }
// 查询当前用户信息 // 查询当前用户的佣金
$user = UserModel::get($userId); $query = RewardModel::where('agent_id', $userId)
if (!$user || $user->commission <= 0) { ->where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
return null;
switch ($timeType) {
case 'month':
$query->whereTime('commission_time', 'month');
break;
case 'week':
$query->whereTime('commission_time', 'week');
break;
case 'day':
$query->whereTime('commission_time', 'today');
break;
} }
$totalCommission = $user->commission; $totalCommission = $query->sum('commission');
$orderCount = $query->count('distinct order_id');
// 时间筛选时,检查用户是否有对应时间范围内的佣金记录
if ($timeType !== 'all') {
$query = RewardModel::where('agent_id', $userId)
->where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
switch ($timeType) {
case 'month':
$query->whereTime('commission_time', 'month');
break;
case 'week':
$query->whereTime('commission_time', 'week');
break;
case 'day':
$query->whereTime('commission_time', 'today');
break;
}
if (!$query->find()) { if ($totalCommission <= 0) {
return null; return null;
}
} }
// 查询订单数量 // 计算排名(排除已删除用户)
$orderQuery = RewardModel::where('agent_id', $userId) $rankWhereQuery = RewardModel::where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
->where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED); switch ($timeType) {
case 'month':
if ($timeType !== 'all') { $rankWhereQuery->whereTime('commission_time', 'month');
switch ($timeType) { break;
case 'month': case 'week':
$orderQuery->whereTime('commission_time', 'month'); $rankWhereQuery->whereTime('commission_time', 'week');
break; break;
case 'week': case 'day':
$orderQuery->whereTime('commission_time', 'week'); $rankWhereQuery->whereTime('commission_time', 'today');
break; break;
case 'day':
$orderQuery->whereTime('commission_time', 'today');
break;
}
} }
$orderCount = $orderQuery->count('distinct order_id'); // 只统计正常状态用户的佣金
$rank = $rankWhereQuery->group('agent_id')
->having('sum(commission) > ' . $totalCommission)
->select();
// 计算排名:统计佣金大于当前用户的人数 // 过滤掉已删除的用户
$rank = UserModel::where('commission', '>', $totalCommission) $validRank = 0;
->where('status', 'normal') if ($rank) {
->count() + 1; $rankIds = array_column($rank, 'agent_id');
$validUsers = UserModel::whereIn('id', $rankIds)
->where('status', 'normal')
->column('id');
$validRank = count($validUsers);
}
$user = UserModel::where('id', $userId)->where('status', 'normal')->find();
if (!$user) {
return null;
}
return [ return [
'rank' => $rank, 'rank' => $validRank + 1,
'user_id' => $userId, 'user_id' => $userId,
'nickname' => $user->nickname ?: '我', 'nickname' => $user->nickname ?: '我',
'avatar' => $user->avatar ?: '', 'avatar' => $user->avatar ?: '',
......
...@@ -34,8 +34,8 @@ class Company extends Common ...@@ -34,8 +34,8 @@ class Company extends Common
*/ */
public function store_list() public function store_list()
{ {
$page = $this->request->get('page') ?? 1; //页码 $page = $this->request->get('page') ?? 1; //页码
$limit = $this->request->get('limit') ?? 10; //每页显示条数 $limit = $this->request->get('limit') ?? 10; //每页显示条数
$storeList = Store::where('status', 'normal') $storeList = Store::where('status', 'normal')
->field('id,store_name,store_logo,detail_address,phone,longitude,latitude,store_intro') ->field('id,store_name,store_logo,detail_address,phone,longitude,latitude,store_intro')
->order('weigh', 'desc') ->order('weigh', 'desc')
...@@ -50,13 +50,15 @@ class Company extends Common ...@@ -50,13 +50,15 @@ class Company extends Common
*/ */
public function technology_list() public function technology_list()
{ {
$page = $this->request->get('page') ?? 1; //页码 $limit = $this->request->get('limit') ?? 3; //每页显示条数
$limit = $this->request->get('limit') ?? 3; //每页显示条数 if ($limit < 1) {
$limit = 1;
}
$technologyList = CompanyTechnology::where('status', 'normal') $technologyList = CompanyTechnology::where('status', 'normal')
->field('id,tech_name,tech_image,tech_desc') ->field('id,tech_name,tech_image,tech_desc')
->order('weigh', 'desc') ->order('weigh', 'desc')
->paginate(["page" => $page, "list_rows" => $limit]); ->limit($limit)
->select();
return $this->success('成功', $technologyList); return $this->success('成功', $technologyList);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment