<?php
namespace app\admin\controller;
use think\Db;
class Index extends Home{ 
  public function index() {    
			$t = time();
			$start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t));
			$end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));

	 	    $this->meta_title="首页";
		    $this->assign('meta_title', $this->meta_title);
		
		    $map[]=['reg_time','egt',$start];
		    $map[]=['reg_time','elt',$end];
		    $info["user"]=Db::name('User')->where($map)->field("id")->count();  
		    // dump($map);
		    unset($map);
		    $map[]=['update_time','egt',$start];
		    $map[]=['update_time','elt',$end];  $map[]=['status','eq',1];
		      $info["order"]=Db::name('order')->where($map)->field("id,update_time")->count();
		   
		    
		      $info["total"]=$this->getTotal($map);
		    unset($map);
		     $map[]=['create_time','egt',$start];
		    $map[]=['create_time','elt',$end];
			
		   $info["flow"]=Db::name('flow')->where($map)->field("id")->count();
		    $info["category"]=Db::name('category')->field("id")->count();
		   $info["type"]=Db::name('types')->field("id")->count();
		   $info["brand"]=Db::name('brand')->field("id")->count();
		   $info["goods"]=Db::name('goods')->field("id")->count();
		    $info["fensi"]=Db::name('follow')->field("id")->count();
			 $info["sku"]=$this->getNum();
			
		   $this->assign('info', $info);
		   //访客数
		    unset($map);$where = [
["model",'=',"visitor"],
['create_time', ['lt', time()], ['gt', strtotime('-29 days')], 'and']
];
		  
		  //$list["visitor_list"]=Db::name('count')->where($where)->select();
		
		   unset($map);
		   //注册会员数
		   $map[]=["model",'=',"member"];
		   //$list["member_list"]=Db::name('count')->where($map)->select();
		   
		    
		   $date[0] = date('Y-m-d', strtotime('-6 days'));
		   $date[1] = date('Y-m-d', strtotime('-5 days'));
		   $date[2] = date('Y-m-d', strtotime('-4 days'));
		   $date[3] = date('Y-m-d', strtotime('-3 days'));
		   $date[4] = date('Y-m-d', strtotime('-2 days'));
		   $date[5] = date('Y-m-d', strtotime('-1 days'));
		   $date[6] = date('Y-m-d', time());
		   $this->assign('date', $date);
		   
	       $list=$this->getLine();
		
		   $arr = []; $data= [];
		  
			   foreach ($list as $key => $val) {
				$arr["name"]=(string)$key;
				$arr["value"]=(string)$val;
				$data[]=$arr;
			   }
		

		    $this->assign('list', $data);
			return $this->fetch();	
		  
		
	}
	function getdata(){
		$perfix=config("database.prefix");
         $arr=Db::query("SELECT DATE_FORMAT(FROM_UNIXTIME(pay_time),'%Y%m') AS month,COUNT(*) AS num 
						 FROM {$perfix}order 
						 WHERE `pay_time`
						 BETWEEN UNIX_TIMESTAMP(now()) - 31556926
						 AND UNIX_TIMESTAMP(now())
						 GROUP BY month");		
		$today = date('m');
		$res = [];
		foreach ($arr as $key => $val) {
		    $res[$val['month']] = $val['num'];
		}
		$ext = date('Y');
		for ($i=1; $i < 13; $i++) {
		    if($today < 10){
		        if($i < 10 && $i >$today) $key = ($ext-1).'0';
		        elseif($i >= 10) $key = $ext-1;
		        else $key = $ext.'0';
		        if(!array_key_exists($key.$i,$res)){
		            $res[$key.$i] = 0;
		        }
		    }else{
		        if($i > $today) $key = $ext-1;
		        elseif($i >= 10 && $i <= $today) $key = $ext;
		        else $key = ($ext).'0';
		        if(!array_key_exists($key.$i,$res)){
		            $res[$key.$i] = 0;
		        }
		    }
 
		}ksort($res);
 return $res;
}
 

	function getLine(){
 $perfix=config("database.prefix");
    $arr=Db::query("SELECT DATE_FORMAT(FROM_UNIXTIME(reg_time),'%Y%m') AS month,COUNT(*) AS num 
						 FROM {$perfix}user 
						 WHERE `reg_time`
						 BETWEEN UNIX_TIMESTAMP(now()) - 31556926
						 AND UNIX_TIMESTAMP(now())
						 GROUP BY month");
		
		$today = date('m');
		$res = [];
		foreach ($arr as $key => $val) {
		    $res[$val['month']] = $val['num'];
		}
		$ext = date('Y');
		for ($i=1; $i < 13; $i++) {
		    if($today < 10){
		        if($i < 10 && $i >$today) $key = ($ext-1).'0';
		        elseif($i >= 10) $key = $ext-1;
		        else $key = $ext.'0';
		        if(!array_key_exists($key.$i,$res)){
		            $res[$key.$i] = 0;
		        }
		    }else{
		        if($i > $today) $key = $ext-1;
		        elseif($i >= 10 && $i <= $today) $key = $ext;
		        else $key = ($ext).'0';
		        if(!array_key_exists($key.$i,$res)){
		            $res[$key.$i] = 0;
		        }
		    }
 
		}ksort($res);
 return $res;
}
 

	 public function getTotal($map) { 
        $total =0.00;
        $data = db("order")->where($map)->select();
        foreach ($data as $k=>$val) {
			$price=$val['total_money'];
            $total += $price;
        }
    
	   return sprintf("%01.2f", $total);
    }
    public function getNum() { 
          $total =0;
        $data = db("sku")->select();
        foreach ($data as $k=>$val) {
			$num=$val['num'];
            $total += $num;
        }
    
	   return $total;
    }
}
