// +---------------------------------------------------------------------- namespace Admin\Controller; use User\Api\UserApi as UserApi; /** * 后台首页控制器 * @author 麦当苗儿 */ class IndexChartSetController extends AdminController { public $beginTime; public $endTime; public $date; public $nowdata; public $UserModel; public $ChartModel; public $LoginModel; public $SpendModel; public $PromoteModel; public $addid; public $adddata; public $continue=false; public function _initialize(){ //初始化 $this->UserModel =M("User","tab_"); $this->ChartModel =M("IndexChart","tab_"); $this->LoginModel =M("user_login_record","tab_"); $this->SpendModel =M("spend","tab_"); $this->PromoteModel =M("promote","tab_"); $this->nowdata =time(); } /** * 更新某月数据 */ public function setChartCount() { // $month = I("count_date"); // if(empty($month)) die("参数错误"); $date = I("date"); $this->date = $date; $tarry = explode('-',$date); $this->beginTime=mktime(0,0,0,$tarry[1],$tarry[2],$tarry[0]); $this->endTime=mktime(0,0,0,$tarry[1],$tarry[2]-0+1,$tarry[0])-1; set_time_limit(0); $t1 = microtime(true); $this->newUser(); $this->activeUser(); $this->payUser(); $this->payMoney(); $this->promoteNew(); $this->allCount(); $this->createDb(); $t2 = microtime(true); die("success runtime:".round($t2-$t1,3).'s'); } public function setMonth($month) { if($month == 11){ $j = date("d"); }else{ $j = date("t",strtotime("2019-{$month}-1")); } $start_time = strtotime(date("Y-{$month}-01")); for($i=0;$i<$j;$i++){ $date = date('Y-m-d',$start_time+$i*86400); $this->setMonthCount($date); } } // public function setMonthCount($date) { $this->date = $date; $tarry = explode('-',$date); $this->beginTime=mktime(0,0,0,$tarry[1],$tarry[2],$tarry[0]); $this->endTime=mktime(0,0,0,$tarry[1],$tarry[2]-0+1,$tarry[0])-1; set_time_limit(0); $t1 = microtime(true); $this->newUser(); $this->activeUser(); $this->payUser(); $this->payMoney(); $this->promoteNew(); $this->allCount(); $this->createDb(); $t2 = microtime(true); echo ("{$this->date} success runtime:".round($t2-$t1,3).'s'.PHP_EOL); # code... } //获取每日的增长用户 public function newUser() { //获取当天的数据 // $userdata =array( // "date"=>$this->beginTime, // "create_time"=>$this->nowdata // ); $map = ['register_time'=>['between',[$this->beginTime,$this->endTime]],"puid"=>0];//0不是小号 $hoursnews = $this->UserModel->field('FROM_UNIXTIME(register_time, "%H") as time,COUNT(1) AS news') ->where($map) ->group("time") ->select(); $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']; $user_hours = array(); foreach($hours as $v) { $user_hours[$v] = 0; } foreach($hoursnews as $h) { $user_hours[$h['time']] = (integer)$h['news']; } $this->adddata["new_user_hours"] = serialize($user_hours); unset($user_hours); //获取当日所有的注册用户 $userList = $this->UserModel->field('count(1) count') ->where($map) ->find(); $this->adddata["new_user_count"] = $userList['count']; } /** * 活跃用户计算 */ public function activeUser() { $map = ['login_time'=>['between',[$this->beginTime,$this->endTime]]];//0不是小号 $hoursnews = $this->LoginModel->field('FROM_UNIXTIME(login_time, "%H") as time,COUNT(DISTINCT user_id) AS active') ->where($map) ->group("time") ->select(); $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']; $user_hours = array(); foreach($hours as $v) { $user_hours[$v] = 0; } foreach($hoursnews as $h) { $user_hours[$h['time']] = (integer)$h['active']; } $this->adddata["active_user_hours"] = serialize($user_hours); unset($user_hours); // $activeCount = $this->LoginModel->field('count(DISTINCT user_id) count') // ->where($map) // ->find(); //$this->adddata["active_user_count"] = $activeCount['count']; //获取活跃用户列表 $activeCount = $this->LoginModel->field('user_id') ->where($map) ->group('user_id') ->select(); $this->adddata["active_user_count"] = count($activeCount); $this->adddata["active_user_list"] = serialize(array_column($activeCount,'user_id')); unset($activeCount); } /** * 计算充值用户 */ public function payUser() { $map = ['pay_time'=>['between',[$this->beginTime,$this->endTime]],'pay_status'=>1];//1支付成功 $hoursnews = $this->SpendModel->field('FROM_UNIXTIME(pay_time, "%H") as time,COUNT(DISTINCT user_id) AS payuser') ->where($map) ->group("time") ->select(); $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']; $user_hours = array(); foreach($hours as $v) { $user_hours[$v] = 0; } foreach($hoursnews as $h) { $user_hours[$h['time']] = (integer)$h['payuser']; } $this->adddata["pay_user_hours"] = serialize($user_hours); unset($user_hours); // $activeCount = $this->LoginModel->field('count(DISTINCT user_id) payuser') // ->where($map) // ->find(); // $this->adddata["pay_user_count"] = $activeCount['payuser']; $activeCount = $this->SpendModel->field('user_id') ->where($map) ->group('user_id') ->select(); $this->adddata["pay_user_count"] = count($activeCount); $this->adddata["pay_user_list"] = serialize(array_column($activeCount,'user_id')); unset($activeCount); // $res = $this->ChartModel->save($savedata); } /** * 充值金额计算 */ public function payMoney() { $map = ['pay_time'=>['between',[$this->beginTime,$this->endTime]],"pay_status"=>1];//1支付成功 $hoursnews = $this->SpendModel->field('FROM_UNIXTIME(pay_time, "%H") as time,sum(pay_amount) AS money') ->where($map) ->group("time") ->select(); $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']; $user_hours = array(); foreach($hours as $v) { $user_hours[$v] = 0; } foreach($hoursnews as $h) { $user_hours[$h['time']] = (integer)$h['money']; } $this->adddata["pay_money_hours"] = serialize($user_hours); unset($user_hours); $activeCount = $this->SpendModel->field('sum(pay_amount) AS money') ->where($map) ->find(); $this->adddata["pay_money_count"] = $activeCount['money']?:0; } /** * 新增推广 */ public function promoteNew() { $map = ['create_time'=>['between',[$this->beginTime,$this->endTime]]]; $hoursnews = $this->PromoteModel->field('FROM_UNIXTIME(create_time, "%H") as time,COUNT(1) AS news') ->where($map) ->group("time") ->select(); $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']; $user_hours = array(); foreach($hours as $v) { $user_hours[$v] = 0; } foreach($hoursnews as $h) { $user_hours[$h['time']] = (integer)$h['news']; } $this->adddata["promote_new_hours"] = serialize($user_hours);//promote_new_hours unset($user_hours); $activeCount = $this->PromoteModel->field('COUNT(1) AS news') ->where($map) ->find(); $this->adddata["promote_new_count"] = $activeCount['news']; } /** * 统计其他综合信息 * 1. 7天活跃 * 2. 平台累计用户 * 3. 累计付费用户 */ public function allCount() { $user = D('User'); $spend = D('Spend'); $promote = D('Promote'); $allcount = array(); $allcount['user_count'] = $user->old(); $allcount['active_count'] =$user->active(['tab_user_login_record.login_time'=>['between',[mktime(0,0,0,date('m'),date('d')-7,date('Y')),mktime(0,0,0,date('m'),date('d'),date('Y'))-1]]]); $allcount['player_count'] =$spend->player(); $allcount['money_sum'] =$spend->totalAmount(); $allcount['promote_sum'] =$promote->total(); $this->adddata["all_count"] = serialize($allcount); } public function createDb() { $this->adddata['date']=$this->beginTime; $this->adddata['create_time']=$this->nowdata; // dump($this->adddata); $this->ChartModel->add($this->adddata); } }