178 lines
7.2 KiB
PHP
178 lines
7.2 KiB
PHP
<?php
|
||
|
||
namespace app\api\model;
|
||
|
||
use think\Db;
|
||
use think\Log;
|
||
use think\Model;
|
||
use think\Env;
|
||
|
||
class RoomHourRanking extends Model
|
||
{
|
||
|
||
//房间小时榜(更正版)
|
||
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 = 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];
|
||
}
|
||
|
||
$coin_exp = get_system_config_value('coin_charm_exp');
|
||
|
||
$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();
|
||
|
||
if($rankList){
|
||
foreach ($rankList as &$item){
|
||
$item['redpacket_status'] = Db::name('redpacket')->where(['room_id' => $item['room_id'], 'status' => ['<=', 1]])->count();
|
||
}
|
||
}
|
||
|
||
|
||
// 查询这个时间段内收礼的房间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' =>$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');
|
||
$now = date('Y-m-d H:i:s'); // 统一时间变量,避免重复生成
|
||
|
||
// 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
|
||
(?, ?, ?, ?, ?, ?) -- 新增部分的6个?
|
||
ON DUPLICATE KEY UPDATE
|
||
gift_amount = gift_amount + ?,
|
||
gift_count = gift_count + ?,
|
||
update_time = ?";
|
||
|
||
// 4. 构造参数数组:严格按SQL中?的顺序排列(核心!)
|
||
$params = [
|
||
// 新增部分的6个参数(对应VALUES后的6个?)
|
||
$room_id, // ?1: room_id
|
||
$stat_hour, // ?2: stat_hour
|
||
$amount, // ?3: 新增的gift_amount
|
||
$count, // ?4: 新增的gift_count
|
||
$now, // ?5: create_time
|
||
$now, // ?6: 新增的update_time
|
||
// 更新部分的3个参数(对应ON DUPLICATE KEY UPDATE后的3个?)
|
||
$amount, // ?7: 累加的gift_amount
|
||
$count, // ?8: 累加的gift_count
|
||
$now // ?9: 更新的update_time
|
||
];
|
||
|
||
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;
|
||
}
|
||
}
|
||
|
||
|
||
|
||
} |