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'); // 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; } } }