<?php
/**
* 礼品卡统计
* @Copyright (C) 2016 汉潮 All rights reserved.
* @License http://www.hanchao9999.com
* @Author xiaogg <xiaogg@sina.cn>
*/
namespace Admin\Controller;
class GiftStatisticsController extends BaseController{
    public function index($is_return=false){
        $s_map = array();$s_map['id'] = array('gt',0);$s_map['status'] = 1;
        $shop_list=D('Shops')->shop_list();
        $this->assign('shop_list',$shop_list);

        $sou_type=strtoarray(array('1'=>'按仓库','2'=>'按小时','3'=>'按日','4'=>'按月'));
        $this->assign('sou_type',$sou_type);

        $map=$map_oi=array();
        $postdata=I('param.');
        $map_oi['order_id']=array('gt','0');
        if($postdata['shopid'])$map_oi['shop_id']= $postdata['shopid'];
        $now_time=strtotime(date('Y-m-d',NOW_TIME));
        if($postdata['reg_from'] || $postdata['reg_to']){
            $date_from = strtotime($postdata['reg_from']);
            if($date_from){$map_oi['sell_time'][] = array('egt',$date_from);}
            $date_to = strtotime($postdata['reg_to']);
            if($date_to){$date_to = $date_to+86399;$map_oi['sell_time'][] = array('lt',$date_to);}
            $postdata['dates']=0;
        }else{
            if(in_array($postdata['dates'],array(1,2,7,30))){
                $between=quick_time($postdata['dates']);
                $map_oi['sell_time']=array('between',array($between[0],$between[1]));
            }else{
                $postdata['dates']=99;
            }
        }
        $field="count(id) as num,sum(card_money) as card_money,sum(shop_price) as shop_price,shop_id,sell_time";
        switch($postdata['sou_value']){
            case 2://按小时
                for($i=0;$i<24;$i++){
                    $day_arr[$i] = $i; //处理时间数组
                }
                $list_t=M('GiftCard')->where($map_oi)->group("DATE_FORMAT(FROM_UNIXTIME(sell_time),'%k')")->field($field)->select();
                $orderinfo=M('GiftCard')->where($map_oi)->field($field)->find();
                $list_time=array();
                if($list_t){
                    foreach ($list_t as $k => $v) {
                        $list[date('G',$v['sell_time'])]['sell_time']=date('G',$v['sell_time']).'点';
                        $list[date('G',$v['sell_time'])]['num']=$v['num'];
                        $list[date('G',$v['sell_time'])]['shop_price']=$v['shop_price'];
                        $list[date('G',$v['sell_time'])]['card_money']=format_decimal($v['card_money']);
                        $list_time[]=date('G',$v['sell_time']);
                    }unset($k,$v);
                    $new_day=array_diff($day_arr,$list_time);
                    foreach($new_day as $vt){
                        $list[$vt]['sell_time']=$vt.'点';
                        $list[$vt]['card_money']=0.00;
                        $list[$vt]['shop_price']=0.00;
                        $list[$vt]['num']=0;
                    }
                    ksort($list);
                }
                break;
            case 3://按日
                for($i=1;$i<32;$i++){
                    $day_arr[$i] = $i; //处理时间数组
                }

                $list_t=M('GiftCard')->where($map_oi)->group("DATE_FORMAT(FROM_UNIXTIME(sell_time),'%e')")->field($field)->select();
                $orderinfo=M('GiftCard')->where($map_oi)->field($field)->find();
                $orderinfo['avg_price']=0;
                if($list_t){
                    foreach ($list_t as $k => $v) {
                        $list[date('j',$v['sell_time'])]['sell_time']=date('j',$v['sell_time']).'日';
                        $list[date('j',$v['sell_time'])]['num']=$v['num'];
                        $list[date('j',$v['sell_time'])]['shop_price']=$v['shop_price'];
                        $list[date('j',$v['sell_time'])]['card_money']=format_decimal($v['card_money']);
                        $list_time[]=date('j',$v['sell_time']);
                    }unset($k,$v);
                }
                $new_day=array_diff($day_arr,$list_time);
                foreach($new_day as $vt){
                    $list[$vt]['sell_time']=$vt.'日';
                    $list[$vt]['card_money']=0.00;
                    $list[$vt]['shop_price']=0.00;
                    $list[$vt]['num']=0;
                }
                ksort($list);
                break;
            case 4://月
                for($i=1;$i<13;$i++){
                    $day_arr[$i] = $i; //处理时间数组
                }
                $list_t=M('GiftCard')->where($map_oi)->group("DATE_FORMAT(FROM_UNIXTIME(sell_time),'%c')")->field($field)->select();
                $orderinfo=M('GiftCard')->where($map_oi)->field($field)->find();
                $orderinfo['avg_price']=0;
                if($list_t){
                    foreach ($list_t as $k => $v) {
                        $list[date('n',$v['sell_time'])]['sell_time']=date('n',$v['sell_time']).'月';
                        $list[date('n',$v['sell_time'])]['num']=$v['num'];
                        $list[date('n',$v['sell_time'])]['shop_price']=$v['shop_price'];
                        $list[date('n',$v['sell_time'])]['card_money']=format_decimal($v['card_money']);
                        $list_time[]=date('n',$v['sell_time']);
                    }unset($k,$v);
                }
                $new_day=array_diff($day_arr,$list_time);
                foreach($new_day as $vt){
                    $list[$vt]['sell_time']=$vt.'月';
                    $list[$vt]['num']=0;
                    $list[$vt]['shop_price']=0.00;
                    $list[$vt]['card_money']=0.00;
                }
                ksort($list);
                break;
            default://按店
                $shoplist=$shop_list;
                if($postdata['shopid']){
                    $map_s=array();
                    $map_s['id']=$postdata['shopid'];
                    $shoplist=M('Shops')->where($map_s)->getField('id,name');
                }
                $shop_ids=array_keys($shoplist);
                $postdata['sou_value']=1;

                $list_s=M('GiftCard')->where($map_oi)->group("shop_id")->field($field)->select();
                $orderinfo=M('GiftCard')->where($map_oi)->field($field)->find();
                $orderinfo['avg_price']=0;
                if($list_s){
                    foreach ($list_s as $k => $v) {
                        $list[$v['shop_id']]['shop_name']=$shop_list[$v['shop_id']]?$shop_list[$v['shop_id']]:'该仓库已删除或禁用('.$v['shop_id'].')';
                        $list[$v['shop_id']]['num']=$v['num'];
                        $list[$v['shop_id']]['shop_price']=format_decimal($v['shop_price']);
                        $list[$v['shop_id']]['card_money']=format_decimal($v['card_money']);
                        $list_time[]=$v['shop_id'];
                    }unset($k,$v);
                }
                $new_day=array_diff($shop_ids,$list_time);
                foreach($new_day as $vt){
                    $list[$vt]['shop_name']=$shop_list[$vt];
                    $list[$vt]['num']=0;
                    $list[$vt]['shop_price']=0.00;
                    $list[$vt]['card_money']=0.00;
                }
                break;

        }

        if($is_return){
            $return['list']=$list;
            $return['info']=$orderinfo;
            if($postdata['sou_value']==1){
                $return['title'] = array('仓库','卡数','销售额','面值');
            }else{
                $return['title'] = array('时间','卡数','销售额','面值');
            }

            return $return;
        }else{
            $this->assign($orderinfo);
            $this->assign($postdata);
            $this->assign('list',$list);
            $this->display();
        }
    }
/**
* 代金券导出
*
*/
    public function export(){
        $rest=$this->index(true);
        $data=array();
        $data=$rest['list'];
        $data[]=array('总计',$rest['info']['num'],$rest['info']['shop_price'],$rest['info']['card_money']);
        export_csv('礼品卡销售统计.csv',$rest['title'],$data);
        exit;
    }

}