统计30天订单 12周订单 6月订单

浏览:955 发布日期:2019/10/15 分类:技术分享 关键字: 数据统计 订单统计
//商户首页统计 
    public static function storeChart($sid){
        $orwhere = ['status'=>6,'store_id'=>$sid];
        $tarr = self::getTimeArr(30,12,6); //生成统计数组
        $arr = self::ChartData($tarr,$orwhere); //统计查询  数据组装
        $time = strtotime(date("Y-m"));//本月初时间

        //消费人数 平台会员数   菜品数量   订单数量(本月) 订单总数 销售金额    30日统计  12周统计  6月统计
        $arr['total_member'] = MemberModel::where('status',0)->count();
        $arr['consumer'] = orderModel::where('store_id',$sid)->group('mid')->count();
        $arr['product'] = ProductModel::where('store_id',$sid)->count();
        return $arr;
    }

//统计数据查询
    public static function ChartData($arr,$orwhere=[]){
        $data['day'] = self::getCharData($arr['day'],$arr['num']['daytime'],$orwhere,"from_unixtime(create_time, '%Y-%m-%d')");
        $data['week'] = self::getCharData($arr['week'],$arr['num']['weektime'],$orwhere,"from_unixtime(create_time, '%u')");
        $data['month'] = self::getCharData($arr['month'],$arr['num']['monthtime'],$orwhere,"from_unixtime(create_time, '%Y-%m')");
        return $data;
    }
//查询数据
    public static function getCharData($arr,$time,$orwhere,$where){
        $day = orderModel::field($where." as dat, count(*) as  num ,sum(price) as price")->group($where)->where($orwhere)->whereTime("create_time",'>=',$time)->order('dat','desc')->select()->toArray();
        $day =getArrayKey($day,'dat');
        foreach ($arr as $k => $v) {
            $data[$v] = isset($day[$v])?$day[$v]:0;
        }
        return $data;
    }

    //生成 统计时间数组 
    public static function getTimeArr($day=30,$week=12,$month=6){
        for ($i=0; $i < $day ; $i++) { 
            $data['day'][] = date("Y-m-d",strtotime("-".$i."day"));
            if ($i==($day-1))$data['num']['daytime'] = strtotime(date("Y-m-d",strtotime("-".$i." day")));
        }
        for ($i=0; $i < $week ; $i++) { 
            $data['week'][$i] = date("W",strtotime("-".$i."week"));
            if ($i==($week-1))$data['num']['weektime'] = strtotime(date("Y-m-d",strtotime("-".$i." week Monday")));
            // dump(date("Y-m-d",strtotime("-".$i." week Monday")));
        }
        for ($i=0; $i < $month ; $i++) { 
            $data['month'][] = date("Y-m",strtotime("-".$i."month"));
            if ($i==($month-1))$data['num']['monthtime'] = strtotime(date("Y-m",strtotime("-".$i." month")));
        }
        $data['num']['day'] = $data['day'][$day-1];
        $data['num']['week'] = $data['week'][$week-1];
        $data['num']['month'] = $data['month'][$month-1];
        return $data;
    }


//key 组装数组
function getArrayKey($arr,$key='id'){
    $data ="";
    foreach ($arr as $k => $v) {
        $data[$v[$key]] = $v;
    }
    return $data;
}
最佳答案
评论( 相关
后面还有条评论,点击查看>>