Files
midi-php/application/adminapi/controller/GiveGift.php

234 lines
9.3 KiB
PHP
Raw Normal View History

2025-08-13 10:43:56 +08:00
<?php
namespace app\adminapi\controller;
2025-09-19 16:13:41 +08:00
ini_set('memory_limit', '512M'); // 临时增加到512MB
2025-08-13 10:43:56 +08:00
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=[];
2025-10-22 14:32:56 +08:00
$gwhere = [];
2025-08-13 10:43:56 +08:00
if($send_user != ''){
$user_id = db::name('user')->where('user_code', $send_user)->value('id');
$where['user_id'] = $user_id;
2025-10-22 14:32:56 +08:00
$gwhere['gg.user_id'] = $user_id;
2025-08-13 10:43:56 +08:00
}
if($gift_user != ''){
$gift_user_id = db::name('user')->where('user_code', $gift_user)->value('id');
$where['gift_user'] = $gift_user_id;
2025-10-22 14:32:56 +08:00
$gwhere['gg.gift_user'] = $gift_user_id;
2025-08-13 10:43:56 +08:00
}
if($from_id != ''){
$room_id = db::name('vs_room')->where('room_number', $from_id)->value('id');
if($room_id){
$where['from_id'] = $room_id;
2025-10-22 14:32:56 +08:00
$gwhere['gg.from_id'] = $room_id;
2025-08-13 10:43:56 +08:00
}else{
$where['from_id'] = $from_id;
2025-10-22 14:32:56 +08:00
$gwhere['gg.from_id'] = $from_id;
2025-08-13 10:43:56 +08:00
}
}
if($gift_id != ''){
$where['gift_id'] = $gift_id;
2025-10-22 14:32:56 +08:00
$gwhere['gg.gift_id'] = $gift_id;
2025-08-13 10:43:56 +08:00
}
if($from != ''){
$where['from'] = $from;
2025-10-22 14:32:56 +08:00
$gwhere['gg.from'] = $from;
2025-08-13 10:43:56 +08:00
}
// 时间筛选优化
2025-09-19 16:13:41 +08:00
if (!empty($start_time) || !empty($end_time)) {
if (!empty($start_time) && !empty($end_time)) {
2025-09-27 17:29:15 +08:00
$where['createtime'] = ['between', [strtotime($start_time), strtotime($end_time)]];
2025-10-22 14:32:56 +08:00
$gwhere['gg.createtime'] = ['between', [strtotime($start_time), strtotime($end_time)]];
2025-09-19 16:13:41 +08:00
} elseif (!empty($start_time)) {
$where['createtime'] = ['>=', strtotime($start_time)];
2025-10-22 14:32:56 +08:00
$gwhere['gg.createtime'] = ['>=', strtotime($start_time)];
2025-09-19 16:13:41 +08:00
} elseif (!empty($end_time)) {
2025-09-27 17:29:15 +08:00
$where['createtime'] = ['<=', strtotime($end_time)];
2025-10-22 14:32:56 +08:00
$gwhere['gg.createtime'] = ['<=', strtotime($end_time)];
2025-09-19 16:13:41 +08:00
}
2025-08-13 10:43:56 +08:00
}
//礼物总数
$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;
// 获取所有送礼记录
2025-09-19 16:13:41 +08:00
$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)){
2025-10-22 14:32:56 +08:00
$earning_list = db::name('vs_give_gift_ratio_log')
->alias('egl')
->join('vs_give_gift gg', 'egl.give_gift_id = gg.id')
->where($gwhere) // 替换为实际的筛选条件
->field('egl.app_earning,egl.gift_user_earning,egl.room_owner_earning')
->select();
2025-09-19 16:13:41 +08:00
}else{
2025-08-13 10:43:56 +08:00
// 批量获取所有收益记录
$earning_list = db::name('vs_give_gift_ratio_log')
2025-09-19 16:13:41 +08:00
->field('app_earning,gift_user_earning,room_owner_earning')
2025-08-13 10:43:56 +08:00
->order('id', 'desc')
->select();
}
2025-09-19 16:13:41 +08:00
// 计算总收益
foreach ($earning_list as $earning) {
$app_earning += $earning['app_earning'];
$receive_earning += $earning['gift_user_earning'];
$room_owner_earning += $earning['room_owner_earning'];
}
2025-08-13 10:43:56 +08:00
// 提取所有需要关联查询的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;
}
2025-10-21 18:24:13 +08:00
//比例
$config = get_system_config();
2025-08-13 10:43:56 +08:00
$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),
2025-10-21 18:24:13 +08:00
'room_owner_earning' => round($room_owner_earning,2),
'app_earning_ratio' => 100 - $config['room_gift_ratio'] - $config['room_author_ratio'],
'receive_earning_ratio' => $config['room_gift_ratio'] ?? 0,
'room_owner_earning_ratio' => $config['room_author_ratio'] ?? 0,
2025-08-13 10:43:56 +08:00
]
];
return V(1,"成功", $return_data);
}
// 自定义分页函数
function array_pagination($array, $page, $limit) {
$start = ($page - 1) * $limit;
return array_slice($array, $start, $limit);
}
}