<?php
/**
* 余额统计
* @Copyright (C) 2016 汉潮 All rights reserved.
* @License http://www.hanchao9999.com
* @Author xiaogg <xiaogg@sina.cn>
*/
namespace Admin\Controller;
class MoneyTongController extends BaseController{
	//余额列表及会员注册量统计
	public function index(){
		/*充值日增长量统计开始*/
		/*获取当月时间数组开始*/
		$type = I('type');
		$day_arr = array();
		if($type == 30){
			$j = date(j); //获取当前月份天数
			$start_time = NOW_TIME;  //获取本月第一天时间戳
			for($i=0;$i<30;$i++){
				$day_arr[date('Y-m-d',$start_time-$i*86400)] = date('Y-m-d',$start_time-$i*86400); //每隔一天赋值给数组
			}
		}else{
			$j = date(j); //获取当前月份天数
			$start_time = NOW_TIME;  //获取本月第一天时间戳
			for($i=0;$i<7;$i++){
				$day_arr[date('Y-m-d',$start_time-$i*86400)] = date('Y-m-d',$start_time-$i*86400); //每隔一天赋值给数组
			}
		}
		$postdata = I('param.');
        $map = $cz_map = $ye_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)$map['addtime'][] = array('lt',$date_to);
        }else{
			if($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')));
			}else{
				$type=3;
			}
		}
		
		// 充值订单统计开始
		$cz_map = $map;
        $cz_map['status'] = 3;
        $cz_map['pay_status'] = 3;
        $cz_map['type'] = 2;
        $info['cz']=M('OrderInfo')->where($cz_map)->field("sum(order_amount) as money,DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y-%m-%d') as mon")->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y-%m-%d')")->select();

        // 每个折线的图标代表什么
        $name_cz = array("充值总金额");
		if($info['cz']){
			foreach($info['cz'] as $key => $value){
				if(!empty($value)){
                    $file_time_cz[$value['mon']] = $value['mon'];
                    $file_money_cz[$value['mon']] = $value['money'];
				}else{
					$file_time_cz[date('Y-m-d',strtotime($postdata['date_from']))] = date('Y-m-d',strtotime($postdata['date_from']));
                    $file_money_cz[date('Y-m-d',strtotime($postdata['date_from']))] = 0;
				}
			}unset($value,$key);
        }
		
        $new_day_cz = array_diff($day_arr,$file_time_cz);
        foreach($new_day_cz as $v){
			$file_money_cz[$v]=0;
        }unset($v);
        ksort($file_money_cz);
        // 得到折线图
        $cz_count_cz = array(
            "legend"=>array('data'=>array($name_cz[0])),
            "xaxis"=>array("type"=>"category","data"=>array_keys($file_money_cz)),
            "series"=>array(
				0=>array("name"=>$name_cz[0],"type"=>"line","stack"=>$name_cz[0],"data"=>array_values($file_money_cz)),
            ),
        );
		// 充值订单统计结束
		
		// 余额消耗订单开始
		$ye_map = $map;
        $ye_map['status'] = 3;
        $ye_map['pay_status'] = 3;
        $ye_map['used_money'] = array('gt',0);
        $info['ye']=M('OrderInfo')->where($ye_map)->field("sum(used_money) as money,DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y-%m-%d') as mon")->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y-%m-%d')")->select();

        // 每个折线的图标代表什么
        $name_ye = array("总使用余额");
		if($info['ye']){
			foreach($info['ye'] as $key => $value){
				if(!empty($value)){
                    $file_time_ye[$value['mon']] = $value['mon'];
                    $file_money_ye[$value['mon']] = $value['money'];
				}else{
					$file_time_ye[date('Y-m-d',strtotime($postdata['date_from']))] = date('Y-m-d',strtotime($postdata['date_from']));
                    $file_money_ye[date('Y-m-d',strtotime($postdata['date_from']))] = 0;
				}
			}unset($value,$key);
        }
		
        $new_day_ye = array_diff($day_arr,$file_time_ye);
        foreach($new_day_ye as $v){
			$file_money_ye[$v]=0;
        }unset($v);
        ksort($file_money_ye);
        //得到折线图
        $cz_count_ye = array(
            "legend"=>array('data'=>array($name_ye[0])),
            "xaxis"=>array("type"=>"category","data"=>array_keys($file_money_ye)),
            "series"=>array(
				0=>array("name"=>$name_ye[0],"type"=>"line","stack"=>$name_ye[0],"data"=>array_values($file_money_ye)),
            ),
        );
		// 余额消耗订单结束
		
		
		// 积分获得统计
		$map = array();
		if($postdata['date_from'] && $postdata['date_to']){
            $date_from = strtotime($postdata['date_from']);
            if($date_from)$map['change_time'][] = array('egt',$date_from);
            $date_to = strtotime($postdata['date_to']);
            if($date_to)$map['change_time'][] = array('lt',$date_to);
        }else{
			if($type == 1){
				$map['change_time'][] = array('egt',mktime(0,0,0,date('m'),date('d'),date('Y')));
				$map['change_time'][] = array('lt',NOW_TIME);
			}elseif($type == 2){
				$map['change_time'][] = array('egt',mktime(0,0,0,date('m'),date('d')-1,date('Y')));
				$map['change_time'][] = array('lt',mktime(0,0,0,date('m'),date('d'),date('Y'))-1);
			}elseif($type == 7){
				$map['change_time'][] = array('egt',mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')));
				$map['change_time'][] = array('lt',mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')));
			}elseif($type == 30){
				$map['change_time'][] = array('egt',mktime(0,0,0,date('m'),1,date('Y')));
				$map['change_time'][] = array('lt',mktime(23,59,59,date('m'),date('t'),date('Y')));
			}else{
				$type=3;
			}
		}
		$jf_map = $map;
		$jf_map['type'] = 4;
		// 签到赠积分
		$qd_jf_map = $jf_map;
		$qd_jf_map['change_desc'] = array('like','%签到赠积分%');
		$qd_sum = M('AccountLog')->where($qd_jf_map)->sum('change_num');
		// 消费赠积分
		$xf_jf_map = $jf_map;
		$xf_jf_map['change_desc'] = array('like','%会员等级特权活动赠送%');
		$xf_sum = M('AccountLog')->where($xf_jf_map)->sum('change_num');
		$pay_style_count = array(
            // "legend"=>array('data'=>array('消费赠积分','充值赠积分','签到赠积分')),
            "legend"=>array('data'=>array('消费赠积分','充值赠积分')),
            "series"=>array(
                array("name"=>"","type"=>"pie",
                    "data"=>array(
                        array("value"=>$xf_sum,"name"=>"消费赠积分"),
                        // array("value"=>$pay_style_num,"name"=>"充值赠积分"),
                        array("value"=>$qd_sum,"name"=>"签到赠积分"),
                    ),
                ),
            ),
        );
		
		// 优惠券统计
		if($postdata['date_from'] && $postdata['date_to']){
            $date_from = array('egt',strtotime($postdata['date_from']));
            $date_to = array('lt',strtotime($postdata['date_to']));
        }else{
			if($type == 1){
				$date_from = array('egt',mktime(0,0,0,date('m'),date('d'),date('Y')));
				$date_to = array('lt',NOW_TIME);
			}elseif($type == 2){
				$date_from = array('egt',mktime(0,0,0,date('m'),date('d')-1,date('Y')));
				$date_to = array('lt',mktime(0,0,0,date('m'),date('d'),date('Y'))-1);
			}elseif($type == 7){
				$date_from = array('egt',mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y')));
				$date_to = array('lt',mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y')));
			}elseif($type == 30){
				$date_from = array('egt',mktime(0,0,0,date('m'),1,date('Y')));
				$date_to = array('lt',mktime(23,59,59,date('m'),date('t'),date('Y')));
			}else{
				$date_from = array('egt',mktime(0,0,0,date('m'),1,date('Y')));
				$date_to = array('lt',mktime(23,59,59,date('m'),date('t'),date('Y')));
				$type=3;
			}
		}
		//领券数
        $map=array();
        $map['add_time'][]= $date_from;
        $map['add_time'][]= $date_to;
        $map['coupons_type'] = 1;
        $info['lingqu'] = M('CouponsGet')->where($map)->field('count(*) as num,add_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(add_time),'%Y年%m月%d天')")->select();
		//核销数
		$map=array();
        $map['create_time'][] = $date_from;
        $map['create_time'][] = $date_to;
		$info['hexiao'] = M('CouponsDonation')->where($map)->field('count(*) as num,create_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(create_time),'%Y年%m月%d天')")->select();
		//转赠数
		$map=array();
		$map['add_time'][] = $date_from;
        $map['add_time'][] = $date_to;
        $map['send_id'][] = array('neq','0');
		$info['zhuanzeng'] = M('CouponsDonation')->where($map)->field('count(*) as num,add_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(add_time),'%Y年%m月%d天')")->select();
        //每个折线的图标代表什么
        $names = 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',$date_from)] =0;
					$file_time[$key][]=date('Y-m-d',$date_from);
				}
			}unset($value,$key,$k,$v);
        }
        foreach($day_arr as $k => $v){
			foreach($file_time as $ke=>$ve){
				if(!in_array($v,$ve)){
					$file[$ke][$v]=0;
				}
			} 
        }
        unset($value,$key,$k,$v);
        ksort($file['lingqu']);
        ksort($file['hexiao']);
        ksort($file['zhuanzeng']);
        //得到折线图
        $cz_count = array(
            "legend"=>array('data'=>array($names[0],$names[1],$names[2])),
            "xaxis"=>array("type"=>"category","data"=>array_keys($file['lingqu'])),
            "series"=>array(
                0=>array("name"=>$names[0],"type"=>"bar","stack"=>$names[0],'itemStyle'=>array(),"data"=>array_values($file['lingqu'])),
                1=>array("name"=>$names[1],"type"=>"bar","stack"=>$names[1],"data"=>array_values($file['hexiao'])),
                2=>array("name"=>$names[2],"type"=>"bar","stack"=>$names[2],"data"=>array_values($file['zhuanzeng'])),
            ),
        );
		
		
        $ec = new \Common\Vendor\Echarts();
        $this->assign('area_count',$ec->show('area_count',$cz_count_cz,'line'));
        $this->assign('shop_sell',$ec->show('shop_sell',$cz_count_ye,'line'));
        $this->assign('Area_count_pay',$ec->pie_show('Area_count_pay',$pay_style_count,'pie'));
        $this->assign('yhq_count',$ec->show('yhq_count',$cz_count,'bar'));
        $this->assign('type',$type);
        $this->assign($postdata);
        /*订单统计结束*/
		if($postdata['print']==1){
            $this->display('print');
        }else{
            $this->display();
        }
	}
	//站长统计
	public function cnzz(){
        $sitecode=C('STATS_CODE');
        $siteid=session('cnzz_siteid');
        if(empty($siteid)){
            $pattern="|cnzz_stat_icon_(.*)'|isU";
            preg_match($pattern, $sitecode, $matches);$siteid=intval($matches[1]);
            if($siteid)session('cnzz_siteid',$siteid);
        }
        if(empty($siteid) )$this->error('请先到系统配置,内容设置里设置CNZZ的站点统计代码!',U('Admin/Config/group'),15	);
        header('location:http://new.cnzz.com/v1/login.php?siteid='.$siteid);
	}
    public function get_list(){
        $return = array('status'=>0,'data'=>'');
        $type = I('type');
        $map = array();
        if($type == 1){
            $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 == 9){
            $map['addtime'][] = array('egt',mktime(0,0,0,date('m'),date('d'),date('Y')));
            $map['addtime'][] = array('lt',NOW_TIME);
        }elseif($type == 7){
            $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 == 11){
            $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 == 12){
            $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')->select();
        $return['status'] = 1;
        $this->assign('info_list',$info_list);
        $list_html=$this->fetch('index_list');
        $return['data'] = $list_html;
        $this->ajaxReturn($return);
    }
    
    public function biao_index(){
        $type = I('type');
        $date_from = $date_to= '';
        if($type == 2){
            $riqi = 1;
        }elseif($type == 3){
            $riqi = 7;
        }elseif($type == 99){
            $riqi = -1;
            $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();
        //复购率结束
        //等级分析开始
        $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')->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();
            $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 = $today+86400;
                }else{
                    $today = $today - $i*86400;
                    $tomorrow = $today+($i-1)*86400;
                }
                $count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['is_tmp'] = 1;
                $map['reg_time']=array('between',array($today,$tomorrow));
                $count[$i]['f_count'] = M('Member')->where($map)->count();
                $g_map['add_time'] = array('between',array($today,$tomorrow));
                $count[$i]['g_count']=M('ShopsCustomer')->where($g_map)->count();
                $map['is_tmp'] = 0;
                $count[$i]['m_count']=M('Member')->where($map)->count();
                $count[$i]['sum'] = $count[$i]['f_count'] + $count[$i]['g_count'] + $count[$i]['m_count'];
            }
        }else{
            $now_time = NOW_TIME;
            $count=$map=array();
            $today=strtotime(date('Y-m-d',NOW_TIME));
            for($i=0;$i<$riqi;$i++){
                if($i == 0){
                    $tomorrow = $today+86400;
                }else{
                    $today = $today - $i*86400;
                    $tomorrow = $today+($i-1)*86400;
                }
                $count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['is_tmp'] = 1;
                $map['reg_time']=array('between',array($today,$tomorrow));
                $count[$i]['f_count'] = M('Member')->where($map)->count();
                $g_map['add_time'] = array('between',array($today,$tomorrow));
                $count[$i]['g_count']=M('ShopsCustomer')->where($g_map)->count();
                $map['is_tmp'] = 0;
                $count[$i]['m_count']=M('Member')->where($map)->count();
                $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($is_return=0){
        $map = array();
        $map['type'] = array('in','1,11,13,14');
        $map['status'] = 3;
        $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();
        $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();
            $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 = $today+86400;
                }else{
                    $today = $today - $i*86400;
                    $tomorrow = $today+($i-1)*86400;
                }
                $scurce_list[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['source'] = 1;
                $map['reg_time']=array('between',array($today,$tomorrow));
                $scurce_list[$i]['zhijie'] = M('Member')->where($map)->count();
                $map['source'] = 2;
                $scurce_list[$i]['mendian'] = M('Member')->where($map)->count();
                $map['source'] = 3;
                $scurce_list[$i]['lingka'] = M('Member')->where($map)->count();
                $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();
            $today=strtotime(date('Y-m-d',NOW_TIME));
            for($i=0;$i<$riqi;$i++){
                if($i == 0){
                    $tomorrow = $today+86400;
                }else{
                    $today = $today - $i*86400;
                    $tomorrow = $today+($i-1)*86400;
                }
                $scurce_list[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['source'] = 1;
                $map['reg_time']=array('between',array($today,$tomorrow));
                $scurce_list[$i]['zhijie'] = M('Member')->where($map)->count();
                $map['source'] = 2;
                $scurce_list[$i]['mendian'] = M('Member')->where($map)->count();
                $map['source'] = 3;
                $scurce_list[$i]['lingka'] = M('Member')->where($map)->count();
                $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'];
            }
        }
        $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= '';
            if($type == 2){
                $riqi = 1;
            }elseif($type == 3){
                $riqi = 7;
            }elseif($type == 99){
                $riqi = -1;
                $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();
            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(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')->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;
        }
    }
}
?>