<?php
/**
 * 统计
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaol <1094186793@qq.com>
 */

namespace Common\Model;
use Think\Model;
class StatisticsModel extends Model
{
    protected $tableName = 'Goods';
    public function index($postdata){
        $map=$mapi=array();
        $now_time=strtotime(date('Y-m-d',NOW_TIME));
        $start_time=$now_time-86400;
        $end_time=$now_time-1;
        $return['start_time']=date('Y-m-d H:i:s',$start_time);
        $return['end_time']=date('y-m-d H:i:s',$end_time);


        // 昨日 收入
        $mapi['shop_id']=$postdata['shop_id'];
        $mapi['addtime']=array('between',array($start_time,$end_time));
        $mapi['pay_status'] = 3;
        $mapi['type'] = array('in','1,6,11,12,13,14');
        $mapi['is_delete']=0;
        $mapi['status']=array('in','1,2,3,6,8');
        $info=M('OrderInfo')->where($mapi)->field("count(id) as number,sum(order_amount) as money")->find();
        if($info['number']){
            $return['order_n']=$info['number'];
            $return['order_m']=$info['money'];
        }else{
            $return['order_n']=0;
            $return['order_m']= number_format(0.00,2);
        }
        $map['shop_id']=$postdata['shop_id'];
        $map['reg_time']=array('between',array($start_time,$end_time));
        $map['status'] = 1;
        $map['is_tmp'] = 0;
        $map['user_shop'] = $postdata['shop_id'];
        $return['member'] = M('Member')->where($map)->count();
        $map['is_follow'] = 1;
        $return['fans'] = M('Member')->where($map)->count();
        $return['name'] = idtoname($postdata['shop_id'],'Shops','name');
        return $return;
    }

    public function member_trend($postdata){
        //会员
        $map=array();
        $date=$this->process_time($postdata['day']);
        if($date){
            $map['reg_time']=array('between',array($date['date_from'],$date['date_to']));
        }
        $map['status'] = 1;
        $map['is_tmp'] = 0;
        $map['user_shop'] = $postdata['shop_id'];
        $info['member'] = M('Member')->where($map)->field('count(*) as num,reg_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(reg_time),'%Y年%m月%d天')")->select();
        unset($map['is_tmp']);
        //粉丝
        $map['is_follow'] = 1; 
        $info['fans'] = M('Member')->where($map)->field('count(*) as num,reg_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(reg_time),'%Y年%m月%d天')")->select();
        //顾客
        $map['is_follow'] = 0; 
        $info['guke'] = M('Member')->where($map)->field('count(*) as num,reg_time as mon')->group("DATE_FORMAT(FROM_UNIXTIME(reg_time),'%Y年%m月%d天')")->select();
        if($info){
            foreach($info as $key => $value){
                if(!empty($value)){
                    foreach($value as $k=>$v){
                        $file[$key][date('Y-m-d',$v['mon'])]=$v['num'];
                        $file_time[$key][]=date('Y-m-d',$v['mon']);
                    }
                }else{
                    $file[$key][date('Y-m-d',$date['date_from'])] =0;
                    $file_time[$key][]=date('Y-m-d',$date['date_from']);
                }
            }unset($value,$key,$k,$v);
        }
        if($date['day_arr']){
            foreach($date['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['member']);
        $rows=array();
        foreach ($file['member'] as $key => $value) {
            $rows[]=array('日期'=>date('m/d',strtotime($key)),'粉丝'=>$file['fans'][$key]?$file['fans'][$key]:0,'顾客'=>$file['guke'][$key]?$file['guke'][$key]:0,'会员'=>$value?$value:0);
        }
        return $rows;
    }
    private function process_time($type){//获取搜索的 时间段
        $day_arr=array();
        $start_time = strtotime(date('Y-m-d',NOW_TIME)); 
        // if($type == 1){
        //     $return['date_from']=mktime(0,0,0,date('m'),date('d'),date('Y'));
        //     $return['date_to']=NOW_TIME;
        // }else
        if($type == 15){
            $return['date_from']=$start_time-14*86400;
            $return['date_to']=$start_time+86399;
            for($i=0;$i<15;$i++){
                $day_arr[date('Y-m-d',$start_time-$i*86400)] = date('Y-m-d',$start_time-$i*86400); //每隔一天赋值给数组
            }
            $return['day_arr']=$day_arr;
        }elseif($type == 1){
            $return['date_from']=$start_time;
            $return['date_to']=$start_time+86399; 
            // for($i=0;$i<7;$i++){
            //     $day_arr[date('Y-m-d',$start_time-$i*86400)] = date('Y-m-d',$start_time-$i*86400); //每隔一天赋值给数组
            // }
            // $return['day_arr']=$day_arr;
        }elseif($type == -1){
            $return['date_from']=$start_time-86400;
            $return['date_to']=$start_time-1; 
            // for($i=0;$i<7;$i++){
            //     $day_arr[date('Y-m-d',$start_time-$i*86400)] = date('Y-m-d',$start_time-$i*86400); //每隔一天赋值给数组
            // }
            // $return['day_arr']=$day_arr;
        }elseif($type == 7){
            $return['date_from']=$start_time-6*86400;
            $return['date_to']=$start_time+86399; 
            for($i=0;$i<7;$i++){
                $day_arr[date('Y-m-d',$start_time-$i*86400)] = date('Y-m-d',$start_time-$i*86400); //每隔一天赋值给数组
            }
            $return['day_arr']=$day_arr;
        }elseif($type == 30){
            $return['date_from']=$start_time-29*86400;
            $return['date_to']=$start_time+86399; 
            for($i=0;$i<30;$i++){
                $day_arr[date('Y-m-d',$start_time-$i*86400)] = date('Y-m-d',$start_time-$i*86400); //每隔一天赋值给数组
            }
            $return['day_arr']=$day_arr;
        }else{
            $return='';
        }
        return $return;
    }
    public function member_biao($postdata){
        // $date=$this->process_time($postdata['day']);
        $riqi=$postdata['day'];
        $now_time = NOW_TIME;
        $count=$map=$g_map=array();
        $map['status']=1;
        $map['user_shop'] = $postdata['shop_id'];
        $g_map['shop_id'] = $postdata['shop_id'];
        $start_time=strtotime(date('Y-m-d',NOW_TIME));
        for($i=0;$i<$riqi;$i++){
            if($i == 0){
                $today = $start_time;
                $tomorrow = $start_time+86400;
            }else{
                $today = $start_time - $i*86400;
                $tomorrow = $start_time-($i-1)*86400;
            }
            $count[$i]['time'] = date('Y-m-d',$now_time-$i*86400);
            $map['is_follow'] = 1;
            $map['reg_time']=array('between',array($today,$tomorrow));
            $count[$i]['f_count'] = M('Member')->where($map)->count();
            $map['is_follow'] = 0;
            $count[$i]['g_count']=M('Member')->where($map)->count();
            unset($map['is_follow']);
            $map['is_tmp'] = 0;
            $count[$i]['m_count']=M('Member')->where($map)->count();
            unset($map['is_tmp']);
            $count[$i]['sum'] = $count[$i]['f_count'] + $count[$i]['g_count'] + $count[$i]['m_count'];
        }
        return $count;
    }
    // 复购率
    public function buy_rate($postdata){
        $map = array();        
        $date=$this->process_time($postdata['day']);
        if($date){
            $map['addtime']=array('between',array($date['date_from'],$date['date_to']));
        }
        $map['type'] = array('in','1,6,11,13,14');
        $map['status'] = array('in','1,2,3,6,8');
        $map['shop_id'] = $postdata['shop_id'];
        $buy_info = M('OrderInfo')->where($map)->group('uid')->field('count(id) as num')->select();
        
        $a=$b=$c=$d=$e=$f=$g=$h=$j=$k=0;
        foreach($buy_info as $ke=>$v){
            switch ($v['num'])
            {
                case 1:
                  $a++;
                  break;  
                case 2:
                  $b++;
                  break;
                case 3:
                  $c++;
                  break;
                case 4:
                  $d++;
                  break;
                case 5:
                  $e++;
                  break;
                case 6:
                  $f++;
                  break;
                case 7:
                  $g++;
                  break;
                case 8:
                  $h++;
                  break;
                case 9:
                  $j++;
                  break;
                default:
                  $k++;
            }
        }
        $rows=array(array('次数'=>'1次','人数'=>$a),array('次数'=>'2次','人数'=>$b),array('次数'=>'3次','人数'=>$c),array('次数'=>'4次','人数'=>$d),array('次数'=>'5次','人数'=>$e),array('次数'=>'6次','人数'=>$f),array('次数'=>'7次','人数'=>$g),array('次数'=>'8次','人数'=>$h),array('次数'=>'9次','人数'=>$j),array('次数'=>'10次以上','人数'=>$k));
        return $rows;
       
    }
    public function buy_rate_biao($postdata){
        $map = array();
        $map['type'] = array('in','1,6,11,13,14');
        $map['status'] = array('in','1,2,3,6,8');
       
        $date=$this->process_time($postdata['day']);
        if($date){
            $map['addtime']=array('between',array($date['date_from'],$date['date_to']));
        }
        $map['shop_id'] = $postdata['shop_id'];
        $buy_info = M('OrderInfo')->where($map)->group('uid')->field('count(id) as num')->select();
        $a=$b=$c=$d=$e=$f=$g=$h=$j=$k=0;
        if($buy_info){
            $buy_count=array();
            foreach($buy_info as $ke=>$v){
                switch ($v['num'])
                {
                    case 1:
                      $a++;
                      break;  
                    case 2:
                      $b++;
                      break;
                    case 3:
                      $c++;
                      break;
                    case 4:
                      $d++;
                      break;
                    case 5:
                      $e++;
                      break;
                    case 6:
                      $f++;
                      break;
                    case 7:
                      $g++;
                      break;
                    case 8:
                      $h++;
                      break;
                    case 9:
                      $j++;
                      break;
                    default:
                      $k++;
                }
            }
            $buy_count['1次'] =$a; 
            $buy_count['2次'] = $b;
            $buy_count['3次'] = $c;
            $buy_count['4次'] = $d;
            $buy_count['5次'] = $e;
            $buy_count['6次'] = $f;
            $buy_count['7次'] = $g;
            $buy_count['8次'] = $h;
            $buy_count['9次'] = $j;
            $buy_count['10次以上'] = $k;
        }
        return $buy_count;
    }
    public function consume($postdata){
        $date=$this->process_time($postdata['day']);
        $new_day=$file_time=$info=array();
        $map['status'] = array('in','1,2,3,6,8');
        $map['pay_status'] = 3;
        $map['shop_id'] = $postdata['shop_id'];
        $map['type'] = array('in','1,6,11,12,13,14');
        $map['is_delete']=0;
        $map['addtime']=array('between',array($date['date_from'],$date['date_to']));
        $info=M('OrderInfo')->where($map)->field("sum(order_amount) 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();
        //每个折线的图标代表什么
        if($info){
            foreach($info as $key => $value){
                if(!empty($value)){
                    $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($date['day_arr'],$file_time);
        foreach($new_day as $v){
            $file_money[$v]=0;
            $file_profit[$v]=0;
        }unset($v);
        ksort($file_money);
        ksort($file_profit);
        $rows=array();
        foreach ($file_money as $key => $value) {
            $rows[]=array('日期'=>date('m/d',strtotime($key)),'订单金额'=>$file_money[$key],'利润'=>$file_profit[$key]);
        }
        return $rows;
    }
    public function consume_biao($postdata){
        $date=$this->process_time($postdata['day']);
        $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;
        $map['shop_id']=$postdata['shop_id'];
        $riqi=$postdata['day'];
        $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("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);
        }
       return $order_count;
    }
    public function order($postdata){
        $date=$this->process_time($postdata['day']);
        $new_day=$file_time=$info=array();
        $map['status'] = array('in','1,2,3,6,8');
        $map['pay_status'] = 3;
        $map['shop_id'] = $postdata['shop_id'];
        $map['type'] = array('in','1,6,11,12,13,14');
        $map['is_delete']=0;
        $map['addtime']=array('between',array($date['date_from'],$date['date_to']));
        $info=M('OrderInfo')->where($map)->field("count(id) as number,DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y-%m-%d') as mon")->group("DATE_FORMAT(FROM_UNIXTIME(addtime),'%Y-%m-%d')")->select();
        //每个折线的图标代表什么
        if($info){
            foreach($info as $key => $value){
                if(!empty($value)){
                    $file_time[$value['mon']]=$value['mon'];
                    $file_n[$value['mon']]=$value['number'];

                }
            }unset($value,$key);
        }
        $new_day=array_diff($date['day_arr'],$file_time);
        foreach($new_day as $v){
            $file_n[$v]=0;
        }unset($v);
        ksort($file_n);
        $rows=array();
        foreach ($file_n as $key => $value) {
            $rows[]=array('日期'=>date('m/d',strtotime($key)),'订单量'=>$file_n[$key]);
        }
        return $rows;
    }
    public function order_biao($postdata){
        $date=$this->process_time($postdata['day']);
        $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;
        $map['shop_id']=$postdata['shop_id'];
        $riqi=$postdata['day'];
        $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")->find();
            $order_count[$i]['order_n'] = $info['number'];
        }
       return $order_count;
    }
    public function goods_sales($postdata){
        $date=$this->process_time($postdata['day']);
        $map['shop_id']=$postdata['shop_id'];
        $map['type'] = array('in','1,6,13,14');
        $map['pay_status'] = 3;
        $map['status'] = array('in','1,2,3,6,8');
        if($postdata['day']!=99){
            $map['addtime'] = array('between',array($date['date_from'],$date['date_to']));
        }
        
        $order_ids = M('OrderInfo')->where($map)->getField('id',true);
        if(!empty($order_ids)){
            $map = array();
            $map['order_id'] = array('in',$order_ids);
            $map['group'] = 'goods_id';
            $field = 'sum(goods_number) as num,goods_id,goods_sn,goods_name,sum(goods_number*goods_price) as all_price';
            $order = 'sum(goods_number) desc';
            // if($postdata['size'] > 0){
            //     $order = 'sum(goods_number) desc';
            //     $size = $postdata['size'];
            // }else{
            //     $order = 'sum(goods_number) asc';
            //     $size = 10;
            // }
            $list = page_list('OrderGoods',$map,$order,$field,25);
        }
        if($list){
            foreach ($list['list'] as $key => $value) {
                $list['list'][$key]['num']=floatval($value['num']);
                $list['list'][$key]['all_price']=number_format($value['all_price'],2);
            }
        }else{
            $list['list']='';
        }
        return $list['list'];
    }
    public function recharge($postdata){
        $date=$this->process_time($postdata['day']);
        $new_day=$file_time=$info=array();
        $map['status'] = 3;
        $map['pay_status'] = 3;
        $map['shop_id'] = $postdata['shop_id'];
        $map['type'] = 2;
        $map['addtime']=array('between',array($date['date_from'],$date['date_to']));
        $info=M('OrderInfo')->where($map)->field("count(id) 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();
        //每个折线的图标代表什么
        if($info){
            foreach($info as $key => $value){
                if(!empty($value)){
                    $file_time[$value['mon']]=$value['mon'];
                    $file_m[$value['mon']]=$value['money'];

                }
            }unset($value,$key);
        }
        $new_day=array_diff($date['day_arr'],$file_time);
        foreach($new_day as $v){
            $file_m[$v]=0;
        }unset($v);
        ksort($file_m);
        $rows=array();
        foreach ($file_m as $key => $value) {
            $rows[]=array('日期'=>date('m/d',strtotime($key)),'充值金额'=>$file_m[$key]);
        }
        return $rows;
    }
    public function recharge_biao($postdata){
        $date=$this->process_time($postdata['day']);
        $order_count=$map=array();
        $map['status'] = 3;
        $map['pay_status'] = 3;
        $map['type'] = 2;
        $map['shop_id']=$postdata['shop_id'];
        $riqi=$postdata['day'];
        $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")->find();
            $order_count[$i]['order_n'] = $info['number'];
            $order_count[$i]['money'] = ($info['number']==0?0.00:$info['money']);
        }
       return $order_count;
    }
    public function goods_category($postdata){
        $date=$this->process_time($postdata['day']);
        $map = $map_c=array();
        $map_c['status']=1;
        $map_c['pid']=0;
        $carids = M('PurchaseCategory')->where($map_c)->field('id,name');
        unset($map_c['pid']);
        if($carids){
            foreach($carids as $v){
                $map_c['pid']=$v;
                $child_catid = M('PurchaseCategory')->where($map_c)->getField('id',true);
            }
        }else{
            $rows[0]=array('分类'=>'默认分类','销量'=>0);
            return $rows;
        }
        $map['shop_id']=$postdata['shop_id'];
        $map['type'] = array('in','1,6,13,14');
        $map['pay_status'] = 3;
        $map['status'] = array('in','1,2,3,6,8');
        $map['addtime'] = array('between',array($date['date_from'],$date['date_to']));
        
       
        $order_ids = M('OrderInfo')->where($map)->getField('id',true);
        if(!empty($order_ids)){
            $map = array();
            $map['order_id'] = array('in',$order_ids);
            $map['group'] = 'goods_id';
            $field = 'sum(goods_number) as num,goods_id,goods_sn,goods_name';
          
            $list = page_list('OrderGoods',$map,$order,$field,$size);
        }
        if($list){
            foreach ($list['list'] as $key => $value) {
                $list['list'][$key]['num']=intval($value['num']);
            }
        }else{
            $list['list']='';
        }
        return $list['list'];
    }
}
?>