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.

373 lines
13 KiB
PHTML

2 years ago
<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use User\Api\UserApi as UserApi;
/**
* 后台首页控制器
* @author 麦当苗儿 <zuojiazi@vip.qq.com>
*/
class PresidentIndexChartSetController extends AdminController {
public $beginTime;
public $endTime;
public $date;
public $nowdata;
public $UserModel;
public $ChartModel;
public $LoginModel;
public $SpendModel;
public $PromoteModel;
public $President_id;
public $President_data;
public $reset=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();
$group_concat_max_len = M()->execute("SET SESSION group_concat_max_len = 4294967295");
echo "set group_concat_max_len ok,beging count".PHP_EOL;
if(I("reset")) $this->reset = true;
echo "<pre>";
}
/**
* 每日更新接口
*/
public function setChartCount()
{
$begin = date("Y-m-d",strtotime("-1 day"));
$this->setDateCount($begin);
}
public function setFreeDateCount($begin,$end='')
{
if($end == '') $end = $begin;
//判断日期格式
$patten = "/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])$/";
if (!preg_match($patten, $begin)) {
die("开始时间格式错误");
}
if (!preg_match($patten, $end)) {
die("结束时间格式错误");
}
if(strtotime($end) < strtotime($begin)){
die("结束时间不能比开始时间小");
}
if(strtotime($end)+86399 > time()){
die("结束时间不能包含今日");
}
if($begin == $end){
$this->setDateCount($begin);
}else{
$starttime = $begin?strtotime($begin):mktime(0,0,0,date('m'),date('d')-1,date('Y'));
$endtime = $end?strtotime($end)+86399:$starttime+86399;
$datelist = get_date_list($starttime,$endtime,1);
$countdate = count($datelist);
for($i=0;$i<$countdate;$i++){
$this->setDateCount($datelist[$i]);
}
}
}
//
public function setDateCount($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);
//判断是否存在
$res = $this->ChartModel->where("date = '{$this->beginTime}'")->find();
$reset='';
if(!empty($res)){
if($this->reset){
$this->ChartModel->where("date = '{$this->beginTime}'")->delete();
$reset = "reset and creat ";
}else{
echo ("{$this->date} 已统计,请勿重复提交".PHP_EOL);
return;
}
}
$this->newUser();
$this->activeUser();
$this->payUser();
// $this->payMoney();
// $this->promoteNew();
// $this->allCount();
// $this->createDb();
dump($this->President_data);
$t2 = microtime(true);
echo ("{$this->date} {$reset}success runtime:".round($t2-$t1,3).'s'.PHP_EOL);
# code...
}
//获取每日的增长用户
public function newUser()
{
$newUserdata = [];
$map = ['register_time'=>['between',[$this->beginTime,$this->endTime]],"puid"=>0];//0不是小号
$hoursnews = $this->UserModel->field('FROM_UNIXTIME(register_time, "%H") as time,GROUP_CONCAT(DISTINCT id) AS user_id')
->where($map)
->group("time")
->select();
//按小时统计每个会长的信息
for ($i=0; $i < count($hoursnews); $i++) {
$tempres = self::getPresidentCount($hoursnews[$i]['user_id'],"COUNT(1) AS news,IFNULL(p.account,'官方渠道') account");
for($j=0; $j < count($tempres); $j++){
$newUserdata[$tempres[$j]['p_id']]["hoursnews"][$hoursnews[$i]['time']] = $tempres[$j]['news'];
$newUserdata[$tempres[$j]['p_id']]["president_account"]= $tempres[$j]['account'];
$newUserdata[$tempres[$j]['p_id']]["president_id"]= $tempres[$j]['p_id'];
}
}
//拼凑小时
foreach ($newUserdata as $k=>&$v) {
$h = $v['hoursnews'];
$t = [];
$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'];
$v['new_user_count'] = 0;
foreach($hours as $ht) {
if(isset($h[$ht])){
$t[] = $h[$ht];
$v['new_user_count'] += (int)$h[$ht];
}else{
$t[] = 0;
}
}
unset($v["hoursnews"]);
$v["new_user_hours"] = json_encode(array_map("intFun",$t));
// print_r($v['p_id']);die();
$this->President_data[$v['president_id']] = $v;
}
unset($newUserdata);
unset($hoursnews);
}
/**
* 活跃用户计算
*/
public function activeUser()
{
$newUserdata = [];
$map = ['login_time'=>['between',[$this->beginTime,$this->endTime]]];//0不是小号
$hoursnews = $this->LoginModel->field('FROM_UNIXTIME(login_time, "%H") as time,GROUP_CONCAT(DISTINCT user_id) AS user_id')
->where($map)
->group("time")
->select();
//按小时统计每个会长的信息
for ($i=0; $i < count($hoursnews); $i++) {
$tempres = self::getPresidentCount($hoursnews[$i]['user_id'],"COUNT(1) AS news,IFNULL(p.account,'官方渠道') account,GROUP_CONCAT(DISTINCT u.id) AS active_user_list");
for($j=0; $j < count($tempres); $j++){
$newUserdata[$tempres[$j]['p_id']]["hoursnews"][$hoursnews[$i]['time']] = $tempres[$j]['news'];
$newUserdata[$tempres[$j]['p_id']]["president_account"]= $tempres[$j]['account'];
$newUserdata[$tempres[$j]['p_id']]["president_id"]= $tempres[$j]['p_id'];
$newUserdata[$tempres[$j]['p_id']]["active_user_list"] .= (",".$tempres[$j]['active_user_list']);
}
}
//拼凑小时
foreach ($newUserdata as $k=>&$v) {
$h = $v['hoursnews'];
$t = [];
$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'];
foreach($hours as $ht) {
if(isset($h[$ht])){
$t[] = $h[$ht];
}else{
$t[] = 0;
}
}
unset($v["hoursnews"]);
$v["active_user_hours"] = json_encode(array_map("intFun",$t));
$ta = array_values(array_flip(array_flip(explode(",",trim($v["active_user_list"],",")))));
$v["active_user_count"] = count($ta);
$v["active_user_list"] = json_encode($ta);
if(array_key_exists($v['president_id'],$this->President_data)){
$this->President_data[$v['president_id']]['active_user_hours'] = $v["active_user_hours"];
$this->President_data[$v['president_id']]['active_user_count'] = $v["active_user_count"];
$this->President_data[$v['president_id']]['active_user_list'] = $v["active_user_list"];
}else{
$this->President_data[$v['president_id']] = $v;
}
}
unset($newUserdata);
unset($hoursnews);
}
/**
* 计算充值用户
*/
public function payUser()
{
$newUserdata = [];
$map = ['pay_time'=>['between',[$this->beginTime,$this->endTime]],'pay_status'=>1];//1支付成功
$hoursnews = $this->SpendModel->field('FROM_UNIXTIME(pay_time, "%H") as time,GROUP_CONCAT(DISTINCT user_id) AS user_id')
->where($map)
->group("time")
->select();
//按小时统计每个会长的信息
for ($i=0; $i < count($hoursnews); $i++) {
$tempres = self::getPresidentCount($hoursnews[$i]['user_id'],"COUNT(1) AS news,IFNULL(p.account,'官方渠道') account,GROUP_CONCAT(DISTINCT u.id) AS pay_user_list");
for($j=0; $j < count($tempres); $j++){
$newUserdata[$tempres[$j]['p_id']]["hoursnews"][$hoursnews[$i]['time']] = $tempres[$j]['news'];
$newUserdata[$tempres[$j]['p_id']]["president_account"]= $tempres[$j]['account'];
$newUserdata[$tempres[$j]['p_id']]["president_id"]= $tempres[$j]['p_id'];
$newUserdata[$tempres[$j]['p_id']]["pay_user_list"] .= (",".$tempres[$j]['pay_user_list']);
}
}
//拼凑小时
foreach ($newUserdata as $k=>&$v) {
$h = $v['hoursnews'];
$t = [];
$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'];
foreach($hours as $ht) {
if(isset($h[$ht])){
$t[] = $h[$ht];
}else{
$t[] = 0;
}
}
unset($v["hoursnews"]);
$v["pay_user_hours"] = json_encode(array_map("intFun",$t));
$ta = array_values(array_flip(array_flip(explode(",",trim($v["pay_user_list"],",")))));
$v["pay_user_count"] = count($ta);
$v["pay_user_list"] = json_encode($ta);
if(array_key_exists($v['president_id'],$this->President_data)){
$this->President_data[$v['president_id']]['pay_user_hours'] = $v["pay_user_hours"];
$this->President_data[$v['president_id']]['pay_user_count'] = $v["pay_user_count"];
$this->President_data[$v['president_id']]['pay_user_list'] = $v["pay_user_list"];
}else{
$this->President_data[$v['president_id']] = $v;
}
}
unset($newUserdata);
unset($hoursnews);
}
/**
* 充值金额计算
*/
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"] = json_encode($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"] = json_encode($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();//推广员总数
$allcount['game_count'] = M("game","tab_")->where("game_status = 1")->field("count(1) game_count")->find()['game_count'];//游戏接入数量
$allcount['android_gamesource_count'] = M("GameSource","tab_")->where("file_type = 1")->field("count(1) game_count")->find()['game_count'];//安卓游戏原包
$allcount['ios_gamesource_count'] = M("GameSource","tab_")->where("file_type = 2")->field("count(1) game_count")->find()['game_count'];//IOS游戏原包
$allcount['promote_user_count'] = $user->where("promote_id > 0 and puid = 0")->field("count(1) user_count")->find()['user_count'];//推广员注册用户
$allcount['promote_spend_sum'] = $spend->where("promote_id > 0 and pay_status = 1")->field("sum(pay_amount) pay_amount")->find()['pay_amount'];//推广员总充值
$this->adddata["all_count"] = json_encode($allcount);
}
public function createDb()
{
$this->adddata['date']=$this->beginTime;
$this->adddata['create_time']=$this->nowdata;
$this->ChartModel->add($this->adddata);
}
//通过用户id串获取会长并统计
private function getPresidentCount($ids,$field=false)
{
if($field){
$fields = "IFNULL(if(substring_index(substring_index(`chain`,'/',2),'/',-1)='',u.promote_id,substring_index(substring_index(`chain`,'/',2),'/',-1)),0) p_id,{$field}";
}else{
$fields = "IFNULL(if(substring_index(substring_index(`chain`,'/',2),'/',-1)='',u.promote_id,substring_index(substring_index(`chain`,'/',2),'/',-1)),0) p_id";
}
$userres = M("user u","tab_")
->field($fields)
->join("tab_promote p on u.promote_id = p.id","left")
->where("u.id in ({$ids})")
->group("p_id")
->select();
return $userres;
# code...
}
}