分表数据
This commit is contained in:
301
application/api/controller/GiftBase.php
Normal file
301
application/api/controller/GiftBase.php
Normal file
@@ -0,0 +1,301 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\controller\BaseCom;
|
||||
use app\common\model\GiveGiftBase;
|
||||
use think\Cache;
|
||||
use think\Log;
|
||||
|
||||
class GiftBase extends BaseCom
|
||||
{
|
||||
protected $noNeedLogin = [];
|
||||
protected $noNeedRight = ['*'];
|
||||
|
||||
protected $giftModel;
|
||||
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->giftModel = new GiveGiftBase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取送礼记录
|
||||
*/
|
||||
public function getGiftRecords()
|
||||
{
|
||||
$params = $this->request->param();
|
||||
|
||||
$where = [];
|
||||
$options = [];
|
||||
|
||||
// 构建查询条件
|
||||
if (!empty($params['from_id'])) {
|
||||
$where['from_id'] = $params['from_id'];
|
||||
}
|
||||
if (!empty($params['user_id'])) {
|
||||
$where['user_id'] = $params['user_id'];
|
||||
}
|
||||
if (!empty($params['gift_user'])) {
|
||||
$where['gift_user'] = $params['gift_user'];
|
||||
}
|
||||
if (!empty($params['gift_id'])) {
|
||||
$where['gift_id'] = $params['gift_id'];
|
||||
}
|
||||
if (!empty($params['type'])) {
|
||||
$where['type'] = $params['type'];
|
||||
}
|
||||
if (!empty($params['from'])) {
|
||||
$where['from'] = $params['from'];
|
||||
}
|
||||
if (!empty($params['gift_type'])) {
|
||||
$where['gift_type'] = $params['gift_type'];
|
||||
}
|
||||
|
||||
// 时间范围
|
||||
if (!empty($params['start_time'])) {
|
||||
$options['start_time'] = intval($params['start_time']);
|
||||
}
|
||||
if (!empty($params['end_time'])) {
|
||||
$options['end_time'] = intval($params['end_time']);
|
||||
}
|
||||
|
||||
// 分页参数
|
||||
$options['page'] = $params['page'] ?? 1;
|
||||
$options['limit'] = min($params['limit'] ?? 20, 100); // 限制最大100条
|
||||
|
||||
// 排序
|
||||
if (!empty($params['order'])) {
|
||||
$options['order'] = $params['order'];
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
$result = $this->giftModel->getGiftRecords($where, $options);
|
||||
|
||||
Log::info("查询送礼记录,条件: " . json_encode($where) . ", 结果数: " . count($result['data']));
|
||||
|
||||
$this->success('获取成功', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 房间送礼统计
|
||||
*/
|
||||
public function getRoomStatistics()
|
||||
{
|
||||
$fromId = $this->request->param('from_id/d', 0);
|
||||
$startTime = $this->request->param('start_time/d', strtotime('-7 days'));
|
||||
$endTime = $this->request->param('end_time/d', time());
|
||||
|
||||
if (!$fromId) {
|
||||
$this->error('房间ID不能为空');
|
||||
}
|
||||
|
||||
// 尝试从缓存获取
|
||||
$cacheKey = "gift:room:stats:{$fromId}:" . date('Ymd', $startTime) . '-' . date('Ymd', $endTime);
|
||||
$cachedData = Cache::get($cacheKey);
|
||||
|
||||
if ($cachedData !== false) {
|
||||
Log::info("从缓存获取房间统计: {$cacheKey}");
|
||||
$this->success('获取成功', $cachedData);
|
||||
}
|
||||
|
||||
$where = ['from_id' => $fromId];
|
||||
$options = [
|
||||
'start_time' => $startTime,
|
||||
'end_time' => $endTime,
|
||||
];
|
||||
|
||||
// 统计房间总数据
|
||||
$totalStats = $this->giftModel->getGiftStatistics($where, $options);
|
||||
|
||||
// 按送礼人分组统计
|
||||
$userStats = $this->giftModel->getGiftStatistics($where, array_merge($options, [
|
||||
'group_by' => 'user_id'
|
||||
]));
|
||||
|
||||
// 按收礼人分组统计
|
||||
$receiverStats = $this->giftModel->getGiftStatistics($where, array_merge($options, [
|
||||
'group_by' => 'gift_user'
|
||||
]));
|
||||
|
||||
$result = [
|
||||
'room_id' => $fromId,
|
||||
'time_range' => [
|
||||
'start_time' => $startTime,
|
||||
'end_time' => $endTime,
|
||||
'start_date' => date('Y-m-d H:i:s', $startTime),
|
||||
'end_date' => date('Y-m-d H:i:s', $endTime),
|
||||
],
|
||||
'total_stats' => $totalStats,
|
||||
'user_stats' => $userStats,
|
||||
'receiver_stats' => $receiverStats,
|
||||
];
|
||||
|
||||
// 缓存5分钟
|
||||
Cache::set($cacheKey, $result, 300);
|
||||
|
||||
Log::info("房间统计计算完成: {$fromId}, 送礼人数: " . count($userStats) . ", 收礼人数: " . count($receiverStats));
|
||||
|
||||
$this->success('获取成功', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户送礼统计
|
||||
*/
|
||||
public function getUserStatistics()
|
||||
{
|
||||
$userId = $this->request->param('user_id/d', 0);
|
||||
$startTime = $this->request->param('start_time/d', strtotime('-30 days'));
|
||||
$endTime = $this->request->param('end_time/d', time());
|
||||
|
||||
if (!$userId) {
|
||||
$this->error('用户ID不能为空');
|
||||
}
|
||||
|
||||
$cacheKey = "gift:user:stats:{$userId}:" . date('Ymd', $startTime) . '-' . date('Ymd', $endTime);
|
||||
$cachedData = Cache::get($cacheKey);
|
||||
|
||||
if ($cachedData !== false) {
|
||||
Log::info("从缓存获取用户统计: {$cacheKey}");
|
||||
$this->success('获取成功', $cachedData);
|
||||
}
|
||||
|
||||
$where = ['user_id' => $userId];
|
||||
$options = [
|
||||
'start_time' => $startTime,
|
||||
'end_time' => $endTime,
|
||||
];
|
||||
|
||||
// 用户总送礼统计
|
||||
$totalStats = $this->giftModel->getGiftStatistics($where, $options);
|
||||
|
||||
// 按房间分组统计
|
||||
$roomStats = $this->giftModel->getGiftStatistics($where, array_merge($options, [
|
||||
'group_by' => 'from_id'
|
||||
]));
|
||||
|
||||
// 按收礼人分组统计
|
||||
$receiverStats = $this->giftModel->getGiftStatistics($where, array_merge($options, [
|
||||
'group_by' => 'gift_user'
|
||||
]));
|
||||
|
||||
$result = [
|
||||
'user_id' => $userId,
|
||||
'time_range' => [
|
||||
'start_time' => $startTime,
|
||||
'end_time' => $endTime,
|
||||
'start_date' => date('Y-m-d H:i:s', $startTime),
|
||||
'end_date' => date('Y-m-d H:i:s', $endTime),
|
||||
],
|
||||
'total_stats' => $totalStats,
|
||||
'room_stats' => $roomStats,
|
||||
'receiver_stats' => $receiverStats,
|
||||
];
|
||||
|
||||
// 缓存5分钟
|
||||
Cache::set($cacheKey, $result, 300);
|
||||
|
||||
Log::info("用户统计计算完成: {$userId}, 房间数: " . count($roomStats) . ", 收礼人数: " . count($receiverStats));
|
||||
|
||||
$this->success('获取成功', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 收礼统计
|
||||
*/
|
||||
public function getReceiveStatistics()
|
||||
{
|
||||
$giftUser = $this->request->param('gift_user/d', 0);
|
||||
$startTime = $this->request->param('start_time/d', strtotime('-30 days'));
|
||||
$endTime = $this->request->param('end_time/d', time());
|
||||
|
||||
if (!$giftUser) {
|
||||
$this->error('用户ID不能为空');
|
||||
}
|
||||
|
||||
$cacheKey = "gift:receive:stats:{$giftUser}:" . date('Ymd', $startTime) . '-' . date('Ymd', $endTime);
|
||||
$cachedData = Cache::get($cacheKey);
|
||||
|
||||
if ($cachedData !== false) {
|
||||
Log::info("从缓存获取收礼统计: {$cacheKey}");
|
||||
$this->success('获取成功', $cachedData);
|
||||
}
|
||||
|
||||
$where = ['gift_user' => $giftUser];
|
||||
$options = [
|
||||
'start_time' => $startTime,
|
||||
'end_time' => $endTime,
|
||||
];
|
||||
|
||||
// 总收礼统计
|
||||
$totalStats = $this->giftModel->getGiftStatistics($where, $options);
|
||||
|
||||
// 按房间分组统计
|
||||
$roomStats = $this->giftModel->getGiftStatistics($where, array_merge($options, [
|
||||
'group_by' => 'from_id'
|
||||
]));
|
||||
|
||||
// 按送礼人分组统计
|
||||
$giverStats = $this->giftModel->getGiftStatistics($where, array_merge($options, [
|
||||
'group_by' => 'user_id'
|
||||
]));
|
||||
|
||||
$result = [
|
||||
'gift_user' => $giftUser,
|
||||
'time_range' => [
|
||||
'start_time' => $startTime,
|
||||
'end_time' => $endTime,
|
||||
'start_date' => date('Y-m-d H:i:s', $startTime),
|
||||
'end_date' => date('Y-m-d H:i:s', $endTime),
|
||||
],
|
||||
'total_stats' => $totalStats,
|
||||
'room_stats' => $roomStats,
|
||||
'giver_stats' => $giverStats,
|
||||
];
|
||||
|
||||
// 缓存5分钟
|
||||
Cache::set($cacheKey, $result, 300);
|
||||
|
||||
Log::info("收礼统计计算完成: {$giftUser}, 房间数: " . count($roomStats) . ", 送礼人数: " . count($giverStats));
|
||||
|
||||
$this->success('获取成功', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取送礼趋势
|
||||
*/
|
||||
public function getGiftTrend()
|
||||
{
|
||||
$params = $this->request->param();
|
||||
|
||||
$startTime = $params['start_time'] ?? strtotime('-7 days');
|
||||
$endTime = $params['end_time'] ?? time();
|
||||
$interval = $params['interval'] ?? 'day';
|
||||
|
||||
if (!in_array($interval, ['day', 'week', 'month'])) {
|
||||
$interval = 'day';
|
||||
}
|
||||
|
||||
$cacheKey = "gift:trend:" . date('Ymd', $startTime) . '-' . date('Ymd', $endTime) . ":{$interval}";
|
||||
$cachedData = Cache::get($cacheKey);
|
||||
|
||||
if ($cachedData !== false) {
|
||||
$this->success('获取成功', $cachedData);
|
||||
}
|
||||
|
||||
$trendData = $this->giftModel->getTrendStatistics($startTime, $endTime, $interval);
|
||||
|
||||
// 缓存10分钟
|
||||
Cache::set($cacheKey, $trendData, 600);
|
||||
|
||||
$this->success('获取成功', [
|
||||
'trend_data' => $trendData,
|
||||
'interval' => $interval,
|
||||
'time_range' => [
|
||||
'start_time' => $startTime,
|
||||
'end_time' => $endTime
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user