Files
yusheng-php/application/adminapi/controller/GiveGift.php
2025-10-20 09:59:39 +08:00

222 lines
8.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\adminapi\controller;
ini_set('memory_limit', '512M'); // 临时增加到512MB
use app\common\controller\adminApi;
use think\Db;
class GiveGift extends adminApi
{
protected $noNeedLogin = [];
protected $noNeedRight = [];
protected $table = 'vs_give_gift';
public function _initialize()
{
parent::_initialize();
}
//送礼物列表
public function give_gift_lists(){
$page = input('page', 1);
$page_limit = input('page_limit', 30);
$send_user = input('send_user', '');//送礼物用户
$gift_user = input('gift_user', '');//接受礼物用户
$from_id = input('from_id', '');//礼物来源id
$gift_id = input('gift_id', '');//礼物id
$from = input('from', '');// 来源类型1聊天送礼物 2房间语聊送礼 3直播送礼 4动态打赏 5系统任务 6-cp房间送礼
$start_time = input('start_time', '');
$end_time = input('end_time', '');
$where=[];
if($send_user != ''){
$user_id = db::name('user')->where('user_code', $send_user)->value('id');
$where['user_id'] = $user_id;
}
if($gift_user != ''){
$gift_user_id = db::name('user')->where('user_code', $gift_user)->value('id');
$where['gift_user'] = $gift_user_id;
}
if($from_id != ''){
$room_id = db::name('vs_room')->where('room_number', $from_id)->value('id');
if($room_id){
$where['from_id'] = $room_id;
}else{
$where['from_id'] = $from_id;
}
}
if($gift_id != ''){
$where['gift_id'] = $gift_id;
}
if($from != ''){
$where['from'] = $from;
}
// 时间筛选优化
if (!empty($start_time) || !empty($end_time)) {
if (!empty($start_time) && !empty($end_time)) {
$where['createtime'] = ['between', [strtotime($start_time), strtotime($end_time)]];
} elseif (!empty($start_time)) {
$where['createtime'] = ['>=', strtotime($start_time)];
} elseif (!empty($end_time)) {
$where['createtime'] = ['<=', strtotime($end_time)];
}
}
//礼物总数
$gift_num = db::name('vs_give_gift')->where($where)->sum('number');
//总金额
$total_price = db::name('vs_give_gift')->where($where)->sum('total_price');
//平台收益
$app_earning = 0;
//接收人收益
$receive_earning = 0;
//房主收益
$room_owner_earning = 0;
// 获取所有送礼记录
$lists = db::name('vs_give_gift')->where($where)->order('id', 'desc')->page($page, $page_limit)->select();
$count = db::name('vs_give_gift')->where($where)->count();
$earning_list = [];
if(!empty($where)){
$gift_ids = db::name('vs_give_gift')->where($where)->column('id');
if (!empty($gift_ids)) {
// 批量获取所有收益记录
$earning_list = db::name('vs_give_gift_ratio_log')
->field('app_earning,gift_user_earning,room_owner_earning')
->where('give_gift_id', 'in', $gift_ids)
->order('id', 'desc')
->select();
}
}else{
// 批量获取所有收益记录
$earning_list = db::name('vs_give_gift_ratio_log')
->field('app_earning,gift_user_earning,room_owner_earning')
->order('id', 'desc')
->select();
}
// 计算总收益
foreach ($earning_list as $earning) {
$app_earning += $earning['app_earning'];
$receive_earning += $earning['gift_user_earning'];
$room_owner_earning += $earning['room_owner_earning'];
}
// 提取所有需要关联查询的ID
$user_ids = array_merge(
array_column($lists, 'user_id'),
array_column($lists, 'gift_user')
);
$room_ids = array_filter(array_merge(
array_column($lists, 'room_id'),
array_column($lists, 'from_id')
));
$gift_ids_list = array_column($lists, 'gift_id');
// 批量获取用户信息
$users = [];
if (!empty($user_ids)) {
$user_list = db::name('user')
->where('id', 'in', array_unique($user_ids))
->field('id,nickname,user_code')
->select();
foreach ($user_list as $user) {
$users[$user['id']] = $user;
}
}
// 批量获取房间信息
$rooms = [];
if (!empty($room_ids)) {
$room_list = db::name('vs_room')
->where('id', 'in', array_unique($room_ids))
->field('id,room_name,room_number')
->select();
foreach ($room_list as $room) {
$rooms[$room['id']] = $room;
}
}
// 批量获取礼物信息
$gifts = [];
if (!empty($gift_ids_list)) {
$gift_list = db::name('vs_gift')
->where('gid', 'in', array_unique($gift_ids_list))
->field('gid,gift_name,gift_price')
->select();
foreach ($gift_list as $gift) {
$gifts[$gift['gid']] = $gift;
}
}
// 批量获取收益详情
$earnings = [];
if (!empty(array_column($lists, 'id'))) {
$earning_details = db::name('vs_give_gift_ratio_log')
->where('give_gift_id', 'in', array_column($lists, 'id'))
->order('id', 'desc')
->select();
foreach ($earning_details as $earning) {
$earnings[$earning['give_gift_id']] = $earning;
}
}
// 处理列表数据
foreach ($lists as &$value) {
$value['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
// 用户信息
$send_user_info = isset($users[$value['user_id']]) ? $users[$value['user_id']] : null;
$value['send_user'] = $send_user_info ? $send_user_info['nickname'].'-'.$send_user_info['user_code'] : '';
$gift_user_info = isset($users[$value['gift_user']]) ? $users[$value['gift_user']] : null;
$value['gift_user'] = $gift_user_info ? $gift_user_info['nickname'].'-'.$gift_user_info['user_code'] : '';
// 礼物信息
$gift_info = isset($gifts[$value['gift_id']]) ? $gifts[$value['gift_id']] : null;
$value['gift_name'] = $gift_info ? $gift_info['gift_name'].'-'.$value['gift_id'] : '';
$value['gift_price'] = $gift_info ? $gift_info['gift_price'] : 0;
// 房间信息处理
if($value['from'] == 1){
$value['room'] = '聊天送礼';
}elseif ($value['from'] == 2 || $value['from'] == 3){
$room_info = isset($rooms[$value['from_id']]) ? $rooms[$value['from_id']] : null;
$value['room'] = $room_info ? $room_info['room_number'].$room_info['room_name'].'-' : '';
}elseif ($value['from'] == 4){
$value['room'] = '动态打赏 -'.$value['from_id'];
}elseif ($value['from'] == 5){
$value['room'] = '系统任务';
}elseif ($value['from'] == 6){
$value['room'] = 'cp房间送礼-'.$value['from_id'];
}else{
$value['room'] = '未知来源';
}
// 收益信息
$earning_info = isset($earnings[$value['id']]) ? $earnings[$value['id']] : null;
$value['app_earning'] = $earning_info ? $earning_info['app_earning'] : 0;
$value['gift_user_earning'] = $earning_info ? $earning_info['gift_user_earning'] : 0;
$value['room_owner_earning'] = $earning_info ? $earning_info['room_owner_earning'] : 0;
}
$return_data = [
'page' =>$page,
'page_limit' => $page_limit,
'count' => $count,
'lists' => $lists,
'total' =>[
'gift_num' => $gift_num,
'total_price' => $total_price,
'app_earning' => round($app_earning,2) ,
'receive_earning' => round($receive_earning,2),
'room_owner_earning' => round($room_owner_earning,2)
]
];
return V(1,"成功", $return_data);
}
// 自定义分页函数
function array_pagination($array, $page, $limit) {
$start = ($page - 1) * $limit;
return array_slice($array, $start, $limit);
}
}