<?php
/**
* 会员统计
* @Copyright (C) 2016 汉潮 All rights reserved.
* @License http://www.hanchao9999.com
* @Author xiaogg <xiaogg@sina.cn>
*/

namespace Admin\Controller;
class MemberTongController extends BaseController{
//会员列表及会员注册量统计
    public function index(){
        $postdata=I('param.');
        /*会员日增长量统计开始*/
        /*获取当月时间数组开始*/
        $type = I('type');
        $day_arr = $map=array();
        $now_time=strtotime(date('Y-m-d',NOW_TIME));
        $new_map=$map =$map_z=array();
        if($postdata['date_from'] || $postdata['date_to']){
            $date_from = strtotime($postdata['date_from']);
            if($date_from){
                $map['reg_time'][] = array('egt',$date_from);
	      $map_z[] = $date_from;
            }else{
                $this->error("请选择搜索开始时间");exit;
            }
            $date_to = strtotime($postdata['date_to']);
            if($date_to){
                $map['reg_time'][] = array('lt',$date_to+86399);
	      $map_z[] = $date_to+86399;
            }else{
                $this->error("请选择搜索结束时间");exit;
            }
            $type=0;
        }else{
	  if(in_array($type,array(1,2,7,30))){
	      $map_z=quick_time($type);
	  }else{
	      $map_z=array();
	  }
            if(in_array($type,array(7,30))){
                $between=quick_time($type);
                $date_from=$between[0];
                $date_to =$between[1];
                $map['reg_time']=array('between',array($between[0],$between[1]));
            }else{
                $type=3;
                $date_from=$now_time-6*86400;
                $date_to =$now_time+86399;
                $map['reg_time']=array('between',array($date_from,$date_to));
            }
        }

        if(in_array($type,array(1,2,3,7))){
            $days=7;
        }else if($type ==30){
            $days=30;
        }else{
            $days=ceil(($date_to-$date_from)/86400);
        }
        for($i=0;$i<$days;$i++){
            $day_arr[date('Y-m-d',$now_time-$i*86400)] = date('Y-m-d',$now_time-$i*86400); //每隔一天赋值给数组
        }
        $new_map=$map;
        $this->info($map_z);//会员概况
        //会员
        $map['status'] = 1;
        $map['is_tmp'] = 0;
        $info['member'] = M('Member')->where($map)->field('count(*) as num,reg_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(reg_time),'%Y年%m月%d天')")->select();
        unset($map['is_tmp']);
        //粉丝
        $map['is_follow'] = 1;
        $info['fans'] = M('Member')->where($map)->field('count(*) as num,reg_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(reg_time),'%Y年%m月%d天')")->select();
        //每个折线的图标代表什么
        $name = array("粉丝","会员");
        if($info){
	  foreach($info as $key => $value){
	      if(!empty($value)){
	          foreach($value as $k=>$v){
		    $file[$key][date('Y-m-d',$v['mon'])]=$v['num'];
		    $file_time[$key][]=date('Y-m-d',$v['mon']);
	          }
	      }else{
	          $file[$key][date('Y-m-d',$v['mon'])] =0;
	          $file_time[$key][]=date('Y-m-d',$v['mon']);
	      }
	  }unset($value,$key,$k,$v);
        }
        $new_day=array_diff($day_arr,$file_time['member']);
        foreach($new_day as $v){
            $file['member'][$v]=0;
        }unset($v);
        $new_fans_day=array_diff($day_arr,$file_time['fans']);
        foreach($new_fans_day as $v){
            $file['fans'][$v]=0;
        }unset($v);
        ksort($file['member']);
        ksort($file['fans']);
        //得到折线图
        $cz_count = array(
            "legend"=>array('data'=>array($name[0],$name[1])),
            "xaxis"=>array("type"=>"category","data"=>array_keys($file['member'])),
            "series"=>array(
                0=>array("name"=>$name[0],"type"=>"line","stack"=>$name[0],"data"=>array_values($file['fans'])),
                1=>array("name"=>$name[1],"type"=>"line","stack"=>$name[1],"data"=>array_values($file['member'])),
            ),
        );
        $ec = new \Common\Vendor\Echarts();
        $this->assign('Area_count',$ec->show('Area_count',$cz_count,'line'));
        $this->assign('type',$type);
        /*会员增长量统计结束*/
        /*等级会员占比开始*/
        $map = array();
        $map['status'] = 1;
        $map['is_tmp'] = 0;
        $member_info = M('Member')->where($map)->getField('rank_id',true);
        
        $m_style_num=array_count_values($member_info);
        $rank_info = M('MemberRank')->getField('id,name',true);
        $data_rank = M('MemberRank')->getField('name',true);
        $i = 0;
        foreach($m_style_num as $k=>$v){
            $arr[$i]['value'] = $v;
            $arr[$i]['name'] = $rank_info[$k];
            $i++;
        }
        $rank_style_count = array(
            'title'=>array(
                'text'=>'会员等级占比',
                'x'=>'center'
            ),
	  "legend"=>array('data'=>$data_rank),
	  "series"=>array(
	      array("name"=>"会员等级占比","type"=>"pie",
		"data"=>$arr,
	      ),
	  ),
        );
        $this->assign('Area_count_member',$ec->pie_show('Area_count_member', $rank_style_count,'pie'));
        /*等级会员占比结束*/
        /*会员来源统计开始*/
        $map=array();
        $map=$new_map;
        $source_info = M('Member')->where($map)->getField('source',true);
        $source_style_num=array_count_values($source_info);
        $source_style_count = array(
	  "legend"=>array('data'=>array('直接访问','仓库办理','消费领卡','全民拓客')),
	  "series"=>array(
	      array("name"=>"会员来源占比","type"=>"pie",
		"data"=>array(
		        array("value"=>$source_style_num['1']+$source_style_num['0'],"name"=>"直接访问"),
		        array("value"=>$source_style_num['2'],"name"=>"仓库办理"),
		        array("value"=>$source_style_num['3'],"name"=>"消费领卡"),
		        array("value"=>$source_style_num['4'],"name"=>"全民拓客"),
		    ),
		),
	        ),
        );
        $this->assign('Area_count_source',$ec->pie_show('Area_count_source', $source_style_count,'pie'));
        /*会员来源统计结束*/
        /*会员性别占比开始*/
        $map = array();
        $map['status'] = 1;
        $sex_info = M('Member')->where($map)->getField('sex',true);
        $sex_style_num=array_count_values($sex_info);
        $sex_style_count = array(
            'title'=>array(
                'text'=>'会员性别占比',
                'x'=>'center'
            ),
	  "legend"=>array('data'=>array('保密','男','女')),
	  "series"=>array(
	      array("name"=>"会员性别占比","type"=>"pie",
		"data"=>array(
		    array("value"=>$sex_style_num['0'],"name"=>"保密"),
		    array("value"=>$sex_style_num['1'],"name"=>"男"),
		    array("value"=>$sex_style_num['2'],"name"=>"女"),
		),
	      ),
	  ),
        );
        $this->assign('Area_count_sex',$ec->pie_show('Area_count_sex', $sex_style_count,'pie'));
        /*会员性别占比结束*/
        /*会员年龄段人数统计开始*/
        $map = array();
        $map['birthday'] = array('neq','');
        $member_info = M('Member')->where($map)->getField('birthday',true);
        $member_count = M('Member')->where($map)->count();
        $year = date('Y',NOW_TIME);
        $a=$b=$c=$d=$e=$f=$g=0;
        foreach($member_info as $k=>$v){
	  $str = substr($v,0,4);
	  if($str != '0000'){
	      if($year-$str < 10){
		$a++;
	      }elseif($year-$str < 20 && $year-$str > 10){
		$b++;
	      }elseif($year-$str < 30 && $year-$str > 20){
		$c++;
	      }elseif($year-$str < 40 && $year-$str > 30){
		$d++;
	      }elseif($year-$str < 50 && $year-$str > 40){
		$e++;
	      }elseif(50 < $year-$str){
		$f++;
	      }
	  }else{
	      $g++;
	  }
        }
        $member_age['10岁以下'] =$a;
        $member_age['10-20'] = $b;
        $member_age['20-30'] = $c;
        $member_age['30-40'] = $d;
        $member_age['40-50'] = $e;
        $member_age['50岁以上'] = $f;
        $member_age['未知人数'] = $g;
        $names = "人数";
        $ma_count = array(
            "legend"=>array('data'=>array($names)),
            "xaxis"=>array("type"=>"category","data"=>array_keys($member_age)),
            "series"=>array(
	      0=>array("name"=>$names,"type"=>"bar","stack"=>$names,'itemStyle'=>array(),"data"=>array_values($member_age)),
            ),
        );
        $this->assign('MemberAge_count',$ec->show('MemberAge_count',$ma_count,'bar'));
        /*会员年龄段人数统计结束*/
        /*会员复购率分析1-10次 1-20次  1-50次 开始*/

        $map = array();
        if($map_z){
	  $map['addtime']=array('between',array($map_z[0],$map_z[1]));
        }
        $map['type'] = array('in','1,6,11,13,14');
        $map['status'] = array('in','1,2,3,6,8');
        $buy_info = M('OrderInfo')->where($map)->group('uid')->field('count(id) as num')->select();

        $a=$b=$c=$d=$e=$f=$g=$h=$j=$k=0;
        foreach($buy_info as $ke=>$v){
	  switch ($v['num'])
	  {
	      case 1:
	        $a++;
	        break;
	      case 2:
	        $b++;
	        break;
	      case 3:
	        $c++;
	        break;
	      case 4:
	        $d++;
	        break;
	      case 5:
	        $e++;
	        break;
	      case 6:
	        $f++;
	        break;
	      case 7:
	        $g++;
	        break;
	      case 8:
	        $h++;
	        break;
	      case 9:
	        $j++;
	        break;
	      default:
	        $k++;
	  }
        }
        $member_buy['1次'] =$a;
        $member_buy['2次'] = $b;
        $member_buy['3次'] = $c;
        $member_buy['4次'] = $d;
        $member_buy['5次'] = $e;
        $member_buy['6次'] = $f;
        $member_buy['7次'] = $g;
        $member_buy['8次'] = $h;
        $member_buy['9次'] = $j;
        $member_buy['10次以上'] = $k;
        $names = "人数";
        $ma_count = array(
            "legend"=>array('data'=>array($names)),
            "xaxis"=>array("type"=>"category","data"=>array_keys($member_buy)),
            "series"=>array(
	  0=>array("name"=>$names,"type"=>"bar","stack"=>$names,'itemStyle'=>array(),"data"=>array_values($member_buy)),
            ),
        );
        $this->assign('MemberBuy_count',$ec->show('MemberBuy_count',$ma_count,'bar'));
//        $this->assign('m_buy',$m_buy);
        /*会员复购率分析1-10次 1-20次  1-50次 结束*/
        $this->assign($postdata);
        if($postdata['print']==1){
	  $this->display('print');
        }else{
	  $this->display();
        }
    }
    public function info($map_z){
        $map_oi=$map=array();//新增
        if($map_z){
	  $map['reg_time']=array('between',array($map_z[0],$map_z[1]));
	  $map_oi['addtime']=array('between',array($map_z[0],$map_z[1]));
        }
        $map['status']=1;
        $map['is_tmp']=0;
        $map['user_shop']=0;
        $xs_source=M('Member')->where($map)->count('id');//新增线上会员
        unset($map['user_shop']);
        $map['user_shop']=array('gt',0);
        $xx_source=M('Member')->where($map)->count('id');//新增线下会员
        unset($map['user_shop']);unset($map['is_tmp']);
        $map['is_follow']=0;
        $gukex=M('Member')->where($map)->count('id');//新增顾客
        // 总数
        $map=array();
        $map['status']=1;
        $map['is_tmp']=0;
        $map['user_shop']=0;
        $zxs_source=M('Member')->where($map)->count('id');//线上会员
        unset($map['user_shop']);
        $map['user_shop']=array('gt',0);
        $zxx_source=M('Member')->where($map)->count('id');//线下会员
        unset($map['user_shop']);unset($map['is_tmp']);
        $map['is_follow']=0;
        $gukez=M('Member')->where($map)->count('id');//顾客总数
        $cz_count=$xs_source+$xx_source;
        $this->assign('cz_count',$cz_count);
        $this->assign('xs_source',$xs_source);
        $this->assign('xx_source',$xx_source);
        $zcount=$zxs_source+$zxx_source;
        $this->assign('zcount',$zcount);
        $this->assign('zxs_source',$zxs_source);
        $this->assign('zxx_source',$zxx_source);
        $this->assign('gukex',$gukex);
        $this->assign('gukez',$gukez);
        // 会员活跃率
        $map_oi['status'] = array('in','1,2,3,6,8');
        $map_oi['pay_status'] = 3;
        $map_oi['type'] = array('in','1,6,11,12,13,14');
        $map_oi['is_delete']=0;
        $map_oi['uid']=array('gt',0);
        $m_info = M('OrderInfo')->where($map_oi)->group('uid')->field('uid')->select();
        $m_count = count($m_info);//活跃的会员数
        $active_percent=format_decimal($m_count/$zcount*100);
        $this->assign('active_percent',$active_percent.'%');


        // 会员复购率
        unset($map_oi['addtime']);
        $count_order=M('OrderInfo')->where($map_oi)->count('id');//总笔数
        $buy_info = M('OrderInfo')->where($map_oi)->group('uid')->field('count(id) as num')->having('count(id) = 1')->select();//第一次购买人数
        $one_num=count($buy_info);
        $buy_percent=format_decimal(($count_order-$one_num)/$count_order*100).'%';
        $this->assign('buy_percent',$buy_percent);
        //新增粉丝
        $map=array();
        $map['status']=1;
        $map['is_follow']=1;
        if($map_z){
	  $map['follow_time']=array('between',array($map_z[0],$map_z[1]));
        }
        $g_fans=M('Member')->where($map)->count('id');
        //减少粉丝
        $map['is_follow']=0;
        unset($map['follow_time']);
        if($map_z){
	  $map['unfollow_time']=array('between',array($map_z[0],$map_z[1]));
        }else{
	  $map['unfollow_time']=array('gt',0);
        }
        $c_fans=M('Member')->where($map)->count('id');
        $this->assign('g_fans',$g_fans);
        $this->assign('c_fans',$c_fans);
    }
    public function buy(){//会员消费排行
        $postdata=I('param.');
        $map=array();
        if($postdata['date_from'] || $postdata['date_to']){
	  $date_from = strtotime($postdata['date_from']);
	  if($date_from){$map['addtime'][] = array('egt',$date_from);}
	  $date_to = strtotime($postdata['date_to']);
	  if($date_to){$date_to = $date_to+86399;$map['addtime'][] = array('lt',$date_to);}
        }else{
	  if(in_array($postdata['type'],array(1,2,7,30))){
	      $between=quick_time($postdata['type']);
	      $map['addtime']=array('between',array($between[0],$between[1]));
	  }
        }
        $map['type'] = array('in','1,11,12,13,14');
        $map['status'] = array('in','1,2,3,6,8');
        $map['is_delete'] =0;
        $map['uid'] = array('gt',0);
        $limit=$postdata['buy_top']?$postdata['buy_top']:10;
        $info_list = M('OrderInfo')->where($map)->field("sum(order_money) as allmoney,uid,count(id) as ids")->group('uid')->order('allmoney desc')->limit($limit)->select();
        $this->assign('info_list',$info_list);
        $list_html=$this->fetch('index_list');
        $return['html']=str_replace("\n",'',$list_html);
        $this->ajaxReturn($return);
    }
    public function chong(){//会员消费排行
        $postdata=I('param.');
        $map=array();
        if($postdata['date_from'] || $postdata['date_to']){
	  $date_from = strtotime($postdata['date_from']);
	  if($date_from){$map['addtime'][] = array('egt',$date_from);}
	  $date_to = strtotime($postdata['date_to']);
	  if($date_to){$date_to = $date_to+86399;$map['addtime'][] = array('lt',$date_to);}
        }else{
	  if(in_array($postdata['type'],array(1,2,7,30))){
	      $between=quick_time($postdata['type']);
	      $map['addtime']=array('between',array($between[0],$between[1]));
	  }
        }
        $map['type'] = array('in','2,5');
        $map['status'] = 3;
        $map['is_delete'] =0;
        $map['uid'] = array('gt',0);
        $limit=$postdata['chong_top']?$postdata['chong_top']:10;
        $info_list = M('OrderInfo')->where($map)->field("sum(order_money) as allmoney,uid,count(id) as ids")->group('uid')->order('allmoney desc')->limit($limit)->select();
        $this->assign('info_list',$info_list);
        $list_html=$this->fetch('index_list');
        $return['html']=$list_html;
        $this->ajaxReturn($return);
    }
    //站长统计
    public function cnzz(){
        $site= new \Common\Vendor\Matomo;
        $result=$site->getviewurl();
        if(empty($result) )$this->error('获取统计信息错误');
        $configpassword=C('STATS_CODE');
        if(strlen($configpassword)!=32){
            $this->success('请稍后,统计代码生成中',U('cnzz'));
        }else{
            echo "<script type='text/javascript'>location.href='".$result."';</script>";
        }
    }
    
    public function biao_index(){
        $type = I('type');
        $date_from = $date_to= '';
        $array_day = array(1,2,7,3);
        if(in_array($type, $array_day)){
            $riqi = 7;
        }elseif($type == 99){
            $riqi = 7;
            $date_from = I('date_from');
            $date_to = I('date_to');
            if(!$date_from || !$date_to){
                $riqi = 7;
                $type = 7;
                $date_from = '';$date_to = '';
            }
        }else{
            $riqi = $type;
        }
        //会员增长表开始
        $this->num_count($riqi,$date_from,$date_to);
        //会员增长表结束
        //年龄表开始
        $this->age_count();
        //年龄表结束
        //性别表开始
        $map = array();
        $map['status'] = 1;
        $map['sex'] = 1;
        $sex_count['boy'] = M('Member')->where($map)->count();
        $map['sex'] =2;
        $sex_count['girl'] = M('Member')->where($map)->count();
        $map['sex'] = '';
        $sex_count['baomi'] = M('Member')->where($map)->count();
        $this->assign('sex_count',$sex_count);
        //性别表结束
        //复购率开始
        $this->buy_count($riqi,$date_from,$date_to);
        //复购率结束
        //等级分析开始
        $this->rank_count();
        //等级分析结束
        //会员来源分析开始
        $this->source_count($riqi,$date_from,$date_to);
        //会员来源分析结束
        $map = array();
        if($type == '时间'){
            $date_from = strtotime(I('date_from'));
            if($date_from)$map['addtime'][] = array('egt',$date_from);
            $date_to = strtotime(I('date_to'));
            if($date_to)$map['addtime'][] = array('lt',$date_to);
        }elseif($type == 1){
            $map['addtime'][] = array('egt',mktime(0,0,0,date('m'),date('d'),date('Y')));
            $map['addtime'][] = array('lt',NOW_TIME);
        }elseif($type == 2){
            $map['addtime'][] = array('egt',mktime(0,0,0,date('m'),date('d')-1,date('Y')));
            $map['addtime'][] = array('lt',mktime(0,0,0,date('m'),date('d'),date('Y'))-1);
        }elseif($type == 7){
            $map['addtime'][] = array('egt',mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')));
            $map['addtime'][] = array('lt',mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')));
        }elseif($type == 30){
            $map['addtime'][] = array('egt',mktime(0,0,0,date('m'),1,date('Y')));
            $map['addtime'][] = array('lt',mktime(23,59,59,date('m'),date('t'),date('Y')));
        }
        $map['type'] = array('in','1,11,12,13,14');
        $map['status'] = 3;
        $info_list = M('OrderInfo')->where($map)->field("sum(order_amount) as allmoney,uid,count(id) as ids")->group('uid')->order('allmoney desc')->limit($limit)->select();
        $this->assign('info_list',$info_list);
        $list_html=$this->fetch('index_list');
        $this->assign('list_html',$list_html);
        $this->assign('type',$type);
        $this->assign('date_from',$date_from);
        $this->assign('date_to',$date_to);
        $this->display();
    }
    /**
      *注册数统计
      */
    private function num_count($riqi=7,$date_from=0,$date_to=0,$is_return=0){
        if($riqi == -1){
            $now_time = strtotime($date_to);
            $count=$map=array();
            $map['status']=1;
            $start_time=strtotime(date('Y-m-d',$now_time));
            $riqi = (strtotime($date_to) - strtotime($date_from))/86400+1;
            for($i=0;$i<$riqi;$i++){
                if($i == 0){
                    $today = $start_time;
                    $tomorrow = $start_time+86400;
                }else{
                    $today = $start_time - $i*86400;
                    $tomorrow = $start_time-($i-1)*86400;
                }
                $count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['is_follow'] = 1;
                $map['reg_time']=array('between',array($today,$tomorrow));
                $count[$i]['f_count'] = M('Member')->where($map)->count();
                $map['is_follow'] = 0;
                $count[$i]['g_count']=M('Member')->where($map)->count();
                unset($map['is_follow']);
                $map['is_tmp'] = 0;
                $count[$i]['m_count']=M('Member')->where($map)->count();
                unset($map['is_tmp']);
                $count[$i]['sum'] = $count[$i]['f_count'] + $count[$i]['g_count'] + $count[$i]['m_count'];
            }
        }else{
            $now_time = NOW_TIME;
            $count=$map=array();
            $map['status']=1;
            $start_time=strtotime(date('Y-m-d',NOW_TIME));
            for($i=0;$i<$riqi;$i++){
                if($i == 0){
                    $today = $start_time;
                    $tomorrow = $start_time+86400;
                }else{
                    $today = $start_time - $i*86400;
                    $tomorrow = $start_time-($i-1)*86400;
                }
                $count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['is_follow'] = 1;
                $map['reg_time']=array('between',array($today,$tomorrow));
                $count[$i]['f_count'] = M('Member')->where($map)->count();
                $map['is_follow'] = 0;
                $count[$i]['g_count']=M('Member')->where($map)->count();
                unset($map['is_follow']);
                $map['is_tmp'] = 0;
                $count[$i]['m_count']=M('Member')->where($map)->count();
                unset($map['is_tmp']);
                $count[$i]['sum'] = $count[$i]['f_count'] + $count[$i]['g_count'] + $count[$i]['m_count'];
            }
        }
        $this->assign('count',$count);
        if($is_return == 1){
            return $count;
        }
    }
    /**
      *年龄数统计
      */
    private function age_count($is_return=0){
        $map = array();
		$map['birthday'] = array('neq','');
		$member_info = M('Member')->where($map)->getField('birthday',true);
		$year = date('Y',NOW_TIME);
		$a=$b=$c=$d=$e=$f=$g=0;
		foreach($member_info as $k=>$v){
			$str = substr($v,0,4);
			if($str != '0000'){
				if($year-$str < 10){
					$a++;
				}elseif($year-$str < 20 && $year-$str > 10){
					$b++;
				}elseif($year-$str < 30 && $year-$str > 20){
					$c++;
				}elseif($year-$str < 40 && $year-$str > 30){
					$d++;
				}elseif($year-$str < 50 && $year-$str > 40){
					$e++;
				}elseif(50 < $year-$str){
					$f++;
				}
			}else{
				$g++;
			}
		}
		$age_count['0'] = $a; 
		$age_count['1'] = $b;
		$age_count['2'] = $c;
		$age_count['3'] = $d;
		$age_count['4'] = $e;
		$age_count['5'] = $f;
		$age_count['6'] = $g;
		$this->assign('age_count',$age_count);
        if($is_return == 1){
            return $age_count;
        }
    }
    private function buy_count($riqi,$date_from,$date_to,$is_return=0){
        $map = array();
        $map['type'] = array('in','1,6,11,13,14');
        $map['status'] = array('in','1,2,3,6,8');
        if($riqi==-1){
            $map['addtime'] = array('between',array($date_from,$date_to));
        }else{
            $start_time=strtotime(date('Y-m-d',NOW_TIME));
            $today = $start_time - ($riqi-1)*86400;
            $tomorrow = $start_time+86400;
            $map['addtime'] = array('between',array($today,$tomorrow));
        }
        $buy_info = M('OrderInfo')->where($map)->group('uid')->field('count(id) as num')->select();
        $a=$b=$c=$d=$e=$f=$g=$h=$j=$k=0;
        foreach($buy_info as $ke=>$v){
            switch ($v['num'])
            {
                case 1:
		$a++;
		break;
                case 2:
		$b++;
		break;
                case 3:
		$c++;
		break;
                case 4:
    		$d++;
		break;
                case 5:
		$e++;
		break;
                case 6:
		$f++;
		break;
                case 7:
		$g++;
		break;
                case 8:
		$h++;
		break;
                case 9:
		$j++;
		break;
                default:
		$k++;
            }
        }
        $buy_count['1'] =$a;
        $buy_count['2'] = $b;
        $buy_count['3'] = $c;
        $buy_count['4'] = $d;
        $buy_count['5'] = $e;
        $buy_count['6'] = $f;
        $buy_count['7'] = $g;
        $buy_count['8'] = $h;
        $buy_count['9'] = $j;
        $buy_count['10'] = $k;
        $this->assign('buy_count',$buy_count);
        if($is_return == 1){
            return $buy_count;
        }
    }
    private function rank_count(){
        $map = array();
        $map['status']=1;
        $map['is_tmp']=0;
        $rank_info = M('MemberRank')->field('id,name')->select();
        foreach($rank_info as $k=>$v){
            $map['rank_id'] = $v['id'];
            $rank_info[$k]['count'] = M('Member')->where($map)->count();
        } 
        $list['name'] = i_array_column($rank_info,'name');
        $list['count'] = i_array_column($rank_info,'count');
        $this->assign('list',$list);
    }
    private function source_count($riqi=7,$date_from=0,$date_to=0,$is_return=0){
        if($riqi == -1){
            $now_time = strtotime($date_to);
            $scurce_list=$map=array();
            $startime=strtotime(date('Y-m-d',$now_time));
            $today=strtotime(date('Y-m-d',$now_time));
            $riqi = (strtotime($date_to) - strtotime($date_from))/86400+1;
            for($i=0;$i<$riqi;$i++){
                if($i == 0){
                    $tomorrow = $startime+86400;
                }else{
                    $today = $startime - $i*86400;
                    $tomorrow = $startime+($i-1)*86400;
                }
                $scurce_list[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['source'] = array('in','0,1');
                $map['reg_time']=array('between',array($today,$tomorrow));
                $scurce_list[$i]['zhijie'] = M('Member')->where($map)->count();
                unset($map['source']);
                $map['source'] = 2;
                $scurce_list[$i]['mendian'] = M('Member')->where($map)->count();
                unset($map['source']);
                $map['source'] = 3;
                $scurce_list[$i]['lingka'] = M('Member')->where($map)->count();
                unset($map['source']);
                $map['source'] = 4;
                $scurce_list[$i]['tuoke'] = M('Member')->where($map)->count();
                $scurce_list[$i]['sum'] = $scurce_list[$i]['zhijie'] + $scurce_list[$i]['mendian'] + $scurce_list[$i]['lingka'] + $scurce_list[$i]['tuoke'];
            }
        }else{
            $now_time = NOW_TIME;
            $scurce_list=$map=array();
            $startime=strtotime(date('Y-m-d',NOW_TIME));
            for($i=0;$i<$riqi;$i++){
                if($i == 0){
                    $tomorrow = $startime+86400;
                }else{
                    $today = $startime - $i*86400;
                    $tomorrow = $startime+($i-1)*86400;
                }
                $scurce_list[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['source'] = array('in','0,1');
                $map['reg_time']=array('between',array($today,$tomorrow));
                $scurce_list[$i]['zhijie'] = M('Member')->where($map)->count();
                unset($map['source']);
                $map['source'] = 2;
                $scurce_list[$i]['mendian'] = M('Member')->where($map)->count();
                unset($map['source']);
                $map['source'] = 3;
                $scurce_list[$i]['lingka'] = M('Member')->where($map)->count();
                unset($map['source']);
                $map['source'] = 4;
                $scurce_list[$i]['tuoke'] = M('Member')->where($map)->count();
                unset($map['source']);
                $scurce_list[$i]['sum'] = $scurce_list[$i]['zhijie'] + $scurce_list[$i]['mendian'] + $scurce_list[$i]['lingka'] + $scurce_list[$i]['tuoke'];
            }
        }
        $this->assign('scurce_list',$scurce_list);
        if($is_return == 1){
            return $scurce_list;
        }
    }
    public function membertong_detail_export(){
        $null = array();
        $postdata=I('param.');
		if($postdata){
            $type = $postdata['type'];
            $date_from = $date_to= '';
            
            $array_day = array(1,2,7,3);
            if(in_array($type, $array_day)){
                $riqi = 7;
            }elseif($type == 99){
                $riqi = 7;
                $date_from = I('date_from');
                $date_to = I('date_to');
                if(!$date_from || !$date_to){
                    $riqi = 7;
                    $type = 7;
                    $date_from = '';$date_to = '';
                }
            }else{
                $riqi = $type;
            }
            //表一开始（新增会员分析）
            $one_count = $this->num_count($riqi,$date_from,$date_to,1);
            $one_title=array('日期','粉丝','顾客','会员','用户总数');
            $title_name = array('新增会员分析');
            export_xls($title_name,$null,'utf8');
            export_xls($one_title,$one_count,'utf8');
            export_xls(array(),$null,'utf8');
            //表一结束
            //表二开始（会员来源分析）
            $two_count = $this->source_count($riqi,$date_from,$date_to,1);
            $two_title=array('日期','直接访问','仓库办理','消费领卡','全名拓客','用户总数');
            $title_name = array('会员来源分析');
            export_xls($title_name,$null,'utf8');
            export_xls($two_title,$two_count,'utf8');
            export_xls(array(),$null,'utf8');
            //表二结束
            //表三开始（会员年龄段分析）
            $three_info = $this->age_count(1);
            $three_count[0][] = $three_info[0];
            $three_count[0][] = $three_info[1];
            $three_count[0][] = $three_info[2];
            $three_count[0][] = $three_info[3];
            $three_count[0][] = $three_info[4];
            $three_count[0][] = $three_info[5];
            $three_count[0][] = $three_info[6];
            $three_title=array('10岁以下','10-20','20-30','30-40','40-50','50岁以上','未知人数');
            $title_name = array('会员年龄段分析');
            export_xls($title_name,$null,'utf8');
            export_xls($three_title,$three_count,'utf8');
            export_xls(array(),$null,'utf8');
            //表三结束
            //表四开始（会员性别占比分析）
            $map = array();
            $map['status'] = 1;
            $map['sex'] = 1;
            $four_count[0][] = M('Member')->where($map)->count();
    		$map['sex'] =2;
            $four_count[0][] = M('Member')->where($map)->count();
            $map['sex'] = '';
            $four_count[0][] = M('Member')->where($map)->count();
            $four_title=array('男','女','保密');
            $title_name = array('会员性别占比分析');
            export_xls($title_name,$null,'utf8');
            export_xls($four_title,$four_count,'utf8');
            export_xls(array(),$null,'utf8');
            //表四结束
            //表五开始（会员等级占比分析）
            $map = array();
            $rank_info = M('MemberRank')->field('id,name')->select();
            $map['status']=1;
            $map['is_tmp']=0;
            foreach($rank_info as $k=>$v){
                $map['rank_id'] = $v['id'];
                $five_count[0][] = M('Member')->where($map)->count();
            }
            $five_title=i_array_column($rank_info,'name');
            $title_name = array('会员等级占比分析');
            export_xls($title_name,$null,'utf8');
            export_xls($five_title,$five_count,'utf8');
            export_xls(array(),$null,'utf8');
            //表五结束
            //表六开始（会员复购率分析）
            $six_info = $this->buy_count($riqi,$date_from,$date_to,1);
            $six_count[0][] = $six_info[1];
            $six_count[0][] = $six_info[2];
            $six_count[0][] = $six_info[3];
            $six_count[0][] = $six_info[4];
            $six_count[0][] = $six_info[5];
            $six_count[0][] = $six_info[6];
            $six_count[0][] = $six_info[7];
            $six_count[0][] = $six_info[8];
            $six_count[0][] = $six_info[9];
            $six_count[0][] = $six_info[10];
            $six_title=array('1次','2次','3次','4次','5次','6次','7次','8次','9次','10次以上');
            $title_name = array('会员复购率分析');
            export_xls($title_name,$null,'utf8');
            export_xls($six_title,$six_count,'utf8');
            export_xls(array(),$null,'utf8');
            //表六结束
			$name = CONTROLLER_NAME;
			$map=array();
			if($postdata['date_from'])$map['addtime'][] = array('egt',$date_from);
			if($postdata['date_to'])$map['addtime'][] = array('lt',$date_to);
			$map['type'] = array('in','1,11,12,13,14');
            $map['status'] = 3;
            $result = M('OrderInfo')->where($map)->field("sum(order_amount) as allmoney,uid,count(id) as ids")->group('uid')->order('allmoney desc')->limit($limit)->select();
			$title=array('序号','会员名','订单数','订单金额');
			foreach($result as $k => $v){
				$data[$k][]=$k+1;
				$data[$k][]=strip_tags(showuser($v['uid']));
				$data[$k][]=$v['ids'];
				$data[$k][]=$v['allmoney']?$v['allmoney']:'0.00';
			}
            $title_name = array('会员消费排行');
            export_xls($title_name,$null,'utf8');
			export_xls($title,$data,'utf8');
            export_xls(array(),$null,'utf8');
			unset($title,$data);exit;
        }
    }
}
?>