<?php
/**
 * 会员统计
 *
 */

namespace app\admin\controller;

use think\Lang;
use think\Loader;

class Statmember extends AdminControl
{
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH.'admin/lang/zh-cn/stat.lang.php');
        Lang::load(APP_PATH.'admin/lang/zh-cn/member.lang.php');
        Loader::import('mall.statistics');
        Loader::import('mall.datehelper');
        $model = Model('stat');
        //存储参数
        $this->search_arr = input('param.');
        //处理搜索时间
        if (in_array(request()->action(),array('newmember','analyze','scale','area','buying'))){
            $this->search_arr = $model->dealwithSearchTime($this->search_arr);
            //获得系统年份
            $year_arr = getSystemYearArr();
            //获得系统月份
            $month_arr = getSystemMonthArr();
            //获得本月的周时间段
            $week_arr = getMonthWeekArr($this->search_arr['week']['current_year'], $this->search_arr['week']['current_month']);
            $this->assign('year_arr', $year_arr);
            $this->assign('month_arr', $month_arr);
            $this->assign('week_arr', $week_arr);
        }
        $this->assign('search_arr', $this->search_arr);
    }

    /**
     * 新增会员
     */
    public function newmember(){
        if(!isset($this->search_arr['search_type'])){
            $this->search_arr['search_type'] = 'day';
        }
        $model = Model('stat');
        $statlist = array();//统计数据列表
        //新增总数数组
        $count_arr = array('up'=>0,'curr'=>0);
        $where = array();
        $field = ' COUNT(*) as allnum ';
        if($this->search_arr['search_type'] == 'day'){
            //构造横轴数据
            for($i=0; $i<24; $i++){
                //统计图数据
                $curr_arr[$i] = 0;//今天
                $up_arr[$i] = 0;//昨天
                //统计表数据
                $currlist_arr[$i]['timetext'] = $i;

                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = $this->search_arr['day']['search_time']+$i*3600;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime']+3600;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = "$i";
            }
            $stime = $this->search_arr['day']['search_time'] - 86400;//昨天0点
            $etime = $this->search_arr['day']['search_time'] + 86400 - 1;//今天24点
            //总计的查询时间
            $count_arr['seartime'] = ($stime+86400).'|'.$etime;

            $today_day = @date('d', $this->search_arr['day']['search_time']);//今天日期
            $yesterday_day = @date('d', $stime);//昨天日期

            $where['member_add_time'] = array('between',array($stime,$etime));
            $field .= ' ,DAY(FROM_UNIXTIME(member_add_time)) as dayval,HOUR(FROM_UNIXTIME(member_add_time)) as hourval ';
            $memberlist = $model->statByMember($where, $field, 0, '', '');
            if($memberlist){
                foreach($memberlist as $k => $v){
                    if($today_day == $v['dayval']){
                        $curr_arr[$v['hourval']] = intval($v['allnum']);
                        $currlist_arr[$v['hourval']]['val'] = intval($v['allnum']);
                        $count_arr['curr'] += intval($v['allnum']);
                    }
                    if($yesterday_day == $v['dayval']){
                        $up_arr[$v['hourval']] = intval($v['allnum']);
                        $uplist_arr[$v['hourval']]['val'] = intval($v['allnum']);
                        $count_arr['up'] += intval($v['allnum']);
                    }
                }
            }
            $stat_arr['series'][0]['name'] = '昨天';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '今天';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);

            //统计数据标题
            $statlist['headertitle'] = array('小时','昨天','今天','同比');
            $this->assign('actionurl','statmember/newmember?search_type=day&search_time='.date('Y-m-d',$this->search_arr['day']['search_time']));
        }

        if($this->search_arr['search_type'] == 'week'){
            $current_weekarr = explode('|', $this->search_arr['week']['current_week']);
            $stime = strtotime($current_weekarr[0])-86400*7;
            $etime = strtotime($current_weekarr[1])+86400-1;
            //总计的查询时间
            $count_arr['seartime'] = ($stime+86400*7).'|'.$etime;

            $up_week = @date('W', $stime);//上周
            $curr_week = @date('W', $etime);//本周

            //构造横轴数据
            for($i=1; $i<=7; $i++){
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                $tmp_weekarr = getSystemWeekArr();
                //统计表数据
                $currlist_arr[$i]['timetext'] = $tmp_weekarr[$i];
                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = strtotime($current_weekarr[0])+($i-1)*86400;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime']+86400 - 1;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = $tmp_weekarr[$i];
                unset($tmp_weekarr);
            }
            $where['member_add_time'] = array('between', array($stime,$etime));
            $field .= ',WEEKOFYEAR(FROM_UNIXTIME(member_add_time)) as weekval,WEEKDAY(FROM_UNIXTIME(member_add_time))+1 as dayofweekval ';
            $memberlist = $model->statByMember($where, $field, 0, '');

            if($memberlist){
                foreach($memberlist as $k=>$v){
                    if ($up_week == intval($v['weekval'])){
                        $up_arr[$v['dayofweekval']] = intval($v['allnum']);
                        $uplist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
                        $count_arr['up'] += intval($v['allnum']);
                    }
                    if ($curr_week == $v['weekval']){
                        $curr_arr[$v['dayofweekval']] = intval($v['allnum']);
                        $currlist_arr[$v['dayofweekval']]['val'] = intval($v['allnum']);
                        $count_arr['curr'] += intval($v['allnum']);
                    }
                }
            }

            $stat_arr['series'][0]['name'] = '上周';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '本周';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            //统计数据标题
            $statlist['headertitle'] = array('星期','上周','本周','同比');
            $this->assign('actionurl','statmember/newmember?search_type=week&searchweek_year='.$this->search_arr['week']['current_year'].'&searchweek_month='.$this->search_arr['week']['current_month'].'&searchweek_week='.$this->search_arr['week']['current_week']);
        }

        if($this->search_arr['search_type'] == 'month'){
            $stime = strtotime($this->search_arr['month']['current_year'].'-'.$this->search_arr['month']['current_month']."-01 -1 month");
            $etime = getMonthLastDay($this->search_arr['month']['current_year'],$this->search_arr['month']['current_month'])+86400-1;
            //总计的查询时间
            $count_arr['seartime'] = strtotime($this->search_arr['month']['current_year'].'-'.$this->search_arr['month']['current_month']."-01").'|'.$etime;

            $up_month = date('m',$stime);
            $curr_month = date('m',$etime);
            //计算横轴的最大量（由于每个月的天数不同）
            $up_dayofmonth = date('t',$stime);
            $curr_dayofmonth = date('t',$etime);
            $x_max = $up_dayofmonth > $curr_dayofmonth ? $up_dayofmonth : $curr_dayofmonth;

            //构造横轴数据
            for($i=1; $i<=$x_max; $i++){
                //统计图数据
                $up_arr[$i] = 0;
                $curr_arr[$i] = 0;
                //统计表数据
                $currlist_arr[$i]['timetext'] = $i;
                //方便搜索会员列表，计算开始时间和结束时间
                $currlist_arr[$i]['stime'] = strtotime($this->search_arr['month']['current_year'].'-'.$this->search_arr['month']['current_month']."-01")+($i-1)*86400;
                $currlist_arr[$i]['etime'] = $currlist_arr[$i]['stime']+86400 - 1;

                $uplist_arr[$i]['val'] = 0;
                $currlist_arr[$i]['val'] = 0;
                //横轴
                $stat_arr['xAxis']['categories'][] = $i;
                unset($tmp_montharr);
            }
            $where['member_add_time'] = array('between', array($stime,$etime));
            $field .= ',MONTH(FROM_UNIXTIME(member_add_time)) as monthval,day(FROM_UNIXTIME(member_add_time)) as dayval ';
            $memberlist = $model->statByMember($where, $field, 0, '');
            if($memberlist){
                foreach($memberlist as $k=>$v){
                    if ($up_month == $v['monthval']){
                        $up_arr[$v['dayval']] = intval($v['allnum']);
                        $uplist_arr[$v['dayval']]['val'] = intval($v['allnum']);
                        $count_arr['up'] += intval($v['allnum']);
                    }
                    if ($curr_month == $v['monthval']){
                        $curr_arr[$v['dayval']] = intval($v['allnum']);
                        $currlist_arr[$v['dayval']]['val'] = intval($v['allnum']);
                        $count_arr['curr'] += intval($v['allnum']);
                    }
                }
            }
            $stat_arr['series'][0]['name'] = '上月';
            $stat_arr['series'][0]['data'] = array_values($up_arr);
            $stat_arr['series'][1]['name'] = '本月';
            $stat_arr['series'][1]['data'] = array_values($curr_arr);
            //统计数据标题
            $statlist['headertitle'] = array('日期','上月','本月','同比');
            $this->assign('actionurl','statmember/newmember?search_type=month&searchmonth_year='.$this->search_arr['month']['current_year'].'&searchmonth_month='.$this->search_arr['month']['current_month']);
        }

        //计算同比
        foreach ((array)$currlist_arr as $k=>$v){
            $tmp = array();
            $tmp['timetext'] = $v['timetext'];
            $tmp['seartime'] = $v['stime'].'|'.$v['etime'];
            $tmp['currentdata'] = $v['val'];
            $tmp['updata'] = $uplist_arr[$k]['val'];
            $tmp['tbrate'] = getTb($tmp['updata'], $tmp['currentdata']);
            $statlist['data'][]  = $tmp;
        }
        //计算总结同比
        $count_arr['tbrate'] = getTb($count_arr['up'], $count_arr['curr']);

        //导出Excel
        if (input('param.exporttype') == 'excel'){
            //导出Excel
            $excel_obj = new \excel\Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
            //header
            foreach ($statlist['headertitle'] as $v){
                $excel_data[0][] = array('styleid'=>'s_title','data'=>$v);
            }
            //data
            foreach ($statlist['data'] as $k=>$v){
                $excel_data[$k+1][] = array('data'=>$v['timetext']);
                $excel_data[$k+1][] = array('format'=>'Number','data'=>$v['updata']);
                $excel_data[$k+1][] = array('format'=>'Number','data'=>$v['currentdata']);
                $excel_data[$k+1][] = array('data'=>$v['tbrate']);
            }
            $excel_data[count($statlist['data'])+1][] = array('data'=>'总计');
            $excel_data[count($statlist['data'])+1][] = array('format'=>'Number','data'=>$count_arr['up']);
            $excel_data[count($statlist['data'])+1][] = array('format'=>'Number','data'=>$count_arr['curr']);
            $excel_data[count($statlist['data'])+1][] = array('data'=>$count_arr['tbrate']);

            $excel_data = $excel_obj->charset($excel_data,CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset('新增会员统计',CHARSET));
            $excel_obj->generateXML($excel_obj->charset('新增会员统计',CHARSET).date('Y-m-d-H',time()));
            exit();
        } else {
            //得到统计图数据
            $stat_arr['title'] = '新增会员统计';
            $stat_arr['yAxis'] = '新增会员数';
            $stat_json = getStatData_LineLabels($stat_arr);
            $this->assign('stat_json',$stat_json);
            $this->assign('statlist',$statlist);
            $this->assign('count_arr',$count_arr);
            $this->setAdminCurItem('newmember');
            return $this->fetch('stat_newmember');
        }
    }
    /**
     * 会员分析
     */
    public function analyze(){
        if(!isset($this->search_arr['search_type'])){
            $this->search_arr['search_type'] = 'day';
        }
        $model = Model('stat');
        //构造横轴数据
        for($i=1; $i<=15; $i++){
            //横轴
            $stat_arr['xAxis']['categories'][] = $i;
        }
        $stat_arr['title'] = '买家排行Top15';
        $stat_arr['legend']['enabled'] = false;

        //获得搜索的开始时间和结束时间
        $searchtime_arr = $model->getStarttimeAndEndtime($this->search_arr);

        $where = array();
        $where['statm_time'] = array('between',$searchtime_arr);
        //下单量
        $where['statm_ordernum'] = array('gt',0);
        $field = ' statm_memberid, statm_membername, SUM(statm_ordernum) as ordernum ';
        $ordernum_listtop15 = $model->statByStatmember($where, $field, 0, 15, 'ordernum desc,statm_memberid desc', 'statm_memberid');
        $stat_ordernum_arr = $stat_arr;
        $stat_ordernum_arr['series'][0]['name'] = '下单量';
        $stat_ordernum_arr['series'][0]['data'] = array();
       if(!empty($ordernum_listtop15)) {
           for ($i = 0; $i < 15; $i++) {
               $stat_ordernum_arr['series'][0]['data'][] = array(
                   'name' => strval($ordernum_listtop15[$i]['statm_membername']),
                   'y' => intval($ordernum_listtop15[$i]['ordernum'])
               );
           }
       }
        $stat_ordernum_arr['yAxis'] = '下单量';
        $statordernum_json = getStatData_Column2D($stat_ordernum_arr);
        unset($stat_ordernum_arr);
        $this->assign('statordernum_json',$statordernum_json);
        $this->assign('ordernum_listtop15',$ordernum_listtop15);

        //下单商品件数
        $where['statm_goodsnum'] = array('gt',0);
        $field = ' statm_memberid, statm_membername, SUM(statm_goodsnum) as goodsnum ';
        $goodsnum_listtop15 = $model->statByStatmember($where, $field, 0, 15, 'goodsnum desc,statm_memberid desc', 'statm_memberid');
        $stat_goodsnum_arr = $stat_arr;
        $stat_goodsnum_arr['series'][0]['name'] = '下单商品件数';
        $stat_goodsnum_arr['series'][0]['data'] = array();
        if(!empty($goodsnum_listtop15)) {
            for ($i = 0; $i < 15; $i++) {
                $stat_goodsnum_arr['series'][0]['data'][] = array(
                    'name' => strval($goodsnum_listtop15[$i]['statm_membername']),
                    'y' => intval($goodsnum_listtop15[$i]['goodsnum'])
                );
            }
        }
        $stat_goodsnum_arr['yAxis'] = '下单商品件数';
        $statgoodsnum_json = getStatData_Column2D($stat_goodsnum_arr);
        unset($stat_goodsnum_arr);
        $this->assign('statgoodsnum_json',$statgoodsnum_json);
        $this->assign('goodsnum_listtop15',$goodsnum_listtop15);

        //下单金额
        $where['statm_orderamount'] = array('gt',0);
        $field = ' statm_memberid, statm_membername, SUM(statm_orderamount) as orderamount ';
        $orderamount_listtop15 = $model->statByStatmember($where, $field, 0, 15, 'orderamount desc,statm_memberid desc', 'statm_memberid');
        $stat_orderamount_arr = $stat_arr;
        $stat_orderamount_arr['series'][0]['name'] = '下单金额';
        $stat_orderamount_arr['series'][0]['data'] = array();
        if(!empty($orderamount_listtop15)) {
            for ($i = 0; $i < 15; $i++) {
                $stat_orderamount_arr['series'][0]['data'][] = array(
                    'name' => strval($orderamount_listtop15[$i]['statm_membername']),
                    'y' => floatval($orderamount_listtop15[$i]['orderamount'])
                );
            }
        }
        $stat_orderamount_arr['yAxis'] = '下单金额';
        $statorderamount_json = getStatData_Column2D($stat_orderamount_arr);
        unset($stat_orderamount_arr);
        $this->assign('statorderamount_json',$statorderamount_json);
        $this->assign('orderamount_listtop15',$orderamount_listtop15);
        $this->assign('searchtime',implode('|',$searchtime_arr));
        $this->setAdminCurItem('analyze');
        return $this->fetch('stat_memberanalyze');
    }

    /**
     * 会员分析异步详细列表
     */
    public function analyzeinfo(){
        $model = Model('stat');
        $where = array();
        $searchtime_arr_tmp = explode('|',$this->search_arr['t']);
        foreach ((array)$searchtime_arr_tmp as $k=>$v){
            $searchtime_arr[] = intval($v);
        }
        $where['statm_time'] = array('between',$searchtime_arr);
        $memberlist = array();
        //查询统计数据
        $field = ' statm_memberid, statm_membername ';
        switch (input('param.type')){
            case 'orderamount':
                $where['statm_orderamount'] = array('gt',0);
                $field .= ' ,SUM(statm_orderamount) as orderamount ';
                $caption = '下单金额';
                break;
            case 'goodsnum':
                $where['statm_goodsnum'] = array('gt',0);
                $field .= ' ,SUM(statm_goodsnum) as goodsnum ';
                $caption = '商品件数';
                break;
            default:
                $_GET['type'] = 'ordernum';
                $where['statm_ordernum'] = array('gt',0);
                $field .= ' ,SUM(statm_ordernum) as ordernum ';
                $caption = '下单量';
                break;
        }
        //查询记录总条数
        $count_arr = $model->statByStatmember($where, 'COUNT(DISTINCT statm_memberid) as countnum');
        $countnum = intval($count_arr[0]['countnum']);
        if (input('param.exporttype') == 'excel'){
            $memberlist = $model->statByStatmember($where, $field, 0, 0, "{input('param.type')} desc,statm_memberid desc", 'statm_memberid');
        } else {
            $memberlist = $model->statByStatmember($where, $field, array(10,$countnum), 0, "{input('param.type')} desc,statm_memberid desc", 'statm_memberid');
        }
        $curpage = ($t = intval(input('param.curpage')))?$t:1;
        foreach ((array)$memberlist as $k=>$v){
            $v['number'] = ($curpage - 1) * 10 + $k + 1;
            $memberlist[$k] = $v;
        }
        //导出Excel
        if (input('param.exporttype') == 'excel'){
            //导出Excel
            $excel_obj = new \excel\Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
            //header
            $excel_data[0][] = array('styleid'=>'s_title','data'=>'序号');
            $excel_data[0][] = array('styleid'=>'s_title','data'=>'会员名称');
            $excel_data[0][] = array('styleid'=>'s_title','data'=>$caption);
            //data
            foreach ($memberlist as $k=>$v){
                $excel_data[$k+1][] = array('format'=>'Number','data'=>$v['number']);
                $excel_data[$k+1][] = array('data'=>$v['statm_membername']);
                $excel_data[$k+1][] = array('data'=>$v[$_GET['type']]);
            }
            $excel_data = $excel_obj->charset($excel_data,CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset('会员'.$caption.'统计',CHARSET));
            $excel_obj->generateXML($excel_obj->charset('会员'.$caption.'统计',CHARSET).date('Y-m-d-H',time()));
            exit();
        } else {
            $this->assign('caption',$caption);
            $this->assign('stat_field',$_GET['type']);
            $this->assign('memberlist',$memberlist);
            $this->assign('show_page',$model->page_info->render());
            echo $this->fetch('stat_memberanalyze_info');
        }
    }

    /**
     * 查看会员列表
     */
    public function showmember(){
        $model = Model('stat');
        $where = array();
        $actionurl ='';
        if (in_array(input('param.type'),array('newbyday','newbyweek','newbymonth'))){
            $actionurl = url('statmember/showmember',['type'=>'newbyday','t'=>input('param.t')]);
            $searchtime_arr_tmp = explode('|',input('param.t'));
            foreach ((array)$searchtime_arr_tmp as $k=>$v){
                $searchtime_arr[] = intval($v);
            }
            $where['member_time'] = array('between',$searchtime_arr);
        }
        if (isset($this->search_arr['exporttype'])&&$this->search_arr['exporttype'] == 'excel'){
            $member_list = $model->getMemberList($where);
        } else {
            $member_list = $model->getMemberList($where, '', 10);
        }
        if (is_array($member_list)){
            foreach ($member_list as $k=> $v){
                $member_list[$k]['member_time'] = isset($v['member_time'])?date('Y-m-d H:i:s',$v['member_time']):'';
                $member_list[$k]['member_login_time'] = $v['member_login_time']?date('Y-m-d H:i:s',$v['member_login_time']):'';
            }
        }
        //导出Excel
        if (isset($this->search_arr['exporttype'])&&$this->search_arr['exporttype'] == 'excel'){
            //导出Excel
            $excel_obj = new \excel\Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
            //header
            $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('member_index_name'));
            $excel_data[0][] = array('styleid'=>'s_title','data'=>'注册时间');
            $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('member_index_login_time'));
            $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('member_index_last_login'));
            $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('member_index_points'));
            $excel_data[0][] = array('styleid'=>'s_title','data'=>lang('member_index_prestore'));
            //data
            foreach ($member_list as $k=>$v){
                $excel_data[$k+1][] = array('data'=>$v['member_name'].'('.lang('member_index_true_name,ds_colon').$v['member_truename'].')');
                $excel_data[$k+1][] = array('data'=>$v['member_time']);
                $excel_data[$k+1][] = array('format'=>'Number','data'=>$v['member_login_num']);
                $excel_data[$k+1][] = array('data'=>$v['member_login_time'].'(IP:'.$v['member_login_ip'].')');
                $excel_data[$k+1][] = array('data'=>$v['member_points']);
                $excel_data[$k+1][] = array('data'=>lang('member_index_available,ds_colon').$v['available_predeposit'].lang('currency_zh').'('.lang('member_index_frozen,ds_colon').$v['freeze_predeposit'].lang('currency_zh').')');
            }
            $excel_data = $excel_obj->charset($excel_data,CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset('新增会员',CHARSET));
            $excel_obj->generateXML($excel_obj->charset('新增会员',CHARSET).date('Y-m-d-H',time()));
            exit();
        }
        $this->assign('actionurl',$actionurl);
        $this->assign('member_list',$member_list);
        $this->assign('show_page',$model->page_info->render());
        $this->setAdminCurItem('showmember');
        return $this->fetch('memberlist');
    }

    /**
     * 会员规模
     */
    public function scale(){
        if(!isset($this->search_arr['search_type'])){
            $this->search_arr['search_type'] = 'day';
        }
        $model = Model('stat');
        $statlist = array();//统计数据列表
        //获得搜索的开始时间和结束时间
        $searchtime_arr = $model->getStarttimeAndEndtime($this->search_arr);
        $where = array();
        $where['statm_time'] = array('between',$searchtime_arr);
        if (trim(isset($this->search_arr['membername']))){
            $where['statm_membername'] = array('like',"%".trim($this->search_arr['membername'])."%");
        }
        $field = ' statm_memberid, statm_membername, statm_time, SUM(statm_orderamount) as orderamount, SUM(statm_predincrease) as predincrease, -SUM(statm_predreduce) as predreduce, SUM(statm_pointsincrease) as pointsincrease, -SUM(statm_pointsreduce) as pointsreduce ';
        //排序
        $orderby_arr = array('orderamount asc','orderamount desc','predincrease asc','predincrease desc','predreduce asc','predreduce desc','pointsincrease asc','pointsincrease desc','pointsreduce asc','pointsreduce desc');
        if (!in_array(trim(isset($this->search_arr['orderby'])),$orderby_arr)){
            $this->search_arr['orderby'] = 'orderamount desc';
        }
        $orderby = trim($this->search_arr['orderby']).',statm_memberid desc';

        //查询记录总条数
        $count_arr = $model->statByStatmember($where, 'COUNT(DISTINCT statm_memberid) as countnum');
        $countnum = intval($count_arr[0]['countnum']);
        if (input('param.exporttype') == 'excel'){
            $statlist = $model->statByStatmember($where, $field, 0, 0, $orderby, 'statm_memberid');
        } else {
            $statlist = $model->statByStatmember($where, $field, array(10,$countnum), 0, $orderby, 'statm_memberid');
        }
        //导出Excel
        if (isset($this->search_arr['exporttype'])&&$this->search_arr['exporttype'] == 'excel'){
            //导出Excel
            $excel_obj = new \excel\Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
            //header
            $excel_data[0][] = array('styleid'=>'s_title','data'=>'会员名称');
            $excel_data[0][] = array('styleid'=>'s_title','data'=>'下单金额');
            $excel_data[0][] = array('styleid'=>'s_title','data'=>'增预存款');
            $excel_data[0][] = array('styleid'=>'s_title','data'=>'减预存款');
            $excel_data[0][] = array('styleid'=>'s_title','data'=>'增积分');
            $excel_data[0][] = array('styleid'=>'s_title','data'=>'减积分');
            //data
            foreach ($statlist as $k=>$v){
                $excel_data[$k+1][] = array('data'=>$v['statm_membername']);
                $excel_data[$k+1][] = array('data'=>$v['orderamount']);
                $excel_data[$k+1][] = array('data'=>$v['predincrease']);
                $excel_data[$k+1][] = array('data'=>$v['predreduce']);
                $excel_data[$k+1][] = array('data'=>$v['pointsincrease']);
                $excel_data[$k+1][] = array('data'=>$v['pointsreduce']);
            }
            $excel_data = $excel_obj->charset($excel_data,CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset('会员规模分析',CHARSET));
            $excel_obj->generateXML($excel_obj->charset('会员规模分析',CHARSET).date('Y-m-d-H',time()));
            exit();
        }
        $this->assign('statlist',$statlist);
        $this->assign('show_page',$model->page_info->render());
        $this->assign('orderby',$this->search_arr['orderby']);
        $this->setAdminCurItem('scale');
        return $this->fetch('stat_memberscale');
    }

    /**
     * 区域分析
     */
    public function area(){
        if(!isset($this->search_arr['search_type'])){
            $this->search_arr['search_type'] = 'day';
        }
        $model = Model('stat');
        //获得搜索的开始时间和结束时间
        $searchtime_arr = $model->getStarttimeAndEndtime($this->search_arr);
        $this->assign('searchtime',implode('|',$searchtime_arr));
        $this->setAdminCurItem('area');
        return $this->fetch('stat_memberarea');
    }
    /**
     * 区域分析之详细列表
     */
    public function area_list(){
        $model = Model('stat');
        $where = array();
        $where['order_isvalid'] = 1;//计入统计的有效订单
        $searchtime_arr_tmp = explode('|',$this->search_arr['t']);
        foreach ((array)$searchtime_arr_tmp as $k=>$v){
            $searchtime_arr[] = intval($v);
        }
        $where['order_add_time'] = array('between',$searchtime_arr);
        $field = ' reciver_province_id, COUNT(*) as ordernum,SUM(order_amount) as orderamount, COUNT(DISTINCT buyer_id) as membernum ';

        $orderby_arr = array('membernum asc','membernum desc','orderamount asc','orderamount desc','ordernum asc','ordernum desc');
        if (!isset($this->search_arr['orderby']) || !in_array(trim($this->search_arr['orderby']),$orderby_arr)){
            $this->search_arr['orderby'] = 'membernum desc';
        }
        $orderby = trim($this->search_arr['orderby']).',reciver_province_id';

        $count_arr = $model->getoneByStatorder($where, 'COUNT(DISTINCT reciver_province_id) as countnum');
        $countnum = intval($count_arr['countnum']);
        if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel'){
            $statlist_tmp = $model->statByStatorder($where, $field, 0, 0, $orderby, 'reciver_province_id');
        } else {
            $statlist_tmp = $model->statByStatorder($where, $field, array(10,$countnum), 0, $orderby, 'reciver_province_id');
        }
        // 地区
        $province_array = Model('area')->getTopLevelAreas();
        $statheader = array();
        $statheader[] = array('text'=>'省份','key'=>'provincename');
        $statheader[] = array('text'=>'下单会员数','key'=>'membernum','isorder'=>1);
        $statheader[] = array('text'=>'下单金额','key'=>'orderamount','isorder'=>1);
        $statheader[] = array('text'=>'下单量','key'=>'ordernum','isorder'=>1);
        $statlist = array();
        foreach ((array)$statlist_tmp as $k=>$v){
            $province_id = intval($v['reciver_province_id']);
            $tmp = array();
            $tmp['provincename'] = ($t = $province_array[$province_id]) ? $t : '其他';
            $tmp['membernum'] = $v['membernum'];
            $tmp['orderamount'] = $v['orderamount'];
            $tmp['ordernum'] = $v['ordernum'];
            $statlist[] = $tmp;
        }
        //导出Excel
        if (isset($this->search_arr['exporttype']) && $this->search_arr['exporttype'] == 'excel'){
            //导出Excel
            $excel_obj = new \excel\Excel();
            $excel_data = array();
            //设置样式
            $excel_obj->setStyle(array('id'=>'s_title','Font'=>array('FontName'=>'宋体','Size'=>'12','Bold'=>'1')));
            //header
            foreach ($statheader as $k=>$v){
                $excel_data[0][] = array('styleid'=>'s_title','data'=>$v['text']);
            }
            //data
            foreach ($statlist as $k=>$v){
                foreach ($statheader as $h_k=>$h_v){
                    $excel_data[$k+1][] = array('data'=>$v[$h_v['key']]);
                }
            }
            $excel_data = $excel_obj->charset($excel_data,CHARSET);
            $excel_obj->addArray($excel_data);
            $excel_obj->addWorksheet($excel_obj->charset('区域分析',CHARSET));
            $excel_obj->generateXML($excel_obj->charset('区域分析',CHARSET).date('Y-m-d-H',time()));
            exit();
        }
        $this->assign('statlist',$statlist);
        $this->assign('statheader',$statheader);
        $this->assign('orderby',$this->search_arr['orderby']);
        $this->assign('actionurl',url('statmember/area_list',['t'=>$this->search_arr['t']]));
        $this->assign('show_page',$model->page_info->render());
        $this->setAdminCurItem('area');
        echo $this->fetch('stat_listandorder');
    }
    /**
     * 区域分析之地图数据
     */
    public function area_map(){
        $model = Model('stat');
        $where = array();
        $where['order_isvalid'] = 1;//计入统计的有效订单
        $searchtime_arr_tmp = explode('|',$this->search_arr['t']);
        foreach ((array)$searchtime_arr_tmp as $k=>$v){
            $searchtime_arr[] = intval($v);
        }
        $where['order_add_time'] = array('between',$searchtime_arr);
        $memberlist = array();
        //查询统计数据
        $field = ' reciver_province_id ';
        $type=input('param.type');
        switch ($type){
            case 'orderamount':
                $field .= ' ,SUM(order_amount) as orderamount ';
                $orderby = 'orderamount desc';
                break;
            case 'ordernum':
                $field .= ' ,COUNT(*) as ordernum ';
                $orderby = 'ordernum desc';
                break;
            default:
                $type = 'membernum';
                $field .= ' ,COUNT(DISTINCT buyer_id) as membernum ';
                $orderby = 'membernum desc';
                break;
        }
        $orderby .= ',reciver_province_id';
        $statlist_tmp = $model->statByStatorder($where, $field, 10, 0, $orderby, 'reciver_province_id');
        // 地区
        $province_array = Model('area')->getTopLevelAreas();
        //地图显示等级数组
        $level_arr = array(array(1,2,3),array(4,5,6),array(7,8,9),array(10,11,12));
        $statlist = array();
        foreach ((array)$statlist_tmp as $k=>$v){
            $v['level'] = 4;//排名
            foreach ($level_arr as $lk=>$lv){
                if (in_array($k+1,$lv)){
                    $v['level'] = $lk;//排名
                }
            }
            $province_id = intval($v['reciver_province_id']);
            $statlist[$province_id] = $v;
        }
        $stat_arr = array();
        foreach ((array)$province_array as $k=>$v){
            if (!empty($statlist[$k])){
                switch ($type){
                    case 'orderamount':
                        $des = "，下单金额：{$statlist[$k]['orderamount']}";
                        break;
                    case 'ordernum':
                        $des = "，下单量：{$statlist[$k]['ordernum']}";
                        break;
                    default:
                        $des = "，下单会员数：{$statlist[$k]['membernum']}";
                        break;
                }
                $stat_arr[] = array('cha'=>$k,'name'=>$v,'des'=>$des,'level'=>$statlist[$k]['level']);
            } else {
                $des = "，无订单数据";
                $stat_arr[] = array('cha'=>$k,'name'=>$v,'des'=>$des,'level'=>4);
            }
        }
        $stat_json = getStatData_Map($stat_arr);
        $this->assign('stat_field',$_GET['type']);
        $this->assign('stat_json',$stat_json);
        echo $this->fetch('stat_map');
    }
    /**
     * 购买分析
     */
    public function buying(){
        if(!isset($this->search_arr['search_type'])){
            $this->search_arr['search_type'] = 'day';
        }
        $model = Model('stat');
        //获得搜索的开始时间和结束时间
        $searchtime_arr = $model->getStarttimeAndEndtime($this->search_arr);
        /*
         * 客单价分布
         */
        $where = array();
        $where['order_isvalid'] = 1;//计入统计的有效订单
        $where['order_add_time'] = array('between',$searchtime_arr);

        $field = '*';
        $pricerange_arr = ($t = trim(config('stat_orderpricerange')))?unserialize($t):'';
        if ($pricerange_arr){
            $stat_arr['series'][0]['name'] = '下单量';
            //设置价格区间最后一项，最后一项只有开始值没有结束值
            $pricerange_count = count($pricerange_arr);
            if ($pricerange_arr[$pricerange_count-1]['e']){
                $pricerange_arr[$pricerange_count]['s'] = $pricerange_arr[$pricerange_count-1]['e'] + 1;
                $pricerange_arr[$pricerange_count]['e'] = '';
            }
            foreach ((array)$pricerange_arr as $k=>$v){
                $v['s'] = intval($v['s']);
                $v['e'] = intval($v['e']);
                //构造查询字段
               /* if ($v['e']){
                    $field .= " ,SUM(IF(order_amount > {$v['s']} and order_amount <= {$v['e']},1,0)) as ordernum_{$k}";
                } else {
                    $field .= " ,SUM(IF(order_amount > {$v['s']},1,0)) as ordernum_{$k}";
                }*/
            }
            $orderlist = $model->getoneByStatorder($where, $field);
            if($orderlist){
                foreach ((array)$pricerange_arr as $k=>$v){
                    //横轴
                    if($v['e']){
                        $stat_arr['xAxis']['categories'][] = $v['s'].'-'.$v['e'];
                    } else {
                        $stat_arr['xAxis']['categories'][] = $v['s'].'以上';
                    }
                    //统计图数据
                    if ($orderlist['ordernum_'.$k]){
                        $stat_arr['series'][0]['data'][] = intval($orderlist['ordernum_'.$k]);
                    } else {
                        $stat_arr['series'][0]['data'][] = 0;
                    }
                }
            }
            //得到统计图数据
            $stat_arr['title'] = '客单价分布';
            $stat_arr['legend']['enabled'] = false;
            $stat_arr['yAxis'] = '下单量';
            $guestprice_statjson = getStatData_LineLabels($stat_arr);
        } else {
            $guestprice_statjson = '';
        }
        unset($stat_arr);

        /*
         * 购买频次分析
         */
        //统计期间会员下单量
        $where = array();
        $where['statm_time'] = array('between',$searchtime_arr);
        $where['statm_ordernum'] = array('gt',0);
        $field = 'COUNT(*) as countnum';
        $countnum_arr = $model->getOneStatmember($where,$field);
        $countnum = intval($countnum_arr['countnum']);
        $member_arr = array();
        for ($i=0; $i<$countnum; $i+=1000){//由于数据库底层的限制，所以每次查询1000条
            $statmember_list = array();
            $statmember_list = $model->statByStatmember($where, 'statm_memberid,statm_ordernum', 0, $i.',1000', 'statm_id');
            foreach ((array)$statmember_list as $k=>$v){
                $member_arr[$v['statm_memberid']] = intval($member_arr[$v['statm_memberid']]) + intval($v['statm_ordernum']);
            }
        }
        $stattimes_arr = array();
        if ($member_arr){
            //整理期间各个频次的下单客户数
            for ($i=1; $i<=10; $i++){
                $stattimes_arr[$i] = array('num'=>0,'rate'=>0.00);
                if ($i >= 10){
                    $stattimes_arr[$i]['text'] = '期间购买10次以上';
                } else {
                    $stattimes_arr[$i]['text'] = "期间购买{$i}次";
                }
            }
            foreach ($member_arr as $k=>$v){
                if ($v >= 10){
                    $stattimes_arr[10]['num'] = intval($stattimes_arr[10]['num']) + 1;
                } else {
                    $stattimes_arr[$v]['num'] = intval($stattimes_arr[$v]['num']) + 1;
                }
            }
            //计算期间各个频次的下单客户数占总数比例
            foreach ($stattimes_arr as $k=>$v){
                $stattimes_arr[$k]['rate'] = round(intval($v['num'])/count($member_arr)*100,2);
            }
        }

        //购买时段分布
        $where = array();
        $where['order_isvalid'] = 1;//计入统计的有效订单
        $where['order_add_time'] = array('between',$searchtime_arr);
        $field = ' HOUR(FROM_UNIXTIME(order_add_time)) as hourval,COUNT(*) as ordernum ';
        $orderlist = $model->statByStatorder($where, $field, 0, 0, '', '');
        $stat_arr = array();
        $stat_arr['series'][0]['name'] = '下单量';
        //构造横轴坐标
        for ($i=0; $i<24; $i++){
            //横轴
            $stat_arr['xAxis']['categories'][] = $i;
            $stat_arr['series'][0]['data'][$i] = 0;
        }
        foreach ((array)$orderlist as $k=>$v){
            //统计图数据
            $stat_arr['series'][0]['data'][$v['hourval']] = intval($v['ordernum']);
        }
        //得到统计图数据
        $stat_arr['title'] = '购买时段分布';
        $stat_arr['legend']['enabled'] = false;
        $stat_arr['yAxis'] = '下单量';
        $hour_statjson = getStatData_LineLabels($stat_arr);

        $this->assign('hour_statjson',$hour_statjson);
        $this->assign('stattimes_arr',$stattimes_arr);
        $this->assign('guestprice_statjson',$guestprice_statjson);
        $this->setAdminCurItem('buying');
        return $this->fetch('stat_buying');
    }
    
    protected function getAdminItemList()
    {
       $menu_array=array(
           array('name'=>'newmember','text'=>lang('stat_newmember'),'url'=>url('Statmember/newmember')),
           array('name'=>'analyze','text'=>lang('stat_memberanalyze'),'url'=>url('Statmember/analyze')),
           array('name'=>'scale','text'=>lang('stat_scaleanalyze'),'url'=>url('Statmember/scale')),
           array('name'=>'area','text'=>lang('stat_areaanalyze'),'url'=>url('Statmember/area')),
           array('name'=>'buying','text'=>lang('stat_buying'),'url'=>url('Statmember/buying')),
           array('name'=>'showmember','text'=>lang('stat_memberlist'),'url'=>url('Statmember/showmember'))
       );
       return $menu_array;
    }
}