You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

324 lines
12 KiB

<?php
namespace App\Http\Controllers\System;
use App\Http\Controllers\Controller;
use App\Models\ExportFiles;
use App\Models\Multisys;
use App\Models\MultisysEntry;
use App\Models\MultisysEquipment;
use App\Models\OverSpeedRed;
use App\Models\OverSpeedRedEquipment;
use App\Models\ViolationParking;
use App\Models\ViolationParkingEquipment;
use Carbon\Carbon;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use ZipArchive;
use Illuminate\Support\Str;
class SystemController extends Controller
{
public function __construct()
{
}
public function getDashboardData(Request $request)
{
$device = explode(',', auth("api")->user()->device) ?? [1];
if ($request->has('location')) {
$location = $request->input('location');
}
if ($request->has('year')) {
$year = $request->input('year');
} else {
$year = Carbon::now('Asia/Taipei')->year;
}
if ($request->has('month')) {
$month = $request->input('month');
} else {
$month = Carbon::now('Asia/Taipei')->month;
}
#region 首頁儀錶板 - 總違規數 - 路口多功能
$data = Multisys::query();
$data->whereYear('datatime', $year);
if ($month != 0)
$data->whereMonth('datatime', $month);
if (isset($location))
$data->where('serialnumber', $location);
$data->whereIn('serialnumber', $device);
$data->groupBy('violationtype');
$data->select('violationtype', DB::raw('COUNT(*) as count'));
$data = $data->get();
$data = $data->toArray();
#endregion
#region 首頁儀錶板 - 總違規數 - 違規停車
$data2 = ViolationParking::query();
$data2->whereYear('datatime', $year);
if ($month != 0)
$data2->whereMonth('datatime', $month);
if (isset($location))
$data2->where('serialnumber', $location);
$data2->whereIn('serialnumber', $device);
$data2->groupBy('violationtype');
$data2->select('violationtype', DB::raw('COUNT(*) as count'));
$data2 = $data2->get();
$data2 = $data2->toArray();
#endregion
#region 首頁儀錶板 - 總違規數 - 路口多功能
$data3 = OverSpeedRed::query();
$data3->whereYear('datatime', $year);
if ($month != 0)
$data3->whereMonth('datatime', $month);
if (isset($location))
$data3->where('serialnumber', $location);
$data3->whereIn('serialnumber', $device);
$data3->groupBy('violationtype');
$data3->select('violationtype', DB::raw('COUNT(*) as count'));
$data3 = $data3->get();
$data3 = $data3->toArray();
#endregion
// $data = ViolationParking::groupBy('violationtype')->select('violationtype', DB::raw('COUNT(*) as count'))->get()->toArray();
// $data2 = Multisys::groupBy('violationtype')->select('violationtype', DB::raw('COUNT(*) as count'))->get()->toArray();
# merge array
$data = array_merge($data, $data2);
$data = array_merge($data, $data3);
$response = [];
foreach ($data as $key => $value) {
$response[] = [
'col' => 6,
'eventType' => $value['violationtype'],
'todayCount' => $value['count'],
'lastUpdate' => Carbon::now('Asia/Taipei')->toDateString(),
];
}
return response()->json(['data' => $response], 200);
}
public function getDashboardPie(Request $request)
{
$device = explode(',', auth("api")->user()->device) ?? [1];
if ($request->has('location')) {
$location = $request->input('location');
}
if ($request->has('year')) {
$year = $request->input('year');
} else {
$year = Carbon::now('Asia/Taipei')->year;
}
if ($request->has('month')) {
$month = $request->input('month');
} else {
$month = Carbon::now('Asia/Taipei')->month;
}
#region 首頁儀錶板 - 違規類型統計
$multisysQuery = Multisys::query();
$multisysQuery->whereYear('datatime', $year);
if ($month != 0)
$multisysQuery->whereMonth('datatime', $month);
if (isset($location))
$multisysQuery->where('serialnumber', $location);
$multisysQuery->whereIn('serialnumber', $device);
$multisysQuery->groupBy('violationtype');
$multisysQuery->select('violationtype', DB::raw('COUNT(*) as count'));
$data = $multisysQuery->get();
$data = $data->toArray();
$violationparkingQuery = ViolationParking::query();
$violationparkingQuery->whereYear('datatime', $year);
if ($month != 0)
$violationparkingQuery->whereMonth('datatime', $month);
if (isset($location))
$violationparkingQuery->where('serialnumber', $location);
$violationparkingQuery->whereIn('serialnumber', $device);
$violationparkingQuery->groupBy('violationtype');
$violationparkingQuery->select('violationtype', DB::raw('COUNT(*) as count'));
$data2 = $violationparkingQuery->get();
$data2 = $data2->toArray();
$overspeedredQuery = OverSpeedRed::query();
$overspeedredQuery->whereYear('datatime', $year);
if ($month != 0)
$overspeedredQuery->whereMonth('datatime', $month);
if (isset($location))
$overspeedredQuery->where('serialnumber', $location);
$overspeedredQuery->whereIn('serialnumber', $device);
$overspeedredQuery->groupBy('violationtype');
$overspeedredQuery->select('violationtype', DB::raw('COUNT(*) as count'));
$data3 = $overspeedredQuery->get();
$data3 = $data3->toArray();
# merge array
$data = array_merge($data, $data2);
$data = array_merge($data, $data3);
$response = [];
foreach ($data as $key => $value) {
$response[] = [
'violationtype' => $value['violationtype'],
'count' => $value['count'],
];
}
#endregion
return response()->json(['data' => $response], 200);
}
public function getDashboardBar(Request $request)
{
$device = explode(',', auth("api")->user()->device) ?? [1];
if ($request->has('location')) {
$location = $request->input('location');
}
if ($request->has('year')) {
$year = $request->input('year');
} else {
$year = Carbon::now('Asia/Taipei')->year;
}
if ($request->has('month')) {
$month = $request->input('month');
} else {
$month = Carbon::now('Asia/Taipei')->month;
}
#region 首頁儀錶板 - 違規類型統計
// 取得所有違規類型 存陣列
$violationTypes = Multisys::whereIn('serialnumber', $device)->groupBy('violationtype')->select('violationtype')->get()->toArray();
$violationTypes2 = ViolationParking::whereIn('serialnumber', $device)->groupBy('violationtype')->select('violationtype')->get()->toArray();
$violationTypes3 = OverSpeedRed::whereIn('serialnumber', $device)->groupBy('violationtype')->select('violationtype')->get()->toArray();
$violationTypes = array_merge($violationTypes, $violationTypes2);
$violationTypes = array_merge($violationTypes, $violationTypes3);
$violationTypes = array_column($violationTypes, 'violationtype');
// violationTypes key value 互換
$violationTypesKey = array_flip($violationTypes);
// dd($violationTypes);
// 初始化結果陣列
$response = [];
for ($hour = 0; $hour <= 23; $hour++) {
// 補零 01, 02, 03, ..., 09
// $hour = str_pad($hour, 2, '0', STR_PAD_LEFT);
// 初始化結果陣列
$response[$hour] = [
'category' => $hour,
];
for ($i = 0; $i < count($violationTypes); $i++) {
$response[$hour][$i] = 0;
}
}
$records = Multisys::query();
if ($year != 0)
$records->whereYear('datatime', $year);
if ($month != 0)
$records->whereMonth('datatime', $month);
if (isset($location))
$records->where('serialnumber', $location);
$records->whereIn('serialnumber', $device);
$data = $records->get();
$data = $data->groupBy(function ($item, $key) {
return Carbon::parse($item->datatime)->format('H');
})->map(function ($item, $key) use (&$response, $violationTypesKey) {
$item->groupBy('violationtype')->map(function ($item, $viotype) use ($key, &$response, $violationTypesKey) {
$response[intval($key)][$violationTypesKey[$viotype]] = $item->count();
});
});
$records = ViolationParking::query();
if ($year != 0)
$records->whereYear('datatime', $year);
if ($month != 0)
$records->whereMonth('datatime', $month);
if (isset($location))
$records->where('serialnumber', $location);
$records->whereIn('serialnumber', $device);
$data = $records->get();
$data = $data->groupBy(function ($item, $key) {
return Carbon::parse($item->datatime)->format('H');
})->map(function ($item, $key) use (&$response, $violationTypesKey) {
$item->groupBy('violationtype')->map(function ($item, $viotype) use ($key, &$response, $violationTypesKey) {
$response[intval($key)][$violationTypesKey[$viotype]] = $item->count();
});
});
#endregion
return response()->json(['data' => $response, 'violationtype' => $violationTypes], 200);
}
public function index()
{
// 取得所有的機號 地點 從設備表
$device = explode(',', auth()->user()->device) ?? [1];
$devices = [];
// array merge MultisysEquipment::select('serialnumber', 'location')->get();
// violationparking
$devices = array_merge($devices, MultisysEquipment::select('serialnumber', 'location')->whereIn('serialnumber', $device)->get()->toArray());
$devices = array_merge($devices, OverSpeedRedEquipment::select('serialnumber', 'location')->whereIn('serialnumber', $device)->get()->toArray());
$devices = array_merge($devices, ViolationParkingEquipment::select('serialnumber', 'location')->whereIn('serialnumber', $device)->get()->toArray());
return view('system.dashboard', compact('devices'));
}
public function getViolationImage($path)
{
$filePath = str_replace('*', '/', $path);
// dd($filePath);
if (Storage::disk('local')->exists($filePath)) {
$content = Storage::get($filePath);
return response($content)->header("Content-Type", "image");
} else {
return abort(404);
}
}
function getViolationVideo($path)
{
ini_set('memory_limit', '512M'); // 擴充記憶體,否則 Load 影片會爆掉
$filePath = str_replace('*', '/', $path);
// return view('video_faster')->with('path',$filePath);
//return response()->view('video_test')->with('path',$filePath);
if (Storage::disk('local')->exists($filePath)) {
$content = Storage::get($filePath);
return response($content)->header("Content-Type", "video/mp4"); //video/mp4
} else {
return abort(404);
}
}
public function Setting()
{
return view('system.setting.setting');
}
public function Export(Request $request, $fileName)
{
$file = ExportFiles::where('name', $fileName)->first();
$filePath = storage_path('app/public/exports/' . $fileName);
if (file_exists($filePath)) {
return response()->download($filePath, $file->remark . '.' . $file->type);
} else {
return response()->json(['error' => '檔案不存在']);
}
}
public function downloadZip(){
$zip = new ZipArchive;
$rand = Str::random(5);
$fileName = "_$rand.zip";
if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE) {
$zip->addFile(public_path().'/favicon.ico', "favicon.ico");
$zip->close();
}
return response()->download(public_path($fileName));
}
}