Commit 8b06f02b authored by 龚双喜's avatar 龚双喜

增加商品批量收藏和佣金、推广人排行

parent 0da9431f
<?php
namespace addons\shopro\controller\commission;
use app\admin\model\shopro\commission\Agent as AgentModel;
use app\admin\model\shopro\commission\Reward as RewardModel;
use app\admin\model\shopro\user\User as UserModel;
use think\Db;
class Ranking extends Commission
{
protected $noNeedLogin = [];
protected $noNeedRight = ['*'];
/**
* 佣金排行榜
* 按已结算佣金金额排序
*/
public function commission()
{
$limit = $this->request->param('limit', 20);
$timeType = $this->request->param('time_type', 'all'); // all|month|week|day
$query = RewardModel::field([
'agent_id',
'sum(commission) as total_commission',
'count(distinct order_id) as order_count'
])
->where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED)
->group('agent_id');
// 时间筛选
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('total_commission', 'desc')
->limit($limit)
->select();
if (empty($rankings)) {
$this->success('获取成功', []);
}
$agentIds = array_column($rankings, 'agent_id');
// 获取用户信息
$users = UserModel::whereIn('id', $agentIds)
->field('id, nickname, avatar')
->select();
$userMap = [];
if (is_array($users)) {
$userMap = array_column($users, null, 'id');
} else {
$userMap = $users->column(null, 'id');
}
$result = [];
foreach ($rankings as $index => $item) {
$user = $userMap[$item->agent_id] ?? null;
$result[] = [
'rank' => $index + 1,
'user_id' => $item->agent_id,
'nickname' => $user ? $user->nickname : '匿名用户', //string_hide($user->nickname, 5) : '匿名用户',
'avatar' => $user ? $user->avatar : '',
'total_commission' => round($item->total_commission, 2),
'order_count' => $item->order_count
];
}
// 获取当前用户排名
$currentUser = auth_user();
$currentRank = null;
if ($currentUser) {
$currentRank = $this->getCurrentUserCommissionRank($currentUser->id, $timeType, $result);
}
$this->success('获取成功', [
'list' => $result,
'current_user_rank' => $currentRank
]);
}
/**
* 推广人排行榜
* 按推广人数排序
*/
public function promoter()
{
$limit = $this->request->param('limit', 20);
$timeType = $this->request->param('time_type', 'all'); // all|month|week|day
$query = UserModel::field([
'parent_user_id',
'count(*) as invite_count'
])
->where('parent_user_id', '>', 0)
->where('status', 'normal')
->group('parent_user_id');
// 时间筛选
switch ($timeType) {
case 'month':
$query->whereTime('jointime', 'month');
break;
case 'week':
$query->whereTime('jointime', 'week');
break;
case 'day':
$query->whereTime('jointime', 'today');
break;
}
$rankings = $query->order('invite_count', 'desc')
->limit($limit)
->select();
if (empty($rankings)) {
$this->success('获取成功', []);
}
$parentIds = array_column($rankings, 'parent_user_id');
// 获取推广人信息
$users = UserModel::whereIn('id', $parentIds)
->field('id, nickname, avatar')
->select();
$userMap = [];
if (is_array($users)) {
$userMap = array_column($users, null, 'id');
} else {
$userMap = $users->column(null, 'id');
}
$result = [];
foreach ($rankings as $index => $item) {
$user = $userMap[$item->parent_user_id] ?? null;
$result[] = [
'rank' => $index + 1,
'user_id' => $item->parent_user_id,
'nickname' => $user ? $user->nickname : '匿名用户',
'avatar' => $user ? $user->avatar : '',
'invite_count' => $item->invite_count
];
}
// 获取当前用户排名
$currentUser = auth_user();
$currentRank = null;
if ($currentUser) {
$currentRank = $this->getCurrentUserPromoterRank($currentUser->id, $timeType, $result);
}
$this->success('获取成功', [
'list' => $result,
'current_user_rank' => $currentRank
]);
}
/**
* 获取当前用户佣金排名
*/
private function getCurrentUserCommissionRank($userId, $timeType, $existingList)
{
// 先检查是否在已有列表中
foreach ($existingList as $item) {
if ($item['user_id'] == $userId) {
return $item;
}
}
// 查询当前用户的佣金
$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;
}
$totalCommission = $query->sum('commission');
$orderCount = $query->count('distinct order_id');
if ($totalCommission <= 0) {
return null;
}
// 计算排名
$rankWhereQuery = RewardModel::where('status', RewardModel::COMMISSION_REWARD_STATUS_ACCOUNTED);
switch ($timeType) {
case 'month':
$rankWhereQuery->whereTime('commission_time', 'month');
break;
case 'week':
$rankWhereQuery->whereTime('commission_time', 'week');
break;
case 'day':
$rankWhereQuery->whereTime('commission_time', 'today');
break;
}
$rank = $rankWhereQuery->group('agent_id')
->having('sum(commission) > ' . $totalCommission)
->count() + 1;
$user = UserModel::get($userId);
return [
'rank' => $rank,
'user_id' => $userId,
'nickname' => $user ? $user->nickname : '我',
'avatar' => $user ? $user->avatar : '',
'total_commission' => round($totalCommission, 2),
'order_count' => $orderCount,
'is_out_list' => true
];
}
/**
* 获取当前用户推广排名
*/
private function getCurrentUserPromoterRank($userId, $timeType, $existingList)
{
// 先检查是否在已有列表中
foreach ($existingList as $item) {
if ($item['user_id'] == $userId) {
return $item;
}
}
// 查询当前用户的推广人数
$query = UserModel::where('parent_user_id', $userId)
->where('status', 'normal');
switch ($timeType) {
case 'month':
$query->whereTime('jointime', 'month');
break;
case 'week':
$query->whereTime('jointime', 'week');
break;
case 'day':
$query->whereTime('jointime', 'today');
break;
}
$inviteCount = $query->count();
if ($inviteCount <= 0) {
return null;
}
// 计算排名
$rankWhereQuery = UserModel::where('parent_user_id', '>', 0)
->where('status', 'normal');
switch ($timeType) {
case 'month':
$rankWhereQuery->whereTime('jointime', 'month');
break;
case 'week':
$rankWhereQuery->whereTime('jointime', 'week');
break;
case 'day':
$rankWhereQuery->whereTime('jointime', 'today');
break;
}
$rank = $rankWhereQuery->group('parent_user_id')
->having('count(*) > ' . $inviteCount)
->count() + 1;
$user = UserModel::get($userId);
return [
'rank' => $rank,
'user_id' => $userId,
'nickname' => $user ? $user->nickname : '我',
'avatar' => $user ? $user->avatar : '',
'invite_count' => $inviteCount,
'is_out_list' => true
];
}
}
...@@ -36,6 +36,69 @@ class GoodsLog extends Common ...@@ -36,6 +36,69 @@ class GoodsLog extends Common
/** /**
* 批量收藏商品
*
* @return void
*/
public function batchFavorite()
{
if (!$this->request->isPost()) {
$this->error('请求方式错误');
}
$user = auth_user();
$goodsIds = $this->request->param('goods_ids/a');
if (empty($goodsIds) || !is_array($goodsIds)) {
$this->error('请选择要收藏的商品');
}
// 过滤已存在的收藏
$existingFavorites = UserGoodsLogModel::favorite()
->whereIn('goods_id', $goodsIds)
->where('user_id', $user->id)
->column('goods_id');
$newGoodsIds = array_diff($goodsIds, $existingFavorites);
if (empty($newGoodsIds)) {
$this->success('所选商品已收藏', [
'total' => count($goodsIds),
'success' => 0,
'exists' => count($existingFavorites),
'failed' => 0
]);
}
// 验证商品是否存在且上架
$validGoods = Goods::show()->whereIn('id', $newGoodsIds)->column('id');
$invalidGoodsIds = array_diff($newGoodsIds, $validGoods);
$successCount = 0;
$now = time();
foreach ($validGoods as $goodsId) {
$log = new UserGoodsLogModel();
$log->goods_id = $goodsId;
$log->user_id = $user->id;
$log->type = 'favorite';
$log->createtime = $now;
$log->updatetime = $now;
$log->save();
$successCount++;
}
$this->success('收藏成功', [
'total' => count($goodsIds),
'success' => $successCount,
'exists' => count($existingFavorites),
'failed' => count($invalidGoodsIds),
'failed_goods_ids' => array_values($invalidGoodsIds)
]);
}
/**
* 收藏/取消收藏 * 收藏/取消收藏
* *
* @param Request $request * @param Request $request
......
...@@ -5,7 +5,7 @@ return [ ...@@ -5,7 +5,7 @@ return [
'default' => 'default', // 默认的队列名称 'default' => 'default', // 默认的队列名称
'host' => '127.0.0.1', // redis 主机ip 'host' => '127.0.0.1', // redis 主机ip
'port' => 6379, // redis 端口 'port' => 6379, // redis 端口
'password' => '123456', // redis 密码 'password' => '', // redis 密码
'select' => 0, // 使用哪一个 db,默认为 db0 'select' => 0, // 使用哪一个 db,默认为 db0
'timeout' => 0, // redis连接的超时时间 'timeout' => 0, // redis连接的超时时间
'persistent' => false, 'persistent' => false,
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
return [ return [
'host' => '127.0.0.1', 'host' => '127.0.0.1',
'password' => '123456', 'password' => '',
'port' => '6379', 'port' => '6379',
'select' => '1', 'select' => '1',
'timeout' => '0', 'timeout' => '0',
......
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