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.
485 lines
23 KiB
485 lines
23 KiB
<?php |
|
|
|
namespace App\Class; |
|
|
|
use App\Models\Multisys; |
|
use App\Models\MultisysEntry; |
|
use App\Models\OverSpeedRed; |
|
use App\Models\OverSpeedRedEntry; |
|
use App\Models\OverSpeedRedEntry2; |
|
use App\Models\User; |
|
use Carbon\Carbon; |
|
use Illuminate\Support\Facades\DB; |
|
use Illuminate\Support\Facades\Log; |
|
use Illuminate\Support\Facades\Storage; |
|
|
|
use GuzzleHttp\Client; |
|
use GuzzleHttp\Exception\RequestException; |
|
|
|
use Intervention\Image\Laravel\Facades\Image; |
|
|
|
// 入案專用 class |
|
class Entry |
|
{ |
|
|
|
#region 路口多功能科技執法入案 |
|
public static function multisys_entry($days = 7) |
|
{ |
|
$records = Multisys::query(); |
|
$records->where('processcheck', 1); |
|
$records->where('postcheck', 0); |
|
// $records->where('datatime', '>', Carbon::now('Asia/Taipei')->subDays($days)); |
|
$records->where('datatime', '>', '2024-06-01'); |
|
$data = $records->select('id as multisys_id', 'picture as photo')->get()->toArray(); |
|
|
|
$values = []; |
|
foreach ($data as $row) { |
|
unset($row['law_type']); |
|
$values[] = '(\'' . implode('\',\'', $row) . '\')'; |
|
} |
|
// dd($values); |
|
if (count($values) > 0) { |
|
DB::transaction(function () use ($values, $records) { |
|
// 在這裡執行需要交易的資料庫操作,例如新增、修改、刪除等等 |
|
DB::insert('INSERT IGNORE INTO multisys_entry (multisys_id, photo) VALUES' . implode(',', $values)); |
|
$records->update(['postcheck' => 1]); |
|
}); |
|
} |
|
// DB::table('multisys_entry')->insertOrIgnore($data); |
|
Log::notice('路口多功能入案-資料收集 近' . $days . '天,共' . count($values) . '筆'); |
|
} |
|
// 取得要處理的資料 |
|
public static function get_multisys_entry_data($days = 1) |
|
{ |
|
$ids = MultisysEntry::where('status', 0)->where('created_at', '>', Carbon::now('Asia/Taipei')->subDays($days))->pluck('multisys_id'); |
|
$data = Multisys::whereIn('id', $ids)->get(); |
|
// dd($data); |
|
$values = []; |
|
foreach ($data as $row) { |
|
$datetime = $row->datatime; |
|
$date = Carbon::parse($datetime)->toDateString(); |
|
$twYear = Carbon::parse($date)->format('Y') - 1911; // 取得民國年,即西元年減去1911 |
|
$twDate = $twYear . Carbon::parse($date)->format('md'); // 將民國年與日期合併 |
|
$time = Carbon::parse($datetime)->format('His'); |
|
$row_data = [ |
|
'datatime' => $row->datatime, |
|
'SN' => $row->id, |
|
'ViolationDate' => $twDate, |
|
'ViolationTime' => $time, |
|
'UnitId' => User::where('account', $row->jsoncheck)->first()->leader, |
|
'PoliceName' => User::where('account', $row->jsoncheck)->first()->name, |
|
'LicensePlate' => $row->carnumber, |
|
'VehicleType' => $row->cartype, |
|
'RuleId' => $row->violationcode, |
|
'Road' => $row->location, |
|
]; |
|
array_push($values, $row_data); |
|
} |
|
return $values; |
|
} |
|
// 本機建檔備存 |
|
public static function multisys_entry_local($data = []) |
|
{ |
|
|
|
if (count($data) > 0) { |
|
foreach ($data as $row_data) { |
|
$datetime = $row_data['datatime']; |
|
unset($row_data['datatime']); |
|
// 存放檔案的目錄路徑 |
|
$directory = 'entry/twosage/' . Carbon::now('Asia/Taipei')->format('Ymd'); |
|
// 如果目錄不存在,則建立該目錄 |
|
Storage::makeDirectory($directory); |
|
// 將資料轉換為 JSON 格式 |
|
$jsonData = json_encode($row_data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); |
|
// 將 JSON 資料寫入檔案 |
|
$row_date = Carbon::parse($datetime)->format('Ymd_His'); |
|
$fileName = 'Ts_' . $row_date . '_' . $row_data['SN'] . '.json'; |
|
Storage::put($directory . "/$fileName", $jsonData); |
|
} |
|
} |
|
Log::notice('路口多功能入案-資料組合備存,共' . count($data) . '筆'); |
|
} |
|
// 送出入案,紀錄回傳no |
|
public static function multisys_entry_post($data = []) |
|
{ |
|
if (count($data) > 0) { |
|
foreach ($data as $row_data) { |
|
unset($row_data['datatime']); |
|
try { |
|
$client = new Client(['base_uri' => 'http://trafficfine.typd.gov.tw:88']); |
|
$response = $client->request('POST', '/X/sunhouse', ['form_params' => $row_data, 'connect_timeout' => 10]); |
|
// Here the code for successful request |
|
if ($response->getStatusCode() == "200") { |
|
$resp = $response->getBody(); |
|
$no = json_decode($resp, true)['No']; |
|
if(json_decode($resp, true)['Message'] == '新增完成'){ |
|
MultisysEntry::where('multisys_id', $row_data['SN'])->first()->update(['no' => $no, 'response' => $resp, 'status' => 1]); |
|
}else{ |
|
MultisysEntry::where('multisys_id', $row_data['SN'])->first()->update(['no' => $no, 'response' => $resp, 'status' => 3]); |
|
} |
|
} else { |
|
$resp = $response->getBody(); |
|
$no = json_decode($resp, true)['No']; |
|
MultisysEntry::where('multisys_id', $row_data['SN'])->first()->update(['no' => $no, 'response' => $resp, 'status' => 3]); |
|
} |
|
} catch (\Exception $e) { |
|
// There was another exception. |
|
Log::error('multisys_id: ' . $row_data['SN']); |
|
Log::error($e->getMessage()); |
|
} |
|
} |
|
} |
|
|
|
Log::notice('路口多功能入案-資料送往入案系統,共' . count($data) . '筆'); |
|
} |
|
// 送出佐證圖片 |
|
public static function multisys_entry_post_img() |
|
{ |
|
$re_arr = []; |
|
$data = MultisysEntry::where('status', 1) |
|
->where('created_at', '>', Carbon::now('Asia/Taipei')->subDays(3)) |
|
// ->whereIn('multisys_id', $re_arr) |
|
->get(); |
|
if (count($data) > 0) { |
|
foreach ($data as $row_data) { |
|
try { |
|
// 使用 intervention/image 將檔案壓縮至500K以下 |
|
// 將圖片轉換為 base64 格式 不儲存 |
|
// Log::notice($row_data->photo); |
|
$img = Image::read(public_path('ParsingFiles/' . str_replace('*', '/', $row_data->photo))); |
|
// if ($width > $height) then width = 720, height = 720 * $height / $width |
|
// if ($height > $width) then height = 480, width = 480 * $width / $height |
|
$width = $img->width(); |
|
$height = $img->height(); |
|
|
|
if ($width > $height) { |
|
$img->resize(2560, 2560 * $height / $width); |
|
} else { |
|
$img->resize(1440 * $width / $height, 1440); |
|
} |
|
$base64 = base64_encode($img->encode()); |
|
$post_data = [ |
|
'data' => $base64 |
|
]; |
|
// Log::notice($post_data); |
|
$client = new Client(['base_uri' => 'http://trafficfine.typd.gov.tw:88']); |
|
$response = $client->request('POST', '/' . $row_data->no, ['form_params' => $post_data, 'connect_timeout' => 10]); |
|
// Here the code for successful request |
|
if ($response->getStatusCode() == "200") { |
|
$msg = json_decode($response->getBody(), true)['Message']; |
|
if ($msg == '上傳成功') { |
|
$row_data->status = 2; |
|
$row_data->save(); |
|
Log::notice($row_data->no . "-" . $msg); |
|
} else { |
|
$row_data->status = 4; |
|
$row_data->save(); |
|
Log::error($row_data->no . "-" . $msg); |
|
} |
|
} else { |
|
$row_data->status = 3; |
|
$row_data->save(); |
|
} |
|
} catch (\Exception $e) { |
|
// There was another exception. |
|
Log::error('multisys_id: ' . $row_data->id); |
|
Log::error($e->getMessage()); |
|
} |
|
} |
|
} |
|
|
|
Log::notice('路口多功能入案-佐證資料送往入案系統,共' . count($data) . '筆'); |
|
} |
|
#endregion |
|
|
|
#region 闖紅燈超速入案 |
|
public static function overspeedred_entry($days = 7) |
|
{ |
|
$records = OverSpeedRed::query(); |
|
$records->where('processcheck', 1); |
|
$records->where('postcheck', 0); |
|
// $records->where('datatime', '>', Carbon::now('Asia/Taipei')->subDays($days)); |
|
$records->where('datatime', '>', '2024-06-01'); |
|
$data = $records->select('id as overspeedred_id', 'picture as photo', 'violationtype', 'datatime')->get()->toArray(); |
|
|
|
// 嚴重超速 |
|
$records2 = OverSpeedRed::query(); |
|
$records2->where('processcheck', 1); |
|
$records2->where('postcheck', 0); |
|
// $records->where('datatime', '>', Carbon::now('Asia/Taipei')->subDays($days)); |
|
$records2->where('datatime', '>', '2024-06-01'); |
|
$records2->whereIn('violationcode', ['4310240', '4310241', '4310242']); |
|
$data2 = $records2->select('id as overspeedred_id', 'picture as photo', 'violationtype', 'datatime')->get()->toArray(); |
|
|
|
$values = []; |
|
foreach ($data as $row) { |
|
// Log::notice($row); |
|
unset($row['law_type']); |
|
$violationtype = $row['violationtype']; |
|
$time = str_replace('-', '', explode(' ', $row['datatime'])[0]); |
|
$photoName = explode('*', $row['photo']); |
|
$photoName = end($photoName); |
|
$photoPath = "merge/$violationtype/$time/$photoName"; |
|
$row['photo'] = $photoPath; |
|
unset($row['violationtype']); |
|
unset($row['datatime']); |
|
$values[] = '(\'' . implode('\',\'', $row) . '\')'; |
|
} |
|
|
|
// 嚴重超速資料彙整 |
|
$values2 = []; |
|
foreach ($data2 as $row) { |
|
// Log::notice($row); |
|
unset($row['law_type']); |
|
$violationtype = $row['violationtype']; |
|
$time = str_replace('-', '', explode(' ', $row['datatime'])[0]); |
|
$photoName = explode('*', $row['photo']); |
|
$photoName = end($photoName); |
|
$photoPath = "merge/$violationtype/$time/$photoName"; |
|
$row['photo'] = $photoPath; |
|
unset($row['violationtype']); |
|
unset($row['datatime']); |
|
$values2[] = '(\'' . implode('\',\'', $row) . '\')'; |
|
} |
|
|
|
if (count($values) > 0) { |
|
DB::transaction(function () use ($values, $values2, $records) { |
|
// 在這裡執行需要交易的資料庫操作,例如新增、修改、刪除等等 |
|
DB::insert('INSERT IGNORE INTO overspeedred_entry (overspeedred_id, photo) VALUES' . implode(',', $values)); |
|
if (count($values2) > 0) { |
|
DB::insert('INSERT IGNORE INTO overspeedred_entry2 (overspeedred_id, photo) VALUES' . implode(',', $values2)); |
|
} |
|
$records->update(['postcheck' => 1]); |
|
}); |
|
} |
|
// DB::table('overspeedred_entry')->insertOrIgnore($data); |
|
Log::notice('闖紅燈超速入案-資料收集 近' . $days . '天,共' . count($values) . '筆'); |
|
} |
|
// 取得要處理的資料 |
|
public static function get_overspeedred_entry_data($days = 1) |
|
{ |
|
$ids = OverSpeedRedEntry::where('status', 0)->where('created_at', '>', Carbon::now('Asia/Taipei')->subDays($days))->pluck('overspeedred_id'); |
|
$data = OverSpeedRed::whereIn('id', $ids)->get(); |
|
// dd($data); |
|
$values = []; |
|
foreach ($data as $row) { |
|
$datetime = $row->datatime; |
|
$date = Carbon::parse($datetime)->toDateString(); |
|
$twYear = Carbon::parse($date)->format('Y') - 1911; // 取得民國年,即西元年減去1911 |
|
$twDate = $twYear . Carbon::parse($date)->format('md'); // 將民國年與日期合併 |
|
$time = Carbon::parse($datetime)->format('His'); |
|
$row_data = [ |
|
'datatime' => $row->datatime, |
|
'SN' => $row->id, |
|
'ViolationDate' => $twDate, |
|
'ViolationTime' => $time, |
|
'UnitId' => User::where('account', $row->jsoncheck)->first()->leader, |
|
'PoliceName' => User::where('account', $row->jsoncheck)->first()->name, |
|
'LicensePlate' => $row->carnumber, |
|
'VehicleType' => $row->cartype, |
|
'RuleId' => $row->violationcode, |
|
'Road' => $row->location, |
|
|
|
]; |
|
if ($row->violationtype == '超速') { |
|
$row_data['Speed'] = intval(str_replace('km/h', '', $row->speed)); |
|
$row_data['LimitSpeed'] = intval(str_replace('km/h', '', $row->limitspeed)); |
|
} |
|
array_push($values, $row_data); |
|
} |
|
return $values; |
|
} |
|
// 本機建檔備存 |
|
public static function overspeedred_entry_local($data = []) |
|
{ |
|
|
|
if (count($data) > 0) { |
|
foreach ($data as $row_data) { |
|
$datetime = $row_data['datatime']; |
|
unset($row_data['datatime']); |
|
// 存放檔案的目錄路徑 |
|
$directory = 'entry/overspeedred/' . Carbon::now('Asia/Taipei')->format('Ymd'); |
|
// 如果目錄不存在,則建立該目錄 |
|
Storage::makeDirectory($directory); |
|
// 將資料轉換為 JSON 格式 |
|
$jsonData = json_encode($row_data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); |
|
// 將 JSON 資料寫入檔案 |
|
$row_date = Carbon::parse($datetime)->format('Ymd_His'); |
|
$fileName = 'OvO_' . $row_date . '_' . $row_data['SN'] . '.json'; |
|
Storage::put($directory . "/$fileName", $jsonData); |
|
} |
|
} |
|
Log::notice('闖紅燈超速入案-資料組合備存,共' . count($data) . '筆'); |
|
} |
|
// 送出入案,紀錄回傳no |
|
public static function overspeedred_entry_post($data = []) |
|
{ |
|
if (count($data) > 0) { |
|
foreach ($data as $row_data) { |
|
unset($row_data['datatime']); |
|
try { |
|
$client = new Client(['base_uri' => 'http://trafficfine.typd.gov.tw:88']); |
|
$response = $client->request('POST', '/X/sunhouse', ['form_params' => $row_data, 'connect_timeout' => 10]); |
|
// Here the code for successful request |
|
if ($response->getStatusCode() == "200") { |
|
$resp = $response->getBody(); |
|
$no = json_decode($resp, true)['No']; |
|
if(json_decode($resp, true)['Message'] == '新增完成'){ |
|
OverSpeedRedEntry::where('overspeedred_id', $row_data['SN'])->first()->update(['no' => $no, 'response' => $resp, 'status' => 1]); |
|
} |
|
else{ |
|
OverSpeedRedEntry::where('overspeedred_id', $row_data['SN'])->first()->update(['no' => $no, 'response' => $resp, 'status' => 3]); |
|
} |
|
} else { |
|
$resp = $response->getBody(); |
|
$no = json_decode($resp, true)['No']; |
|
OverSpeedRedEntry::where('overspeedred_id', $row_data['SN'])->first()->update(['no' => $no, 'response' => $resp, 'status' => 3]); |
|
} |
|
} catch (\Exception $e) { |
|
// There was another exception. |
|
Log::error('overspeedred_id: ' . $row_data['SN']); |
|
Log::error($e->getMessage()); |
|
} |
|
|
|
// 如果 $row->violationcode 是 4310240 4310241 4310242 才執行的條件 |
|
if (in_array($row_data['RuleId'], ['4310240', '4310241', '4310242'])) { |
|
$row_data['RuleId'] = '4340068'; |
|
try { |
|
$client = new Client(['base_uri' => 'http://trafficfine.typd.gov.tw:88']); |
|
$response = $client->request('POST', '/X/sunhouse', ['form_params' => $row_data, 'connect_timeout' => 10]); |
|
// Here the code for successful request |
|
if ($response->getStatusCode() == "200") { |
|
$resp = $response->getBody(); |
|
$no = json_decode($resp, true)['No']; |
|
if(json_decode($resp, true)['Message'] == '新增完成'){ |
|
OverSpeedRedEntry2::where('overspeedred_id', $row_data['SN'])->first()->update(['no' => $no, 'response' => $resp, 'status' => 1]); |
|
} |
|
else{ |
|
OverSpeedRedEntry2::where('overspeedred_id', $row_data['SN'])->first()->update(['no' => $no, 'response' => $resp, 'status' => 3]); |
|
} |
|
} else { |
|
$resp = $response->getBody(); |
|
$no = json_decode($resp, true)['No']; |
|
OverSpeedRedEntry2::where('overspeedred_id', $row_data['SN'])->first()->update(['no' => $no, 'response' => $resp, 'status' => 3]); |
|
} |
|
} catch (\Exception $e) { |
|
// There was another exception. |
|
Log::error('處車主 overspeedred_id: ' . $row_data['SN']); |
|
Log::error($e->getMessage()); |
|
} |
|
} |
|
|
|
} |
|
} |
|
|
|
Log::notice('闖紅燈超速入案-資料送往入案系統,共' . count($data) . '筆'); |
|
} |
|
// 送出佐證圖片 |
|
public static function overspeedred_entry_post_img() |
|
{ |
|
$re_arr = []; |
|
$data = OverSpeedRedEntry::where('status', 1) |
|
->where('created_at', '>', Carbon::now('Asia/Taipei')->subDays(20)) |
|
// ->whereIn('overspeedred_id', $re_arr) |
|
->get(); |
|
if (count($data) > 0) { |
|
foreach ($data as $row_data) { |
|
try { |
|
// 使用 intervention/image 將檔案壓縮至500K以下 |
|
// 將圖片轉換為 base64 格式 不儲存 |
|
|
|
$img = Image::read(public_path(str_replace('*', '/', $row_data->photo))); |
|
// if ($width > $height) then width = 720, height = 720 * $height / $width |
|
// if ($height > $width) then height = 480, width = 480 * $width / $height |
|
$width = $img->width(); |
|
$height = $img->height(); |
|
|
|
if ($width > $height) { |
|
$img->resize(2560, 2560 * $height / $width); |
|
} else { |
|
$img->resize(1440 * $width / $height, 1440); |
|
} |
|
$base64 = base64_encode($img->encode()); |
|
$post_data = [ |
|
'data' => $base64 |
|
]; |
|
$client = new Client(['base_uri' => 'http://trafficfine.typd.gov.tw:88']); |
|
$response = $client->request('POST', '/' . $row_data->no, ['form_params' => $post_data, 'connect_timeout' => 10]); |
|
// Here the code for successful request |
|
if ($response->getStatusCode() == "200") { |
|
$msg = json_decode($response->getBody(), true)['Message']; |
|
if ($msg == '上傳成功') { |
|
$row_data->status = 2; |
|
$row_data->save(); |
|
Log::notice($row_data->no . "-" . $msg); |
|
} else { |
|
$row_data->status = 4; |
|
$row_data->save(); |
|
Log::error($row_data->no . "-" . $msg); |
|
} |
|
} else { |
|
$row_data->status = 3; |
|
$row_data->save(); |
|
} |
|
} catch (\Exception $e) { |
|
// There was another exception. |
|
Log::error('overspeedred_id: ' . $row_data->id); |
|
Log::error($e->getMessage()); |
|
} |
|
} |
|
} |
|
|
|
$data2 = OverSpeedRedEntry2::where('status', 1) |
|
->where('created_at', '>', Carbon::now('Asia/Taipei')->subDays(20)) |
|
// ->whereIn('overspeedred_id', $re_arr) |
|
->get(); |
|
if (count($data2) > 0) { |
|
foreach ($data2 as $row_data) { |
|
try { |
|
// 使用 intervention/image 將檔案壓縮至500K以下 |
|
// 將圖片轉換為 base64 格式 不儲存 |
|
|
|
$img = Image::read(public_path(str_replace('*', '/', $row_data->photo))); |
|
// if ($width > $height) then width = 720, height = 720 * $height / $width |
|
// if ($height > $width) then height = 480, width = 480 * $width / $height |
|
$width = $img->width(); |
|
$height = $img->height(); |
|
|
|
if ($width > $height) { |
|
$img->resize(2560, 2560 * $height / $width); |
|
} else { |
|
$img->resize(1440 * $width / $height, 1440); |
|
} |
|
$base64 = base64_encode($img->encode()); |
|
$post_data = [ |
|
'data' => $base64 |
|
]; |
|
$client = new Client(['base_uri' => 'http://trafficfine.typd.gov.tw:88']); |
|
$response = $client->request('POST', '/' . $row_data->no, ['form_params' => $post_data, 'connect_timeout' => 10]); |
|
// Here the code for successful request |
|
if ($response->getStatusCode() == "200") { |
|
$msg = json_decode($response->getBody(), true)['Message']; |
|
if ($msg == '上傳成功') { |
|
$row_data->status = 2; |
|
$row_data->save(); |
|
Log::notice($row_data->no . "-" . $msg); |
|
} else { |
|
$row_data->status = 4; |
|
$row_data->save(); |
|
Log::error($row_data->no . "-" . $msg); |
|
} |
|
} else { |
|
$row_data->status = 3; |
|
$row_data->save(); |
|
} |
|
} catch (\Exception $e) { |
|
// There was another exception. |
|
Log::error('處車主 overspeedred_id: ' . $row_data->id); |
|
Log::error($e->getMessage()); |
|
} |
|
} |
|
} |
|
Log::notice('闖紅燈超速入案-佐證資料送往入案系統,共' . count($data) . '筆'); |
|
} |
|
#endregion |
|
}
|
|
|