小时榜

This commit is contained in:
2026-01-13 14:12:11 +08:00
parent 7f266b4b15
commit 401c3f8842
3 changed files with 139 additions and 118 deletions

View File

@@ -3,80 +3,20 @@
namespace app\api\model;
use think\Db;
use think\Log;
use think\Model;
use think\Env;
class RoomHourRanking extends Model
{
//房间小时榜
public function room_hour_rankings($page, $page_limit,$start_time = null, $end_time = null)
{
//当前小时开始时间
if($start_time == null){
$start_time = strtotime(date('Y-m-d H:00:00'));
}
//结束时间
if($end_time == null){
$end_time = strtotime(date('Y-m-d H:59:59'));
}
//判断是否开启
$open_time = db::name('vs_hour_ranking_config')->where('id', 1)->value('open_time');
if ($open_time == 0) {
return ['code' => 0, 'msg' => '排行榜暂未开启', 'data' => null];
}
//是否开启巡乐会
$is_open_xlh = db::name('vs_hour_ranking_config')->where('id', 1)->value('is_open_xlh');
// 更进一步的优化版本:
$subQuery = Db::name('vs_give_gift')
->where('from', 2)
->whereBetween('createtime', [$start_time, $end_time])
->buildSql();
$profit = db::name('vs_room')->alias('a')
->join([$subQuery => 'b'], 'a.id = b.from_id', 'left')
->join('vs_room_label c', 'a.label_id = c.id','left')
->field('a.id as room_id,a.user_id,a.room_name,a.label_id,a.room_cover,IFNULL(SUM(b.total_price), 0) as total_price,c.label_icon')
->where('a.room_status', 1)
->where('a.apply_status', 2)
->where('a.type_id', '<>', 6)
->group('a.id')
->order('total_price', 'desc')
->page($page, $page_limit)
->select();
if($profit){
foreach ($profit as &$v) {
$v['total_price'] = $v['total_price'] * get_system_config_value('coin_charm_exp');
if($v['room_id'] > 0 && $is_open_xlh == 1){
$xlh_status = model('BlindBoxTurntableGift')->get_user_xlh_info($v['room_id']);
$v['xlh_status'] = $xlh_status['xlh_status'];
}else{
$v['xlh_status'] = 0;
}
//查询房间是否有红包
if($v['room_id'] > 0){
$red_pack_status = Db::name('redpacket')->where(['room_id' => $v['room_id'], 'status' => ['<=',1]])->count();
$v['redpacket_status'] = $red_pack_status;
}else{
$v['redpacket_status'] = 0;
}
}
}
//当前小时开始时间 和结束时间 00:00-00:59 这样的格式
$time_range = date('H:') . '00-' . date('H:'). '59';
return ['code' => 1, 'msg' => '获取成功', 'data' => ['time_range' => $time_range, 'lists' =>$profit]];
}
//房间小时榜(更正版)
public function room_hour_ranking($page, $page_limit,$start_time = null, $end_time = null)
{
//当前小时开始时间
if($start_time == null){
$start_time = strtotime(date('Y-m-d H:00:00'));
// $start_time = strtotime(date('Y-m-d H:00:00'));
$start_time = date('Y-m-d H:00:00');
}
//结束时间
@@ -90,66 +30,145 @@ class RoomHourRanking extends Model
return ['code' => 0, 'msg' => '排行榜暂未开启', 'data' => null];
}
//是否开启巡乐会
$is_open_xlh = db::name('vs_hour_ranking_config')->where('id', 1)->value('is_open_xlh');
// 查询这个时间段内收礼的房间ID集合并实现分页
$room_query = Db::name('vs_give_gift')
->where(['from' => 2, 'from_id' => ['>',0]])
->whereBetween('createtime', [$start_time, $end_time])
->group('from_id')
->field('from_id, SUM(total_price) as total_price');
$coin_exp = get_system_config_value('coin_charm_exp');
// 应用分页限制
$room_ids_with_price = $room_query
->order('total_price', 'desc')
->limit(($page - 1) * $page_limit, $page_limit)
$rankList = Db::name('room_gift_hourly_sum')->alias('a')
->join('vs_room b', 'a.room_id = b.id', 'left') // 关联房间表取名称/封面
->join('vs_room_label c', 'b.label_id = c.id','left')
->field('a.room_id, a.gift_amount * '.$coin_exp.' as total_price, a.gift_count, b.room_name, b.room_cover, b.label_id, c.label_icon')
->where(['a.stat_hour' => $start_time,'a.room_id' => ['>',0]])
->order('total_price DESC')
->page($page, $page_limit)
->select();
$profit = [];
if($room_ids_with_price){
foreach ($room_ids_with_price as $k => $item){
$room_id = $item['from_id'];
$profit[$k] = db::name('vs_room')->alias('a')
->join('vs_give_gift b', 'a.id = b.from_id', 'left')
->join('vs_room_label c', 'a.label_id = c.id','left')
->field('a.id as room_id,a.room_name,a.label_id,a.room_cover,SUM(b.total_price) as total_price,c.label_icon')
->where('a.type_id', '<>', 6)
->where('a.id', $room_id)
->whereBetween('b.createtime', [$start_time, $end_time])
->find();
if(!empty($profit[$k])) {
$profit[$k]['total_price'] = $profit[$k]['total_price'] * get_system_config_value('coin_charm_exp');
// 查询巡乐会状态
if($room_id > 0 && $is_open_xlh == 1){
$xlh_status = model('BlindBoxTurntableGift')->get_user_xlh_info($room_id);
$profit[$k]['xlh_status'] = $xlh_status['xlh_status'] ?? 0;
} else {
$profit[$k]['xlh_status'] = 0;
}
// 查询房间红包状态
if($room_id > 0){
$red_pack_status = Db::name('redpacket')->where(['room_id' => $room_id, 'status' => ['<=', 1]])->count();
$profit[$k]['redpacket_status'] = $red_pack_status;
} else {
$profit[$k]['redpacket_status'] = 0;
}
} else {
unset($profit[$k]);
}
if($rankList){
foreach ($rankList as &$item){
$item['redpacket_status'] = Db::name('redpacket')->where(['room_id' => $item['room_id'], 'status' => ['<=', 1]])->count();
}
}
// 过滤掉空值并重新索引数组
if($profit){
$profit = array_values(array_filter($profit));
}
// 查询这个时间段内收礼的房间ID集合并实现分页
// $room_query = Db::name('vs_give_gift')
// ->where(['from' => 2, 'from_id' => ['>',0]])
// ->whereBetween('createtime', [$start_time, $end_time])
// ->group('from_id')
// ->field('from_id, SUM(total_price) as total_price');
//
// // 应用分页限制
// $room_ids_with_price = $room_query
// ->order('total_price', 'desc')
// ->limit(($page - 1) * $page_limit, $page_limit)
// ->select();
//
// $profit = [];
// if($room_ids_with_price){
// foreach ($room_ids_with_price as $k => $item){
// $room_id = $item['from_id'];
// $profit[$k] = db::name('vs_room')->alias('a')
// ->join('vs_give_gift b', 'a.id = b.from_id', 'left')
// ->join('vs_room_label c', 'a.label_id = c.id','left')
// ->field('a.id as room_id,a.room_name,a.label_id,a.room_cover,SUM(b.total_price) as total_price,c.label_icon')
// ->where('a.type_id', '<>', 6)
// ->where('a.id', $room_id)
// ->whereBetween('b.createtime', [$start_time, $end_time])
// ->find();
//
// if(!empty($profit[$k])) {
// $profit[$k]['total_price'] = $profit[$k]['total_price'] * get_system_config_value('coin_charm_exp');
//
// // 查询巡乐会状态
// if($room_id > 0 && $is_open_xlh == 1){
// $xlh_status = model('BlindBoxTurntableGift')->get_user_xlh_info($room_id);
// $profit[$k]['xlh_status'] = $xlh_status['xlh_status'] ?? 0;
// } else {
// $profit[$k]['xlh_status'] = 0;
// }
//
// // 查询房间红包状态
// if($room_id > 0){
// $red_pack_status = Db::name('redpacket')->where(['room_id' => $room_id, 'status' => ['<=', 1]])->count();
// $profit[$k]['redpacket_status'] = $red_pack_status;
// } else {
// $profit[$k]['redpacket_status'] = 0;
// }
// } else {
// unset($profit[$k]);
// }
//
// }
// }
//
// // 过滤掉空值并重新索引数组
// if($profit){
// $profit = array_values(array_filter($profit));
// }
//当前小时开始时间 和结束时间 00:00-00:59 这样的格式
$time_range = date('H:') . '00-' . date('H:'). '59';
return ['code' => 1, 'msg' => '获取成功', 'data' => ['time_range' => $time_range, 'lists' =>$profit]];
return ['code' => 1, 'msg' => '获取成功', 'data' => ['time_range' => $time_range, 'lists' =>$rankList]];
}
/**
* 核心方法原生SQL实现每小时送礼流水累加适配无duplicate()的TP/FastAdmin版本
* @param int $room_id 房间ID
* @param float $amount 本次送礼金额如10.00
* @param int $count 本次送礼次数默认1
* @return bool 操作是否成功
*/
public function addGiftHourlySum($room_id, $amount, $count = 1)
{
// 1. 生成当前小时标识(固定为整点,保证每小时数据独立)
$stat_hour = date('Y-m-d H:00:00');
// 2. 获取数据库表前缀自动适配FastAdmin配置避免硬编码
$tablePrefix = Env::get('database.prefix', 'fa_');
$tableName = $tablePrefix . 'room_gift_hourly_sum';
// 3. 构造原生SQL关键每个参数名唯一避免重复
$sql = "INSERT INTO {$tableName}
(room_id, stat_hour, gift_amount, gift_count, create_time, update_time)
VALUES
(:room_id, :stat_hour, :gift_amount_insert, :gift_count_insert, :create_time, :update_time_insert)
ON DUPLICATE KEY UPDATE
gift_amount = gift_amount + :gift_amount_add,
gift_count = gift_count + :gift_count_add,
update_time = :update_time_update";
// 4. 绑定参数关键参数名与SQL完全一致无重复、无拼写错误
$params = [
':room_id' => $room_id,
':stat_hour' => $stat_hour,
':gift_amount_insert' => $amount, // 新增时的初始金额(唯一参数名)
':gift_count_insert' => $count, // 新增时的初始次数(唯一参数名)
':create_time' => date('Y-m-d H:i:s'),
':update_time_insert' => date('Y-m-d H:i:s'), // 新增时的更新时间
':gift_amount_add' => $amount, // 更新时累加的金额(唯一参数名)
':gift_count_add' => $count, // 更新时累加的次数(唯一参数名)
':update_time_update' => date('Y-m-d H:i:s') // 更新时的更新时间
];
try {
// 5. 执行原生SQL
$result = Db::execute($sql, $params);
// 执行成功返回影响行数(新增=1更新=2失败返回false
if ($result === false) {
// 6. 失败则记录异常日志
Log::error("送礼流水累加失败房间ID{$room_id} | 金额:{$amount}");
}
return true;
} catch (\Exception $e) {
// 记录详细错误日志含SQL和参数方便排查
Log::error("送礼流水累加失败:{$e->getMessage()}
| 房间ID{$room_id} | 金额:{$amount}
| SQL{$sql}
| 参数:" . json_encode($params));
return false;
}
}
}

View File

@@ -222,7 +222,7 @@ class RoomSong extends Model
//查询当前用户还有几首没有唱
$user_song = db::name('vs_room_song')->where(['user_id' => $user_id,'room_id' => $room_id,'times' => $data['times'],'status' => 1])->count();
if($user_song >= 3){
return ['code'=>0,'msg'=>'已经点了三首歌曲了!演唱后再点','data'=>null];
return ['code'=>0,'msg'=>'已经点了三首歌曲了!演唱后再点','data'=>null];
}
$data['room_id'] = $room_id;

View File

@@ -1434,7 +1434,9 @@ class SendGift extends Model
'createtime' => time(),
];
GiftQueue::push($data);
GiftQueue::push($data);//推入队列
//记录小时榜
model('api/RoomHourRanking')->addGiftHourlySum($from_id,$gift_price);
return $data['id'];
}