Files
yusheng-php/application/api/model/GiveGiftBases.php
2026-01-17 14:32:43 +08:00

468 lines
13 KiB
PHP

<?php
namespace app\api\model;
use app\common\model\GiveGiftBase;
use think\Cache;
use think\Log;
use think\Model;
class GiveGiftBases extends Model
{
protected $giftModel;
public function __construct()
{
parent::__construct();
$this->giftModel = new GiveGiftBase();
}
/**
* 获取送礼记录
*/
public function getGiftRecords($params,$ext_params=[])
{
// $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($ext_params)) {
if (!empty($ext_params['start_time'])) {
$options['start_time'] = $ext_params['start_time'];
}
if (!empty($ext_params['end_time'])) {
$options['end_time'] = $ext_params['end_time'];
}
}else{
if (!empty($params['start_time'])) {
$options['start_time'] = strtotime($params['start_time']);
}
if (!empty($params['end_time'])) {
$options['end_time'] = strtotime($params['end_time'] . ' 23:59:59');
}
}
// 分页参数
$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);
return $result;
}
/**
* 房间送礼统计
*/
public function getRoomStatistics($params)
{
$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($params,$type)
{
if($type==1){
$userId = $params['user_id'];
}else{
$userId = $params['gift_user'];
}
$startTime = $params['start_time'];
$endTime = $params['end_time'];
if (!$userId) {
return ['code'=>0,'msg'=>'用户ID不能为空','data'=>null];
}
if($type==1){
$where = ['user_id' => $userId];
}else{
$where = ['gift_user' => $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);
return $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
]
]);
}
public function getGiftList()
{
$params = $this->request->param();
$where = [];
$options = [];
// 构建查询条件
$searchFields = ['from_id', 'user_id', 'gift_user', 'gift_id', 'type', 'from', 'gift_type'];
foreach ($searchFields as $field) {
if (!empty($params[$field])) {
$where[$field] = $params[$field];
}
}
// 时间范围
if (!empty($params['start_time'])) {
$options['start_time'] = strtotime($params['start_time']);
}
if (!empty($params['end_time'])) {
$options['end_time'] = strtotime($params['end_time']);
}
// 搜索关键词
if (!empty($params['search'])) {
// 可以扩展搜索逻辑
}
// 分页参数
$options['page'] = $params['page'] ?? 1;
$options['limit'] = $params['limit'] ?? $this->pageSize;
// 查询数据
$result = $this->giftModel->getGiftRecords($where, $options);
// 转换时间格式
foreach ($result['data'] as &$item) {
$item['createtime_text'] = $item['createtime'] ? date('Y-m-d H:i:s', $item['createtime']) : '';
$item['updatetime_text'] = $item['updatetime'] ? date('Y-m-d H:i:s', $item['updatetime']) : '';
}
$total = $result['total'];
$data = $result['data'];
$result = array("total" => $total, "rows" => $data);
}
/**
* 获取送礼排行
*/
public function getGiftRanking($params,$type)
{
$where = [];
$options = [];
// 时间范围
if (!empty($params['start_time'])) {
$options['start_time'] = $params['start_time'];
}
if (!empty($params['end_time'])) {
$options['end_time'] = $params['end_time'];
}
// 分页参数
$options['page'] = $params['page'] ?? 1;
$options['limit'] = min($params['limit'] ?? 20, 100); // 限制最大100条
if($type == 1){
$options['group_by'] = 'user_id';
}else{
$options['group_by'] = 'gift_user';
}
// 查询数据
$result = $this->giftModel->getGiftStatisticsRanking($where, $options);
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;
}
/**
* 获取房间内送礼排行
*/
public function getRoomGiftRanking($params,$type)
{
$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'];
}
// 分页参数
$options['page'] = $params['page'] ?? 1;
$options['limit'] = min($params['limit'] ?? 20, 100); // 限制最大100条
if($type == 1){
$options['group_by'] = 'user_id';
}else{
$options['group_by'] = 'gift_user';
}
// 查询数据
$result = $this->giftModel->getGiftStatisticsRanking($where, $options);
return $result;
}
}