diff --git a/application/api/model/GiveGiftBases.php b/application/api/model/GiveGiftBases.php index 546ce2be..6501257b 100644 --- a/application/api/model/GiveGiftBases.php +++ b/application/api/model/GiveGiftBases.php @@ -383,6 +383,41 @@ class GiveGiftBases extends Model return $result; } + /** + * 统计收礼用户 的送礼用户的分组和 + */ + + public function getToUserStatistics($params) + { + $where = []; + $options = []; + + // 时间范围 + if (!empty($params['start_time'])) { + $options['start_time'] = $params['start_time']; + } + if (!empty($params['end_time'])) { + $options['end_time'] = $params['end_time']; + } + if (!empty($params['from_id'])) { + $where['from_id'] = $params['from_id']; + } + if (!empty($params['gift_user'])) { + $where['gift_user'] = $params['gift_user']; + } + + // 分页参数 + $options['page'] = $params['page'] ?? 1; + $options['limit'] = min($params['limit'] ?? 20, 100); // 限制最大100条 + + $options['group_by'] = 'user_id'; + + // 查询数据 + $result = $this->giftModel->getToUserStatistics($where, $options); + + return $result; + } + /** * 获取房间内送礼排行 diff --git a/application/api/model/Room.php b/application/api/model/Room.php index fac0e225..29d58677 100644 --- a/application/api/model/Room.php +++ b/application/api/model/Room.php @@ -3411,13 +3411,27 @@ class Room extends Model } $clear_time = db::name('vs_room_user_charm')->where(['room_id' => $room_id, 'user_id' => $user_id]) ->order('id', 'desc')->value('clear_time') ?? 0; - $list = db::name('vs_give_gift')->alias('a') - ->join('user b', 'a.user_id=b.id') - ->field('a.user_id,sum(a.total_price) as total_price,b.nickname,b.avatar,b.user_code') - ->where(['a.from_id' => $room_id, 'a.gift_user' => $user_id, 'a.createtime' => ['>',$clear_time],'a.from' => 2]) - ->group('a.user_id')->select(); + + if($clear_time > 0){ + $stime = $clear_time; + }else{ + //今天的开始时间 + $stime = strtotime(date('Y-m-d')); + } + + $params['from_id'] = $room_id; + $params['start_time'] = $stime; + $params['end_time'] = time(); + $params['limit'] = 50; + $params['gift_user'] = $user_id; + $list = model('api/GiveGiftBases')->getToUserStatistics($params); + if($list){ foreach ($list as &$v){ + $v['total_price'] = $v['total']; + $v['nickname'] = db::name('user')->where(['id' => $v['user_id']])->value('nickname'); + $v['avatar'] = db::name('user')->where(['id' => $v['user_id']])->value('avatar'); + $v['user_code'] = db::name('user')->where(['id' => $v['user_id']])->value('user_code'); $v['charm'] = $v['total_price'] * get_system_config_value('coin_charm_exp'); $v['icon'][0] = model('UserData')->user_wealth_icon($v['user_id']);//财富图标 $v['icon'][1] = model('UserData')->user_charm_icon($v['user_id']);//魅力图标 diff --git a/application/api/model/UserWallet.php b/application/api/model/UserWallet.php index ac8d4314..67aa4891 100644 --- a/application/api/model/UserWallet.php +++ b/application/api/model/UserWallet.php @@ -118,12 +118,9 @@ class UserWallet extends Model //从缓存中获取 $exchange_user = Cache::get('exchange_user_' . $uid); if($exchange_user){ - //获取缓存的过期剩余 时间 - $time = ceil((Cache::get('exchange_user_end_time_' . $uid) - time() ) /60); - return ['code' => 0, 'msg' => '请'.$time.'分钟后再次兑换', 'data' => null]; + return ['code' => 0, 'msg' => '手速太快了', 'data' => null]; }else{ - Cache::set('exchange_user_' . $uid, 1, 5 * 60); - Cache::set('exchange_user_end_time_' . $uid, time() + 5 * 60 , 5 * 60 + 1); + Cache::set('exchange_user_' . $uid, 1, 5); } if(empty($earnings_num)){ return ['code' => 0, 'msg' => '请输入需要兑换的钻石数量', 'data' => null]; diff --git a/application/common/model/GiveGiftBase.php b/application/common/model/GiveGiftBase.php index 21c85a70..396dabb7 100644 --- a/application/common/model/GiveGiftBase.php +++ b/application/common/model/GiveGiftBase.php @@ -555,4 +555,99 @@ class GiveGiftBase extends Model return []; } } + + + /** + * 统计收礼用户 的送礼用户的分组和 + * @param array $where 查询条件 + * @param array $options 统计选项 + * @return array + */ + public function getToUserStatistics($where = [], $options = []) + { + $defaultOptions = [ + 'group_by' => null, // 分组字段 + 'start_time' => null, + 'end_time' => null, + ]; + + $options = array_merge($defaultOptions, $options); + + // 获取需要查询的表 + $tables = GiftTableManager::getTablesByTimeRange( + $options['start_time'], + $options['end_time'] + ); + + if (empty($tables)) { + return []; + } + + // 构建查询条件 + $conditions = $this->buildQueryConditions($where); + + $unionSql = ''; + $params = []; + + // 构建统计SQL + $fields = 'SUM(total_price) as total_price'; + if ($options['group_by']) { + $fields .= ", {$options['group_by']}"; + } + + foreach ($tables as $table) { + $sql = "SELECT {$fields} FROM `{$table}` WHERE 1=1"; + + foreach ($conditions as $field => $condition) { + if (is_array($condition) && count($condition) == 2) { + $operator = $condition[0]; + $value = $condition[1]; + $sql .= " AND `{$field}` {$operator} ?"; + $params[] = $value; + } + } + + // 时间范围 + if ($options['start_time']) { + $sql .= " AND createtime >= ?"; + $params[] = $options['start_time']; + } + + if ($options['end_time']) { + $sql .= " AND createtime <= ?"; + $params[] = $options['end_time']; + } + + if ($options['group_by']) { + $sql .= " GROUP BY {$options['group_by']}"; + } + + if ($unionSql) { + $unionSql .= " UNION ALL "; + } + $unionSql .= "(" . $sql . ")"; + } + + // 最终统计 + if ($options['group_by']) { + $finalSql = "SELECT {$options['group_by']}, + SUM(total_price) * 10 as total + FROM ({$unionSql}) as tmp + GROUP BY {$options['group_by']} + ORDER BY total desc"; + } else { + $finalSql = "SELECT SUM(total_number) as total_number, + SUM(total_price) as total, + SUM(total_count) as total_count + FROM ({$unionSql}) as tmp"; + } + + try { + $result = Db::query($finalSql, $params); + return $options['group_by'] ? $result : ($result[0] ?? []); + } catch (\Exception $e) { + Log::error("统计送礼数据失败: " . $e->getMessage()); + return []; + } + } } \ No newline at end of file