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.
 
 
 

897 lines
40 KiB

<?php
namespace App\Http\Controllers\System;
use App\Class\LogWriter;
use App\Exports\ArrayExport;
use App\Http\Controllers\Controller;
use App\Models\ExportFiles;
use App\Models\OverSpeedRed;
use App\Models\OverSpeedRedEquipment;
use App\Models\ViolationLaw;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Str;
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Gd\Driver;
use ZipArchive;
class OverSpeedController extends Controller
{
public $destPath_root;
public $clientDestPath_root;
public $unreportPath_root;
public $month;
function __construct()
{
$this->middleware('permission:overspeed-review|overspeed-reduction|overspeed-device-setting|overspeed-statistics|overspeed-analysis|overspeed-read', ['only' => ['Review']]);
$this->middleware('permission:overspeed-review|permission:overspeed-reduction', ['only' => ['update']]);
$this->middleware('permission:overspeed-device-setting', ['only' => ['DeviceSetting']]);
$this->middleware('permission:overspeed-statistics', ['only' => ['Statistics']]);
$this->middleware('permission:overspeed-analysis', ['only' => ['Analysis']]);
// OK path
$this->destPath_root = 'D:\\OK\\OverSpeed\\';
$this->clientDestPath_root = 'Y:\\';
// NOK path
$this->unreportPath_root = 'C:\\xampp\\SMMS\\storage\\app\\ParsingFiles\\NOK\\OverSpeed\\';
$this->month['start'] = Carbon::now()->subDays(8)->format('Y-m-d');
$this->month['current'] = Carbon::now()->subDays(1)->format('Y-m-d');
}
public function Review()
{
$device = explode(',', auth()->user()->device) ?? [1];
// dd($device);
$equipmentData = new OverSpeedRedEquipment();
$serialNumber = $equipmentData::whereIn('serialnumber', $device)->groupBy('serialnumber')->get(['serialnumber', 'location']);
$violationparking = new OverSpeedRed();
$locationtype = $violationparking::where('processcheck', 0)->whereIn('violationtype', ['超速'])->whereIn('serialnumber', $device)->groupBy('location')->pluck('location');
// dd($data,$page_data);
$vio_arr = [];
foreach (ViolationLaw::whereIn('type', ['超速'])->get() as $key => $item) {
$vio_arr[$key]['violationcode'] = $item->violationcode;
$vio_arr[$key]['display_name'] = $item->display_name;
}
// dd($vio_arr);
return view('system.overspeed.index')
->with('serialNumber', $serialNumber)
->with('locationtype', $locationtype)
->with('vio_arr', $vio_arr)
->with('month', $this->month);
}
#region 已審 View
public function Censored(Request $request)
{
$device = explode(',', auth()->user()->device) ?? [1];
$equipmentData = new OverSpeedRedEquipment();
$serialNumber = $equipmentData::whereIn('serialnumber', $device)->groupBy('serialnumber')->get(['serialnumber', 'location']);
$violationparking = new OverSpeedRed();
$locationtype = $violationparking::where('processcheck', 1)->whereIn('violationtype', ['超速'])->whereIn('serialnumber', $device)->groupBy('location')->pluck('location');
// dd($data,$page_data);
return view('system.overspeed.manage')
->with('serialNumber', $serialNumber)
->with('locationtype', $locationtype)
->with('month', $this->month);
}
#endregion
#region 不舉發 View
public function Unreport(Request $request)
{
$device = explode(',', auth()->user()->device) ?? [1];
$equipmentData = new OverSpeedRedEquipment();
$serialNumber = $equipmentData::whereIn('serialnumber', $device)->groupBy('serialnumber')->get(['serialnumber', 'location']);
$violationparking = new OverSpeedRed();
$locationtype = $violationparking::where('processcheck', 2)->whereIn('violationtype', ['超速'])->whereIn('serialnumber', $device)->groupBy('location')->pluck('location');
$unreportreason = $violationparking->where('processcheck', 2)->whereIn('violationtype', ['超速'])->whereIn('serialnumber', $device)->groupBy('unreportreason')->pluck('unreportreason');
// dd($locationtype,$unreportreason);
return view('system.overspeed.unreport')
->with('serialNumber', $serialNumber)
->with('locationtype', $locationtype)
->with('unreportreason', $unreportreason)
->with('month', $this->month);
}
#endregion
public function mergePic(Request $request)
{
$path = $request->path;
$image = $request->image;
// dd($path,$image);
$data = OverSpeedRed::where('picture', 'like', '%' . $path . '%')->first();
// dd($path,$image,$data);
if ($data) {
// 使用 Intervention Image 套件開啟圖片檔案
$manager = new ImageManager(new Driver());
$photoPath = str_replace('*', '\\', storage_path('app' . '\\' . 'ParsingFiles' . '\\' . "$data->picture"));
$originalImage = $manager->read($photoPath);
// $originalImage = Image::read($photoPath);
$cnum = "";
// 取得前端傳來的 base64 圖片資料
$croppedImage = $manager->read($request->image);
// $croppedImage = Image::read($request->image);
$croppedImage->resize($croppedImage->width() * 4, $croppedImage->height() * 4);
// 獲取圖片的寬度和高度
$originalWidth = $originalImage->width();
$originalHeight = $originalImage->height();
$croppedWidth = $croppedImage->width();
$croppedHeight = $croppedImage->height();
// dd($originalWidth, $originalHeight, $croppedWidth, $croppedHeight, $request->position);
// 將裁切後的圖片合成於原圖
// 如果 $request->position 為 1,則將圖片合成於左下角,2 為右下角,3 為左上角,4 為右上角
switch ($request->position) {
case 1:
$originalImage->place($croppedImage, 'bottom-left');
break;
case 2:
$originalImage->place($croppedImage, 'bottom-right');
break;
case 3:
$originalImage->place($croppedImage, 'top-left', 0, 210);
break;
case 4:
$originalImage->place($croppedImage, 'top-right', 0, 210);
break;
default:
$originalImage->place($croppedImage, 'bottom-left');
break;
}
// $originalImage->place($croppedImage, 'bottom-left', 0, 0);
// 儲存合成後的圖片檔案
$photoName = explode('*', $data['picture']);
$photoName = end($photoName);
$time = str_replace('-', '', explode(' ', $data['datatime'])[0]);
$violationtype = $data['violationtype'];
$photoPath = public_path("merge\\$violationtype\\$time\\$photoName");
if (is_dir(public_path("merge\\$violationtype\\$time")) === false) {
mkdir(public_path("merge\\$violationtype\\$time"), 0777, true);
}
$originalImage->save($photoPath);
// $originalImage->save(public_path('merge\\' . $path));
// 將合成後的圖片路徑存入資料庫
// $data->mergepic = $this->merge_root.$path;
// $data->save();
return response()->json(['path' => "/merge/$violationtype/$time/$path", 'carnubmer' => $cnum], 200);
}
return response()->json(['path' => 'error'], 400);
}
public function update(Request $request, $id)
{
$data = OverSpeedRed::find($id);
// 先確認 processcheck 狀態 0 修改 1 舉發 2 不舉發
// 確認使用者是否可以 review
DB::beginTransaction();
try {
// 如果使用者具 審查權限
if (auth('api')->user()->can('overspeed-review')) {
// 修改 processcheck = 0
if ($request->processcheck == 0) {
$data->carnumber = $request->carnumber ?? $data->carnumber;
$data->violationtype = $request->violationtype ?? $data->violationtype;
$data->cartype = $request->cartype ?? $data->cartype;
$data->violationcode = $request->viocode ?? $data->violationcode;
$logData = [
'action' => 'edit',
'action_detail' => '編輯案件',
'ip' => request()->ip(),
'remark' => "編輯闖紅/超速案件: $data->carnumber ,案件日期: $data->datatime",
];
}
// 舉發 processcheck = 1
elseif ($request->processcheck == 1) {
if ($request->carnumber == null) {
return response()->json(['error' => '請輸入車牌號碼']);
}
if (isset($request->viocode)) {
if ($request->viocode == null)
return response()->json(['error' => '請選擇法條代碼']);
// 如果法條代碼不再資料表中
if (!ViolationLaw::where('violationcode', $request->viocode)->exists()) {
return response()->json(['error' => '此法條代碼不存在']);
}
} else {
return response()->json(['error' => '請選擇法條代碼']);
}
$this->updateIni($data->picture, $data->carnumber, "$request->carnumber", $data->violationtype, $request->violationtype);
$data->carnumber = $request->carnumber ?? $data->carnumber;
$data->violationtype = $request->violationtype ?? $data->violationtype;
$data->cartype = $request->cartype ?? $data->cartype;
$data->violationcode = $request->viocode ?? $data->violationcode;
$data->processcheck = 1;
$logData = [
'action' => 'review',
'action_detail' => '舉發案件',
'ip' => request()->ip(),
'remark' => "舉發闖紅/超速案件: $data->carnumber ,案件日期: $data->datatime",
];
$pth = explode('*', $data->picture);
$photoPath = str_replace('*', '\\', storage_path('app' . '\\' . 'ParsingFiles' . '\\' . "$data->picture"));
$pth = end($pth);
$violationtype = $data->violationtype;
$time = str_replace('-', '', explode(' ', $data->datatime)[0]);
// public_path('merge\\'.$path)
$mgpath = public_path("merge\\$violationtype\\$time\\$pth");
if (!file_exists($mgpath)) {
if (is_dir(public_path("merge\\$violationtype\\$time")) === false) {
mkdir(public_path("merge\\$violationtype\\$time"), 0777, true);
}
copy($photoPath, $mgpath);
}
$aaData['picture'] = $data->picture;
$aaData['picture2'] = $data->picture2;
$aaData['datatime'] = $data->datatime;
$aaData['violationtype'] = $data->violationtype;
$this->saveFinishFile($aaData);
}
// 不舉發 processcheck = 2
elseif ($request->processcheck == 2) {
$data->carnumber = $request->carnumber ?? $data->carnumber;
$data->violationtype = $request->violationtype ?? $data->violationtype;
$data->cartype = $request->cartype ?? $data->cartype;
$data->unreportreason = $request->unreportreason ?? $data->unreportreason;
$data->processcheck = 2;
$logData = [
'action' => 'review',
'action_detail' => '不舉發案件',
'ip' => request()->ip(),
'remark' => "不舉發闖紅/超速案件: $data->carnumber ,案件日期: $data->datatime",
];
}
// 還原 processcheck = 3
elseif ($request->processcheck == 3) {
$data->violationcode = null;
$data->unreportreason = '';
$data->processcheck = 0;
$logData = [
'action' => 'reduction',
'action_detail' => '還原案件',
'ip' => request()->ip(),
'remark' => "還原闖紅/超速案件: $data->carnumber ,案件日期: $data->datatime",
];
}
}
// 紀錄審查者 帳號
$data->jsoncheck = auth('api')->user()->account;
// 資料儲存
$data->save();
// 紀錄 LOG
LogWriter::writeLog($logData, 'api');
DB::commit();
// all good
return response()->json(['success' => $logData['remark']]);
} catch (\Exception $e) {
DB::rollback();
return response()->json(['error' => '系統錯誤']);
// something went wrong
}
}
#region DataTable (AJAX刷新用 參數 processcheckStatus 0未審 1已審 2不舉發 "99清冊用")使用
public function getDataTable(Request $request)
{
// dd($request->all());
#region 設備限制
$device = explode(',', auth("api")->user()->device) ?? [1];
// dd($device);
#endregion
$processcheckStatus = $request->processcheckStatus;
// dd($processcheckStatus, $device);
[$draw, $totalRecords, $totalRecordswithFilter, $data_arr] = $this->getData($device, $request, $processcheckStatus);
$response = array(
"draw" => intval($draw),
"iTotalRecords" => $totalRecords,
"iTotalDisplayRecords" => $totalRecordswithFilter,
"aaData" => $data_arr
);
// dd($response);
echo json_encode($response);
exit;
}
#endregion
#region 取得資料
function getData($device, $request, $processcheckStatus = null)
{
// 審查狀態 0 未審 1 已審查 2 不舉發 99清冊
// $processcheckStatus = 2;
// dd($device, $request, $processcheckStatus);
#region Request 搜尋值
if (isset($request->searchByFromdate))
$searchByFromdate = $request->searchByFromdate;
if (isset($request->searchByTodate))
$searchByTodate = $request->searchByTodate;
//地點搜尋
if (isset($request->location))
$location = $request->location;
//車牌搜尋
if (isset($request->carnumber))
$carnumber = $request->carnumber;
//不舉發理由搜尋
if (isset($request->unreportreason))
$unreportreason = $request->unreportreason;
#endregion
#region DataTable 搜尋屬性
if (!isset($request->export)) {
$draw = $request->get('draw');
$start = $request->get("start");
$rowperpage = $request->get("length"); // Rows display per page
$columnIndex_arr = $request->get('order');
$columnName_arr = $request->get('columns');
$order_arr = $request->get('order');
$search_arr = $request->get('search');
$columnIndex = $columnIndex_arr[0]['column']; // Column index
$columnName = $columnName_arr[$columnIndex]['data']; // Column name
$columnSortOrder = $order_arr[0]['dir']; // asc or desc
$searchValue = $search_arr['value']; // Search value
} else {
$draw = 0;
}
#endregion
// Fetch records
$records = OverSpeedRed::query();
$records->whereIn('violationtype', ['超速']);
if (isset($columnName))
$records->orderBy($columnName, $columnSortOrder);
if (isset($searchValue))
$records->where('carnumber', 'like', '%' . $searchValue . '%');
//已審未審
//清冊用 99
$records->where('violationtype', '!=', '違規臨時停車');
if ($processcheckStatus == 99) {
$processcheckStatus = $request->processCheck;
if ($processcheckStatus == 99 || $processcheckStatus == null)
$records->whereIn('processcheck', [0, 1, 2]);
else {
$records->where('processcheck', $processcheckStatus);
}
} else {
$records->where('processcheck', $processcheckStatus);
}
//設備限制
$records->whereIn('serialnumber', $device);
//時間限制
if (isset($searchByFromdate))
$records->where('datatime', ">", $searchByFromdate . ' 00:00:00');
if (isset($searchByTodate))
$records->where('datatime', "<", $searchByTodate . ' 23:59:59');
//地點篩選
if (isset($location))
$records->where('serialnumber', $location);
//車牌篩選
if (isset($carnumber))
$records->where('carnumber', 'like', '%' . $carnumber . '%');
//不舉發理由
if (isset($unreportreason))
$records->where('unreportreason', 'like', '%' . $unreportreason . '%');
$totalRecords = $records->count();
$totalRecordswithFilter = $records->count();
//如果有報表類型(1,2,3,...),並需要特別處理欄位的報表
if (isset($request->statisticstype)) {
if ($request->statisticstype == 2)
$records->groupBy('serialnumber', 'cartype')->select('*', DB::raw('count(*) as count'));
if ($request->statisticstype == 3) {
$groupbyPara1 = "CAST(DATE_FORMAT(datatime, '%H') AS INT)";
$records->groupBy(DB::raw($groupbyPara1), 'serialnumber', 'violationtype')->select('*', DB::raw('COUNT(*) as count, CONCAT(CAST(DATE_FORMAT(datatime,"%H") as INT),"-",CAST(DATE_FORMAT(DATE_ADD(datatime,INTERVAL +1 HOUR),"%H") as INT)) AS period'));
$records->orderBy(DB::raw($groupbyPara1), 'ASC');
}
} else {
$records->select('*');
}
if (!isset($request->export)) {
if (isset($start))
$records->skip($start);
if (isset($rowperpage))
$records->take($rowperpage);
}
$records = $records->get();
$data_arr = array();
// $sno = $start + 1;
foreach ($records as $record) {
//還原用id
$id = $record->id;
//不舉發原因
$unreportreason = $record->unreportreason;
$datatime = $record->datatime;
$serialnumber = $record->serialnumber;
$location = $record->location;
$carnumber = $record->carnumber;
$picture = $record->picture;
$picture2 = $record->picture2;
$violationtype = $record->violationtype;
$violationcode = $record->violationcode;
$cartype = $record->cartype;
$unreportpicture = $record->unreportpicture;
$processcheck = $record->processcheck;
$postcheck = $record->postcheck ?? 0;
//報表用的欄位,避免前端錯誤,給預設值
$count = $record->count ?? 0;
$period = $record->period ?? "";
$cartypeTitle = [
'1' => "汽車",
'2' => "機車",
'3' => "重型機車",
'4' => "輕型機車",
'8' => "微型電動二輪車"
];
$processCheckTitle = [
'0' => "未審",
'1' => "已審查",
'2' => "不舉發"
];
if (!isset($cartype))
$cartype = 1;
// 如果車牌 = None 則 顯示 ""
if ($carnumber == "None")
$carnumber = "";
if ($violationcode == null) {
if ($record->speed != 0) {
// 去除 km/h
$speed = explode('km/h', $record->speed)[0];
$speed = (int) $speed;
// 去除 km/h
$limitspeed = explode('km/h', $record->limitspeed)[0];
$limitspeed = (int) $limitspeed;
// 超速
// 如果 speed > $record->limitspeed 20以內法條 4000005 20~40 4000006 40~60 4000007
if ($speed > $limitspeed) {
$violationcode = 4000005;
if ($speed > $limitspeed + 20) {
$violationcode = 4000006;
if ($speed > $limitspeed + 40) {
$violationcode = 4000007;
}
}
}
}
}
$pthar = explode('*', $record->picture);
$pth = $pthar[count($pthar) - 1];
$time = str_replace('-', '', explode(' ', $datatime)[0]);
// public_path('merge\\'.$path)
$mgpath = public_path("merge\\$violationtype\\$time\\$pth");
$mergepic = 0;
if (file_exists($mgpath)) {
$mergepic = "merge/$violationtype/$time/$pth";
}
if ($violationcode != null) {
$display_name = ViolationLaw::where('violationcode', $violationcode)->first()->display_name ?? '';
$violationdata = "[$violationcode]" . $display_name;
// $violationdata = "[$violationcode]" . ViolationLaw::where('violationcode', $violationcode)->first()->display_name;
}
$data_arr[] = array(
"id" => $id,
"datatime" => $datatime,
"serialnumber" => $serialnumber,
"location" => $location,
"carnumber" => $carnumber,
"picture" => $picture,
"picture2" => $picture2,
//影片連結 (用照片的路徑取代成mp4 picture= _A picture2= _B)
"video" => str_replace('.jpg', '.mp4', $picture),
"violationtype" => $violationtype,
"violationcode" => $violationcode ?? '',
"violationdata" => $violationdata ?? '',
"lawType" => $record->lawType ?? '',
"cartype" => $cartype,
"carkind" => $cartypeTitle[$cartype],
"speed" => $record->speed ?? '',
"unreportreason" => $unreportreason ?? '',
"unreportpicture" => $unreportpicture ?? $picture,
"mergepic" => $mergepic ?? '0',
"postcheck" => $postcheck,
// 清冊用
"processcheck" => $processCheckTitle[$processcheck],
// "searchByFromdate" => $searchByFromdate,
// "searchByTodate"=> $searchByTodate,
"count" => $count,
"period" => $period
);
}
return [$draw, $totalRecords, $totalRecordswithFilter, $data_arr];
}
#endregion
// 清冊用
public function Statistics()
{
$device = explode(',', auth()->user()->device) ?? [1];
// 違規地點 group by
$equipmentData = new OverSpeedRedEquipment();
$serialNumber = $equipmentData::whereIn('serialnumber', $device)->groupBy('serialnumber')->get(['serialnumber', 'location']);
// 違規類型 group by
$violationparking = new OverSpeedRed();
$violationtype = $violationparking::whereIn('serialnumber', $device)->whereIn('violationtype', ['超速'])->groupBy('violationtype')->pluck('violationtype');
return view('system.overspeed.statistics')
->with('serialNumber', $serialNumber)
->with('violationtype', $violationtype);
}
public function getStatisticsData(Request $request)
{
#region 設備限制
$device = explode(',', auth("api")->user()->device) ?? [1];
#endregion
$processcheckStatus = 99;
[$draw, $totalRecords, $totalRecordswithFilter, $data_arr] = $this->getData($device, $request, $processcheckStatus);
$response = array(
"draw" => intval($draw),
"iTotalRecords" => $totalRecords,
"iTotalDisplayRecords" => $totalRecordswithFilter,
"aaData" => $data_arr
);
return $response;
}
public function getStatisticsDataExport(Request $request)
{
#region 設備限制
$device = explode(',', auth("api")->user()->device) ?? [1];
#endregion
$processcheckStatus = 99;
if ($request->statisticstype == 1) {
$remark = '違規件數清冊';
$columns = ['datatime', 'serialnumber', 'location', 'carnumber', 'violationtype', 'processcheck'];
$columnTitle = [
["【表單名稱】:", $remark],
["【違規日期區間】:", $request->searchByFromdate, " ~ ", $request->searchByTodate],
['違規時段', '設備編號', '違規地點', '車號', '違規類型', '審查狀態']
];
} elseif ($request->statisticstype == 2) {
$remark = '違規件數統計清冊';
$columns = ['serialnumber', 'location', 'violationtype', 'carkind', 'count'];
$columnTitle = [
["【表單名稱】:", $remark],
["【違規日期區間】:", $request->searchByFromdate, " ~ ", $request->searchByTodate],
['設備編號', '違規地點', '違規類型', '車種', '統計']
];
} elseif ($request->statisticstype == 3) {
$remark = '違規時段件數統計清冊';
$columns = ['serialnumber', 'location', 'period', 'violationtype', 'count'];
$columnTitle = [
["【表單名稱】:", $remark],
["【違規日期區間】:", $request->searchByFromdate, " ~ ", $request->searchByTodate],
['設備編號', '違規地點', '違規時段', '違規類型', '統計']
];
}
[$draw, $totalRecords, $totalRecordswithFilter, $data_arr] = $this->getData($device, $request, $processcheckStatus);
$data = array_map(function ($row) use ($columns) {
return array_merge(array_flip($columns), array_intersect_key($row, array_flip($columns)));
}, $data_arr);
$fileName = 'overspeed-' . Str::random(10) . '.xlsx';
ExportFiles::create([
'name' => $fileName,
'path' => 'public/exports',
'type' => 'xlsx',
'status' => '1',
'remark' => "路口多功能-$remark",
'user_id' => auth('api')->user()->id,
]);
Excel::store(new ArrayExport($data, $columnTitle), 'public/exports/' . $fileName, 'local', \Maatwebsite\Excel\Excel::XLSX);
return response()->json(['url' => route('overspeed-export', ['fileName' => $fileName])], 200);
}
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' => '檔案不存在']);
}
}
// 數據分析 vpkAnalysis
public function Analysis()
{
$device = explode(',', auth()->user()->device) ?? [1];
// 違規地點 group by
$equipmentData = new OverSpeedRedEquipment();
$serialNumber = $equipmentData::whereIn('serialnumber', $device)->groupBy('serialnumber')->get(['serialnumber', 'location']);
// 違規類型 group by
$violationparking = new OverSpeedRed();
$violationtype = $violationparking::whereIn('serialnumber', $device)->whereIn('violationtype', ['超速'])->groupBy('violationtype')->pluck('violationtype');
return view('system.overspeed.analysis')
->with('serialNumber', $serialNumber)
->with('violationtype', $violationtype);
}
public function getAnalysisData(Request $request)
{
$device = explode(',', auth("api")->user()->device) ?? [1];
$records = OverSpeedRed::query();
$records->whereIn('serialnumber', $device)->whereIn('violationtype', ['超速'])->whereIn('processcheck', [1, 3]);
// dd($request->all());
if (isset($request->search_fromdate))
$records->where('datatime', ">", $request->search_fromdate . ' 00:00:00');
if (isset($request->search_todate))
$records->where('datatime', "<", $request->search_todate . ' 23:59:59');
//地點搜尋
if (isset($request->location))
$records->where('serialnumber', $request->location);
if (isset($request->violation_type))
$records->where('violationtype', $request->violation_type);
$data = $records->get();
if (isset($request->chart_type)) {
if ($request->chart_type == '1') {
// 根據違規類型分類 並計算每一類的總數
$data = $data->groupBy('violationtype')->map(function ($item, $key) {
return [
'title' => $key,
'times' => $item->count()
];
})->toArray();
$data = array_values($data);
} elseif ($request->chart_type == '2') {
// 根據違規類型分類 並計算每一類的總數
$data = $data->groupBy('location')->map(function ($item, $key) {
return [
'title' => $key,
'times' => $item->count()
];
})->toArray();
$data = array_values($data);
} elseif ($request->chart_type == '3') {
$records2 = OverSpeedRed::query();
$records2->whereIn('serialnumber', $device)->whereIn('processcheck', [1, 3]);
// dd($request->all());
if (isset($request->search_fromdate))
$records2->where('datatime', ">", $request->search_fromdate . ' 00:00:00');
if (isset($request->search_todate))
$records2->where('datatime', "<", $request->search_todate . ' 23:59:59');
//地點搜尋
if (isset($request->location))
$records2->where('serialnumber', $request->location);
if (isset($request->violation_type))
$records2->where('violationtype', $request->violation_type);
$violationTypes = $records2->whereIn('violationtype', ['超速'])->groupBy('violationtype')->select('violationtype')->get()->toArray();
$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;
}
}
$data = $records->get();
$data = $data->groupBy(function ($item, $key) {
return intval(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[$key][$violationTypesKey[$viotype]] = $item->count();
});
});
$data = $response;
// dd($data);
}
}
// dd($data);
return response()->json(['data' => $data, 'chart_type' => $request->chart_type, 'violationtype' => $violationTypes ?? ''], 200);
}
public function getStatisticsTest(Request $request)
{
$request->statisticstype;
// 讀取json file (storage/app/data/get-overspeed-statistics-1~3.json )
$json = file_get_contents(storage_path('app/data/get-overspeed-statistics-1.json'));
}
#region 更新ini檔案
function updateIni($path, $oldCarNumber, $newCarNumber, $oldViolationType, $newViolationType)
{
// $oldCarNumber = explode('"', "$oldCarNumber")[1];
$iniPath = str_replace('jpg', 'ini', str_replace('*', '\\', storage_path('app' . '\\' . 'ParsingFiles' . '\\' . "$path")));
$iniFile = fopen("$iniPath", "r+");
$iniData = fread($iniFile, filesize("$iniPath"));
$iniData = iconv('Big5', 'UTF-8', "$iniData");
// if(strpos($iniData,'車號')){
// $iniData = str_replace("$oldCarNumber", "$newCarNumber", "$iniData");
// }else{
// $iniData = str_replace("\r\n操作者姓名", "\r\n車號="."$newCarNumber"."\r\n操作者姓名", "$iniData");
// }
$iniData = str_replace("$oldCarNumber", "$newCarNumber", "$iniData");
$iniData = str_replace("$oldViolationType", "$newViolationType", "$iniData");
$iniData = iconv('UTF-8', 'Big5', "$iniData");
$iniFile = fopen("$iniPath", "w+");
fwrite($iniFile, "$iniData");
fclose($iniFile);
}
function saveFinishFile($data)
{
//路徑位置設定
$destPath_root = $this->destPath_root;
$clientDestPath_root = $this->clientDestPath_root;
// $ftpPath_root = $this->ftpPath_root;
$violationtype = $data['violationtype'];
$path = $data['picture'];
// $path2 = $data['picture2'];
$time = str_replace('-', '', explode(' ', $data['datatime'])[0]);
$iniPath = str_replace('jpg', 'ini', str_replace('*', '\\', storage_path('app' . '\\' . 'ParsingFiles' . '\\' . "$path")));
$photoPath = str_replace('*', '\\', storage_path('app' . '\\' . 'ParsingFiles' . '\\' . "$path"));
// $photoPath2 = str_replace('*','\\', storage_path('app'.'\\'.'ParsingFiles'.'\\'."$path2"));
// $photoName = explode('*', $data['picture'])[4]; //原始有"時" 資料夾的層數
$photoName = explode('*', $data['picture']);
$photoName = end($photoName);
$photoPath = public_path("merge\\$violationtype\\$time\\$photoName");
// $photoName2 = explode('*', $data['picture2'])[3];
$destPath = $destPath_root . Auth::user()->account . "\\" . $time;
if (is_dir($destPath) === false) {
mkdir($destPath, 0777, true);
}
try {
copy($iniPath, $destPath . '\\' . str_replace('jpg', 'ini', $photoName));
copy($photoPath, $destPath . '\\' . $photoName);
// copy($photoPath2, $destPath.'\\'.$photoName2);
// 存入第二路徑供客戶操作資料
$clientDestPath = $clientDestPath_root . $time;
if (is_dir($clientDestPath) === false) {
mkdir($clientDestPath, 0777, true);
}
copy($iniPath, $clientDestPath . '\\' . str_replace('jpg', 'ini', $photoName));
copy($photoPath, $clientDestPath . '\\' . $photoName);
// copy($photoPath2, $clientDestPath.'\\'.$photoName2);
// 存入第FTP路徑供客戶操作資料
// $ftpPath = $ftpPath_root.$time;
// if( is_dir($ftpPath) === false ) { mkdir($ftpPath, 0777, true);}
// copy($iniPath, $ftpPath.'\\'.str_replace('jpg','ini', $photoName));
// copy($photoPath, $ftpPath.'\\'.$photoName);
} catch (\Throwable $th) {
//throw $th;
}
}
#endregion
#region 下載ZIP
function downloadZipPicture(Request $request)
{
set_time_limit(0);
ini_set('memory_limit', '2048M');
$device = explode(',', auth("api")->user()->device) ?? [1];
if (isset($request->searchByFromdate))
$searchByFromdate = $request->searchByFromdate;
if (isset($request->searchByTodate))
$searchByTodate = $request->searchByTodate;
if (isset($request->location))
$serialnumber = $request->location;
if (isset($request->carnumber))
$carnumber = $request->carnumber;
if (isset($request->processcheck))
$processcheck = $request->processcheck;
else $processcheck = 1;
$records = OverSpeedRed::query();
$records->where('location', 'not like', '%測試%');
$records->whereIn('serialnumber', $device)->whereIn('violationtype', ['超速']);
$records->where('processcheck', $processcheck);
if (isset($searchByFromdate))
$records->where('datatime', ">", $searchByFromdate . ' 00:00:00');
else {
$records->where('datatime', ">", Carbon::now()->subMonths(2));
$searchByFromdate = '';
}
if (isset($searchByTodate))
$records->where('datatime', "<", $searchByTodate . ' 23:59:59');
else $searchByTodate = '';
if (isset($serialnumber))
$records->where('serialnumber', $request->location);
if (isset($carnumber))
$records->where('carnumber', 'like', '%' . $carnumber . '%');
$records = $records->pluck("picture");
for ($i = 0; $i < count($records); $i++) {
$records[$i] = storage_path("app\\ParsingFiles\\" . str_replace('*', '\\', $records[$i]));
}
$fileName = $this->downloadZip($records, $request->location, $searchByFromdate, $searchByTodate, $processcheck);
if (file_exists($fileName)) {
ExportFiles::create([
'name' => $fileName,
'path' => 'public',
'type' => 'zip',
'status' => '1',
'remark' => "超速-下載ZIP",
'user_id' => auth('api')->user()->id,
]);
return $fileName;
} else return "沒有檔案";
}
public function downloadZip($files, $serialnumber, $searchByFromdate, $searchByTodate, $processcheck)
{
set_time_limit(0);
ini_set('memory_limit', '2048M');
foreach (glob(public_path() . '\*.zip') as $file) {
unlink($file);
}
$zip = new ZipArchive;
$rand = Str::random(5);
$fileName = "_$rand.zip";
if (isset($searchByTodate))
$fileName = str_replace('-', '', $searchByTodate) . $fileName;
if (isset($searchByFromdate))
$fileName = str_replace('-', '', $searchByFromdate) . "_" . $fileName;
if (isset($serialnumber))
$fileName = $serialnumber . "_" . $fileName;
if (isset($processcheck)) {
if ($processcheck == 1)
$fileName = "OSR_OK_" . $fileName;
if ($processcheck == 2)
$fileName = "OSR_X_" . $fileName;
}
if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE) {
foreach ($files as $file) {
$name = explode("\\", $file);
$iniPath = str_replace('.jpg', '.ini', $file);
$iniName = explode("\\", $iniPath);
if (end($name) != '') {
$zip->addFile($file, end($name));
$zip->addFile($iniPath, end($iniName));
}
}
$zip->close();
}
// response()->download(public_path($fileName))->deleteFileAfterSend(true);
return "$fileName";
}
#endregion
}