Files
yusheng-php/application/adminapi/model/Gift.php

182 lines
6.2 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\model;
use app\common\library\GiftTableManager;
use think\Log;
use think\Model;
use think\Session;
use think\Db;
class Gift extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $name = 'vs_gift';
//礼物类型 1普通 2盲盒 3礼包礼物
public $giftType = [
1 => '普通礼物',
2 => '盲盒礼物',
3 => '礼包礼物',
6 => '酒吧房礼物'
];
//送礼流水来源1聊天送礼物 2房间语聊送礼 3直播送礼 4动态打赏
public $GiveGiftFromStr = [
1 => '聊天送礼',
2 => '房间语聊送礼',
3 => '直播送礼',
4 => '动态打赏',
5 => '房心愿礼物',
6 => '酒吧房自定义礼物',
7 => '酒吧房普通礼物',
8 => '酒吧房小黑屋礼物'
];
public function getList($where = [], $page = 1, $limit = 10)
{
$where['delete_time'] = 0;
$list = $this->where($where)->page($page, $limit)->select();
$list = collection($list)->toArray();
return $list;
}
public function getCount($where = [])
{
$where['delete_time'] = 0;
return $this->where($where)->count();
}
public function getOne($where = [])
{
$one = $this->where($where)->find();
return $one;
}
public function add($data)
{
$res = $this->save($data);
if (!$res) {
return false;
}
$guild_id = $this->id;
return $guild_id;
}
public function edit($where = [], $data = [])
{
$res = $this->where($where)->update($data);
return $res;
}
public function del($where = [])
{
$res = $this->where($where)->delete();
return $res;
}
//软删除
public function setDel($where = []){
$res = $this->where($where)->setField('delete_time',time());
if(!$res){
return false;
}
return $res;
}
/**
* 获取送礼记录
*/
public function getGiftRecords($search_time,$where,$page_array)
{
// 获取需要查询的表
$tables = GiftTableManager::getTablesByTimeRange(
$search_time[0],
$search_time[1]
);
//数组排序fa_vs_give_gift_202601 表明数字大的排在前面
arsort($tables);
$gift_count = 0;
$gift_total_price = 0;
$gift_number = 0;
$all_lists = []; // 存储所有查询结果
$remaining_page_offset = ($page_array['page'] - 1) * $page_array['page_limit']; // 需要跳过的记录数
$collected_count = 0; // 已收集的记录数
if($tables){
foreach ($tables as $table) {
$table_name = substr($table, 3);//截取可查询的表名
// 获取当前表的总记录数
$count = db::name($table_name)->where($where)->count();
if($count > 0){
// 如果还需要跳过记录
if($remaining_page_offset > 0){
// 如果当前表的记录数大于需要跳过的记录数
if($count > $remaining_page_offset){
// 从当前表中获取所需数据,跳过前面的记录
$table_lists = db::name($table_name)
->where($where)
->order('createtime', 'desc')
->limit($remaining_page_offset, $page_array['page_limit'])
->select();
// 添加到结果中
foreach($table_lists as $item){
if($collected_count < $page_array['page_limit']){
$all_lists[] = $item;
$collected_count++;
} else {
break;
}
}
$remaining_page_offset = 0; // 已经跳过所需的记录
} else {
// 当前表的所有记录都需要跳过
$remaining_page_offset -= $count;
}
} else if($collected_count < $page_array['page_limit']) {
// 如果不需要跳过记录,且还没有收集足够的数据
$remaining_needed = $page_array['page_limit'] - $collected_count;
// 从当前表获取剩余所需的数据
$table_lists = db::name($table_name)
->where($where)
->order('createtime', 'desc')
->limit($remaining_needed)
->select();
// 添加到结果中
foreach($table_lists as $item){
if($collected_count < $page_array['page_limit']){
$all_lists[] = $item;
$collected_count++;
} else {
break;
}
}
}
$total_price = db::name($table_name)->where($where)->sum('total_price');
$number = db::name($table_name)->where($where)->sum('number');
$gift_count += $count;
$gift_total_price += $total_price;
$gift_number += $number;
// 如果已经收集到足够数据,跳出循环
if($collected_count >= $page_array['page_limit']){
break;
}
}
}
}
$result = [
'lists' => $all_lists,
'count' => $gift_count,
'total_price' => $gift_total_price,
'number' => $gift_number,
];
return $result;
}
}