HomepageController.php 6.73 KB
Newer Older
冯超鹏's avatar
冯超鹏 committed
1 2 3 4 5 6 7 8 9 10
<?php

namespace App\Http\Controllers;

use App\Http\Resources\PermissionResource;
use App\Http\Requests\UsersRequest;
use App\Http\Resources\UserResource;
use App\Laravue\JsonResponse;
use App\Laravue\Models\Device;
use App\Laravue\Models\Users;
11
use App\Laravue\Models\Homepage;
冯超鹏's avatar
冯超鹏 committed
12 13 14 15 16 17 18 19
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Redis;
use Validator;
20

冯超鹏's avatar
冯超鹏 committed
21 22 23 24 25 26 27 28 29
class HomepageController extends Controller
{
    //返回用户数量 设备数量, 报警数量 ,
    public function homepagecount()
    {
        $usercount = Users::count();
        $devicecount = Device::count();
        $devicepolice = Device::where('devicepolice', '>', '1')->count();
        $t = time();//当前时间
30 31 32 33 34 35 36 37 38
        $start = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));//今天的开始
        $end = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t));//今天的结束
        $timedevicepolice = DB::table('reportpolice')->where('status', '=', '1')->whereBetween('starttime', [$start, $end])->count();
        return $this->jsonSuccessData(['usercount' => $usercount, 'devicecount' => $devicecount, 'devicepolice' => $devicepolice, 'timedevicepolice' => $timedevicepolice]);
    }

    public function gettimeline()
    {
        return $this->jsonSuccessData(DB::table('timeline')->orderByDesc('id')->get());
冯超鹏's avatar
冯超鹏 committed
39 40 41
    }

    //返回一年当前月份
42 43 44
    public function devicemonthcount()
    {
        $year_start = strtotime(date("Y") . "-01-01"); // 获取当前的1月份的时间戳
冯超鹏's avatar
冯超鹏 committed
45
        $endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y')); //获取当月的时间戳
46
        $devicedata = Device::whereBetween('deviceaddtime', [$year_start, $endThismonth])
冯超鹏's avatar
冯超鹏 committed
47 48 49 50
            ->selectRaw('from_unixtime(deviceaddtime,"%Y-%m") as date,COUNT(id) as value')
            ->groupBy('date')
            ->get()
            ->toArray();
冯超鹏's avatar
冯超鹏 committed
51 52
        $policedata = Users::whereBetween('created_at', [date('Y-m-d H:i:s',$year_start), date('Y-m-d H:i:s',$endThismonth)])
            ->selectRaw('from_unixtime(unix_timestamp(created_at),"%Y-%m") as date,COUNT(id) as value')
冯超鹏's avatar
冯超鹏 committed
53 54 55
            ->groupBy('date')
            ->get()
            ->toArray();
56 57
        $devicepolice = Device::whereBetween('deviceaddtime', [$year_start, $endThismonth])
            ->where('devicepolice', '>', 1)
冯超鹏's avatar
冯超鹏 committed
58 59 60 61
            ->selectRaw('from_unixtime(deviceaddtime,"%Y-%m") as date,COUNT(id) as value')
            ->groupBy('date')
            ->get()
            ->toArray();
62
        return $this->jsonSuccessData(['devicedata' => $this->datamonth($devicedata), 'policedata' => $this->datamonth($policedata), 'devicepolice' => $this->datamonth($devicepolice)]);
冯超鹏's avatar
冯超鹏 committed
63 64
    }

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    //用户统计
    public function UserStatistics (){
        $devicecount = Device::where('uid','=',Auth::id())->count();
        $devicepolice = Device::where('devicepolice', '>', '1')->where('uid','=',Auth::id())->count();
        $t = time();//当前时间
        $start = mktime(0, 0, 0, date("m", $t), date("d", $t), date("Y", $t));//今天的开始
        $end = mktime(23, 59, 59, date("m", $t), date("d", $t), date("Y", $t));//今天的结束
        $timedevicepolice = DB::table('reportpolice as r')
            ->join('device as d','d.devicenum','=','r.devicenumber')
            ->where('d.uid','=',Auth::id())
            ->where('r.status', '=', '1')
            ->whereBetween('r.starttime', [$start, $end])
            ->count();
        return $this->jsonSuccessData(['devicecount' => $devicecount, 'devicepolice' => $devicepolice, 'timedevicepolice' => $timedevicepolice]);
    }

    //返回一年当前月份
82
    public function userdevicemonthcount(Request $request)
83
    {
84
        \Session::put(['loginuid' => Auth::id()]);
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        $year_start = strtotime(date("Y") . "-01-01"); // 获取当前的1月份的时间戳
        $endThismonth = mktime(23, 59, 59, date('m'), date('t'), date('Y')); //获取当月的时间戳
        $devicedata = Device::where('uid','=',Auth::id())->whereBetween('deviceaddtime', [$year_start, $endThismonth])
            ->selectRaw('from_unixtime(deviceaddtime,"%Y-%m") as date,COUNT(id) as value')
            ->groupBy('date')
            ->get()
            ->toArray();
        $devicepolice = Device::where('uid','=',Auth::id())->whereBetween('deviceaddtime', [$year_start, $endThismonth])
            ->where('devicepolice', '>', 1)
            ->selectRaw('from_unixtime(deviceaddtime,"%Y-%m") as date,COUNT(id) as value')
            ->groupBy('date')
            ->get()
            ->toArray();
        return $this->jsonSuccessData(['devicedata' => $this->datamonth($devicedata), 'devicepolice' => $this->datamonth($devicepolice)]);
    }


//    项目统计
    public function Project_statistics()
    {
        $start_time = microtime(true);
        $memory = (!function_exists('memory_get_usage')) ? '0' : round(memory_get_usage() / 1024 / 1024, 2);
        $unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PD');
        $phay = base_path();
        $size = $this->getDirSize($phay);
        $base_path = @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2);
        $end_time = microtime(true);
        return $this->jsonSuccessData(['memory' => round($memory), 'base_path' => round($base_path) / 10, 'end_time' => round($end_time - $start_time), 'dabatime' => ceil($this->dabadas()),'rand'=>rand(1,100)]);

    }

    //获取接口访问时间
    private function datamonth($monthdata = []): array
    {
        $year = date('Y', time());
        $month = date('m', time());
冯超鹏's avatar
冯超鹏 committed
121 122 123 124 125 126 127 128 129 130 131 132
        $data = [];
        foreach ($monthdata as $value) {
            $data[$value['date']] = $value['value'];
        }
        $out = [];
        for ($i = 1; $i <= $month; $i++) {
            $mont = $i < 10 ? "0{$i}" : $i;
            $val = isset($data["{$year}-{$mont}"]) ? $data["{$year}-{$mont}"] : 0;
            array_push($out, $val);
        }
        return $out;
    }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

    private function getDirSize($dir)
    {
        @$dh = opendir($dir);
        $size = 0;
        while ($file = @readdir($dh)) {
            if ($file != "." and $file != "..") {
                $path = $dir . "/" . $file;
                if (is_dir($path)) {
                    $size += $this->getDirSize($path);
                } elseif (is_file($path)) {
                    $size += filesize($path);
                }
            }
        }
        @closedir($dh);
        return $size;
    }

    private function dabadas()
    {
        $start_time = microtime(true);
        DB::table('device')->select();
        DB::table('timeline')->select();
        DB::table('BackgroundUser')->select();
        $end_time = microtime(true);
        return $end_time - $start_time;
    }

冯超鹏's avatar
冯超鹏 committed
162
}