You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

997 lines
36 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace Admin\Controller;
use User\Api\UserApi as UserApi;
/**
* 后台首页控制器
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
class StatisticsController extends ThinkController {
public function overview(){
$shuju = M('Data','tab_')->order('create_time desc')->find();
$this->assign('shuju',$shuju);
$this->assign('openegretmain','openegretmain');//模板还样式使用
$this->meta_title = '总览';
//实时数据概况
$today = $this->total(1);
$thisweek = $this->total(2);
$thismounth = $this->total(3);
$thisyear = $this->total(4);
$yesterday=$this->total(5);
$lastweek=$this->total(6);
$lastmounth=$this->total(7);
$lastyear=$this->total(8);
// 游戏排行
$type=$_REQUEST['type'];
if($type==1 || $type==''){
$list_data=$this->data_order($today,$yesterday);
}elseif($type==2){
$list_data=$this->data_order($thisweek,$lastweek);
}elseif($type==3){
$list_data=$this->data_order($thismounth,$lastmounth);
}elseif($type==4){
$list_data=$this->data_order($thisyear,$lastyear);
}
// 推广员排行
switch($_REQUEST['category']) {
case 2:{$promote_data = $this->promote_data_order($thisweek,$lastweek);};break;
case 3:{$promote_data = $this->promote_data_order($thismounth,$lastmounth);};break;
case 4:{$promote_data = $this->promote_data_order($thisyear,$lastyear);};break;
default:
$promote_data = $this->promote_data_order($today,$yesterday);
}
$this->assign('zhuce',$list_data['zhuce']);
$this->assign('active',$list_data['active']);
$this->assign('pay',$list_data['pay']);
$this->assign('game_chart',$list_data['chart']);
$this->assign('promotereg',$promote_data['reg']);
$this->assign('promoteactive',$promote_data['active']);
$this->assign('promotepay',$promote_data['pay']);
$this->assign('promote_chart',$promote_data['chart']);
// 日历
R('Index/calendar');
// 折线图
$this->foldLineDiagram($_REQUEST['start'],$_REQUEST['end'],$_REQUEST['num']);
$this->display();
}
/*
* 折线图
* @param integer $start 开始时间
* @param integer $end 结束时间
* @param boolean $flag 是否ajax返回
* @author 鹿文学
*/
public function foldLineDiagram($start='',$end='',$num='',$flag=false) {
$starttime = $start?strtotime($start):mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$endtime = $end?strtotime($end)+86399:$starttime+86399;
$start = date('Y-m-d',$starttime);
$end = date('Y-m-d',$endtime);
$user = D('User');
$spend = D('Spend');
if ($start == $end) {
if ($start == date('Y-m-d',strtotime('-1 day'))) {$num = 2;}
$hours = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23'];
$data['date'] = [$start];
$data['hours'] = 1;
foreach($hours as $v) {
$data['news'][$v] = 0;
$data['active'][$v] = 0;
$data['player'][$v] = 0;
$data['money'][$v] = 0;
}
// 新增用户
$hoursnews = $user->newsAdd(['register_time'=>['between',[$starttime,$endtime]]],'news','time',5);
// 活跃用户
$hoursactive = $user->totalPlayerByGroup($hours,['tab_user_login_record.login_time'=>['between',[$starttime,$endtime]]],'active','time',5);
// 付费用户
$hoursplayer = $spend->totalPlayerByGroup(['pay_time'=>['between',[$starttime,$endtime]]],'player','time',5);
// 充值金额
$hoursmoney = $spend->totalAmountByGroup(['pay_time'=>['between',[$starttime,$endtime]]],'money','time',5);
foreach($hours as $v) {
foreach($hoursnews as $h) {
$time = explode(' ',$h['time']);
if ($time[1] == $v){
$data['news'][$v] = (integer)$h['news'];break;
}
}
foreach($hoursactive as $h) {
if ($h['time'] == $v){
$data['active'][$v] = (integer)$h['active'];break;
}
}
foreach($hoursplayer as $h) {
$time = explode(' ',$h['time']);
if ($time[1] == $v){
$data['player'][$v] = (integer)$h['player'];break;
}
}
foreach($hoursmoney as $h) {
$time = explode(' ',$h['time']);
if ($time[1] == $v){
$data['money'][$v] = $h['money'];break;
}
}
}
} else {
$datelist = get_date_list($starttime,$endtime,$num==7?4:1);
$data['date'] = $datelist;
$data['hours'] = 0;
foreach($datelist as $k => $v) {
$data['news'][$v] = 0;
$data['active'][$v] = 0;
$data['player'][$v] = 0;
$data['money'][$v] = 0;
}
// 新增用户
$news = $user->newsAdd(['register_time'=>['between',[$starttime,$endtime]]],'news','time',$num==7?2:1);
// 活跃用户
$active = $user->totalPlayerByGroup($datelist,['tab_user_login_record.login_time'=>['between',[$starttime,$endtime]]],'active','time',$num==7?2:1);
// 付费用户
//$player = $spend->totalPlayerByGroup(['pay_time'=>['between',[$starttime,$endtime]]],'player','time',$num==7?2:1);
$player = $spend->totalPlayerByTime($starttime,$endtime,'player','time',$num==7?2:1);
// 充值金额
$money = $spend->totalAmountByGroup(['pay_time'=>['between',[$starttime,$endtime]]],'money','time',$num==7?2:1);
foreach($datelist as $v) {
foreach($news as $h) {
if ($v == $h['time']) {
$data['news'][$v] = (integer)$h['news'];break;
}
}
foreach($active as $h) {
if ($v == $h['time']) {
$data['active'][$v] = (integer)$h['active'];break;
}
}
foreach($player as $h) {
if ($v == $h['time']) {
$data['player'][$v] = (integer)$h['player'];break;
}
}
foreach($money as $h) {
if ($v == $h['time']) {
$data['money'][$v] = $h['money'];break;
}
}
}
}
foreach($data as $k => $v) {
if (is_array($v)) {
if($data['hours']!=1){$table[$k] = $v;}
if ($k == 'date'){
$data[$k] = '"'.implode('","',$v).'"';
}else{
$sum = 0;$x='';$y=0;$tempexport=[];
foreach($v as $t => $s){
$sum += $s;
if($data['hours']==1){
if ($t%2==1) {$tab[$x.'~'.$t] = $y+$s;$x='';$y=0;}else{$x .= $t;$y += $s;}
$tempexport[]=['time'=>((integer)substr($t,0,2)).':00','count'=>$s];
}else{
$tempexport[]=['time'=>$t,'count'=>$s];
}
}
$table['sum'][$k]=$sum;
if($data['hours']==1)$table[$k]=$tab;
$export[$k]=$tempexport;
$export['sum'][$k]=$sum;
$data[$k] = implode(',',$v);
}
}
}
file_put_contents(dirname(__FILE__).'/access_data_foldline.txt',json_encode($export));
if ($flag) {
$data['table'] = $table;
echo json_encode($data);
} else {
$this->assign('foldline',$data);
$this->assign('table',$table);
$this->assign('num',$num);
}
}
/**
* 排行榜(推广员)
* @param string $nowtime 现在时间段between 开始时间戳 and 结束时间戳)
* @param string $othertime 过去时间段between 开始时间戳 and 结束时间戳)
* @return array 结果集
* @author lwx
*/
private function promote_data_order($nowtime,$othertime){
$user = M("User","tab_");
$spend = M('Spend',"tab_");
$chart1 = [];
$chart2 = [];
$chart3 = [];
//今日注册排行
$ri_ug_order=$user->field('tab_user.promote_id,tab_promote.account as promote_account,count(tab_user.id) as cg')
->join('tab_promote on (tab_promote.id = tab_user.promote_id)','left')
->where(array('register_time'.$nowtime,'promote_id'=>array('gt',0),'puid'=>0))
->group('promote_id')->order('cg desc')->limit(10)->select();
$ri_ug_order=array_order($ri_ug_order);
$regids = array_column($ri_ug_order,'promote_id');
if ($regids) {
$yes_ug_order=$user->field('tab_user.promote_id,tab_promote.account as promote_account,count(tab_user.id) as cg')
->join('tab_promote on (tab_promote.id = tab_user.promote_id)')
->where([array('register_time'.$othertime,'promote_id'=>array('gt',0),'puid'=>0),array('register_time'.$othertime,'promote_id'=>array('in',$regids),'puid'=>0),'_logic'=>'or'])
->group('promote_id')->order('cg desc')->select();
$yes_ug_order=array_order($yes_ug_order);
// dump($ri_ug_order);
// dump($yes_ug_order);exit;
foreach ($ri_ug_order as $key => $value) {
$ri_ug_order[$key]['change'] = $value['rand']-count($ri_ug_order)-1;
$chart1['p'.$value['promote_id']] = $value;
foreach ($yes_ug_order as $k => $v) {
if($value['promote_id']==$v['promote_id']){
$ri_ug_order[$key]['change']=$value['rand']-$v['rand'];break;
}
}
}
}
// //今日活跃排行
$duser = D('User');
$ri_active_order = $duser->activeRankOnPromote($nowtime,'cg');
$ri_active_order=array_order($ri_active_order);
$activeids = array_column($ri_active_order,'promote_id');
if ($activeids) {
$yes_active = $duser->activeRankOnPromote($othertime,'cg',$activeids);
$yes_active=array_order($yes_active);
foreach ($ri_active_order as $key => $value) {
$ri_active_order[$key]['change'] = $value['rand']-count($ri_active_order)-1;
$chart2['p'.$value['promote_id']] = $value;
foreach ($yes_active as $k => $v) {
if($value['promote_id']==$v['promote_id']){
$ri_active_order[$key]['change']=$value['rand']-$v['rand'];break;
}
}
}
}
// //充值排行
//spend
$ri_spay_sql=$spend->field('tab_spend.promote_id,tab_promote.account as promote_account,sum(pay_amount) as cg')
->join('tab_promote on(tab_spend.promote_id = tab_promote.id)')
->union('select promote_id,tab_promote.account as promote_account,sum(pay_amount) as cg from tab_deposit inner join tab_promote on(tab_promote.id = tab_deposit.promote_id) where pay_status=1 and promote_id>0 and tab_deposit.create_time '.$nowtime.' group by promote_id ')
->where(array('pay_time'.$nowtime,'promote_id'=>array('gt',0),'pay_status'=>1))
->group('promote_id')->select(false);
$ri_spay_order = $spend->field('promote_id,promote_account,sum(cg) as cg')->table('('.$ri_spay_sql.') as a')->group('promote_id')->order('cg desc')->limit(10)->select();
$ri_spay_order=array_order($ri_spay_order);
$payids = array_column($ri_spay_order,'promote_id');
if ($payids) {
$yes_spay_sql=$spend->field('tab_spend.promote_id,tab_promote.account as promote_account,sum(pay_amount) as cg')
->join('tab_promote on(tab_spend.promote_id = tab_promote.id)')
->union('select promote_id,tab_promote.account as promote_account,sum(pay_amount) as cg from tab_deposit inner join tab_promote on(tab_promote.id = tab_deposit.promote_id) where pay_status=1 and promote_id>0 and tab_deposit.create_time '.$othertime.' group by promote_id ')
->where([array('pay_status'=>1,'pay_time'.$othertime,'promote_id'=>array('gt',0)),array('pay_time'.$othertime,'promote_id'=>array('in',$payids)),'_logic'=>'or'])
->group('promote_id')->select(false);
$yes_spay=$spend->field('promote_id,promote_account,sum(cg) as cg')->table('('.$yes_spay_sql.') as a')->group('promote_id')->order('cg desc')->select();
$yes_spay=array_order($yes_spay);
foreach ($ri_spay_order as $key => $value) {
$ri_spay_order[$key]['change'] = $value['rand']-count($ri_spay_order)-1;
$chart3['p'.$value['promote_id']] = $value;
foreach ($yes_spay as $k => $v) {
if($value['promote_id']==$v['promote_id']){
$ri_spay_order[$key]['change']=$value['rand']-$v['rand'];
}
}
}
}
$data['reg']=$ri_ug_order;
$data['active']=$ri_active_order;
$data['pay']=$ri_spay_order;
$chart4 = array_merge($chart1,$chart2,$chart3);
foreach($chart4 as $k => $v) {
$chart['promote'][$k] = $v['promote_account'];
foreach($chart1 as $c) {
$chart['reg'][$k] = 0;
if ($v['promote_id'] == $c['promote_id']) {
$chart['reg'][$k] = (integer)$c['cg'];break;
}
}
foreach($chart2 as $c) {
$chart['active'][$k] = 0;
if ($v['promote_id'] == $c['promote_id']) {
$chart['active'][$k] = (integer)$c['cg'];break;
}
}
foreach($chart3 as $c) {
$chart['pay'][$k] = 0;
if ($v['promote_id'] == $c['promote_id']) {
$chart['pay'][$k] = $c['cg'];break;
}
}
}
foreach($chart as $k => $v) {
if ($k == 'promote')
$data['chart'][$k] = '"'.implode('","',$v).'"';
else
$data['chart'][$k] = implode(',',$v);
}
return $data;
}
/**
* 排行榜(游戏)
* @param string $nowtime 现在时间段between 开始时间戳 and 结束时间戳)
* @param string $othertime 过去时间段between 开始时间戳 and 结束时间戳)
* @return array 结果集
* @author lwx edit
*/
private function data_order($nowtime,$othertime){
$user = M("User","tab_");
$spend = M('Spend',"tab_");
$chart1 = [];
$chart2 = [];
$chart3 = [];
//今日注册排行
/* $ri_ug_order=$user->field('fgame_id as game_id,fgame_name as game_name,count(tab_user.id) as cg')
->where(array('register_time'.$nowtime,'fgame_id'=>array('gt',0),'puid'=>0))
->group('fgame_id')->order('cg desc')->limit(10)->select(false);
$ri_ug_order=$user->field('a.*,@rank:=@rank+1 as rank_no')->table('(' .$ri_ug_order. ') as a,(SELECT @rank:= 0) b')->select();
$regids = array_column($ri_ug_order,'game_id');
if($regids) {
$yes_ug_order=$user->field('fgame_id as game_id,fgame_name as game_name,count(tab_user.id) as cg')
->where([array('register_time'.$othertime,'fgame_id'=>array('gt',0),'puid'=>0),array('register_time'.$othertime,'fgame_id'=>array('in',$regids),'puid'=>0),'_logic'=>'or'])
->group('fgame_id')->order('cg desc')->select(false);
$yes_ug_order=$user->field('a.*,@rank:=@rank+1 as rank_no')->table('(' .$yes_ug_order. ') as a,(SELECT @rank:= 0) b')->select();
foreach ($ri_ug_order as $key => $value) {
$ri_ug_order[$key]['change']=0;
$chart1['g'.$value['game_id']] = $value;
foreach ($yes_ug_order as $k => $v) {
if($value['game_id']==$v['game_id']){
$ri_ug_order[$key]['change']=$value['rank_no']-$v['rank_no'];break;
}
}
if($ri_ug_order[$key]['change']==0) {
$ri_ug_order[$key]['change'] = $value['rank_no']-count($ri_ug_order)-1;
}
}
} */
$ri_ug_order=$user->field('fgame_id as game_id,fgame_name as game_name,count(tab_user.id) as cg')
->where(array('register_time'.$nowtime,'fgame_id'=>array('gt',0),'puid'=>0))
->group('fgame_id')->order('cg desc')->limit(10)->select();
$ri_ug_order=array_order($ri_ug_order);
$regids = array_column($ri_ug_order,'game_id');
if($regids) {
$yes_ug_order=$user->field('fgame_id as game_id,fgame_name as game_name,count(tab_user.id) as cg')
->where([array('register_time'.$othertime,'fgame_id'=>array('gt',0),'puid'=>0),array('register_time'.$othertime,'fgame_id'=>array('in',$regids),'puid'=>0),'_logic'=>'or'])
->group('fgame_id')->order('cg desc')->select();
$yes_ug_order=array_order($yes_ug_order);
foreach ($ri_ug_order as $key => $value) {
$ri_ug_order[$key]['change'] = $value['rand']-count($ri_ug_order)-1;
$chart1['g'.$value['game_id']] = $value;
foreach ($yes_ug_order as $k => $v) {
if($value['game_id']==$v['game_id']){
$ri_ug_order[$key]['change']=$value['rand']-$v['rand'];break;
}
}
}
}
// //今日活跃排行
$duser = D('User');
$ri_active_order = $duser->activeRankOnGame($nowtime,'cg');
$ri_active_order=array_order($ri_active_order);
$activeids = array_column($ri_active_order,'game_id');
if($activeids) {
$yes_active = $duser->activeRankOnGame($othertime,'cg',$activeids);
$yes_active=array_order($yes_active);
foreach ($ri_active_order as $key => $value) {
$ri_active_order[$key]['change'] = $value['rand']-count($ri_active_order)-1;
$chart2['g'.$value['game_id']] = $value;
foreach ($yes_active as $k => $v) {
if($value['game_id']==$v['game_id']){
$ri_active_order[$key]['change']=$value['rand']-$v['rand'];break;
}
}
}
}
// //充值排行
//spend
$ri_spay_order=$spend->field('game_id,game_name,sum(pay_amount) as cg')
->where(array('pay_time'.$nowtime,'game_id'=>array('gt',0),'pay_status'=>1))
->group('game_id')->order('cg desc')->limit(10)->select();
$ri_spay_order=array_order($ri_spay_order);
$payids = array_column($ri_spay_order,'game_id');
if ($payids) {
$yes_spay=$spend->field('game_id,game_name,sum(pay_amount) as cg')
->where([array('pay_status'=>1,'pay_time'.$othertime,'game_id'=>array('gt',0)),array('pay_time'.$othertime,'game_id'=>array('in',$payids)),'_logic'=>'or'])
->group('game_id')->order('cg desc')->select();
$yes_spay=array_order($yes_spay);
foreach ($ri_spay_order as $key => $value) {
$ri_spay_order[$key]['change'] = $value['rand']-count($ri_spay_order)-1;
$chart3['g'.$value['game_id']] = $value;
foreach ($yes_spay as $k => $v) {
if($value['game_id']==$v['game_id']){
$ri_spay_order[$key]['change']=$value['rand']-$v['rand'];
}
}
}
}
$data['zhuce']=$ri_ug_order;
$data['active']=$ri_active_order;
$data['pay']=$ri_spay_order;
$chart4 = array_merge($chart1,$chart2,$chart3);
foreach($chart4 as $k => $v) {
$chart['game'][$k] = $v['game_name'];
foreach($chart1 as $c) {
$chart['reg'][$k] = 0;
if ($v['game_id'] == $c['game_id']) {
$chart['reg'][$k] = (integer)$c['cg'];break;
}
}
foreach($chart2 as $c) {
$chart['active'][$k] = 0;
if ($v['game_id'] == $c['game_id']) {
$chart['active'][$k] = (integer)$c['cg'];break;
}
}
foreach($chart3 as $c) {
$chart['pay'][$k] = 0;
if ($v['game_id'] == $c['game_id']) {
$chart['pay'][$k] = $c['cg'];break;
}
}
}
foreach($chart as $k => $v) {
if ($k == 'game')
$data['chart'][$k] = '"'.implode('","',$v).'"';
else
$data['chart'][$k] = implode(',',$v);
}
return $data;
}
public function zhexian(){
$day=$this->every_day(7);
$time=$this->total(9);
$key=$_REQUEST['key'];
$user = M("User","tab_");
$spend = M('Spend',"tab_");
$deposit = M('Deposit','tab_');
if($key==1){
//注册数据
$data=$user->field('fgame_id,fgame_name,date_format(FROM_UNIXTIME( `register_time`),"%Y-%m-%d") AS time,count(id) as cg')->where(array('register_time'.$time))->where(array('fgame_id'=>array('gt',0)))->group('time,fgame_id')->order('cg desc')->select();
$title=$user->field('fgame_name,count(id) as cg')->where(array('register_time'.$time))->where(array('fgame_id'=>array('gt',0)))->group('fgame_id')->order('cg desc')->select();
$title=array_column($title,'fgame_name');
$data=array_group_by($data,'time');
foreach ($day as $key => $value) {
if(array_key_exists($value, $data)){
foreach ($data[$value] as $kk => $vv) {
$game_name=$vv['fgame_name'];
$dayy[$value][$game_name]=$vv['cg'];
}
}
}
}elseif($key==2){
//活跃数据
$data=$user->field('game_id,game_name,date_format(FROM_UNIXTIME( uu.login_time),"%Y-%m-%d") AS time,count(tab_user.id) as cg')->join('tab_user_login_record as uu on tab_user.id = uu.user_id')->where(array('uu.login_time'.$time))->where(array('game_id'=>array('gt',0)))->group('time,game_id')->order('cg desc')->select();
$title=$user->field('game_name,count(tab_user.id) as cg')->join('tab_user_login_record as uu on tab_user.id = uu.user_id')->where(array('uu.login_time'.$time))->where(array('game_id'=>array('gt',0)))->group('game_id')->order('cg desc')->select();
$title=array_column($title,'game_name');
$data=array_group_by($data,'time');
foreach ($day as $key => $value) {
if(array_key_exists($value, $data)){
foreach ($data[$value] as $kk => $vv) {
$game_name=$vv['game_name'];
$dayy[$value][$game_name]=$vv['cg'];
}
}
}
}elseif($key==3){
//充值数据
$data=$spend->field('game_id,game_name,date_format(FROM_UNIXTIME( pay_time),"%Y-%m-%d") AS time,sum(pay_amount) as cg')->where(array('pay_time'.$time))->where(array('game_id'=>array('gt',0)))->where(array('pay_status'=>1))->group('time,game_id')->order('cg desc')->select();
$title=$spend->field('game_name,sum(pay_amount) as cg')->where(array('pay_time'.$time))->where(array('game_id'=>array('gt',0)))->where(array('pay_status'=>1))->group('game_id')->order('cg desc')->select();
$title=array_column($title,'game_name');
$data=array_group_by($data,'time');
foreach ($day as $key => $value) {
if(array_key_exists($value, $data)){
foreach ($data[$value] as $kk => $vv) {
$game_name=$vv['game_name'];
$dayy[$value][$game_name]=$vv['cg'];
}
}
}
}
$this->assign('day0',$day[0]);
$this->assign('day1',$day[1]);
$this->assign('day2',$day[2]);
$this->assign('day3',$day[3]);
$this->assign('day4',$day[4]);
$this->assign('day5',$day[5]);
$this->assign('day6',$day[6]);
$this->assign('dayy',$dayy);
$this->assign('title1',$title[1]);
$this->assign('title0',$title[0]);
$this->assign('title2',$title[2]);
$this->assign('title3',$title[3]);
$this->assign('title4',$title[4]);
$this->display();
}
/*
* 数据概况
* @author 鹿文学
*/
public function data_profile() {
$user = D('User');
$spend = D('Spend');
$ishours = 0;
if($_REQUEST['key'] == 1 || empty($_REQUEST['key'])) {
$hours = ['00','01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23'];
$data['date'] = $hours;
$ishours = 1;
foreach($hours as $v) {
$data['news'][$v] = 0;
$data['money'][$v] = 0;
}
$between = ['between',array(strtotime('-1 day',strtotime(date('Y-m-d'))),strtotime(date('Y-m-d'))-1)];
// 新增用户
$hoursnews = $user->newsAdd(['register_time'=>$between],'news','time',5);
// 充值金额
$hoursmoney = $spend->totalAmountByGroup(['pay_time'=>$between],'money','time',5);
foreach($hours as $v) {
foreach($hoursnews as $h) {
$time = explode(' ',$h['time']);
if ($time[1] == $v){
$data['news'][$v] = (integer)$h['news'];break;
}
}
foreach($hoursmoney as $h) {
$time = explode(' ',$h['time']);
if ($time[1] == $v){
$data['money'][$v] = $h['money'];break;
}
}
}
} else {
$num = $_REQUEST['key'];
$flag= $num==4?365:($num==3?30:7);
$starttime = strtotime('-'.$flag.' day',strtotime(date('Y-m-d')));
$endtime = strtotime(date('Y-m-d'))-1;
$datelist = get_date_list($starttime,$endtime,$num==4?4:1);
$data['date'] = $datelist;
foreach($datelist as $k => $v) {
$data['news'][$v] = 0;
$data['money'][$v] = 0;
}
// 新增用户
$news = $user->newsAdd(['register_time'=>['between',[$starttime,$endtime]]],'news','time',$num==4?2:1);
// 充值金额
$money = $spend->totalAmountByGroup(['pay_time'=>['between',[$starttime,$endtime]]],'money','time',$num==4?2:1);
foreach($datelist as $v) {
foreach($news as $h) {
if ($v == $h['time']) {
$data['news'][$v] = (integer)$h['news'];break;
}
}
foreach($money as $h) {
if ($v == $h['time']) {
$data['money'][$v] = $h['money'];break;
}
}
}
}
foreach($data as $k => $v) {
if (is_array($v)) {
if ($k == 'date'){
if ($ishours == 1) {
$data[$k] = '"'.implode('","',array_map('addZeroToTime',$v)).'"';
} else {
$data[$k] = '"'.implode('","',$v).'"';
}
}else{
$data[$k] = implode(',',$v);
}
}
}
$this->assign('data',$data);
$this->meta_title = '数据概况';
$this->display();
}
public function data_profile2(){
$keytype=$_REQUEST['key']==""?1:$_REQUEST['key'];
$user=M('User','tab_');
$spend=M('Spend','tab_');
$deposit= M('Deposit','tab_');
if($keytype==1){
$time=$this->time2other();
$tt=$this->total(1);
//注册数据
$udata=$user->field('date_format(FROM_UNIXTIME( register_time),"%H") AS time,count(id) as count')->where('register_time'.$tt)->group('time')->select();
$xtime=$this->for_every_time_point($time,$udata,'time','count');
//充值数据
//spend
$sdata=$spend->field('date_format(FROM_UNIXTIME( pay_time),"%H") AS time,sum(pay_amount) as sum')->where('pay_time'.$tt)->where(array('pay_status'=>1))->group('time')->select();
$xstime=$this->for_every_time_point($time,$sdata,'time','sum');
foreach ($xstime as $key => $value) {
$stime[$key]['sum']=$value['sum'];
}
}elseif($keytype==2){//7天
$time=$this->time2other('7day');
$tt=$this->total(9);
//注册数据
$udata=$user->field('date_format(FROM_UNIXTIME( `register_time`),"%Y-%m-%d") AS time,count(id) as count')->where(array('register_time'.$tt))->group('time')->order('time asc')->select();
$xtime=$this->for_every_time_point($time,$udata,'time','count');
//充值数据
//spend
$sdata=$spend->field('date_format(FROM_UNIXTIME( pay_time),"%Y-%m-%d") AS time,sum(pay_amount) as sum')->where(array('pay_time'.$tt))->where(array('game_id'=>array('gt',0)))->where(array('pay_status'=>1))->group('time')->order('time asc')->select();
$xstime=$this->for_every_time_point($time,$sdata,'time','sum');
foreach ($xstime as $key => $value) {
$stime[$key]['sum']=$value['sum'];
}
}elseif($keytype==3){//30天
$time=$this->time2other('30day');
// var_dump($time);exit;
$tt=$this->total(10);
//注册数据
$udata=$user->field('date_format(FROM_UNIXTIME( `register_time`),"%Y-%m-%d") AS time,count(id) as count')->where(array('register_time'.$tt))->group('time')->order('time asc')->select();
$xtime=$this->for_every_time_point($time,$udata,'time','count');
//充值数据
//spend
$sdata=$spend->field('date_format(FROM_UNIXTIME( pay_time),"%Y-%m-%d") AS time,sum(pay_amount) as sum')->where(array('pay_time'.$tt))->where(array('game_id'=>array('gt',0)))->where(array('pay_status'=>1))->group('time')->order('time asc')->select();
$xstime=$this->for_every_time_point($time,$sdata,'time','sum');
foreach ($xstime as $key => $value) {
$stime[$key]['sum']=$value['sum'];
}
}elseif($keytype==4){//1年
$time=$this->time2other('12mounth');
$tt=$this->total(8);
//注册数据
$udata=$user->field('date_format(FROM_UNIXTIME( `register_time`),"%Y-%m") AS time,count(id) as count')->where(array('register_time'.$tt))->group('time')->order('time asc')->select();
$xtime=$this->for_every_time_point($time,$udata,'time','count');
//充值数据
//spend
$sdata=$spend->field('date_format(FROM_UNIXTIME( pay_time),"%Y-%m") AS time,sum(pay_amount) as sum')->where(array('pay_time'.$tt))->where(array('game_id'=>array('gt',0)))->where(array('pay_status'=>1))->group('time')->order('time asc')->select();
$xstime=$this->for_every_time_point($time,$sdata,'time','sum');
foreach ($xstime as $key => $value) {
$stime[$key]['sum']=$value['sum'];
}
}
// 前台显示
// X轴日期
if($keytype==1){
$xAxis="[";
foreach ($time as $tk => $tv) {
$xAxis.="'".$tk.":00',";
}
$xAxis.="]";
}elseif($keytype==2){
sort($time);
$xAxis="[";
foreach ($time as $tk => $tv) {
$xAxis.="'".$tv."',";
}
$xAxis.="]";
}elseif($keytype==3){
sort($time);
$xAxis="[";
foreach ($time as $tk => $tv) {
$xAxis.="'".$tv."',";
}
$xAxis.="]";
}elseif($keytype==4){
sort($time);
$xAxis="[";
foreach ($time as $tk => $tv) {
$xAxis.="'".$tv."',";
}
$xAxis.="]";
}
//x轴注册数据
$xzdate="[";
foreach ($xtime as $key => $value) {
$xzdate.="".$value['count'].",";
}
$xzdate.="]";
//x轴充值数据
$xsdate="[";
foreach ($stime as $key => $value) {
$xsdate.="".$value['sum'].",";
}
$xsdate.="]";
$this->assign('xzdate',$xzdate);
$this->assign('xsdate',$xsdate);
$this->assign('xAxis',$xAxis);
$this->assign('qingxie',count($time));
$this->meta_title = '数据概况';
$this->display();
}
/**
* [数据折线 分配每个时间段]
* @param [type] $time [时间点]
* @return [type] [description]
*/
private function for_every_time_point($time,$data,$key1,$key2){
foreach ($time as $key => $value) {
$newdata[$key][$key2]=0;
foreach ($data as $k => $v) {
if($v[$key1]==$key){
$newdata[$key][$key2]=$v[$key2];
}
}
}
return $newdata;
}
//把时间戳 当前时间一天分成24小时 前七天 前30天 前12个月
function time2other($type='day'){
if($type=='day'){//一天分成24小时
$start = mktime(0,0,0,date("m"),date("d"),date("y"));
for($i = 0; $i < 24; $i++){
static $x=0;
$xx=$x++;
if($xx<10){
$xxx='0'.$xx;
}else{
$xxx=$xx;
}
$b = $start + ($i * 3600);
$e = $start + (($i+1) * 3600)-1;
$time[$xxx]="between $b and $e";
}
}
if($type=='7day'){
$ttime=array_reverse($this->every_day());
foreach ($ttime as $key => $value) {
$time[$value]=$value;
}
}
if($type=='30day'){
$ttime=array_reverse($this->every_day(30));
foreach ($ttime as $key => $value) {
$time[$value]=$value;
}
}
if($type=='12mounth'){
$ttime=array_reverse(before_mounth());
foreach ($ttime as $key => $value) {
$time[$value]=$value;
}
}
return $time;
}
//以当前日期 默认前七天
private function every_day($m=7){
$time=array();
for ($i=0; $i <$m ; $i++) {
$time[]=date('Y-m-d',mktime(0,0,0,date('m'),date('d')-$i,date('Y')));
}
return $time;
}
private function total($type) {
switch ($type) {
case 1: { // 今天
$start=mktime(0,0,0,date('m'),date('d'),date('Y'));
$end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
};break;
case 2: { // 本周
//当前日期
$sdefaultDate = date("Y-m-d");
//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
$first=1;
//获取当前周的第几天 周日是 0 周一到周六是 1 - 6
$w=date('w',strtotime($sdefaultDate));
//获取本周开始日期,如果$w是0则表示周日减去 6 天
$week_start=date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days'));
//本周结束日期
$week_end=date('Y-m-d',strtotime("$week_start +6 days"));
//当前日期
$sdefaultDate = date("Y-m-d");
//$first =1 表示每周星期一为开始日期 0表示每周日为开始日期
$first=1;
//获取当前周的第几天 周日是 0 周一到周六是 1 - 6
$w=date('w',strtotime($sdefaultDate));
//获取本周开始日期,如果$w是0则表示周日减去 6 天
$start=strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days');
//本周结束日期
$end=$start+7*24*60*60-1;
};break;
case 3: { // 本月
$start=mktime(0,0,0,date('m'),1,date('Y'));
$end=mktime(0,0,0,date('m')+1,1,date('Y'))-1;
};break;
case 4: { // 本年
$start=mktime(0,0,0,1,1,date('Y'));
$end=mktime(0,0,0,1,1,date('Y')+1)-1;
};break;
case 5: { // 昨天
$start=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
};break;
case 6: { // 上周
$start=mktime(0, 0 , 0,date("m"),date("d")-date("w")+1-7,date("Y"));
$end=mktime(23,59,59,date("m"),date("d")-date("w")+7-7,date("Y"));
};break;
case 7: { // 上月
$start=mktime(0, 0 , 0,date("m")-1,1,date("Y"));
$end=mktime(23,59,59,date("m") ,0,date("Y"));
};break;
case 8: { // 上一年
$start=mktime(0,0,0,1,1,date('Y')-1);
$end=mktime(0,0,0,1,1,date('Y'))-1;
};break;
case 9: { // 前七天
$start = mktime(0,0,0,date('m'),date('d')-6,date('Y'));
$end=mktime(23,59,59,date('m'),date('d'),date('Y'));
};break;
case 10: { // 前30天
$start = mktime(0,0,0,date('m'),date('d')-29,date('Y'));
$end=mktime(23,59,59,date('m'),date('d'),date('Y'));
};break;
default:
$start='';$end='';
}
return " between $start and $end ";
}
private function huanwei($total) {
$total = empty($total)?'0':trim($total.' ');
if(!strstr($total,'.')){
$total=$total.'.00';
}
$len = strlen($total);
if ($len>7) { // 万
$total = (round(($total/10000),2)).'w';
}
return $total;
}
/*
* LTV统计
* @param integer $p 当前页
* @author 鹿文学
*/
public function ltv($p=1) {
$page = intval($p);
$page = $page ? $page : 1; //默认显示第一页数据
if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}
$start = $_GET['start']= I('start',date('Y-m-d',strtotime('-30 day')));
$end = I('end',date('Y-m-d',strtotime('-1 day')));
$end = strtotime($end)>=strtotime(date('Y-m-d'))?date('Y-m-d',strtotime('-1 day')):$end;
$_GET['end'] = $end;
$list = D('user')->ltv(strtotime($start),strtotime($end));
$count = count($list);
$data = array_slice($list,($page-1)*$row,$row,true);
file_put_contents(dirname(__FILE__).'/access_data_ltv.txt',json_encode($list));
$page = set_pagination($count,$row);
if($page) {$this->assign('_page', $page);}
$this->meta_title = 'LTV统计';
$this->assign('data',$data);
$this->display();
}
}