@ -8,6 +8,7 @@ use User\Api\UserApi as UserApi;
* @author 麦当苗儿 < zuojiazi @ vip . qq . com >
*/
class StatisticsController extends ThinkController {
const COUNTLIMIT = 15;
public function overview(){
$shuju = json_decode(M('IndexChart','tab_')->field("all_count")->order('`date` desc')->find()["all_count"],true);
$this->assign('shuju',$shuju);
@ -40,7 +41,7 @@ class StatisticsController extends ThinkController {
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);
$promote_data = $this->promote_data_order2 ($today,$yesterday);
}
$this->assign('zhuce',$list_data['zhuce']);
@ -272,26 +273,114 @@ class StatisticsController extends ThinkController {
}
# code...
}
/**
* 获取推广员排行
* @param [type] $starttime 当前时间
* @param [type] $endtime 比较时间
* @return void
*/
private function promote_data_order($starttime,$endtime){
//注册排行
$PromoteCount = M("PromoteCount","tab_");
$data = array();
//注册排行
//1.获取注册前十名
$new_user_count = $PromoteCount->field("promote_id,promote_account,sum(new_user_count) cg")->where("`date` {$starttime}")->group("promote_id")->order("cg desc")->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.获取注册前十名
$new_active_count = $PromoteCount->field("promote_id,promote_account,sum(active_user_count) cg")->where("`date` {$starttime}")->group("promote_id")->order("cg desc")->limit(self::COUNTLIMIT)->select();
$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.获取注册前十名
$new_money_count = $PromoteCount->field("promote_id,promote_account,sum(pay_money_count) cg")->where("`date` {$starttime}")->group("promote_id")->order("cg desc")->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;
//获取当前注册前十名
$new_user_count = $PromoteCount->field("promote_account,new_user_count")->where("`date` {$starttime}")->order("new_user_count desc")->limit(10)->select(false);
echo "< pre > ";dump($new_user_count);die();
}
//设置排名
public function setRand($randdata,$oldrand)
{
//数据是否为空
$rcount = count($randdata);
if($rcount < 1 ) { return [ ] ; }
//判断是否有旧排名
$isold = true;
$ocount = count($oldrand);
if($ocount < 1 ) {
$isold = false;
}else{
$oldrand = array_column($oldrand,"promote_id");
$oldrand=array_flip($oldrand);//反转数组
}
for ($i=0; $i < $rcount; $i++) {
$rand = $i+1;
$randdata[$i]['rand'] =$rand;
if($isold){
if(empty($oldrand[$randdata[$i]['promote_id']])){
$randdata[$i]['change'] = 0;
}else{
$randdata[$i]['change'] = $oldrand[$randdata[$i]['promote_id']] - $rand;
}
}else{
$randdata[$i]['change'] = 0;
}
}
return $randdata;
# code...
}
/**
* 排行榜(推广员)
* @param string $nowtime 现在时间段( between 开始时间戳 and 结束时间戳)
* @param string $othertime 过去时间段( between 开始时间戳 and 结束时间戳)
* @return array 结果集
* @author lwx
*/
private function promote_data_order2($nowtime,$othertime){
/**
* 排行榜(推广员)
* @param string $nowtime 现在时间段( between 开始时间戳 and 结束时间戳)
* @param string $othertime 过去时间段( between 开始时间戳 and 结束时间戳)
* @return array 结果集
* @author lwx
*/
private function promote_data_order2($nowtime,$othertime){
$user = M("User","tab_");
$spend = M('Spend',"tab_");
@ -326,90 +415,89 @@ class StatisticsController extends ThinkController {
}
// //今日活跃排行
$duser = D('User');
$ri_active_order = $duser->activeRankOnPromote($nowtime,'cg');
$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;
}
}
}
$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'];
}
}
}
}
$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;
}
}
$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($chart as $k => $v) {
if ($k == 'promote')
$data['chart'][$k] = '"'.implode('","',$v).'"';
else
$data['chart'][$k] = implode(',',$v);
}
foreach($chart2 as $c) {
$chart['active'][$k] = 0;
if ($v['promote_id'] == $c['promote_id']) {
$chart['active'][$k] = (integer)$c['cg'];break;
}
echo "< pre > ";dump($data);die();
}
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;
}