分表数据
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
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
179
application/api/controller/GiveGiftBase.php
Normal file
179
application/api/controller/GiveGiftBase.php
Normal file
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\common\library\GiftQueue;
|
||||
use think\Log;
|
||||
use app\common\controller\BaseCom;
|
||||
|
||||
class GiveGiftBase extends BaseCom
|
||||
{
|
||||
protected $noNeedLogin = [];
|
||||
protected $noNeedRight = ['*'];
|
||||
|
||||
/**
|
||||
* 送礼接口
|
||||
*/
|
||||
public function give()
|
||||
{
|
||||
$params = $this->request->param();
|
||||
|
||||
// 验证必要参数
|
||||
$required = ['user_id', 'gift_id', 'gift_user', 'from_id'];
|
||||
foreach ($required as $field) {
|
||||
if (empty($params[$field])) {
|
||||
$this->error("{$field}不能为空");
|
||||
}
|
||||
}
|
||||
|
||||
// 构建送礼数据
|
||||
$giftData = [
|
||||
'user_id' => intval($params['user_id']),
|
||||
'gift_id' => intval($params['gift_id']),
|
||||
'gift_type' => intval($params['gift_type'] ?? 1),
|
||||
'number' => intval($params['number'] ?? 1),
|
||||
'gift_user' => intval($params['gift_user']),
|
||||
'from_id' => intval($params['from_id']),
|
||||
'pit_number' => intval($params['pit_number'] ?? 0),
|
||||
'total_price' => floatval($params['total_price'] ?? 0),
|
||||
'type' => intval($params['type'] ?? 1),
|
||||
'from' => intval($params['from'] ?? 1),
|
||||
'createtime' => time(),
|
||||
];
|
||||
|
||||
// 验证价格
|
||||
if ($giftData['total_price'] <= 0) {
|
||||
$this->error('礼物总价值必须大于0');
|
||||
}
|
||||
|
||||
// 验证数量
|
||||
if ($giftData['number'] <= 0) {
|
||||
$this->error('礼物数量必须大于0');
|
||||
}
|
||||
|
||||
// 使用队列异步处理
|
||||
$result = GiftQueue::push($giftData);
|
||||
|
||||
if ($result) {
|
||||
// 记录日志
|
||||
Log::info("用户送礼成功(队列),用户: {$giftData['user_id']}, 收礼人: {$giftData['gift_user']}, 房间: {$giftData['from_id']}");
|
||||
|
||||
// 立即返回成功,后台异步处理
|
||||
$this->success('送礼成功,正在处理中', [
|
||||
'queue_status' => 'processing',
|
||||
'user_id' => $giftData['user_id'],
|
||||
'gift_user' => $giftData['gift_user'],
|
||||
'timestamp' => time()
|
||||
]);
|
||||
} else {
|
||||
Log::error("用户送礼失败(队列),用户: {$giftData['user_id']}, 收礼人: {$giftData['gift_user']}");
|
||||
$this->error('送礼失败,请重试');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量送礼
|
||||
*/
|
||||
public function batchGive()
|
||||
{
|
||||
$gifts = $this->request->param('gifts/a', []);
|
||||
|
||||
if (empty($gifts) || !is_array($gifts)) {
|
||||
$this->error('礼物数据不能为空');
|
||||
}
|
||||
|
||||
$successCount = 0;
|
||||
$failCount = 0;
|
||||
$giftList = [];
|
||||
|
||||
foreach ($gifts as $gift) {
|
||||
// 验证必要参数
|
||||
if (empty($gift['user_id']) || empty($gift['gift_user']) || empty($gift['from_id'])) {
|
||||
$failCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
$giftData = [
|
||||
'user_id' => intval($gift['user_id']),
|
||||
'gift_id' => intval($gift['gift_id'] ?? 0),
|
||||
'gift_type' => intval($gift['gift_type'] ?? 1),
|
||||
'number' => intval($gift['number'] ?? 1),
|
||||
'gift_user' => intval($gift['gift_user']),
|
||||
'from_id' => intval($gift['from_id']),
|
||||
'pit_number' => intval($gift['pit_number'] ?? 0),
|
||||
'total_price' => floatval($gift['total_price'] ?? 0),
|
||||
'type' => intval($gift['type'] ?? 1),
|
||||
'from' => intval($gift['from'] ?? 1),
|
||||
'createtime' => time(),
|
||||
];
|
||||
|
||||
$giftList[] = $giftData;
|
||||
}
|
||||
|
||||
// 批量加入队列
|
||||
$result = GiftQueue::pushBatch($giftList);
|
||||
|
||||
Log::info("批量送礼提交,总计: {$result['total']}, 成功: {$result['success']}, 失败: {$result['failed']}");
|
||||
|
||||
$this->success("批量送礼提交成功", $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取队列状态
|
||||
*/
|
||||
public function queueStatus()
|
||||
{
|
||||
$stats = GiftQueue::stats();
|
||||
|
||||
$this->success('获取成功', $stats);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试送礼接口(直接写入,用于测试)
|
||||
*/
|
||||
public function testGive()
|
||||
{
|
||||
// 只有测试环境可用
|
||||
if (config('app_debug') != true) {
|
||||
$this->error('测试接口仅限开发环境使用');
|
||||
}
|
||||
|
||||
$params = $this->request->param();
|
||||
|
||||
// 验证必要参数
|
||||
$required = ['user_id', 'gift_id', 'gift_user', 'from_id'];
|
||||
foreach ($required as $field) {
|
||||
if (empty($params[$field])) {
|
||||
$this->error("{$field}不能为空");
|
||||
}
|
||||
}
|
||||
|
||||
// 构建送礼数据
|
||||
$giftData = [
|
||||
'user_id' => intval($params['user_id']),
|
||||
'gift_id' => intval($params['gift_id']),
|
||||
'gift_type' => intval($params['gift_type'] ?? 1),
|
||||
'number' => intval($params['number'] ?? 1),
|
||||
'gift_user' => intval($params['gift_user']),
|
||||
'from_id' => intval($params['from_id']),
|
||||
'pit_number' => intval($params['pit_number'] ?? 0),
|
||||
'total_price' => floatval($params['total_price'] ?? 0),
|
||||
'type' => intval($params['type'] ?? 1),
|
||||
'from' => intval($params['from'] ?? 1),
|
||||
'createtime' => time(),
|
||||
];
|
||||
|
||||
// 直接写入数据库(测试用)
|
||||
$model = new \app\common\model\GiveGiftBase();
|
||||
$result = $model->addGiftRecord($giftData);
|
||||
|
||||
if ($result) {
|
||||
$this->success('测试送礼成功(直接写入)', [
|
||||
'id' => $result,
|
||||
'data' => $giftData
|
||||
]);
|
||||
} else {
|
||||
$this->error('测试送礼失败: ' . $model->getError());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,6 +92,7 @@ class RoomAuction extends Model
|
||||
$auction['relation_icon'] = db::name('vs_relation')->where('id',$auction['relation_id'])->value('icon');
|
||||
$auction['base_image'] = db::name('vs_gift')->where('gid',$auction['gift_id'])->value('base_image');
|
||||
$auction['gift_name'] = db::name('vs_gift')->where('gid',$auction['gift_id'])->value('gift_name');
|
||||
$auction['gift_price'] = db::name('vs_gift')->where('gid',$auction['gift_id'])->value('gift_price');
|
||||
$auction['time_day'] = $auction['time_day'] / 60 / 60;
|
||||
}
|
||||
// var_dump($auction);
|
||||
|
||||
Reference in New Issue
Block a user