<?php
namespace wstmart\admin\controller;
use wstmart\admin\model\Reports as M;
/**
 * ============================================================================
 * 报表控制器
 */
class Reports extends Base{
	/**
	 * 商品销售排行
	 */
	public function toTopSaleGoods(){
        $this->assign("startDate",date('Y-m-d',strtotime("-1month")));
        $this->assign("endDate",date('Y-m-d'));
		return $this->fetch("/reports/top_sale_goods");
	}
    /**
     * 获取商品排行数据
     */
    public function topSaleGoodsByPage(){
        $m = new M();
        return WSTGrid($m->topSaleGoodsByPage());
    }
	/**
     * 店铺销售排行
     */
    public function toTopShopSales(){
        $this->assign("startDate",date('Y-m-d',strtotime("-1month")));
        $this->assign("endDate",date('Y-m-d'));
        return $this->fetch("/reports/top_sale_shop");
    }
    /**
     * 获取店铺排行数据
     */
    public function topShopSalesByPage(){
        $m = new M();
        return WSTGrid($m->topShopSalesByPage());
    }
    /**
     * 获取销售额
     */
    public function toStatSales(){
        $this->assign("startDate",date('Y-m-d',strtotime("-1month")));
        $this->assign("endDate",date('Y-m-d'));
        return $this->fetch("/reports/stat_sales");
    }
    public function statSales(){
        $m = new M();
        return $m->statSales();
    }
    /**
     * 获取订单统计
     */
    public function toStatOrders(){
        $this->assign("startDate",date('Y-m-d',strtotime("-1month")));
        $this->assign("endDate",date('Y-m-d'));
        return $this->fetch("/reports/stat_orders");
    }
    /*
     * 
     */
    public function getOrders(){
    	$m = new M();
        return $m->getOrders();
    }
    public function statOrders(){
        $m = new M();
       return $m->statOrders();
    }


    /**
     * 获取每日新增用户
     */
    public function toStatNewUser(){
        $this->assign("startDate",date('Y-m-d',strtotime("-1month")));
        $this->assign("endDate",date('Y-m-d'));
        return $this->fetch("/reports/stat_new_user");
    }
    public function statNewUser(){
        $start = date('Y-m-d 00:00:00',strtotime(input('startDate')));
        $end = date('Y-m-d 23:59:59',strtotime(input('endDate')));
        $where='dataFlag=1 AND userStatus=1 AND userType=0';
        $totalNum=db('users')->where($where)->count('userId');
        if($start!='' && $end!=''){
            $start = date('Y-m-d 00:00:00',strtotime(input('startDate')));
            $end = date('Y-m-d 23:59:59',strtotime(input('endDate')));
            $where.=' AND createTime BETWEEN "'.$start.'" AND "'.$end.'"';
        }else if($start!=''){
            $start = date('Y-m-d 00:00:00',strtotime(input('startDate')));
            $where.= ' AND createTime>="'.$start.'"';
        }else if($end!=''){
            $end = date('Y-m-d 00:00:00',strtotime(input('endDate')));
            $where.= ' AND createTime<="'.$end.'"';
        }
        $aa=array('dsad');
        $groupNum=db('users')->where($where)->count('userId');
        $groupRate=$groupNum/$totalNum;
        $m = new M();
        $a= $m->statNewUser();
        $a['groupNum']=$groupNum;
        $a['groupRate']=$groupRate;
        $this->assign('a',$a);
return $a;
    }
    /*
     * 首页获取新增用户
     */
    public function getNewUser(){
    	$m = new M();
    	$data = cache('userNumData');
    	if(empty($data)){
    	  $rdata = $m->statNewUser();
    	  cache('userNumData',$rdata,7200);
    	}else{
    	  $rdata = cache('userNumData');
    	}
        return $rdata;
    }
    /**
     * 会员登录统计
     */
    public function toStatUserLogin(){
        $this->assign("startDate",date('Y-m-d',strtotime("-1month")));
        $this->assign("endDate",date('Y-m-d'));
        return $this->fetch("/reports/stat_user_login");
    }
    public function statUserLogin(){
        $m = new M();
        return $m->statUserLogin();
    }
    /**
     * 导出商品销售订单
     */
    public function toExportGoods(){
        $m = new M();
        $rs = $m->toExportGoods();
        $this->assign('rs',$rs);
    }
    /**
     * 导出商家销售订单
     */
    public function toExportShop(){
        $m = new M();
        $rs = $m->toExportShop();
        $this->assign('rs',$rs);
    }
    // 店铺商品销售统计
    public function detail(){
        $this->assign("startDate",date('Y-m-d',strtotime("-1month")));
        $this->assign("endDate",date('Y-m-d'));
        return $this->fetch('detail');
    }
    public  function detailByPage(){
        $m=new M();
        $result=$m->detailByPage();
        return $result;
    }
}