<?php
/**
 * @author xiaogg QQ:756663992
 * @copyright 2017
 */
namespace Admin\Controller;
class CateStatisticsController extends BaseController{
    public function index(){
        GLOBAL $PurchaseCategory;
        if(empty($PurchaseCategory))$PurchaseCategory=getcache('PurchaseCategory');
        $Tree = new \Common\Vendor\Tree();
        $select_categorys = '';
        $treemini = $Tree->treemini($PurchaseCategory);
        $postdata=I('param.');
        $select_categorys .= $Tree->treeminioption($treemini,$postdata['catid']);
        $this->assign('select_categorys', $select_categorys);
        $where_s['status']=1;
        $shops_info = M('Shops')->where($where_s)->order('convert(name using gb2312) ASC')->field('id,name')->select();
        $shops_op = '';
        if (!empty($shops_info)) {
            foreach ($shops_info as $k => $v) {
                if ($postdata['shop_id'] == $v['id']) {
                    $shops_op .= "<option selected='selected' value='" . $v['id'] . "'>" . $v['name'] . "</option>";
                } else {
                    $shops_op .= "<option value='" . $v['id'] . "'>" . $v['name'] . "</option>";
                }
            }
        }
        $this->assign('select_shops', $shops_op);
        $result=$this->catelist($postdata);
        $ec = new \Common\Vendor\Echarts();
        $pay_style_count = array(
	  'title'=>array(
	  'text'=>'分类统计',
	  'x'=>'center'
	  ),
	  "legend"=>array('orient'=>'vertical','left'=>'left','data'=>$result['catename']),
	  "series"=>array(
	      array("name"=>"分类分析",
		    "type"=>"pie",
		'radius'=>'60%',
		"data"=>$result['data'],
	      ),
	  ),
        );
        $this->assign('Area_count_pay',$ec->pie_show('Area_count_pay', $pay_style_count,'pie',''));
        $this->assign('cate_count',$result['cate_count']);
        $this->assign('count',$result['count']);
        $this->assign('allnum',$result['allnum']);
        if(!strpos('1,2,7,30', $postdata['dates']) && $postdata['dates']!=1){
	  $postdata['dates']=99;
        }
        $postdata['dates']=intval($postdata['dates']);
        $this->assign($postdata);
        $this->display();
    }
    public function export_a(){
        $null=array();
        $postdata=I('param.');
        $count=$this->catelist($postdata);
        $title=array('分类','销量','百分比');
        $title_name = array('商品分类统计');
        export_csv('分类统计.csv',$title,$count['cate_count']);
        exit;
    }
    private function catelist($postdata){
        $return=$cate_count=$data=$og_map=$map=$map_c=$g_map=$catename=array();
        $map_c['status']=1;
        if($postdata['catid']){
        	$map_c['pid']=$postdata['catid'];
        }else{
        	$map_c['pid']=0;
        }
        $carids = M('PurchaseCategory')->where($map_c)->field('id,name')->select();
        unset($map_c['pid']);
        if($carids){
	  $map['type'] = array('in','1,6,13,14');
	  $map['pay_status'] = 3;
	  $now_time=strtotime(date('Y-m-d',NOW_TIME));
	  if($postdata['date_from'] || $postdata['date_to']){
	      $date_from = strtotime($postdata['date_from']);
	      if($date_from)$map['addtime'][] = array('egt',$date_from);
	      $date_to = strtotime($postdata['date_to']);
	      if($date_to){$date_to = $date_to+86399;$map['addtime'][] = array('lt',$date_to);}
	      $postdata['dates']=0;
	  }else{
	      if(in_array($postdata['dates'],array(1,2,7,30))){
	      	$between=quick_time($postdata['dates']);
	      	$map['addtime']=array('between',array($between[0],$between[1]));
	      }else{
		$postdata['dates']=99;
	      }
	  }
	  if($postdata['shop_id']){
	      $map['shop_id']=$postdata['shop_id'];
	  }
	  $orderid=M('OrderInfo')->where($map)->getField("id",true);
	  if($orderid){
	      foreach($carids as $k=>$v){
		$catename[]=$v['name'];
	      	$map_c['pid']=$v['id'];
	      	$child_catid = M('PurchaseCategory')->where($map_c)->getField('id',true);
	      	if($child_catid){
		    $child_catid[]=$v['id'];
		    $g_map['cat_id']=array('in',$child_catid);
	      	}else{
		    $g_map['cat_id']=$v['id'];
	      	}
		$goods_ids = M('Goods')->where($g_map)->getField('id',true);
		if($goods_ids){
		    $og_map['goods_id']=array('in',$goods_ids);
		    $og_map['order_id']=array('in',$orderid);
		    $goods_num=M('OrderGoods')->where($og_map)->sum('goods_number');
		    $goodsnum[]=floatval($goods_num);
		}else{
		    $goodsnum[]=0;
		}
	    }
	  }
        }
        $allnum=array_sum($goodsnum);
        $i=0;
        if(count($catename) > 0){
	  foreach ($catename as $key => $value) {
	      $data[]=array("value"=>$goodsnum[$key],"name"=>$value);
	      $cate_count[$key]['catename']=$value;
	      $cate_count[$key]['num']=$goodsnum[$key];
	      $cate_count[$key]['percent']=number_format(($goodsnum[$key]/$allnum)*100,2).'%';
	      $i++;
	  }
        }
        $return['data']=$data;
        $return['catename']=$catename;
        $return['cate_count']=$cate_count;
        $return['allnum']=$allnum;
        $return['count']=$i;
        return $return;
    }
}
?>