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

namespace Supplier\Controller;
use Think\Controller;
class IndexController extends BaseController {
    public function index(){
    	$supplier_info=session('suppliers_info');
    	//系统公告、单品销售排名
    	$this->other_info(MID);
    	//供货商及商品提示
        $this->purchase_goods($supplier_info['id']);
    	//交易提示
    	$this->order($supplier_info['id']);
    	//昨日销售量统计
    	$this->sell_num(MID);
        //销售走势
        $map['suppliers_id']=MID;
        $map['goods_type']=5;
        $goods=M('Goods')->where($map)->field('id')->select();
        //商品数量
        $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_map['type']=10;
        $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(MID);
        $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('goods_num',$goods_num);
    	$this->assign($supplier_info);
        //供货区域
        $region=explode(',',$supplier_info['areas']);
        $this->assign('region',$region);
    	$this->display();
    }
    private function other_info($suppliers_id){
        //系统公告
        $m_map['catid']=2;
        $notice=M('Article')->where($m_map)->field(true)->order('sort asc,addtime desc')->limit(6)->select();
        $this->assign('notice',$notice);
    }
    //销售量统计
    private function sell_num($suppliers_id){
    	$day=strtotime(date('Y-m-d',strtotime("-1 day")));
    	$today=strtotime(date('Y-m-d',NOW_TIME));
    	$map['shop_id']=$suppliers_id;
        $map['type']=10;
    	$map['pay_time']=array('between',array($day,$today));
        $sell_money=M('OrderInfo')->where($map)->sum('goods_allmoney');
    	$sell_num=M('OrderInfo')->where($map)->field('id')->count();

        $week=strtotime(date('Y-m-d',strtotime("-1 week Monday")));
        $tweek=strtotime(date('Y-m-d',NOW_TIME));
        $map['pay_time']=array('between',array($week,$tweek));
        $wsell_money=M('OrderInfo')->where($map)->sum('goods_allmoney');
        $wsell_num=M('OrderInfo')->where($map)->field('id')->count();
        $month=strtotime(date('Y-m',strtotime("last month")));
        $tmonth=strtotime(date('Y-m',NOW_TIME));
        $map['pay_time']=array('between',array($month,$tmonth));
    	$msell_money=M('OrderInfo')->where($map)->sum('goods_allmoney');
    	$msell_num=M('OrderInfo')->where($map)->field('id')->count();
    	$this->assign('sell_money',$sell_money);
    	$this->assign('sell_num',$sell_num);
        $this->assign('wsell_money',$wsell_money);
        $this->assign('wsell_num',$wsell_num);
    	$this->assign('msell_money',$msell_money);
    	$this->assign('msell_num',$msell_num);
    }
    //获取交易提示
    private function order($suppliers_id){
    	//待付款
        $rg_map['shop_id']=$t_map['shop_id']=$suppliers_id;
    	$no_pay=D('Common/Suppliers')->supplier_all_order($t_map,2,1);
    	//待发货
    	$no_ship=D('Common/Suppliers')->supplier_all_order($t_map,3,1);
        $t_map['type']=10;
        //退款中
        $t_map['pay_status']=3;
        $t_map['status']=6;
        $t_map['shipping_status']=1;
        $no_back=M('OrderInfo')->where($t_map)->field('id')->count();
        //已退款
        $t_map['status']=7;
        $all_back=M('OrderInfo')->where($t_map)->field('id')->count();
        //退货中
        $rg_map['status']=0;
        $rg_map['type']=1;
        $no_refunds=M('ReturnGoods')->where($rg_map)->field('id')->count();
        //已退货
        $t_map['status']=7;
        $refunds=M('OrderInfo')->where($t_map)->field('id')->count();
        $this->assign('no_pay',$no_pay);
        $this->assign('no_ship',$no_ship);
        $this->assign('no_back',$no_back);
        $this->assign('all_back',$all_back);
        $this->assign('no_refunds',$no_refunds);
        $this->assign('refunds',$refunds);

    }
    //订货商品
    private function purchase_goods($suppliers_id){
    	//所有商品
    	$map['suppliers_id']=$suppliers_id;
        $map['is_delete']=0;
        $map['goods_type']=5;
    	$all_count=M('Goods')->where($map)->field('id')->count();
    	//出售商品
    	$sell_count=D('Common/Suppliers')->get_goods_count($suppliers_id);
    	//下架商品
    	$map['status']=0;
    	$usell_count=M('Goods')->where($map)->field('id')->count();
        unset($map['is_delete']);
    	// $map['type']=2;
    	$this->assign('all_count',$all_count);
    	$this->assign('sell_count',$sell_count);
    	$this->assign('usell_count',$usell_count);
    }
 
    //供货商走势折线图
    private function sales_trend($suppliers_id){
        $postdata=I('param.');
        $map['shop_id']=$suppliers_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']));
        $map['type'] = 10;
        $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);
    }

    //系统消息
    public function system_message(){
    $tabid=I('param.tabid');
    $shopinfo=session('suppliers_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('Supplier/pur_goods/add');
    }
    /**
      *首页订单管理快捷键
      */
    public function orderindex(){
    	$this->redirect('Supplier/Order/index');
    }
    /**
      *首页退款退货快捷键
      */
    public function backindex(){
    	$this->redirect('Supplier/Order/back');
    }
    /**
      *首页修改密码快捷键
      */
    public function modifypsw(){
    	$this->redirect('Supplier/Account/modify_psw');
    }
}
?>