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.

1142 lines
42 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 {
const COUNTLIMIT = 10;
public function overview(){
$shuju = json_decode(M('IndexChart','tab_')->field("all_count")->order('`date` desc')->find()["all_count"],true);
$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);
//昨天
$yesterdaydate=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$promoteyesterdaydate=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
// 游戏排行
$type=$_REQUEST['type']?:1;
if($type==1){
$list_data=$this->data_order($today,$yesterday,true);
$gamedateinfo = explode(" ",$today);
$yesterdaydate=$gamedateinfo[2];
}elseif($type==2){
$list_data=$this->data_order($thisweek,$lastweek);
$gamedateinfo = explode(" ",$thisweek);
}elseif($type==3){
$list_data=$this->data_order($thismounth,$lastmounth);
$gamedateinfo = explode(" ",$thismounth);
}elseif($type==4){
$list_data=$this->data_order($thisyear,$lastyear);
$gamedateinfo = explode(" ",$thisyear);
}
// 推广员排行
$category = $_REQUEST['category']?:1;
if($category==1){
$promote_data = $this->promote_data_order($today,$yesterday,true);
$promotedateinfo = explode(" ",$today);
$promoteyesterdaydate = $promotedateinfo[2];
}elseif($category==2){
$promote_data = $this->promote_data_order($thisweek,$lastweek);
$promotedateinfo = explode(" ",$thisweek);
}elseif($category==3){
$promote_data = $this->promote_data_order($thismounth,$lastmounth);
$promotedateinfo = explode(" ",$thismounth);
}elseif($category==4){
$promote_data = $this->promote_data_order($thisyear,$lastyear);
$promotedateinfo = explode(" ",$thisyear);
}
$this->assign('zhuce',$list_data['reg']);
$this->assign('active',$list_data['active']);
$this->assign('pay',$list_data['pay']);
$this->assign('game_chart',$list_data['chart']);
$this->assign('game_date_info',"统计[ ".date("Y/m/d",$gamedateinfo[2])." -- ".date("Y/m/d",$yesterdaydate)." ]之间数据");
$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']);
$this->assign('promote_date_info',"统计[ ".date("Y/m/d",$promotedateinfo[2])." -- ".date("Y/m/d",$promoteyesterdaydate)." ]之间数据");
// 日历
R('Index/calendar');
// 折线图
if(strtotime($_REQUEST['start']) == mktime(0,0,0,date('m'),date('d'),date('Y'))){
//今日时时
$this->nowday($_REQUEST['num']);
}else{
$this->foldLineDiagram($_REQUEST['start'],$_REQUEST['end'],$_REQUEST['num']);
}
// $this->foldLineDiagram($_REQUEST['start'],$_REQUEST['end'],$_REQUEST['num']);
$this->display();
}
/*
* 今日折线图
*/
public function nowday($num)
{
$starttime = mktime(0,0,0,date('m'),date('d'),date('Y'));
$endtime = $starttime+86399;
$start = date('Y-m-d',$starttime);
$end = date('Y-m-d',$endtime);
$user = D('User');
$spend = D('Spend');
$promote = D('Promote');
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;
}
}
}
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);
}
}
}
if ($flag) {
echo json_encode($data);
} else {
$this->assign('foldline',$data);
$this->assign('table',$table);
$this->assign('num',$num);
}
# code...
}
/*
* 折线图
* @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');
$promote = D('Promote');
if ($start == $end) { // 单天
if ($start == date('Y-m-d',strtotime('-1 day'))) {$num = 2;}
$data['date'] = [$start];
$data['hours'] = 1;
$tm = strtotime($start);
$dbdata = M("IndexChart","tab_")->field("new_user_count,new_user_hours,active_user_count,active_user_hours,pay_user_count,pay_user_hours,pay_money_count,pay_money_hours,promote_new_count")->where("`date` = '{$tm}'")->find();
$data['news'] = json_decode($dbdata["new_user_hours"],true);
$data['active'] = json_decode($dbdata["active_user_hours"],true);
$data['player']=json_decode($dbdata["pay_user_hours"],true);
$data['money']=json_decode($dbdata["pay_money_hours"],true);
} else {
$datelist = get_date_list($starttime,$endtime,$num==7?4:1);
$data['date'] = $datelist;
$data['hours'] = 0;
$map = array(
"date"=>['between',[$starttime,$endtime]]
);
//获取记录
$count1 = array();
$active_user_list = [];
$pay_user_list = [];
$dbdata = M("IndexChart","tab_")->field("new_user_count,active_user_count,pay_user_count,pay_money_count,promote_new_count")
->where($map)->select();
foreach($dbdata as $k => $v) {
$data['news'][$k] = $v['new_user_count'];
$data['active'][$k] = $v['active_user_count'];
$data['player'][$k] = $v['pay_user_count'];
$data['money'][$k] = $v['pay_money_count'];
}
}
foreach($data as $k => $v) {
// dump($k);
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(RUNTIME_PATH.'/access_data_foldline.txt',json_encode($export));
if ($flag) {
echo json_encode($data);
} else {
$this->assign('foldline',$data);
$this->assign('table',$table);
$this->assign('num',$num);
}
# code...
}
/**
* 获取推广员排行
* @param [type] $starttime 当前时间
* @param [type] $endtime 比较时间
* @return void
*/
private function promote_data_order($starttime,$endtime,$today =false){
$PromoteCount = M("PromoteCount","tab_");
$data = array();
//注册排行
//1.获取注册前十名
if($today){
$map = ["puid"=>0,"fgame_id"=>["gt",0]];//0不是小号
$new_user_count = M("User","tab_")->field('COUNT(1) AS cg,promote_id,promote_account')->where($map)->where("register_time {$starttime}")->group("promote_id")->order("cg desc")->limit(self::COUNTLIMIT)->select();
}else{
$new_user_count = $PromoteCount->field("promote_id,promote_account,sum(new_user_count) cg")->where("`date` {$starttime}")->group("promote_id")->order("cg desc")->having("cg > 0")->limit(self::COUNTLIMIT)->select();
}
$old_user_count = $PromoteCount->field("promote_id,promote_account,sum(new_user_count) cg")->where("`date` {$endtime}")->group("promote_id")->order("cg desc")->limit(self::COUNTLIMIT)->select();
//2.设置排名
$data["reg"] = $this->setRand($new_user_count,$old_user_count);
//活跃排行
//1.获取注册前十名
if($today){
$map = ["l.promote_id"=>["gt",0]];//0不是小号
$new_active_count = M()->table("tab_user_login_record l")->field('COUNT(DISTINCT user_id) AS cg,l.promote_id,p.account promote_account')
->where($map)->where("l.login_time {$starttime}")
->group("promote_id")
->join("tab_promote p ON l.promote_id = p.id")
->order("cg desc")
->limit(self::COUNTLIMIT)->select();
}else{
$new_active_count = $PromoteCount->field("promote_id,promote_account,sum(active_user_count) cg")->where("`date` {$starttime}")->group("promote_id")->order("cg desc")->having("cg > 0")->limit(self::COUNTLIMIT)->select();
if(count($new_active_count) > 0){
$listarr = array();
$promote_id_str = '';
for ($i=0; $i < count($new_active_count); $i++) {
# code...
$promote_id_str .= ($new_active_count[$i]['promote_id'].",");
$new_active_count[$i]["active_user_list"] = [];
$listarr[$new_active_count[$i]['promote_id']] = $new_active_count[$i];
}
$promote_id_str = \rtrim($promote_id_str,",");
//查询
$active_user_list = $PromoteCount->field("active_user_list,promote_id")->where("`date` {$starttime} AND promote_id in ({$promote_id_str})")->select();
for ($i=0; $i < count($active_user_list); $i++) {
$listarr[$active_user_list[$i]["promote_id"]]["active_user_list"] += json_decode($active_user_list[$i]["active_user_list"],true);
}
foreach ($listarr as $key => $value) {
$value['cg'] = count(array_flip(array_flip($value['active_user_list'])));
unset($value['active_user_list']);
$listarr[$key] = $value;
}
$last_names = array_column($listarr,'cg');
array_multisort($last_names,SORT_DESC,$listarr);
$new_active_count = $listarr;
}
}
//获取活跃总量排行
$old_active_count = $PromoteCount->field("promote_id,promote_account,sum(active_user_count) cg")->where("`date` {$endtime}")->group("promote_id")->order("cg desc")->limit(self::COUNTLIMIT)->select();
//2.设置排名
$data["active"] = $this->setRand($new_active_count,$old_active_count);
//支付排行
//1.获取注册前十名
if($today){
$map = ["pay_status"=>1,"promote_id"=>["gt",0]];
$new_money_count = M("Spend","tab_")->field('sum(pay_amount) AS cg,promote_id,promote_account')->where($map)->where("pay_time {$starttime}")->group("promote_id")->order("cg desc")->limit(self::COUNTLIMIT)->select();
}else{
$new_money_count = $PromoteCount->field("promote_id,promote_account,sum(pay_money_count) cg")->where("`date` {$starttime}")->group("promote_id")->order("cg desc")->having("cg > 0")->limit(self::COUNTLIMIT)->select();
}
$old_money_count = $PromoteCount->field("promote_id,promote_account,sum(pay_money_count) cg")->where("`date` {$endtime}")->group("promote_id")->order("cg desc")->limit(self::COUNTLIMIT)->select();
//2.设置排名
$data["pay"] = $this->setRand($new_money_count,$old_money_count);
//设置chart
$char = array();
foreach ($data as $key => $value) {
for ($i=0; $i < count($value); $i++) {
$t = $value[$i];
if(empty($char[$t['promote_id']])){
//不存在
$char[$t['promote_id']] = array(
// "promote_id" =>$t['promote_id'],
"promote_account" =>$t['promote_account'],
"reg" => 0,
"active"=>0,
"pay"=>0
);
}
$char[$t['promote_id']][$key] = $t['cg'];
}
}
foreach ($char as $key => $value) {
$data["chart"]["promote"] .= ("\"".$value['promote_account']."\",");
$data["chart"]["reg"] .= ( $value['reg'].",");
$data["chart"]["active"] .= ( $value['active'].",");
$data["chart"]["pay"] .= ( $value['pay'].",");
}
$data["chart"]["promote"] = \rtrim($data["chart"]["promote"],",");
$data["chart"]["reg"] = \rtrim($data["chart"]["reg"],",");
$data["chart"]["active"] = \rtrim($data["chart"]["active"],",");
$data["chart"]["pay"] = \rtrim($data["chart"]["pay"],",");
return $data;
}
/**
* 获取游戏排行
* @param [type] $starttime 当前时间
* @param [type] $endtime 比较时间
* @return void
*/
private function data_order($starttime,$endtime,$today =false){
$GameCount = M("GameCount","tab_");
$data = array();
//注册排行
//1.获取注册前十名
if($today){
$map = ["puid"=>0,"fgame_id"=>["gt",0]];//0不是小号
$new_user_count = M("User","tab_")->field('COUNT(1) AS cg,fgame_id game_id,fgame_name game_name')->where($map)->where("register_time {$starttime}")->group("fgame_id")->order("cg desc")->limit(self::COUNTLIMIT)->select();
}else{
$new_user_count = $GameCount->field("game_id,game_name,sum(new_user_count) cg")->where("`date` {$starttime}")->group("game_id")->order("cg desc")->having("cg > 0")->limit(self::COUNTLIMIT)->select();
}
$old_user_count = $GameCount->field("game_id,game_name,sum(new_user_count) cg")->where("`date` {$endtime}")->group("game_id")->order("cg desc")->limit(self::COUNTLIMIT)->select();
//2.设置排名
$data["reg"] = $this->setRand($new_user_count,$old_user_count,"game_id");
//活跃排行
//1.获取注册前十名
if($today){
$map = ["game_id"=>["gt",0]];
$new_active_count = M("user_login_record","tab_")->field('COUNT(DISTINCT user_id) AS cg,game_id,game_name')->where($map)->where("login_time {$starttime}")->group("game_id")->order("cg desc")->limit(self::COUNTLIMIT)->select();
}else{
$new_active_count = $GameCount->field("game_id,game_name,sum(active_user_count) cg")->where("`date` {$starttime}")->group("game_id")->order("cg desc")->having("cg > 0")->limit(self::COUNTLIMIT)->select();
if(count($new_active_count) > 0){
$listarr = array();
$promote_id_str = '';
for ($i=0; $i < count($new_active_count); $i++) {
# code...
$promote_id_str .= ($new_active_count[$i]['game_id'].",");
$new_active_count[$i]["active_user_list"] = [];
$listarr[$new_active_count[$i]['game_id']] = $new_active_count[$i];
}
$promote_id_str = \rtrim($promote_id_str,",");
//查询
$active_user_list = $GameCount->field("active_user_list,game_id")->where("`date` {$starttime} AND game_id in ({$promote_id_str})")->select();
for ($i=0; $i < count($active_user_list); $i++) {
$listarr[$active_user_list[$i]["game_id"]]["active_user_list"] += json_decode($active_user_list[$i]["active_user_list"],true);
}
foreach ($listarr as $key => $value) {
$value['cg'] = count(array_flip(array_flip($value['active_user_list'])));
unset($value['active_user_list']);
$listarr[$key] = $value;
}
$last_names = array_column($listarr,'cg');
array_multisort($last_names,SORT_DESC,$listarr);
$new_active_count = $listarr;
}
}
$old_active_count = $GameCount->field("game_id,game_name,sum(active_user_count) cg")->where("`date` {$endtime}")->group("game_id")->order("cg desc")->limit(self::COUNTLIMIT)->select();
//2.设置排名
$data["active"] = $this->setRand($new_active_count,$old_active_count,"game_id");
//支付排行
//1.获取注册前十名
if($today){
$map = ["pay_status"=>1,"game_id"=>["gt",0]];
$new_money_count = M("Spend","tab_")->field('sum(pay_amount) AS cg,game_id,game_name')->where($map)->where("pay_time {$starttime}")->group("game_id")->order("cg desc")->limit(self::COUNTLIMIT)->select();
}else{
$new_money_count = $GameCount->field("game_id,game_name,sum(pay_money_count) cg")->where("`date` {$starttime}")->group("game_id")->order("cg desc")->having("cg > 0")->limit(self::COUNTLIMIT)->select();
}
$old_money_count = $GameCount->field("game_id,game_name,sum(pay_money_count) cg")->where("`date` {$endtime}")->group("game_id")->order("cg desc")->limit(self::COUNTLIMIT)->select();
//2.设置排名
$data["pay"] = $this->setRand($new_money_count,$old_money_count,"game_id");
//设置chart
$char = array();
foreach ($data as $key => $value) {
for ($i=0; $i < count($value); $i++) {
$t = $value[$i];
if(empty($char[$t['game_id']])){
//不存在
$char[$t['game_id']] = array(
// "promote_id" =>$t['promote_id'],
"game_name" =>$t['game_name'],
"reg" => 0,
"active"=>0,
"pay"=>0
);
}
$char[$t['game_id']][$key] = $t['cg'];
}
}
foreach ($char as $key => $value) {
$data["chart"]["game"] .= ("\"".$value['game_name']."\",");
$data["chart"]["reg"] .= ( $value['reg'].",");
$data["chart"]["active"] .= ( $value['active'].",");
$data["chart"]["pay"] .= ( $value['pay'].",");
}
$data["chart"]["game"] = \rtrim($data["chart"]["game"],",");
$data["chart"]["reg"] = \rtrim($data["chart"]["reg"],",");
$data["chart"]["active"] = \rtrim($data["chart"]["active"],",");
$data["chart"]["pay"] = \rtrim($data["chart"]["pay"],",");
return $data;
}
//设置排名
private function setRand($randdata,$oldrand,$field = "promote_id")
{
//数据是否为空
$rcount = count($randdata);
if($rcount < 1){return [];}
//判断是否有旧排名
$isold = true;
$ocount = count($oldrand);
if($ocount < 1){
$isold = false;
}else{
$oldrand = array_column($oldrand,$field);
$oldrand=array_flip($oldrand);//反转数组
}
for ($i=0; $i <$rcount; $i++) {
$rand = $i+1;
$randdata[$i]['rand'] =$rand;
if($isold){
if(empty($oldrand[$randdata[$i][$field]])){
$randdata[$i]['change'] = 0;
}else{
$randdata[$i]['change'] = $rand - $oldrand[$randdata[$i][$field]];
}
}else{
$randdata[$i]['change'] = $rand-1 - self::COUNTLIMIT;
}
}
return $randdata;
# code...
}
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;
case 11: { // 前天
$start=mktime(0,0,0,date('m'),date('d')-2,date('Y'));
$end=mktime(0,0,0,date('m'),date('d')-1,date('Y'))-1;
};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(RUNTIME_PATH.'/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();
// }
public function ltv($p=1, $row=10)
{
$start = I('start',date('Y-m-d',strtotime('-30 day')));
$end = I('end',date('Y-m-d',strtotime('-1 day')));
$start = strtotime($start);
$end = strtotime($end) + 86400;
$start_time = strtotime("+".(($p-1)*$row)." day", $start);
$end_time = strtotime("+".($p*$row+1)." day", $start);
$end_time = $end_time > $end ? $end : $end_time;
$map = " 1=1 ";
$game_map = "";
if (isset($_REQUEST['game_name']) && isset($_REQUEST['server_id'])) {
$game_ids = implode(',', array_column(getGameByName($_REQUEST['game_name'], $_REQUEST['game_type']), 'id'));
$map .= " AND game_id in ({$game_ids})";
$game_map = " AND EXISTS (SELECT upi.user_id FROM tab_user_play_info as upi where upi.game_id in ({$game_ids}) AND upi.user_id = u.id ";
if (isset($_REQUEST['server_id'])) {
$game_map .= " AND upi.server_id = '{$_REQUEST['server_id']}' ";
$map .= " AND server_id = '{$_REQUEST['server_id']}' ";
};
$game_map .= ") ";
} else if (isset($_REQUEST['game_name'])) {
$game_ids = implode(',', array_column(getGameByName($_REQUEST['game_name'], $_REQUEST['game_type']), 'id'));
$map .= " AND game_id in ({$game_ids})";
$game_map = " AND EXISTS (SELECT upi.user_id FROM tab_user_play as upi where upi.game_id in ({$game_ids}) AND upi.user_id = u.id ";
$game_map .= ") ";
}
// 获取包含的用户id
$users = M()->query("
SELECT
count(*) as total_count,
GROUP_CONCAT(u.id) as ids,
FROM_UNIXTIME(u.register_time, '%Y-%m-%d') as reg_date
FROM
tab_user u
WHERE
u.register_time BETWEEN {$start_time}
AND {$end_time} {$game_map}
GROUP BY reg_date
ORDER BY reg_date asc
");
$result = [];
foreach ($users as $index => $item) {
$ltv_start = strtotime($item['reg_date']);
$ltv_end = strtotime($item['reg_date']) + 30 * 86400;
$ltv = M()->query("
SELECT
ROUND(SUM(if (pay_time BETWEEN {$ltv_start} and ".($ltv_start+86400 * 1).", pay_amount, 0))/{$item['total_count']}, 2) as ltv_1,
ROUND(SUM(if (pay_time BETWEEN {$ltv_start} and ".($ltv_start+86400 * 2).", pay_amount, 0))/{$item['total_count']}, 2) as ltv_2,
ROUND(SUM(if (pay_time BETWEEN {$ltv_start} and ".($ltv_start+86400 * 3).", pay_amount, 0))/{$item['total_count']}, 2) as ltv_3,
ROUND(SUM(if (pay_time BETWEEN {$ltv_start} and ".($ltv_start+86400 * 4).", pay_amount, 0))/{$item['total_count']}, 2) as ltv_4,
ROUND(SUM(if (pay_time BETWEEN {$ltv_start} and ".($ltv_start+86400 * 5).", pay_amount, 0))/{$item['total_count']}, 2) as ltv_5,
ROUND(SUM(if (pay_time BETWEEN {$ltv_start} and ".($ltv_start+86400 * 6).", pay_amount, 0))/{$item['total_count']}, 2) as ltv_6,
ROUND(SUM(if (pay_time BETWEEN {$ltv_start} and ".($ltv_start+86400 * 7).", pay_amount, 0))/{$item['total_count']}, 2) as ltv_7,
ROUND(SUM(if (pay_time BETWEEN {$ltv_start} and ".($ltv_start+86400 * 14).", pay_amount, 0))/{$item['total_count']}, 2) as ltv_14,
ROUND(SUM(if (pay_time BETWEEN {$ltv_start} and ".($ltv_start+86400 * 30).", pay_amount, 0))/{$item['total_count']}, 2) as ltv_30,
SUM(if (pay_time BETWEEN {$ltv_start} and ".($ltv_start+86400 * 30).", pay_amount, 0)) as total_amt
FROM
tab_spend
WHERE
{$map}
AND pay_time BETWEEN {$ltv_start}
AND {$ltv_end}
AND user_id IN ({$item['ids']}) AND pay_status = 1
");
$result[] = [
'date'=>$item['reg_date'],
'total_amt'=>$ltv[0]['total_amt'] ?: 0,
'user_count'=>$item['total_count'] ?: 0,
'ltv_1'=>$ltv[0]['ltv_1'] ?: 0,
'ltv_2'=>$ltv[0]['ltv_2'] ?: 0,
'ltv_3'=>$ltv[0]['ltv_3'] ?: 0,
'ltv_4'=>$ltv[0]['ltv_4'] ?: 0,
'ltv_5'=>$ltv[0]['ltv_5'] ?: 0,
'ltv_6'=>$ltv[0]['ltv_6'] ?: 0,
'ltv_7'=>$ltv[0]['ltv_7'] ?: 0,
'ltv_14'=>$ltv[0]['ltv_14'] ?: 0,
'ltv_30'=>$ltv[0]['ltv_30'] ?: 0
];
unset($user[$index]);
}
$data = [];
$k = 0;
for($i=$start_time; $i<$end_time; $i+=86400) {
if (($result[$k]['date'] == date("Y-m-d", $i))) {
$data[] = $result[$k];
$k += 1;
} else {
$data[] = [
'date'=>date("Y-m-d", $i),
'total_amt'=> 0,
'user_count'=> 0,
'ltv_1'=> 0,
'ltv_2'=> 0,
'ltv_3'=> 0,
'ltv_4'=> 0,
'ltv_5'=> 0,
'ltv_6'=> 0,
'ltv_7'=> 0,
'ltv_14'=> 0,
'ltv_30'=> 0
];
}
}
$count = ($end-$start)/86400;
$page = set_pagination($count,$row);
if($page) {$this->assign('_page', $page);}
$this->meta_title = 'LTV统计';
$this->assign('data',$data);
$this->display('ltv');
}
}