<?php
/**
* 经营概况
* @Copyright (C) 2016 汉潮 All rights reserved.
* @License http://www.hanchao9999.com
* @Author xiaogg <xiaogg@sina.cn>
*/

namespace Admin\Controller;
class OrderTongController extends BaseController{
//经营概况
    public function index(){
        /*获取时间数组开始*/
        $type = I('type');
        $postdata = I('param.');
        $day_arr = array();
        $now_time=strtotime(date('Y-m-d',NOW_TIME));
        $new_map = $map = $gc_map = $map_z=array();
        if($postdata['date_from'] || $postdata['date_to']){
            $date_from = strtotime($postdata['date_from']);
            if($date_from){
                $map['addtime'][] = array('egt',$date_from);
                $gc_map['sell_time'][] = array('egt',$date_from);
                $map_z[]=$date_from;
            }else{
                $this->error("请选择搜索开始时间");exit;
            }
            $date_to = strtotime($postdata['date_to']);
            if($date_to){
                $map['addtime'][] = array('lt',$date_to+86399);
                $gc_map['sell_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);
            }
            if(in_array($type,array(7,30))){
                $between=quick_time($type);
                $date_from=$between[0];
                $date_to =$between[1];
                $map['addtime']=array('between',array($between[0],$between[1]));
                $gc_map['sell_time']=array('between',array($between[0],$between[1]));
            }else{
                $type=3;
                $date_from=$now_time-6*86400;
                $date_to =$now_time+86399;
                $map['addtime']=array('between',array($date_from,$date_to));
                $gc_map['sell_time']=array('between',array($date_from,$date_to));
            }
        }

        $this->assign('type',intval($type));
        if(in_array($type,array(1,2,3,7))){
            $days=7;
        }else if($type ==30){
            $days=30;
        }else{
            $days=ceil(($date_from-$date_to)/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);
        $ec = new \Common\Vendor\Echarts();
        /*获取时间数组结束*/

        /*用户消费类型开始*/
        $this->member($map_z,$day_arr,$date_from,$date_to,$ec);unset($map);
        /*用户消费类型占比结束*/


        /*消费会员统计开始*/
        $this->custome($new_map,$day_arr,$date_from,$date_to,$ec);unset($map);
        /*消费会员统计结束*/

        /*订单统计开始*/
        $this->order_tong($new_map,$day_arr,$ec);unset($map);
        /*订单统计结束*/

        /*支付方式统计开始*/
        $this->pay_type($map_z,$day_arr,$ec);unset($map);
        /*支付方式统计结束*/

        /*退款方式开始*/
        $this->tui_type($map_z,$day_arr,$ec);unset($map);
        /*退款方式结束*/

        /*订单类型开始*/
        $this->order_type($map_z,$day_arr,$ec);unset($map);
        /*订单类型结束*/
		
        /*收入TOP10仓库开始*/
        $this->top_shop($new_map,$day_arr,$ec);unset($map);
        /*收入TOP10仓库统计结束*/

        /*优惠方式*/
        $this->discount_type($map_z,$day_arr,$ec);unset($map);
        /*优惠方式*/

        /*充值趋势开始*/
        $this->chongzhi($new_map,$day_arr,$ec);unset($map);
        /*充值趋势结束*/

        /*余额消耗统计开始*/
        $this->yue($new_map,$day_arr,$ec);unset($map);
        /*余额消耗统计结束*/

        /*积分获得统计开始*/
        $this->score_get($map_z,$day_arr,$ec);unset($map);
        /*积分获得统计结束*/

        /*代金券统计开始*/
        $this->coupon_tong($new_map,$day_arr,$date_from,$date_to,$ec);unset($map);
        /*代金券统计结束*/
        /*代金券统计开始*/
        //$this->gift_tong($gc_map,$day_arr,$ec);unset($map);
        /*代金券统计结束*/
        /*代金券统计开始*/
        //$this->gift_analysis($map_z,$day_arr,$ec);unset($map);
        /*代金券统计结束*/
        $this->assign($postdata);
        if($postdata['print']==1){
            $this->display('print');
        }else{
            $this->display();
        }
    }
    public function info($map_z){
        $map_zc=$map_in=$map_t=$map=$map_c=$map_g=array();
        $map['status'] = array('in','1,2,3,6,8');
        $map['pay_status'] = 3;
        $map['type'] = array('in','1,6,11,12,13,14');
        $map['is_delete']=0;
        if($map_z){
            $map['addtime']=array('between',array($map_z[0],$map_z[1]));
            $map_t['addtime']=array('between',array($map_z[0],$map_z[1]));
            $map_c['addtime']=array('between',array($map_z[0],$map_z[1]));
            $map_g['sell_time']=array('between',array($map_z[0],$map_z[1]));
            $map_in['change_time']=array('between',array($map_z[0],$map_z[1]));
            $map_zc['create_time']=array('between',array($map_z[0],$map_z[1]));
        }else{
            $map_g['sell_time']=array('gt',0);
        }
        $sale=M('OrderInfo')->where($map)->field('sum(order_money) as order_money,count(id) as num')->find();//销售总额
        $this->assign($sale);
        $map_t['status']=1;
        $map_t['is_delete']=0;
        $tui=M('RefundOrder')->where($map_t)->field('sum(money) as tui_money,count(id) as tui_num')->find();//退款
        $this->assign($tui);
        $map['in_online']=0;
        $xs_order=M('OrderInfo')->where($map)->field('sum(order_money) as xs_money,count(id) as xs_num')->find();//线上订单
        $this->assign($xs_order);
        $map['in_online']=1;
        $xx_order=M('OrderInfo')->where($map)->field('sum(order_money) as xx_money,count(id) as xx_num')->find();//线下订单
        $this->assign($xx_order);
        $map_c['pay_status'] = 3;
        $map_c['type'] = array('in','2,5');
        $chong=M('OrderInfo')->where($map_c)->field('sum(order_amount) as c_money,count(id) as c_num')->find();//会员充值
        $this->assign($chong);
        $map_c['in_online'] = 0;
        $xs_chong=M('OrderInfo')->where($map_c)->field('sum(order_amount) as xs_cmoney,count(id) as xs_cnum')->find();//线上充值
        $this->assign($xs_chong);
        unset($map_c['in_online']);
        $map_c['in_online'] = 1;
        $xx_chong=M('OrderInfo')->where($map_c)->field('sum(order_amount) as xx_cmoney,count(id) as xx_cnum')->find();//线下充值
        $this->assign($xx_chong);
        $gift=M('GiftCard')->where($map_g)->field('sum(card_money) as g_kmoney,count(id) as g_num,sum(shop_price) as g_money')->find();//线下充值
        $this->assign($gift);
        //总收入
        $map_in['change_type']=0;
        $map_in['type']=1;
        $income=M('ShopbalanceLog')->where($map_in)->sum('change_num');
        //总支出
        $map_in['change_type']=1;
        $map_in['type']=1;
        $outcome=M('ShopbalanceLog')->where($map_in)->sum('change_num');
        $this->assign('income',$income);
        $this->assign('outcome',$outcome);
        unset($map['in_online']);
        $pay_type=M('OrderInfo')->where($map)->field('pay_code,sum(cash_money) as cash_money,sum(scan_money) as scan_money,sum(bank_money) as bank_money,sum(card_money) as card_money,sum(used_money) as used_money,sum(pay_amount) as pay_amount')->group('pay_code')->select();//收入
        $weixin=$hanchao=$card_money=$decca=$chinaums=$weixinsmall=$xianjin=0;
        foreach($pay_type as $k=>$v){
            if($v['pay_code']=='' || $v['pay_code']=='Hanchao' || $v['pay_code']=='HANCHAO'){
                $hanchao+=$v['used_money'];
            }
            if($v['pay_code']=='CHINAUMSPOS' || $v['pay_code']=='CHINAUMS'){
                $chinaums+=$v['scan_money'];
            }
            if($v['pay_code']=='weixinpay'){
                $weixin+=$v['pay_amount'];
            }
            if($v['pay_code']=='weixinsmall'){
                $weixinsmall+=$v['pay_amount'];
            }
            if($v['pay_code']=='HANCHAO_XIANJIN'){
                $xianjin+=$v['cash_money'];
            }
            if($v['pay_code']=='HANCHAO_CARD'){
                $card_money+=$v['card_money'];
            }
            if($v['pay_code']=='HANCHAO_DECCA'){
                $decca+=$v['bank_money'];
            }
        }
        $this->assign('used_money',$hanchao);
        $this->assign('chinaums',$chinaums);
        $this->assign('weixin',$weixin);
        $this->assign('weixinsmall',$weixinsmall);
        $this->assign('xianjin',$xianjin);
        $this->assign('card_money',$card_money);
        $this->assign('decca',$decca);
        $map_zc['type']=array('in','20,21,31');
        $pay_zc=M('PayLog')->where($map_zc)->field('pay_code,sum(cash_money) as cash_money,sum(scan_money) as scan_money,sum(bank_money) as bank_money,sum(card_money) as card_money,sum(use_money) as used_money,sum(pay_amount) as pay_amount')->group('pay_code')->select();//支出
        $zweixin=$zhanchao=$zcard_money=$zdecca=$zchinaums=$zweixinsmall=$zxianjin=0;
        foreach($pay_zc as $k=>$v){
            if($v['pay_code']=='' || $v['pay_code']=='Hanchao' || $v['pay_code']=='HANCHAO'){
                $zhanchao+=$v['used_money'];
            }
            if($v['pay_code']=='CHINAUMSPOS' || $v['pay_code']=='CHINAUMS'){
                $zchinaums+=$v['scan_money'];
            }
            if($v['pay_code']=='weixinpay'){
                $zweixin+=$v['pay_amount'];
            }
            if($v['pay_code']=='weixinsmall'){
                $zweixinsmall+=$v['pay_amount'];
            }
            if($v['pay_code']=='HANCHAO_XIANJIN'){
                $zxianjin+=$v['cash_money'];
            }
            if($v['pay_code']=='HANCHAO_CARD'){
                $zcard_money+=$v['card_money'];
            }
            if($v['pay_code']=='HANCHAO_DECCA'){
                $zdecca+=$v['bank_money'];
            }
        }
        $this->assign('zused_money',$zhanchao);
        $this->assign('zchinaums',$zchinaums);
        $this->assign('zweixin',$zweixin);
        $this->assign('zweixinsmall',$zweixinsmall);
        $this->assign('zxianjin',$zxianjin);
        $this->assign('zcard_money',$zcard_money);
        $this->assign('zdecca',$zdecca);
    }
//    用户信息统计
    public function member($map_z,$day_arr,$date_from,$date_to,$ec){
        $info=$name=$map_m=$map=array();
        $map_m['status']=1;
        $rank=M('MemberRank')->where($map_m)->field('id,name')->select();
        if($map_z) {
            $map['addtime'] = array('between', array($map_z[0], $map_z[1]));
        }
        $map['status'] = array('in','1,2,3,6,8');
        $map['pay_status'] = 3;
        $map['type'] = array('in','1,6,11,12,13,14');
        $map['is_delete']=0;
        foreach($rank as $k=>$v){
            $map_m['rank_id']=$v['id'];
            $member=M('Member')->where($map_m)->getField('id',true);
            $name[]=$v['name'];
            if($member){
                $map['uid']=array('in',$member);
                $order_m=M('OrderInfo')->where($map)->sum('order_money');
                $order_m=$order_m?$order_m:0;
                $info[] = array('value'=>$order_m,'name'=>$v['name']);
            }else{
                $info[]=array('value'=>0,'name'=>$v['name']);
            }
        }
        $member_count = array(
            "legend"=>array('data'=>$name),
            "series"=>array(
                array(
                "name"=>"用户消费类型占比","type"=>"pie",
                'radius'=>'65%',
                'data'=>$info,
                ),
            ),
        );
        $this->assign('member_count',$ec->pie_show('member_count',$member_count,'pie',''));

    }
//    消费会员统计
    public function custome($map,$day_arr,$date_from,$date_to,$ec){
        $savename=serialize($map);
        if(F('custome'.$savename)){
            $custome=F('custome'.$savename);
        }else{
            $file_time=$info=$new_day=array();
            $map['status'] = array('in','1,2,3,6,8');
            $map['pay_status'] = 3;
            $map['type'] = array('in','1,6,11,12,13,14');
            $map['is_delete']=0;
            $map['uid']=array('gt',0);
            $uids=M('OrderInfo')->where($map)->field('uid,addtime,order_amount')->select();
            $info=$file_uid=$file_time=$file_jine=array();
            foreach($uids as $k=>$v){
                $addtime=date('Y-m-d',$v['addtime']);
                $info[$addtime]['number'][$v['uid']]=1;
                $info[$addtime]['money'][$v['uid']]+=$v['order_amount'];
            }unset($k,$v);
            foreach($info as $k=>$v){
                $file_uid[$k]=count($v['number']);
                $file_time[$k]=$k;
                $file_jine[$k]=array_sum($v['money']);
            }

            //每个折线的图标代表什么
            $name = array("消费会员数","消费金额");

            $new_day=array_diff($day_arr,$file_time);
            foreach($new_day as $v){
                $file_uid[$v]=0;
                $file_time[$v]=0;
                $file_jine[$v]=0;
            }unset($v);
            ksort($file_uid);
            ksort($file_jine);
            //得到折线图
            $custome = array(
                "legend"=>array('data'=>array($name[0],$name[1])),
                "xaxis"=>array("type"=>"category","data"=>array_keys($file_uid)),
                "series"=>array(
                    0=>array("name"=>$name[0],"type"=>"line","stack"=>$name[0],'itemStyle'=>array(),"data"=>array_values($file_uid)),
                    1=>array("name"=>$name[1],"type"=>"line","stack"=>$name[1],"data"=>array_values($file_jine)),
                ),
            );
            //F('custome'.$savename,$custome);
        }

        $this->assign('custome',$ec->show('custome',$custome,'line'));
    }
//商品销售走势
    public function order_tong($map,$day_arr,$ec){
        $map_og=array();
        $map_og['a.addtime']=$map['addtime'];
        $file_goods=$file_profit=$file_money=$file_time=$file_num=$new_day=$info=array();
        $map_og['a.status']=$map['status'] = array('in','1,2,3,6,8');
        $map_og['a.pay_status']=$map['pay_status'] = 3;
        $map_og['a.type']=$map['type'] = array('in','1,6,11,12,13,14');
        $map_og['a.is_delete']=$map['is_delete']=0;

        $info=M('OrderInfo')->where($map)->field("count(id) as number,sum(order_money) as money,sum(profit) as profit,DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y-%m-%d') as mon")->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y-%m-%d')")->select();
        $info_og=M('OrderInfo')->alias('a')->where($map_og)->join('__ORDER_GOODS__ g ON g.order_id=a.id')->field("sum(g.goods_number) as goods_number,DATE_FORMAT(FROM_UNIXTIME(a.addtime),'%Y-%m-%d') as mon")->group("DATE_FORMAT(FROM_UNIXTIME(a.addtime),'%Y-%m-%d')")->select();
        //每个折线的图标代表什么
        $name = array("订单数","订单金额","利润","商品数量");
        if($info){
            foreach($info as $key => $value){
                if(!empty($value)){
                    $file_num[$value['mon']]=$value['number'];
                    $file_time[$value['mon']]=$value['mon'];
                    $file_money[$value['mon']]=$value['money'];
                    $file_profit[$value['mon']]=$value['profit'];
                    $file_goods[$value['mon']]=floatval($info_og[$key]['goods_number']);
                }
            }unset($value,$key);
        }        
        $new_day=array_diff($day_arr,$file_time);
        foreach($new_day as $v){
            $file_num[$v]=0;
            $file_money[$v]=0;
            $file_profit[$v]=0;
            $file_goods[$v]=0;
        }unset($v);
        ksort($file_num);
        ksort($file_money);
        ksort($file_profit);
        ksort($file_goods);
        //得到折线图
        $cz_count = array(
            "legend"=>array('data'=>array($name[0],$name[1],$name[2],$name[3])),
            "xaxis"=>array("type"=>"category","data"=>array_keys($file_num)),
            "series"=>array(
                0=>array("name"=>$name[0],"type"=>"line","stack"=>$name[0],'itemStyle'=>array(),"data"=>array_values($file_num)),
                1=>array("name"=>$name[1],"type"=>"line","stack"=>$name[1],"data"=>array_values($file_money)),
                2=>array("name"=>$name[2],"type"=>"line","stack"=>$name[2],"data"=>array_values($file_profit)),
                3=>array("name"=>$name[3],"type"=>"line","stack"=>$name[3],"data"=>array_values($file_goods)),
            ),
        );
        $this->assign('Area_count',$ec->show('Area_count',$cz_count,'line'));
    }
    /*支付方式统计*/
    public function pay_type($map_z,$day_arr,$ec){
        $map=array();
        if($map_z){
            $map['addtime']=array('between',array($map_z[0],$map_z[1]));
        }
        $map['type']=array('in','1,6,11,12,13,14');
        $map['pay_status'] = 3;
        $map['is_delete']=0;
        $pay_info = M('OrderInfo')->where($map)->getField("pay_code",true);
        $pay_style_num=array_count_values($pay_info);
        $pay_hanchao = $pay_style_num['']+$pay_style_num['Hanchao']+$pay_style_num['HANCHAO'];
        $pay_style_count = array(
            "legend"=>array('data'=>array('银联','微信','微信小程序','余额','现金','刷卡','台卡')),
            "series"=>array(
                array("name"=>"支付方式","type"=>"pie",
                    'radius'=>'65%',
                    "data"=>array(
                        array("value"=>$pay_style_num['CHINAUMSPOS']+$pay_style_num['CHINAUMS'],"name"=>"银联"),
                        array("value"=>$pay_style_num['weixinpay'],"name"=>"微信"),
                        array("value"=>$pay_style_num['weixinsmall'],"name"=>"微信小程序"),
                        array("value"=>$pay_hanchao,"name"=>"余额"),
                        array("value"=>$pay_style_num['HANCHAO_XIANJIN'],"name"=>"现金"),
                        array("value"=>$pay_style_num['HANCHAO_CARD'],"name"=>"刷卡"),
                        array("value"=>$pay_style_num['HANCHAO_DECCA'],"name"=>"台卡"),
                    ),
                ),
            ),
        );
        $this->assign('Area_count_pay',$ec->pie_show('Area_count_pay', $pay_style_count,'pie',''));
    }
    /*退款方式统计*/
    public function tui_type($map_z,$day_arr,$ec){
        $map=array();
        $map['type'] = array('in','20,21,31');
        $map['create_time'] = array('between',array($map_z[0],$map_z[1]));
        $map['is_delete']=0;
        $tui_type = M('PayLog')->where($map)->getField("pay_code",true);

        $tui_type_num=array_count_values($tui_type);
        $tui_hanchao = $tui_type_num['']+$tui_type_num['Hanchao'];
        $tui_type_count = array(
            'title'=>array(
                'text'=>'退款方式',
                'x'=>'center'
            ),
            "legend"=>array('data'=>array('银联','微信','微信小程序','余额','现金','刷卡','台卡')),
            "series"=>array(
                array("name"=>"退款方式",
                    "type"=>"pie",
                    'radius'=>'65%',
                    "data"=>array(
                        array("value"=>$tui_type_num['CHINAUMSPOS']+$tui_type_num['CHINAUMS'],"name"=>"银联"),
                        array("value"=>$tui_type_num['weixinpay'],"name"=>"微信"),
                        array("value"=>$tui_type_num['weixinsmall'],"name"=>"微信小程序支付"),
                        array("value"=>$tui_hanchao,"name"=>"余额"),
                        array("value"=>$tui_type_num['HANCHAO_XIANJIN'],"name"=>"现金"),
                        array("value"=>$tui_type_num['HANCHAO_CARD'],"name"=>"刷卡"),
                        array("value"=>$tui_type_num['HANCHAO_DECCA'],"name"=>"台卡"),
                    ),
                ),
            ),
        );
        $this->assign('tui_type',$ec->pie_show('tui_type', $tui_type_count,'pie',''));
    }
    public function order_type($map_z,$day_arr,$ec){
        $map=array();
        if($map_z){
            $map['addtime']=array('between',array($map_z[0],$map_z[1]));
        }
        $map['type'] = array('in','2,5,6,13,14');
        $map['pay_status'] = 3;
        $map['status'] = array('in','1,2,3,6,8');
        $map['is_delete']=0;
        $order_type = M('OrderInfo')->where($map)->getField('type',true);

        $order_type_num=array_count_values($order_type);
        $chongzhi = $order_type_num['2']+$order_type_num['5'];
        $order_style_count = array(
            'title'=>array(
                'text'=>'订单类型',
                'x'=>'center'
            ),
            "legend"=>array('data'=>array('充值订单','网上订单','收银订单')),
            "series"=>array(
                array("name"=>"订单类型","type"=>"pie",
                    'radius'=>'65%',
                    "data"=>array(
                        array("value"=>$chongzhi,"name"=>"充值订单"),
                        array("value"=>$order_type_num['14'],"name"=>"网上订单"),
                        array("value"=>$order_type_num['13'],"name"=>"收银订单"),
                    ),
                ),
            ),
        );
        $this->assign('order_type',$ec->pie_show('order_type', $order_style_count,'pie',''));
    }
    public function top_shop($map,$day_arr,$ec){
        $shop_info=$shop_money=$shop_num=$shop_profit= array();
        // $map['status'] = 3;
        $map['status'] = array('in','1,2,3,6,7,8,9');
        $map['pay_status'] = 3;
        $map['type'] = array('in','1,6,11,12,13,14');
        $map['is_delete']=0;
        $shop_info = M('OrderInfo')->where($map)->field("sum(profit) as profit,shop_id,count(id) as num ,sum(order_amount) as money")->group('shop_id')->order('profit desc')->limit(10)->select();
        if($shop_info){
            foreach($shop_info as $key => $value){
                $shop_money[idtoname($value['shop_id'],'Shops','name')]=$value['money'];
                $shop_num[idtoname($value['shop_id'],'Shops','name')]=$value['num'];
                $shop_profit[idtoname($value['shop_id'],'Shops','name')]=$value['profit'];
            }
        }

        $shop_sell=array('num'=>$shop_num,'money'=>$shop_money,'profit'=>$shop_profit);
        $names = array("订单数","订单金额","利润");
        $ma_count = array(
            "legend"=>array('data'=>array($names[0],$names[1],$names[2])),
            "xaxis"=>array("type"=>"category","data"=>array_keys($shop_sell['num'])),
            "series"=>array(
                0=>array("name"=>$names[0],"type"=>"line","stack"=>$names[0],'itemStyle'=>array(),"data"=>array_values($shop_sell['num'])),
                1=>array("name"=>$names[1],"type"=>"line","stack"=>$names[1],'itemStyle'=>array(),"data"=>array_values($shop_sell['money'])),
                2=>array("name"=>$names[2],"type"=>"line","stack"=>$names[2],'itemStyle'=>array(),"data"=>array_values($shop_sell['profit'])),
            ),
        );
        $this->assign('shop_sell',$ec->show('shop_sell',$ma_count,'line'));
    }
    /*余额消耗*/
    public function yue($map,$day_arr,$ec){
        $ye_map = $map;
        // $ye_map['status'] = 3;
        $ye_map['status'] = array('in','1,2,3,6,7,8,9');
        $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,count(id) as num,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'];
                    $file_num_ye[$value['mon']] = $value['num'];
                }
            }unset($value,$key);
        }

        $new_day_ye = array_diff($day_arr,$file_time_ye);
        foreach($new_day_ye as $v){
            $file_money_ye[$v]=0;
            $file_num_ye[$v]=0;
        }unset($v);
        ksort($file_money_ye);
        ksort($file_num_ye);
        //得到折线图
        $cz_count_ye = array(
            "legend"=>array('data'=>array($name_ye[0],$name_ye[1])),
            "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)),
                1=>array("name"=>$name_ye[1],"type"=>"line","stack"=>$name_ye[1],"data"=>array_values($file_num_ye)),
            ),
        );

        $this->assign('yue_count',$ec->show('yue_count',$cz_count_ye,'line'));
    }
    /*优惠方式*/
    public function discount_type($map_z,$day_arr,$ec){
        $map = array();
        if($map_z){
            $map['addtime']=array('between',array($map_z[0],$map_z[1]));
        }
        $map['type'] = array('in','1,6,13,14');
        $map['pay_status'] = 3;
        // $map['status'] = 3;
        $map['status'] = array('in','1,2,3,6,7,8,9');
        $map['is_delete']=0;
        $discount = M('OrderInfo')->where($map)->field("sum(first_money) as first_money,sum(used_score_money) as score_money,sum(bonus_money) as bonus_money,sum(promot_money) as act_money")->find();

        $discount_count = array(
            'title'=>array(
                'text'=>'优惠方式',
                'x'=>'center'
            ),
            "legend"=>array('data'=>array('积分抵现','优惠券','首单立减','优惠活动')),
            "series"=>array(
                array("name"=>"优惠方式","type"=>"pie",
                    'radius'=>'65%',
                    "data"=>array(
                        array("value"=>$discount['score_money'],"name"=>"积分抵现"),
                        array("value"=>$discount['bonus_money'],"name"=>"优惠券"),
                        array("value"=>$discount['first_money'],"name"=>"首单立减"),
                        array("value"=>$discount['act_money'],"name"=>"优惠活动"),
                    ),
                ),
            ),
        );
        $this->assign('discount_count',$ec->pie_show('discount_count',$discount_count,'pie',''));
    }
    public function chongzhi($map,$day_arr,$ec){
        $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,count(id) as num,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'];
                    $file_num_cz[$value['mon']] = $value['num'];
                }
            }unset($value,$key);
        }

        $new_day_cz = array_diff($day_arr,$file_time_cz);
        foreach($new_day_cz as $v){
            $file_money_cz[$v]=0;
            $file_num_cz[$v]=0;
        }unset($v);
        ksort($file_money_cz);
        ksort($file_num_cz);
        // 得到折线图
        $cz_count_cz = array(
            "legend"=>array('data'=>array($name_cz[0],$name_cz[1])),
            "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)),
                1=>array("name"=>$name_cz[1],"type"=>"line","stack"=>$name_cz[1],"data"=>array_values($file_num_cz)),
            ),
        );
        $this->assign('cz_count',$ec->show('cz_count',$cz_count_cz,'line'));
    }
    public function score_get($map_z,$day_arr,$ec){
        // 积分获得统计
        $map = array();
        if($map_z){
            $map['change_time']=array('between',array($map_z[0],$map_z[1]));
        }
        $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');
        $score_style_count = array(
            'title'=>array(
                'text'=>'积分获得',
                'x'=>'center'
            ),
            "legend"=>array('data'=>array('消费赠积分','签到赠积分')),
            "series"=>array(
                array("name"=>"积分获得","type"=>"pie",
                    'radius'=>'65%',
                    "data"=>array(
                        array("value"=>$xf_sum,"name"=>"消费赠积分"),
                        // array("value"=>$pay_style_num,"name"=>"充值赠积分"),
                        array("value"=>$qd_sum,"name"=>"签到赠积分"),
                    ),
                ),
            ),
        );
        $this->assign('score_count',$ec->pie_show('score_count',$score_style_count,'pie'));
    }
    public function coupon_tong($map,$day_arr,$date_from,$date_to,$ec){
        $map=array();
        if($date_from && $date_to){
            $map['add_time']=array('between',array($date_from,$date_to));
        }
        $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['status']=1;
        $info['hexiao'] =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['send_id'] = array('gt','0');
        if($date_from && $date_to){
            $map['add_time']=array('between',array($date_from,$date_to));
        }
        $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']);
                    }
                }
            }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']);
        //得到折线图
        $yhq_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'])),
            ),
        );
        $this->assign('yhq_count',$ec->show('yhq_count',$yhq_count,'bar','','','个'));
    }
    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->member_info($riqi,$date_from,$date_to);
        //用户信息结束
        //消费会员趋势
        $this->customer_info($riqi,$date_from,$date_to);
        //消费会员结束
        //订单统计
        $this->order($riqi,$date_from,$date_to);
        //订单统计
        //中间3个表开始
        $this->type_type($riqi,$date_from,$date_to);
        //中间3个表结束
        //充值趋势统计开始
        $this->chongzhi_info($riqi,$date_from,$date_to);
        // 充值趋势统计结束

        //余额消耗趋势开始
        $this->money_use($riqi,$date_from,$date_to);
        //余额消耗趋势结束
        //优惠券开始
        $this->coupon($riqi,$date_from,$date_to);
        //优惠券结束
        //礼品卡销售统计
        $this->gift_biao($riqi,$date_from,$date_to);
        //礼品卡销售统计
        //礼品卡销售分析
        $this->gift_abiao($riqi,$date_from,$date_to);
        //礼品卡销售分析
        $this->assign('type',$type);
        $this->assign('date_from',$date_from);
        $this->assign('date_to',$date_to);
        $this->display();
    }
    /**
    *用户订单量分析
    */
    private function member_info($riqi=7,$date_from=0,$date_to=0,$is_return=0){
        if($riqi == -1){
            $now_time = strtotime($date_to);
            $count=$map=array();
            $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;
                }

                $map['status'] = array('in','1,2,3,6,7,8,9');;
                $map['pay_status'] = 3;
                $map['type'] = array('in','1,6,11,12,13,14');
                $map['is_delete']=0;
                $map['uid']=array('gt',0);
                $map['addtime']=array('between',array($today,$tomorrow));
                $count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $count[$i]['m_count']=M('OrderInfo')->where($map)->count();
                $map['uid']=0;
                $count[$i]['y_count']=M('OrderInfo')->where($map)->count();
                $count[$i]['sum'] = $count[$i]['m_count'] + $count[$i]['y_count'];
            }
        }else{
            $now_time = NOW_TIME;
            $count=$map=array();
            $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;
                }
                $map['status'] = array('in','1,2,3,6,7,8,9');
                $map['pay_status'] = 3;
                $map['type'] = array('in','1,6,11,12,13,14');
                $map['is_delete']=0;
                $map['uid']=array('gt',0);
                $map['addtime']=array('between',array($today,$tomorrow));
                $count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $count[$i]['m_count']=M('OrderInfo')->where($map)->count();
                $map['uid']=0;
                $count[$i]['y_count']=M('OrderInfo')->where($map)->count();
                $count[$i]['sum'] = $count[$i]['m_count'] + $count[$i]['y_count'];
            }
        }
        $this->assign('count',$count);
        if($is_return == 1){
            return $count;
        }

    }
    /**
     *消费会员趋势
     */
    private function customer_info($riqi=7,$date_from=0,$date_to=0,$is_return=0){
        $custome_count=$map=array();
        $map['status'] = array('in','1,2,3,6,8');
        $map['pay_status'] = 3;
        $map['type'] = array('in','1,6,11,12,13,14');
        $map['is_delete']=0;
        $map['uid']=array('gt',0);
        if($riqi == -1){
            $now_time = strtotime($date_to);
            $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;
                }
                $custome_count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['addtime']=array('between',array($today,$tomorrow));
                $info=M('OrderInfo')->where($map)->distinct('uid')->field("count(uid) as number,sum(order_amount) as money")->find();
                $custome_count[$i]['cmember'] = $info['number'];
                $custome_count[$i]['cmoney'] = number_format($info['money'],2);

            }
        }else{
            $now_time = NOW_TIME;
            $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;
                }

                $custome_count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['addtime']=array('between',array($today,$tomorrow));
                $info=M('OrderInfo')->where($map)->distinct('uid')->field("count(uid) as number,sum(order_amount) as money")->find();
                $custome_count[$i]['cmember'] = $info['number'];
                $custome_count[$i]['cmoney'] = number_format($info['money'],2);
            }
        }
        $this->assign('custome_count',$custome_count);
        if($is_return == 1){
            return $custome_count;
        }

    }
    /**
     *订单统计
     */
    private function order($riqi=7,$date_from=0,$date_to=0,$is_return=0){
        $order_count=$map=array();
        $map['status'] = array('in','1,2,3,6,8');
        $map['pay_status'] = 3;
        $map['type'] = array('in','1,6,11,12,13,14');
        $map['is_delete']=0;
        if($riqi == -1){
            $now_time = strtotime($date_to);
            $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;
                }
                $order_count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['addtime']=array('between',array($today,$tomorrow));
                $info=M('OrderInfo')->where($map)->field("count(id) as number,sum(order_amount) as money,sum(profit) as profit")->find();

                $order_count[$i]['order_n'] = $info['number'];
                $order_count[$i]['order_money'] = number_format($info['money'],2);
                $order_count[$i]['order_profit'] = number_format($info['profit'],2);

            }
        }else{
            $now_time = NOW_TIME;
            $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;
                }
                $order_count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['addtime']=array('between',array($today,$tomorrow));
                $info=M('OrderInfo')->where($map)->field("count(id) as number,sum(order_amount) as money,sum(profit) as profit")->find();
				

                $order_count[$i]['order_n'] = $info['number'];
                $order_count[$i]['order_money'] = number_format($info['money'],2);
                $order_count[$i]['order_profit'] = number_format($info['profit'],2);
            }
        }
        $this->assign('order_count',$order_count);
        if($is_return == 1){
            return $order_count;
        }

    }
    private function type_type($riqi=7,$date_from=0,$date_to=0,$is_return=0){
        $return=$map=$list= array();
        $start_time = strtotime(date('Y-m-d',NOW_TIME)); 
        if($riqi == -1){
            if($date_from)$map['addtime'][] = array('egt',strtotime($date_from));
            if($date_to)$map['addtime'][] = array('lt',strtotime($date_to));
        }elseif($riqi == 1){
            $date_from=$start_time;
            $date_to =$start_time+86399;
            $map['addtime'][] = array('egt',$date_from);
            $map['addtime'][] = array('lt',$date_to);
        }elseif($riqi == 2){
            $date_from=$start_time-86400;
            $date_to =$start_time-1;
            $map['addtime'][] = array('egt',$date_from);
            $map['addtime'][] = array('lt',$date_to);
        }elseif($riqi == 7){
            $date_from=$start_time-6*86400;
            $date_to =$start_time+86399;
            $map['addtime'][] = array('egt',$date_from);
            $map['addtime'][] = array('lt',$date_to);
        }elseif($riqi == 30){
            $date_from=$start_time-29*86400;
            $date_to =$start_time+86399;
            $map['addtime'][] = array('egt',$date_from);
            $map['addtime'][] = array('lt',$date_to);
        }

        //top10开始
        $shop_info=array();
        $map['status'] = array('in','1,2,3,6,8');
        $map['pay_status'] = 3;
        $map['type'] = array('in','1,6,11,12,13,14');
        $map['is_delete']=0;
        $shop_info = M('OrderInfo')->where($map)->field("sum(profit) as profit,shop_id,count(id) as num ,sum(order_amount) as money")->group('shop_id')->order('profit desc')->limit(10)->select();
        //top10结束


        // 支付方式统计开始
        $pay_info = M('OrderInfo')->where($map)->getField("pay_code",true);
        $pay_style_num=array_count_values($pay_info);
        $pay_hanchao = $pay_style_num['']+$pay_style_num['Hanchao']+$pay_style_num['HANCHAO'];
        $payapi=$pay_style_num['alipay']+$pay_style_num['Alipayapi']+$pay_style_num['Alibarpay'];
        $scan=$pay_style_num['MMSCAN']+$pay_style_num['scanalipay'];
        //支付方式统计结束


        //订单类型统计开始
        unset($map['type']);
        $map['type'] = array('in','2,5,6,13,14');
        $order_type = M('OrderInfo')->where($map)->getField('type',true);

        $order_type_num=array_count_values($order_type);
        $chongzhi = $order_type_num['2']+$order_type_num['5'];
//        订单类型统计结束

//优惠方式统计开始
//        unset($map['type']);
//        $map['type'] = array('in','1,6,13,14');
//        $discount = M('OrderInfo')->where($map)->field("sum(first_money) as first_money,sum(used_score_money) as score_money,sum(bonus_money) as bonus_money,sum(discount) as act_money")->find();
//        $this->assign('discount',$discount);
//优惠方式统计结束

        if($is_return==1){

            $return['shop_info']=$shop_info;

            $return['pay_hanchao']=number_format($pay_hanchao,2);
            $return['payapi']=number_format($payapi,2);
            $return['scan']=number_format($scan,2);
            $return['weixinpay']=number_format($pay_style_num['weixinpay'],2);
            $return['weixinsmall']=number_format($pay_style_num['weixinsmall'],2);
            $return['HANCHAO_XIANJIN']=number_format($pay_style_num['HANCHAO_XIANJIN'],2);
            $return['HANCHAO_CARD']=number_format($pay_style_num['HANCHAO_CARD'],2);
            $return['CHINAUMSPOS']=number_format($pay_style_num['CHINAUMSPOS'],2);

            $return['chongzhi']=$order_type_num;
            return $return;
        }else{

            $this->assign('shop_info',$shop_info);
            $this->assign('pay_hanchao',$pay_hanchao);
            $this->assign('payapi',$payapi);
            $this->assign('scan',$scan);
            $this->assign('order_type',$order_type_num);
            $this->assign('chongzhi',$chongzhi);
            $this->assign('payinfo',$pay_style_num);
        }

    }

    /**
     *充值趋势统计
     */
    private function chongzhi_info($riqi=7,$date_from=0,$date_to=0,$is_return=0){
        $cz_count=$map=$info=array();
        $map['status'] = 3;
        $map['pay_status'] = 3;
        $map['type'] = 2;
        if($riqi == -1){
            $now_time = strtotime($date_to);
            $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;
                }
                $cz_count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['addtime']=array('between',array($today,$tomorrow));
                $info=M('OrderInfo')->where($map)->field("sum(order_amount) as money,count(id) as num")->find();
                $cz_count[$i]['cz_num'] = $info['num'];
                $cz_count[$i]['cz_money'] = number_format($info['money'],2);
            }
        }else{
            $now_time = NOW_TIME;
            $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;
                }
                $cz_count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['addtime']=array('between',array($today,$tomorrow));
                $info=M('OrderInfo')->where($map)->field("sum(order_amount) as money,count(id) as num")->find();
                $cz_count[$i]['cz_num'] = $info['num'];
                $cz_count[$i]['cz_money'] = number_format($info['money'],2);
            }
        }
        $this->assign('cz_count',$cz_count);
        if($is_return == 1){
            return $cz_count;
        }

    }
    /**
     *余额消耗趋势统计
     */
    private function money_use($riqi=7,$date_from=0,$date_to=0,$is_return=0){
        $ye_count=$map=$info=array();
        $map['status'] = array('in','1,2,3,6,7,8,9');
        $map['pay_status'] = 3;
        $map['used_money'] = array('gt',0);
        if($riqi == -1){
            $now_time = strtotime($date_to);
            $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;
                }
                $ye_count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['addtime']=array('between',array($today,$tomorrow));
                $info=M('OrderInfo')->where($map)->field("sum(used_money) as money,count(id) as num")->find();
                $ye_count[$i]['ye_num'] = $info['num'];
                $ye_count[$i]['ye_money'] = number_format($info['money'],2);
            }
        }else{
            $now_time = NOW_TIME;
            $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;
                }
                $ye_count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['addtime']=array('between',array($today,$tomorrow));
                $info=M('OrderInfo')->where($map)->field("sum(used_money) as money,count(id) as num")->find();
                $ye_count[$i]['ye_num'] = $info['num'];
                $ye_count[$i]['ye_money'] = number_format($info['money'],2);
            }
        }
        $this->assign('ye_count',$ye_count);
        if($is_return == 1){
            return $ye_count;
        }

    }
    /**
     *优惠券统计
     */
    private function coupon($riqi=7,$date_from=0,$date_to=0,$is_return=0){
        $coupon_count=$map=$info=array();
        if($riqi == -1){
            $now_time = strtotime($date_to);
            $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;
                }
                $coupon_count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['add_time']=array('between',array($today,$tomorrow));
                $map['coupons_type'] = 1;
                $coupon_count[$i]['lingqu'] = M('CouponsGet')->where($map)->count();

                //核销数
                $map=array();
                $map['create_time'] = array('between',array($today,$tomorrow));
                $coupon_count[$i]['hexiao'] = M('CouponsDonation')->where($map)->count();
                //转赠数
                $map=array();
                $map['add_time']=array('between',array($today,$tomorrow));
                $map['send_id'] = array('gt','0');
                $coupon_count[$i]['zhuanzeng'] = M('CouponsDonation')->where($map)->count();
            }
        }else{
            $now_time = NOW_TIME;
            $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;
                }
                $coupon_count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $map['add_time']=array('between',array($today,$tomorrow));
                $map['coupons_type'] = 1;
                $coupon_count[$i]['lingqu'] = M('CouponsGet')->where($map)->count();

                //核销数
                $map=array();
                $map['create_time'] = array('between',array($today,$tomorrow));
                $coupon_count[$i]['hexiao'] = M('CouponsDonation')->where($map)->count();
                //转赠数
                $map=array();
                $map['add_time']=array('between',array($today,$tomorrow));
                $map['send_id'] = array('gt','0');
                $coupon_count[$i]['zhuanzeng'] = M('CouponsDonation')->where($map)->count();
            }
        }
        $this->assign('coupon_count',$coupon_count);
        if($is_return == 1){
            return $coupon_count;
        }

    }
    // 礼品卡销售统计
    public function gift_tong($gc_map,$day_arr,$ec){
        $gc_map['order_id'] = array('gt',0);
        $giftcode_info=M('GiftCard')->where($gc_map)->field("count(id) as gc_number,sum(shop_price) as gc_money,DATE_FORMAT(FROM_UNIXTIME(sell_time),'%Y-%m-%d') as mon")->group("DATE_FORMAT(FROM_UNIXTIME(sell_time),'%Y-%m-%d')")->select();
        
        //每个折线的图标代表什么
        $name = array("礼品卡销售数量","礼品卡销售金额");
        if($giftcode_info){
            foreach($giftcode_info as $key => $value){
                if(!empty($value)){
                    $file_gc_num[$value['mon']]=$value['gc_number'];
                    $file_gc_time[$value['mon']]=$value['mon'];
                    $file_gc_money[$value['mon']]=$value['gc_money'];
                }
            }unset($value,$key);
        }
        $new_gc_day=array_diff($day_arr,$file_gc_time);
        foreach($new_gc_day as $v){
            $file_gc_num[$v]=0;
            $file_gc_money[$v]=0;
        }unset($v);
        ksort($file_gc_num);
        ksort($file_gc_money);
        //得到折线图
        $Gift_count = array(
            "legend"=>array('data'=>array($name[0],$name[1])),
            "xaxis"=>array("type"=>"category","data"=>array_keys($file_gc_num)),
            "series"=>array(
                0=>array("name"=>$name[0],"type"=>"line","stack"=>$name[0],"data"=>array_values($file_gc_num)),
                1=>array("name"=>$name[1],"type"=>"line","stack"=>$name[1],"data"=>array_values($file_gc_money)),
            ),
        );
        $this->assign('Gift_count',$ec->show('Gift_count',$Gift_count,'line'));
    }
    // 礼品卡销售分析
    public function gift_analysis($map_z,$day_arr,$ec){
        $gc_map=array();
        $gc_map['sell_time']=array('between',array($map_z[0],$map_z[1]));
        $gc_map['order_id'] = array('gt',0);
        $gift=M('GiftCard')->where($gc_map)->field("count(id) as gc_number,shop_id")->group("shop_id")->select();
        if($gift){
            foreach ($gift as $k => $v) {
                $sname=idtoname($v['shop_id'],'Shops','name');
                $shop_name[]=$sname;
                $data[]=array("value"=>$v['gc_number'],"name"=>$sname);
            }
        }
        $gift_analysis = array(
            'title'=>array(
                'text'=>'礼品卡销售分析',
                'x'=>'center'
            ),
            "legend"=>array('data'=>$shop_name),
            "series"=>array(
                array("name"=>"礼品卡销售分析","type"=>"pie",
                    'radius'=>'65%',
                    "data"=>$data,
                ),
            ),
        );
        $this->assign('gift_analysis',$ec->pie_show('gift_analysis', $gift_analysis,'pie',''));
    }
    /**
    *礼品卡统计表
    */
    private function gift_biao($riqi=7,$date_from=0,$date_to=0,$is_return=0){
        $gift_count=$gc_map=array();
        if($riqi == -1){
            $now_time = strtotime($date_to);
            $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;
                }
                $gift_count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $gc_map['order_id'] = array('gt','0');
                $gc_map['sell_time'] = array('between',array($today,$tomorrow));
                $giftcode_info=M('GiftCard')->where($gc_map)->field("count(id) as gc_number,sum(shop_price) as gc_money")->find();

                $gift_count[$i]['gc_number'] = number_format($giftcode_info['gc_number'],2);
                $gift_count[$i]['gc_money'] = number_format($giftcode_info['gc_money'],2);

            }
        }else{
            $now_time = NOW_TIME;
            $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;
                }
                $gift_count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
                $gc_map['order_id'] = array('gt','0');
                $gc_map['sell_time'] = array('between',array($today,$tomorrow));
                $giftcode_info=M('GiftCard')->where($gc_map)->field("count(id) as gc_number,sum(shop_price) as gc_money")->find();

                $gift_count[$i]['gc_number'] = intval($giftcode_info['gc_number']);
                $gift_count[$i]['gc_money'] = number_format($giftcode_info['gc_money'],2);
            }
        }
        $this->assign('gift_count',$gift_count);
        if($is_return == 1){
            return $gift_count;
        }
    }
    /**
    *礼品卡销售分析表
    */
    private function gift_abiao($riqi=7,$date_from=0,$date_to=0,$is_return=0){
        $return=$gc_map=$giftlist=$gift_list=array();
        $start_time = strtotime(date('Y-m-d',NOW_TIME)); 
        if($riqi == -1){
            if($date_from)$gc_map['sell_time'][] = array('egt',strtotime($date_from));
            if($date_to)$gc_map['sell_time'][] = array('lt',strtotime($date_to));
        }elseif($riqi == 1){
            $date_from=$start_time;
            $date_to =$start_time+86399;
            $map['sell_time'][] = array('egt',$date_from);
            $gc_map['sell_time'][] = array('lt',$date_to);
        }elseif($riqi == 2){
            $date_from=$start_time-86400;
            $date_to =$start_time-1;
            $gc_map['sell_time'][] = array('egt',$date_from);
            $gc_map['sell_time'][] = array('lt',$date_to);
        }elseif($riqi == 7){
            $date_from=$start_time-6*86400;
            $date_to =$start_time+86399;
            $gc_map['sell_time'][] = array('egt',$date_from);
            $gc_map['sell_time'][] = array('lt',$date_to);
        }elseif($riqi == 30){
            $date_from=$start_time-29*86400;
            $date_to =$start_time+86399;
            $gc_map['sell_time'][] = array('egt',$date_from);
            $gc_map['sell_time'][] = array('lt',$date_to);
        }
        $gc_map['order_id'] = array('gt','0');
        $giftlist=M('GiftCard')->where($gc_map)->field("count(id) as gc_number,shop_id")->group('shop_id')->select();
        if($giftlist){
            $i=0;
            foreach ($giftlist as $gk => $gv) {
                $gift_list[$i]['name']=idtoname($gv['shop_id'],'Shops');
                $gift_list[$i]['gc_number']=$gv['gc_number'];
                $i++;
            }
        }
        $this->assign('giftlist',$gift_list);
        if($is_return == 1){
            return $gift_list;
        }

    }
    public function tong_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->member_info($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->customer_info($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_count = $this->order($riqi,$date_from,$date_to,1);
            $three_title=array('日期','订单数','订单金额','利润');
            $title_name = array('订单统计');
            export_xls($title_name,$null,'utf8');
            export_xls($three_title,$three_count,'utf8');
            export_xls(array(),$null,'utf8');
            //表三结束
            //表四开始（收入top10仓库）


            $res=$this->type_type($riqi,$date_from,$date_to,1);
            $four_title=array("仓库名称","订单数","订单金额","利润");
            $i=0;
            $list4=array();
            foreach($res['shop_info'] as $k=>$v){
                $i++;
                $shop_name=idtoname($v['shop_id'],'Shops','name','id',false);
                $list4[$i]['shop_name']=$shop_name;
                if($v['num']){
                    $list4[$i]['num']=$v['num'];
                }else{
                    $list4[$i]['num']=0;
                }

                if($v['money']){
                    $list4[$i]['money']=$v['money'];
                }else{
                    $list4[$i]['money']=0.00;
                }
                if($v['profit']){
                    $list4[$i]['profit']=$v['profit'];
                }else{
                    $list4[$i]['profit']=0.00;
                }
            }
            $title_name = array('收入TOP10仓库');
            export_xls($title_name,$null,'utf8');
            export_xls($four_title,$list4,'utf8');
            export_xls(array(),$null,'utf8');
            //表四结束
            //表五开始（支付方式）
            $five_title=array("双乾","微信","微信小程序","余额","支付宝","现金","刷卡","网银");
            $list5[0]=array($res['scan'],$res['weixinpay'],$res['weixinsmall'],$res['pay_hanchao'],$res['payapi'],$res['HANCHAO_XIANJIN'],$res['HANCHAO_CARD'],$res['CHINAUMSPOS']);
            $title_name = array('订单来源统计');
            export_xls($title_name,$null,'utf8');
            export_xls($five_title,$list5,'utf8');
            export_xls(array(),$null,'utf8');

            //表6开始
            $list6[0]=array(intval($res['chongzhi'][2]+$res['chongzhi'][5]),intval($res['chongzhi'][14]),intval($res['chongzhi'][13]));
            $title_name = array('订单类型统计');
            $title6=array('充值订单','网上订单','收银订单');
            export_xls($title_name,$null,'utf8');
            export_xls($title6,$list6,'utf8');
            export_xls(array(),$null,'utf8');
            //表6结束

            // 表7开始
            $list7=$this->chongzhi_info($riqi,$date_from,$date_to,1);
            $title_name = array('充值趋势统计');
            $title7=array('充值订单','外卖订单','收银订单');
            export_xls($title_name,$null,'utf8');
            export_xls($title7,$list7,'utf8');
            export_xls(array(),$null,'utf8');
            // 表7结束

            // 表8开始
            $list8=$this->money_use($riqi,$date_from,$date_to,1);
            $title_name = array('余额消耗趋势统计');
            $title8=array('日期','金额','次数');
            export_xls($title_name,$null,'utf8');
            export_xls($title8,$list8,'utf8');
            export_xls(array(),$null,'utf8');
            //表8结束
            // 表9开始
            $list9=$this->coupon($riqi,$date_from,$date_to,1);
            $title_name = array('优惠券统计');
            $title9=array('日期','领券数','核销数','转赠数');
            export_xls($title_name,$null,'utf8');
            export_xls($title9,$list9,'utf8');
            export_xls(array(),$null,'utf8');
            //表9结束
            //礼品卡统计

            $ten_count = $this->gift_biao($riqi,$date_from,$date_to,1);
            $ten_title=array('日期','礼品卡销售数','礼品卡销售金额');
            $title_name = array('礼品卡销售统计');
            export_xls($title_name,$null,'utf8');
            export_xls($ten_title,$ten_count,'utf8');
            export_xls(array(),$null,'utf8');
            //礼品卡统计结束
            //礼品卡销售分析
            $eleven_count=$this->gift_abiao($riqi,$date_from,$date_to,1);
            $eleven_title=array("仓库名称","礼品卡销售数");
            
            $title_name = array('礼品卡销售分析');
            export_xls($title_name,$null,'utf8');
            export_xls($eleven_title,$eleven_count,'utf8');
            export_xls(array(),$null,'utf8');
            exit;
        }
    }
}
?>