<?php
/**
 * 店铺中心
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <470035208@qq.com>
 */

namespace Shop\Controller;
use Think\Controller;
class IndexController extends BaseController {
	public function _initialize(){
        parent::_initialize();
		$shops_info=session('shops_info');
        if(empty($shops_info['id'])) $this->error('身份过期，请重新登录',U('Shop/Register/shop_login'));
		$this->mendian();
		$this->inputtype = array('text' => L('TEXT'), 'checkbox' => L('REDIO'));
        $this->assign('inputtype', $this->inputtype);
    }
    public function index(){
        $shops_info=session('shops_info');
        $source_id = M('Shops')->where('id='.$shops_info["shop_id"])->field('origin_id,logistics')->find();
        $this->assign('source_id',$source_id);
    	$shops_info=session('shops_info');
    	//系统公告
    	$this->other_info(MID);
    	//店铺及商品提示
        $this->shop_goods(MID);
    	//交易提示
    	$this->order(MID);
		//昨日销售量统计
		$this->sell_num(MID);
        //缺货提醒
        $this->qh_num(MID);
		//店铺评分
		$shop_score=D('Common/Shops')->get_shop(MID);
		//销售走势
        $this->sales_trend($this->shops_info['shop_id']);
        // 
        $this->num_count();
        $this->statistics();
		$this->assign('shop_score',$shop_score['data']);
        $this->assign('title_ass','账单');
		$this->display();
    }
    private function other_info($shop_id){
		//门店限时促销
        $shop_ids = ','.$shop_id.',';
		$xscx_map['is_limit_time']=1;
        $xscx_map['end_time'] = array('gt',NOW_TIME);
		$xscx_map['status'] = 1;
		$xscx_map['_string'] = "(shop_id like '%".$shop_ids."%' and is_part=0) OR shop_id='0' OR is_part=1";
        $xscx = M('PromotAct')->where($xscx_map)->count('id');
        $this->assign('xscx',$xscx);
        //门店代金券
        $mddjq_map['use_end_date'] = array('gt',NOW_TIME);
		$mddjq_map['bonu_type'] = 2;
		$mddjq_map['_string'] = "(shop_id like '%".$shop_ids."%' and is_part=0) OR shop_id='0' OR is_part=1";
        $mddjq = M('Bonu')->where($mddjq_map)->count('id');
        $this->assign('mddjq',$mddjq);
        //满减活动
        $map['id'] = 2;
		$map['status'] = 1;
		$map['_string'] = "(shop_id like '%".$shop_ids."%' and is_part=0) OR shop_id='0' OR is_part=1";
        $mdmj = M('TakeoutPromote')->where($mdmj_map)->count('id');
        $this->assign('mdmj',$mdmj);
        //首单立减
        $map['id'] = 1;
		$map['status'] = 1;
		$map['_string'] = "(shop_id like '%".$shop_ids."%' and is_part=0) OR shop_id='0' OR is_part=1";
        $sdlj = M('TakeoutPromote')->where($sdlj_map)->count('id');
        $this->assign('sdlj',$sdlj);
    }
    /**
     * 缺货商品数量
     */
    private function qh_num($shop_id){
        if($shop_id){
            // $goods_list = M('ShopsGoodsStock')->where('shop_id = '.$shop_id)->field(true)->select();
            // foreach ($goods_list as $key => $value) {
            //     if($value['goods_number'] <= $value['qh_count'] && $value['qh_count'] != 0){
            //         $list[$key] = $value;
            //     }
            // }
            $map['a.shop_id']=$shop_id;
            $map['_string']='a.goods_number > g.limit_count or a.goods_number < g.lower_count';
            $map['g.limit_count']=array('neq',0);
            $map['g.lower_count']=array('neq',0);
            $map['g.is_delete']=0;
            $join='LEFT JOIN __GOODS__ g ON g.id=a.goods_id';
            $filed="count() as num";
            $goods_list = M('ShopsGoodsStock')->alias('a')->where($map)->join($join)->field($field)->count();
            
            $this->assign('qh_count',$goods_list);
        }
    }
    //销售量统计
    private function sell_num($shop_id){
        $map = $s_map = $m_map = array();
        $day=strtotime(date('Y-m-d',strtotime("-1 day")));
        $y_end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
    	$map['shop_id']=$shop_id;
        // $map['type'] = array('in','11,12,13,14');
         $map['type'] = array('in','1,6,11,13,14');
        $map['pay_status'] = 3;
        $map['status'] = array('in','1,2,3,6,8');

        $s_map = $map;
    	// $s_map['pay_time']=array('between',array($day,$y_end));
        $s_map['addtime']=array('between',array($day,$y_end));
        // $sell_money=M('OrderInfo')->where($s_map)->sum('goods_allmoney');
    	// $sell_num=M('OrderInfo')->where($s_map)->field('id')->count();
        $s_file="count(id) as num,sum(order_money) as money";
        $s_arr=M('OrderInfo')->where($s_map)->field($s_file)->find();
        $sell_money=$s_arr['money'];
        $sell_num=$s_arr['num'];

        $month_start=mktime(0,0,0,date('m'),1,date('Y'));
        $tmonth=NOW_TIME;
        $m_map = $map;
        // $m_map['pay_time']=array('between',array($month_start,$tmonth));
        $m_map['add_time']=array('between',array($month_start,$tmonth));
    	// $msell_money=M('OrderInfo')->where($m_map)->sum('goods_allmoney');
    	// $msell_num=M('OrderInfo')->where($m_map)->field('id')->count();
        $m_arr=M('OrderInfo')->where($m_map)->field($s_file)->find();
        $msell_money=$m_arr['money'];
        $msell_num=$m_arr['num'];
    	$this->assign('sell_money',$sell_money);
    	$this->assign('sell_num',$sell_num);
    	$this->assign('msell_money',$msell_money);
    	$this->assign('msell_num',$msell_num);
    }
    //获取交易提示
    private function order($shop_id){
    	//待付款
    	$t_map['shop_id']=$shop_id;
        $this->shop_all_order($t_map);

		$no_pay=D('Common/Shops')->shop_all_order($t_map,2,1);
		//待发货
		// $no_ship=D('Common/Shops')->shop_all_order($t_map,3,1);
        //待完成
        // $yes_ship=D('Common/Shops')->shop_all_order($t_map,4,1);
		//退款中
		$t_map['pay_status']=3;
		$t_map['status']=6;
		// $t_map['shipping_status']=1;
        // $map['type'] = 14;
		$no_back=M('OrderInfo')->where($t_map)->field('id')->count();
		//已退款
		// $t_map['status']=7;
		// $all_back=M('OrderInfo')->where($t_map)->field('id')->count();
        //待核销
        $t_map['shipping_status']=3;
        $t_map['status']=array('lt',3);
        $t_map['type'] = 11;
        $no_hx = M('OrderInfo')->where($t_map)->field('id')->count();

        //今日新增
        $t_map['pay_status']=3;
        $t_map['addtime'] = array('between',$todayTime= mktime(0,0,0,date("Y"),date("m"),date("d")),NOW_TIME);
        // $new_num=M('OrderInfo')->where($t_map)->field('id')->count();
        // $mew_money = M('OrderInfo')->where($t_map)->sum('goods_allmoney');

        $this->assign('no_hx',$no_hx);
		$this->assign('no_pay',$no_pay);
		// $this->assign('no_ship',$no_ship);
        // $this->assign('yes_ship',$yes_ship);
		$this->assign('no_back',$no_back);
		// $this->assign('all_back',$all_back);
		$this->assign('no_refunds',0);
		$this->assign('refunds',0);
        // $this->assign('new_num',$new_num);
        // $this->assign('mew_money',$mew_money);
    }
        // 获取交易提醒部分数据
    private function shop_all_order($t_map=''){
        $shop_id=$t_map['shop_id']?$t_map['shop_id']:MID;
        $map['is_delete']=0;$map['shop_id']=$shop_id;$map['type']=array('IN','2,6,11,12,14,3');
        $map['pay_status']=3;$map['status']=2;$map['shipping_status']=array('in','3,5');// 待完成 代发货
        $field="shipping_status,count(shipping_status) as num ";
        $arr=M('OrderInfo')->where($map)->group('shipping_status')->getField($field,true);
        // echo M('OrderInfo')->getLastSql();
        $no_ship['num']=$arr[5]?$arr[5]:0;
         $yes_ship['num']=$arr[3]?$arr[3]:0;
            
         // 退货中
         $r_map['shop_id']=$shop_id;
         $r_map['shop_status']=0;
         $all_back=M('ReturnGoods')->where($r_map)->getField('count(id) as num');

         // 今天订单金额 个数
         $t_map=[];
         $t_map['shop_id']=$shop_id;
         $t_map['addtime'] = array('between',[mktime(0,0,0,date("m"),date("d"),date("Y")),NOW_TIME]);
         $field='count(id) as num , sum(goods_allmoney) as money';
         $t_arr=M('OrderInfo')->where($t_map)->field($field)->find();
         $this->assign('new_num',$t_arr['num']);
         $this->assign('new_money',$t_arr['money']);
         $this->assign('all_back',$all_back);
         $this->assign('no_ship',$no_ship);
         $this->assign('yes_ship',$yes_ship);

    }

    //商品及店铺提示
    private function shop_goods($shop_id){
    	//添加商品数
		$map = array();
    	$map['shop_id']=$shop_id;
        $all_count=M('Goods')->where($map)->field('id')->count();

        //待审核商品
        $map['publish_wait']=0;
        $is_nopush=M('Goods')->where($map)->field('id')->count();
        // 审核拒绝
        $map['publish_wait']=2;
        $is_forbade=M('Goods')->where($map)->field('id')->count();

    	//出售商品
        $sell_count=M('ShopsGoods')->where("status =1 and is_delete=0 and shop_id= " .MID)->group('goods_id')->getField('goods_id',true);
        $sell_count=count($sell_count);
  
        unset($map);
    	
        //服务个数
		$map = array();
    	$map['goods_type']=4;
    	$map['status']=1;
        $map['is_delete']=0;
		$shop_ids = ','.$shop_id.',';
        // $map['_string'] = "shop_ids like '%".$shop_ids."%' OR shop_ids='0'";
        $map['_string'] = "shop_ids = ".MID." OR shop_ids='0'";
    	$service_count=M('Goods')->where($map)->field('id')->count();
    	$this->assign('is_nopush',$is_nopush);
        $this->assign('is_forbade',$is_forbade);
        $this->assign('all_count',$all_count);
    	$this->assign('sell_count',$sell_count);
    	$this->assign('service_count',$service_count);
    }
    /**
     * 店铺概况
     */
    public function statistics(){
        // 五个图标
        $today=strtotime(date('Y-m-d',NOW_TIME));
        $tomorrow=$today+86400;
        //新增销售额\今日新增订单
        $today_order_map['type'] = array('IN','1,3,6,11,12,13,14');
        $today_order_map['status']=array('in','1,2,3,6,8');//1207 +
        $today_order_map['pay_time'] = array('between',array($today,$tomorrow));
        $today_order_map['shop_id']=MID;
        $today_order=M('OrderInfo')->where($today_order_map)->field('count(id) as today_order_count,sum(order_money) as today_order_money')->find();
        $this->assign($today_order);
        //待发货订单
        // $order_fh_map['type'] = array('IN','6,13,14,3');
        // $order_fh_map['goods_allmoney'] = array('gt',0);
        // $order_fh_map['send_type']=array('neq',1);
        // $order_fh_map['shipping_status'] = array('in','1,2,5');
        // $order_fh_map['status'] = 2;
        // $order_fh_map['pay_status'] = 3;
        // $order_fh_map['shop_id'] = MID;
        // $order_fh_count=M('OrderInfo')->where($order_fh_map)->count('id');
        // $this->assign('order_fh_count',$order_fh_count);
        //网上销售总金额,总订单
        $all_money_map['shop_id'] = MID;
        $all_money_map['type'] = array('IN','1,3,6,14');
        $all_money_map['pay_time'] = array('between',array($today,$tomorrow));
        $all_money_map['status'] = array('in','1,2,3,6,8');
        $all_money=M('OrderInfo')->where($all_money_map)->field('sum(order_money) as today_all_money,count(id) as today_all_num')->find();
        $this->assign($all_money);
        //今日新增充值数
        $today_cz_map['shop_id']=MID;
        $today_cz_map['type'] = array('IN','2');
        $today_cz_map['status']=array('in','1,2,3,6,8');//1207 +

        $today_cz_map['pay_time'] = array('between',array($today,$tomorrow));
        $today_cz_count=M('OrderInfo')->where($today_cz_map)->count('id');
        $this->assign('today_cz_count',$today_cz_count);
        //今日新增充值金额
        $today_cz_money_map['type'] = array('IN','2');
        $today_cz_money_map['status']=array('in','1,2,3,6,8');//1207 +
        $today_cz_money_map['pay_time'] = array('between',array($today,$tomorrow));
        $today_cz_money=M('OrderInfo')->where($today_cz_money_map)->sum('order_amount');
        $this->assign('today_cz_money',$today_cz_money);
        // //收藏店铺数
        // $map['shop_id']=MID;
        // $collect_shops=M('ShopsCollect')->where($map)->count();
        // //收藏商品数
        // $where['shop_id']=MID;
        // $goods=M('Goods')->where($map)->field('id')->select();
        // $collect_goods=0;
        // foreach ($goods as $k => $val) {
        //     $g_map['goods_id']=$val['id'];
        //     $result=M('GoodsCollect')->where($g_map)->field('id')->count();
        //     $collect_goods+=$result;
        // }
        // //商品数量
        // $goods_num=M('Goods')->where($map)->count();
        // //近30天下单数
        // $lastdate=strtotime(date('Y-m-d',strtotime("-30 day")));
        // $today=strtotime(date('Y-m-d',NOW_TIME));
        // $order_map['shop_id']=MID;
        // $order_map['pay_time']=array('between',array($lastdate,$today));
        // $order_count=M('OrderInfo')->where($order_map)->count();
        // $order_money=M('OrderInfo')->where($order_map)->sum('goods_allmoney');
        // $order_avg=M('OrderInfo')->where($order_map)->avg('goods_allmoney');
        // //下单会员数
        // $order_member=M('OrderInfo')->where($order_map)->count();
        // //销售走势
        // $this->sales_trend($this->shops_info['id']);
        // $this->hotgoods($this->shops_info['id']);
        // $this->assign('order_member',$order_member);
        // $this->assign('order_money',$order_money);
        // $this->assign('order_avg',$order_avg);
        // $this->assign('order_count',$order_count);
        // $this->assign('collect_shops',$collect_shops);
        // $this->assign('collect_goods',$collect_goods);
        // $this->assign('goods_num',$goods_num);
        // $tabid=I('param.tabid');
        // if(!empty($tabid)) $this->display($tabid);
        // else $this->display();
    }
    //店铺走势折线图
    private function sales_trend($shop_id){
        // $postdata=I('param.');
        // $map['shop_id']=$shop_id;
        //  if(empty($postdata['date_to'])){
        //     $today=strtotime(date('Y-m-d',NOW_TIME));
        //     $postdata['date_to']=time_format($today,'Y-m-d');
        // }
        // if(empty($postdata['date_from']))$postdata['date_from']=time_format(strtotime(date('Y-m-d',strtotime("-6 month"))),'Y-m-d');
        // $map['addtime'][]=array('EGT',strtotime($postdata['date_from']));
        // $map['addtime'][]=array('elt',strtotime($postdata['date_to']));
        // $order_money = M('OrderInfo')->where($map)->field("sum(order_amount) as l_money,DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y年%m月') as l_month")->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y年%m月')")->select();
        // $order_num = M('OrderInfo')->where($map)->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y年%m月')")->count('id');
        
        // $months[5]["time"]=time_format(NOW_TIME,'Y年m月');
        // for($i=1;$i<6;$i++){$months[(5-$i)]["time"]=time_format(strtotime('-'.$i.' month'),'Y年m月');}
        // foreach($order_money  as $v){
        //     $order_moneys[$v['l_month']] = $v['l_money'];
        //     $order_nums[$v['l_month']]=$order_num;
        // }
        // $name=array("订单金额（元）","订单数量（个）");
        // $formart = array('order_moneys'=>$order_moneys,'order_nums'=>$order_nums);
        // $formarttime =  array_keys($order_moneys);
        // $formart = echarts_time_formarts($formart,$formarttime);
        // $money_count = array(
        //         "legend"=>array('data'=>array($name[0],$name[1])),
        //         "xaxis"=>array("type"=>"category","data"=>$formarttime),     
        //         "series"=>array(
        //             array("name"=>$name[0],"type"=>"line","stack"=>$name[0],"data"=>array_values($formart['order_moneys'])),
        //             array("name"=>$name[1],"type"=>"line","stack"=>$name[1],"data"=>array_values($formart['order_nums']))
        //         ),
        // );
        // $ec = new \Common\Vendor\Echarts();
        // $this->assign('Area_countm',$ec->show('Area_countm', $money_count,'line','销售额统计'));
        // $this->assign($postdata);
        $start_time = strtotime(date('Y-m-d',NOW_TIME)); 
        $date_from=$start_time-6*86400;
        $date_to =$start_time+86399;
        $time[] = $date_from;
        $time[] = $date_to;
        $map['addtime']=array('between',array($time[0],$time[1]));
        $now_time=strtotime(date('Y-m-d',NOW_TIME));
        for($i=0;$i<7;$i++){
            $day_arr[date('Y-m-d',$now_time-$i*86400)] = date('Y-m-d',$now_time-$i*86400); //每隔一天赋值给数组
        }
        $file_profit=$file_money=$file_time=$file_num=$new_day=$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;
        $map['shop_id']=MID;
        $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();
        //每个折线的图标代表什么
        $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'];
              }
            }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;
        }unset($v);
        ksort($file_num);
        ksort($file_money);
        ksort($file_profit);
        //得到折线图
        $cz_count = array(
            "legend"=>array('data'=>array($name[0],$name[1],$name[2])),
            "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)),
            ),
        );
        $ec = new \Common\Vendor\Echarts();
        $this->assign('Area_count',$ec->show('Area_countm', $cz_count,'line','订单明细统计'));

    }
        /**
      *注册数统计
      */
    private function num_count(){
       
        //会员共享

        $is_share_user=idtoname(MID,'Shops','is_share_user');
        if(empty($is_share_user)){//不共享会员
            $c_map=$customer=array();
            $c_map['shop_id'] = $shop_id;
            $customer = M('ShopsCustomer')->cache(true)->where($c_map)->getField('user_id',true);
            if(empty($customer)){//无顾客
                $map['user_shop']=$shop_id;
            }else{
                $where=array();
                $where['user_shop']=$shop_id;
                $where['id']  = array('in',$customer);
                $where['_logic'] = 'or';
                $map['_complex'] = $where;
            }
        }else{//共享会员
            $smap=array();$cachetime=60*60*24*7;
            $smap['is_share_user']=0;                
            $noshareshopids=M('Shops')->where($smap)->cache(true,$cachetime)->getField('id',true);
            if(!empty($noshareshopids)){
                $map['user_shop'] = array('notin',$noshareshopids);
            }
        }
         $today=strtotime(date('Y-m-d',NOW_TIME));
        $tomorrow=$today+86400;
        // 粉丝独立
        $auth_code = 'fans';
        $ret=D('Shop/Shops')->shop_option(MID,$auth_code);
        for($i=0;$i<=4;$i++){
           switch ($i) {
               case 0:
                   $tomorrow=$today+86400;
                    $map['reg_time']=array('between',array($today,$tomorrow));
                    $count[$i]['time']=$today;
                   break;
               case 1:
                    $yesterday=strtotime(date('Y-m-d',strtotime("-1 day")));
                    $count[$i]['time']=$yesterday;
                    $map['reg_time']=array('between',array($yesterday,$today));
                   break;
                case 2:
                    $sb_yesterday=strtotime(date('Y-m-d',strtotime("-2 day")));
                    $count[$i]['time']=$sb_yesterday;
                    $map['reg_time']=array('between',array($sb_yesterday,$yesterday));
                   break;   
                case 3:
                    $tb_yesterday=strtotime(date('Y-m-d',strtotime("-3 day")));
                    $count[$i]['time']=$tb_yesterday;
                    $map['reg_time']=array('between',array($tb_yesterday,$sb_yesterday));
                   break;   
                case 4:
                  $fb_yesterday=strtotime(date('Y-m-d',strtotime("-4 day")));
                    $count[$i]['time']=$fb_yesterday;
                    $map['reg_time']=array('between',array($fb_yesterday,$tb_yesterday));
                   break;   
              
           }
            $map['is_follow']=1;
            // 粉丝独立
            $auth_code = 'fans';
            if($ret['status']==1){
                $map['_string']="user_shop = ".MID;
            }
            

            $count[$i]['f_count']=M('Member')->where($map)->count('id');
            unset($map['is_follow']);unset($map['_string']);

            $map['is_follow']=0;
            $count[$i]['g_count']=M('Member')->where($map)->count('id');
            unset($map['is_follow']);
            // $g_map['add_time'] = array('between',array($today,$tomorrow));
            // $count[0]['g_count']=M('ShopsCustomer')->where($g_map)->count('id');
            
            $map['is_tmp'] = 0;//会员
            $count[$i]['m_count']=M('Member')->where($map)->count('id');
            $count[$i]['sum'] = $count[$i]['f_count'] + $count[$i]['g_count'] + $count[$i]['m_count'];


        }
            $this->assign('count',$count);


    }
    private function hotgoods($shop_id){
        $map['shop_id']=$shop_id;
        $hot_goods=M('Goods')->where($map)->order('sell_count desc')->limit(5)->select();
        $this->assign('hot_goods',$hot_goods);
        $map['shop_id']=array('neq',0);
        $fellow_goods=M('Goods')->where($map)->order('sell_count desc')->limit(5)->select();
        $this->assign('fellow_goods',$fellow_goods);
    }
    //系统消息
    public function system_message(){
    $tabid=I('param.tabid');
             $shopinfo=session('shops_info');
    $map['touid']=$shopinfo['id'];
            $map['type']=2;
            $info=page_list('Message',$map,'status asc,addtime desc',true);
            $this->assign($info);
    if(!empty($tabid)) $this->display($tabid);
    else $this->display();
    }
    //平台消息管理
    public function min_view(){
        $id=I('id');
        if($id){
            $where['id']=$id;
            $info=M('Message')->where($where)->field('title,content')->find();
            $this->assign($info);
        }
    $this->display();
    }
    /**
     * 首页添加商品快捷键
     */
    public function goodsadd(){
        $this->redirect('Shop/goods/add');
    }
    /**
      *首页订单管理快捷键
      */
    public function orderindex(){
    	$this->redirect('Shop/Order/index');
    }
    /**
      *首页退款退货快捷键
      */
    public function backindex(){
    	$this->redirect('Shop/Order/back');
    }
    /**
      *首页修改密码快捷键
      */
    public function modifypsw(){
    	$this->redirect('Shop/Account/modify_psw');
    }
    /**
     * 订货中心快捷键
     */
    public function purchase(){
        $this->redirect('Shop/Purchase/index');
    }
    /**
     * 添加第三方门店
     */
    function shop_add(){
        if(C("DIS_TRIBUTE") != 1){
            $this->error('物流功能未开通',U('shop/shops/shop_list'));return;
        }
        $post = I('post.');
        if($post){
            $wuliu = M('Distribute')->field('code')->where(array('status'=>1))->select();
            foreach($wuliu as $k=>$v){
                $post['code'] = $v['code'];
                $result = D('Common/Distribute')->shop_add($post);
                if($result['status'] == 0)break;
            }
            if($result['status']){
                $this->success("添加成功",U('Shop/index/index'));return;
            }else{
                $this->error('操作失败:'.$result["msg"],U('Shop/index/shop_add',array('id'=>$post['id'])));return;
            }
        }else{
            $shops_info = session('shops_info');
            $id = $shops_info['shop_id'];
            $shops = M('Shops')->where('id='.$id)->find();
            $this->assign('shop',$shops);
            $this->assign('title_ass','注册');
        }
        $this->display();
    }

    /**
     * 编辑第三方门店
     */
    function shop_update(){
        /*$post = I('post.');
        if($post){
            $result = D('Common/Distribute')->shop_update($post);
            if($result["status"]){
                $this->success('操作成功',U('Shop/shops/shop_list'));exit();
            }else{
                $this->error('操作失败'.$result["msg"],U('Shop/shops/shop_list'));exit();
            }
        }else{        
            $id = I('get.id');        
            $shops_location = M('Shops')->where('id='.$id)->find(); 
            $data = array(
                'origin_shop_id'=> $shops_location['origin_id']
            );
            $reqStatus = D('Common/Distribute')->shop_detail($data);
            $this->assign('shop',$shops_location);
            $this->assign('dada',$reqStatus["result"]);
        }
        $this->display('shop_add');*/
    }
    // 经营概况开始 
    public function tong_index_tu(){
         /*获取时间数组开始*/
        $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=$this->quick_time($type);
            }
            if(in_array($type,array(7,30))){
                $between=$this->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['a.create_time']=array('between',array($map_z[0],$map_z[1]));
        }else{
            $map_g['sell_time']=array('gt',0);
        }
        $map_in['shop_id']=$map_c['shop_id']=$map_g['shop_id']=$map_t['shop_id']=$map['shop_id']=MID;
        $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_money) 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_money) 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_money) 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['a.type']=array('in','20,21,31');
        $map_zc['o.shop_id']=MID;
        // $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();//支出
        $pay_zc=M('PayLog')->alias('a')->where($map_zc)->join('__ORDER_INFO__ as o on a.order_id =o.id')->field('a.pay_code,sum(a.cash_money) as cash_money,sum(a.scan_money) as scan_money,sum(a.bank_money) as bank_money,sum(a.card_money) as card_money,sum(a.use_money) as used_money,sum(a.pay_amount) as pay_amount')->group('a.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 custome($map,$day_arr,$date_from,$date_to,$ec){
        // $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);
        // $map['shop_id']=MID;
        // $info=M('OrderInfo')->where($map)->distinct('uid')->field("count(uid) as number,sum(order_amount) as money,DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y-%m-%d') as mon")->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y-%m-%d')")->select();
        // //每个折线的图标代表什么
        // $name = array("消费会员数","消费金额");
        // if($info){
        //     foreach($info as $key => $value){
        //         if(!empty($value)){
        //             $file_uid[$value['mon']]=$value['number'];
        //             $file_time[$value['mon']]=$value['mon'];
        //             $file_jine[$value['mon']]=$value['money'];

        //         }
        //     }unset($value,$key);
        // }
        // $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)),
        //     ),
        // );

        $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);$map['shop_id']=MID;
        $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)),
            ),
        );
        $this->assign('custome',$ec->show('custome',$custome,'line'));
    }

    //订单统计
    public function order_tong($map,$day_arr,$ec){
        $map['a.addtime']=$map['addtime'];
        unset($map['addtime']);
        $file_goods=$file_profit=$file_money=$file_time=$file_num=$new_day=$info=array();
        $map['a.status'] = array('in','1,2,3,6,8');
        $map['a.pay_status'] = 3;
        $map['a.type'] = array('in','1,6,11,12,13,14');
        $map['a.is_delete']=0;
        $map['a.shop_id']=MID;
        $info=M('OrderInfo')->alias('a')->where($map)->join('__ORDER_GOODS__ g ON g.order_id=a.id')->field("count(a.id) as number,sum(a.order_money) as money,sum(a.profit) as profit,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($value['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;$map['shop_id']=MID;
        $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['shop_id']=MID;
        if($map_z[0]){$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 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);
        $ye_map['shop_id']=MID;
        $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;$map['shop_id']=MID;
        $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;
        $cz_map['shop_id']=MID;
        $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]));
        }
         // 获取本店会员id
            $m_map['user_shop']=MID;
            $ids=M('Member')->where($m_map)->getField('id',true);
            $idstr='';
            foreach ($ids as $val) {
                $idstr.=$val.',';
            }
            $map['uid']=array('in',trim($idstr));
        // 获取 end
        $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));
        }
        // $map['coupons_type'] = 1;
        $map['shop_id']=array('in',array(MID,','.MID.','));
        $info['lingqu'] = M('CouponsGet')->where($map)->field('count(*) as num,add_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(add_time),'%Y年%m月%d天')")->select();
        //核销数
        // $map=array();
        // if($date_from && $date_to){ $map['confirm_time'] = array('between',array($date_from,$date_to));}
        $map['confirm_shopid']=MID;
          $map['status']=1;
        // $info['hexiao'] =M('CouponsGet')->where($map)->field('count(*) as num,confirm_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(confirm_time),'%Y年%m月%d天')")->select();
        $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();
        // if($date_from && $date_to){
        //     $map['a.create_time']=array('between',array($date_from,$date_to));
        // }
        // $map['cg.shop_id']=MID;
        // $info['hexiao'] = M('CouponsDonation')->alias('a')->join('__COUPONS_GET__ as cg on a.card_id=cg.card_id')->where($map)->field('count(a.id) as num,a.create_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(a.create_time),'%Y年%m月%d天')")->select();
        //转赠数
        $map=array();
       
        if($date_from && $date_to){$map['a.add_time']=array('between',array($date_from,$date_to));}
        $map['a.send_id'] = array('gt','0');
        $map['cg.shop_id']=MID;
        $coupon_count[$i]['zhuanzeng'] = M('CouponsDonation')->alias('a')->join('__COUPONS_GET__ as cg on a.card_id=cg.card_id')->where($map)->field('count(a.id) as num,a.add_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(a.add_time),'%Y年%m月%d天')")->select();
        unset($map);
        //每个折线的图标代表什么
        $names = array("领券数","核销数","转赠数");
        if($info){
            foreach($info as $key => $value){
                if(!empty($value)){
                    foreach($value as $k=>$v){
                        $file[$key][date('Y-m-d',$v['mon'])]=$v['num'];
                        $file_time[$key][]=date('Y-m-d',$v['mon']);
                    }
                }
                // else{
                //     $file[$key][date('Y-m-d',$v['mon'])] =0;
                //     $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'));
    }
     // 礼品卡销售统计 售出order归自己
    public function gift_tong($gc_maps,$day_arr,$ec){
        $gc_map['a.sell_time']=$gc_maps['sell_time'];
        $gc_map['a.order_id'] = array('gt',0);
        $gc_map['oi.shop_id']=MID;

        $giftcode_info=M('GiftCard')->alias('a')->join('__ORDER_INFO__ as oi on a.order_id=oi.id')->where($gc_map)->field("count(a.id) as gc_number,sum(a.shop_price) as gc_money,DATE_FORMAT(FROM_UNIXTIME(a.sell_time),'%Y-%m-%d') as mon")->group("DATE_FORMAT(FROM_UNIXTIME(a.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();
        if($map_z[0])$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',''));
    }
    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;$map['shop_id']=MID;
        $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 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);$map['shop_id']=MID;
                $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',''));

    }
   

    function quick_time($type){
        $map=array();
        $start_time = strtotime(date('Y-m-d',NOW_TIME)); 
        if($type == 1){
            $date_from=$start_time;
            $date_to =$start_time+86399;
            $map[] = $date_from;
            $map[] = $date_to;
        }elseif($type == 2){
            $date_from=$start_time-86400;
            $date_to =$start_time-1;
            $map[] = $date_from;
            $map[] = $date_to;
        }elseif($type == 7){
            $date_from=$start_time-6*86400;
            $date_to =$start_time+86399;
            $map[] = $date_from;
            $map[] = $date_to;
        }elseif($type == 30){
            $date_from=$start_time-29*86400;
            $date_to =$start_time+86399;
            $map[] = $date_from;
            $map[] = $date_to;
        }
        return $map;
    }

  
   


}
?>