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.
 
 
 

620 lines
28 KiB

<?php
namespace App\Classes;
use App\TwoStage;
use App\TwostageEntry;
use App\UpdateCarNumber;
use App\ViolationParkingEntry;
use App\IntervalDataBack;
use App\IntervalEntry;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
// 入案專用 class
class NewEntry
{
#region 路口多功能科技執法入案
public static function twostage_entry($days = 7)
{
$records = TwoStage::query();
$records->where('processcheck', 1);
$records->where('postcheck', 0);
$records->whereNotNull('violationcode');
// $records->where('datatime', '>', Carbon::now('Asia/Taipei')->subDays($days));
$records->where('datatime', '>', '2025-01-15');
$data = $records->select('id as twostage_id', 'picture as photo')->get()->toArray();
$values = [];
foreach ($data as $row) {
$values[] = '(\'' . implode('\',\'', $row) . '\')';
}
// dd($values);
if (count($values) > 0) {
DB::transaction(function () use ($values, $records) {
// 在這裡執行需要交易的資料庫操作,例如新增、修改、刪除等等
DB::insert('INSERT IGNORE INTO twostage_entry (twostage_id, photo) VALUES' . implode(',', $values));
$records->update(['postcheck' => 1]);
});
}
// DB::table('twostage_entry')->insertOrIgnore($data);
Log::notice('路口多功能入案-資料收集 近' . $days . '天,共' . count($values) . '筆');
}
// 取得要處理的資料
public static function get_twostage_entry_data($days = 1, $status = [0])
{
$ids = TwostageEntry::whereIn('status', $status)
->where('created_at', '>', Carbon::now('Asia/Taipei')->subDays($days))
// ->where('created_at', '<', Carbon::now('Asia/Taipei')->subDays(2))
->pluck('twostage_id');
$data = Twostage::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' => DB::table('operator')->where('account', $row->jsoncheck)->first()->leader,
'UnitId' => 'A000',
'PoliceName' => DB::table('operator')->where('account', $row->jsoncheck)->first()->name,
'LicensePlate' => $row->carnumber,
'VehicleType' => $row->cartype,
'RuleId' => $row->violationcode,
'Road' => $row->location,
];
// if (isset($row->violationmsg)) {
// $row_data['PunishFACT'] = $row->violationmsg;
// }
array_push($values, $row_data);
}
return $values;
}
// 本機建檔備存
public static function twostage_entry_local($data = [])
{
if (count($data) > 0) {
foreach ($data as $row_data) {
$datetime = $row_data['datatime'];
unset($row_data['datatime']);
// unset($row_data['photo']);
// 存放檔案的目錄路徑
$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 twostage_entry_post($data = [])
{
set_time_limit(0);
if (count($data) > 0) {
foreach ($data as $row_data) {
unset($row_data['datatime']);
$id = $row_data['SN'];
$row_data['SN'] = 'TS' . $row_data['SN'];
Log::notice($row_data);
try {
// $client = new Client(['base_uri' => 'http://220.128.232.102:5000']);
$response = $client->request('POST', '/HX/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'] == '新增完成'){
TwostageEntry::where('twostage_id', $id)->first()->update(['no' => $no, 'response' => $resp, 'status' => 1]);
}else{
TwostageEntry::where('twostage_id', $id)->first()->update(['no' => $no, 'response' => $resp, 'status' => 3]);
}
} else {
$resp = $response->getBody();
$no = json_decode($resp, true)['No'];
TwostageEntry::where('twostage_id', $id)->first()->update(['no' => $no, 'response' => $resp, 'status' => 3]);
}
} catch (\Exception $e) {
// There was another exception.
Log::error('twostage_id: ' . $id);
Log::error($e->getMessage());
}
}
}
Log::notice('路口多功能入案-資料送往入案系統,共' . count($data) . '筆');
}
public static function twostage_entry_post_img()
{
$re_arr = [];
$data = TwostageEntry::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 {
// 取得圖片檔案路徑
$filePath = storage_path('app/ParsingFiles/' . str_replace('*', '/', $row_data['photo']));
// 使用 Intervention/Image 套件進行壓縮
$image = Image::make($filePath);
// 確認圖片寬度是否超過 1920
if ($image->getWidth() > 1920) {
// 計算等比例縮放後的寬度和高度
$width = 1920;
$height = round($image->getHeight() * $width / $image->getWidth());
// 縮放圖片
$image->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});
}
// 壓縮圖片
$image->encode('jpg', 40);
// 將圖片轉換為 base64 編碼
$base64 = base64_encode($image->encoded);
$post_data = [
'data' => $base64
];
Log::notice($post_data);
// $client = new Client(['base_uri' => 'http://220.128.232.102:5000']);
$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('twostage_id: ' . $row_data->id);
Log::error($e->getMessage());
}
}
}
Log::notice('路口多功能入案-佐證資料送往入案系統,共' . count($data) . '筆');
}
#endregion
#region 違規停車入案
public static function violationparking_entry($days = 7)
{
$records = UpdateCarNumber::query();
$records->where('processcheck', 1);
$records->where('postcheck', 0);
$records->whereNotNull('violationcode');
// $records->where('time', '>', Carbon::now('Asia/Taipei')->subDays($days));
$records->where('time', '>', '2025-01-15');
$data = $records->select('id as violationparking_id', 'picture as photo')->get()->toArray();
$values = [];
foreach ($data as $row) {
$values[] = '(\'' . implode('\',\'', $row) . '\')';
}
// dd($values);
if (count($values) > 0) {
DB::transaction(function () use ($values, $records) {
// 在這裡執行需要交易的資料庫操作,例如新增、修改、刪除等等
DB::insert('INSERT IGNORE INTO violationparking_entry (violationparking_id, photo) VALUES' . implode(',', $values));
$records->update(['postcheck' => 1]);
});
}
Log::notice('違停入案-資料收集 近' . $days . '天,共' . count($values) . '筆');
}
// 取得要處理的資料
public static function get_violationparking_entry_data($days = 1, $status = [0])
{
$ids = ViolationParkingEntry::whereIn('status', $status)->where('created_at', '>', Carbon::now('Asia/Taipei')->subDays($days))->pluck('violationparking_id');
$data = UpdateCarNumber::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->time,
'SN' => $row->id,
'ViolationDate' => $twDate,
'ViolationTime' => $time,
// 'UnitId' => DB::table('operator')->where('account', $row->jsoncheck)->first()->leader,
'UnitId' => 'A000',
'PoliceName' => DB::table('operator')->where('account', $row->jsoncheck)->first()->name,
'LicensePlate' => $row->carnumber,
'VehicleType' => $row->cartype,
'RuleId' => $row->violationcode,
'Road' => $row->deviceplace,
];
array_push($values, $row_data);
}
return $values;
}
// 組合資料並建檔備存
public static function violationparking_entry_local($data = [])
{
if (count($data) > 0) {
foreach ($data as $row_data) {
$datetime = $row_data['datatime'];
unset($row_data['datatime']);
// 存放檔案的目錄路徑
$directory = 'entry/violationparking/' . 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 = 'Vpk_' . $row_date . '_' . $row_data['SN'] . '.json';
Storage::put($directory . "/$fileName", $jsonData);
}
}
Log::notice('違停入案-資料組合備存,共' . count($data) . '筆');
}
public static function violationparking_entry_post($data = [])
{
set_time_limit(0);
if (count($data) > 0) {
foreach ($data as $row_data) {
unset($row_data['datatime']);
$id = $row_data['SN'];
$row_data['SN'] = 'TS' . $row_data['SN'];
Log::notice($row_data);
try {
// $client = new Client(['base_uri' => 'http://220.128.232.102:5000']);
$response = $client->request('POST', '/HX/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'] == '新增完成'){
ViolationParkingEntry::where('violationparking_id', $id)->first()->update(['no' => $no, 'response' => $resp, 'status' => 1]);
}else{
ViolationParkingEntry::where('violationparking_id', $id)->first()->update(['no' => $no, 'response' => $resp, 'status' => 3]);
}
} else {
$resp = $response->getBody();
$no = json_decode($resp, true)['No'];
ViolationParkingEntry::where('violationparking_id', $id)->first()->update(['no' => $no, 'response' => $resp, 'status' => 3]);
}
} catch (\Exception $e) {
// There was another exception.
Log::error('violationparking_id: ' . $id);
Log::error($e->getMessage());
}
}
}
Log::notice('違停入案-資料送往入案系統,共' . count($data) . '筆');
}
public static function violationparking_entry_post_img()
{
$re_arr = [];
$data = ViolationParkingEntry::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 {
// 取得圖片檔案路徑
$filePath = storage_path('app/ParsingFiles/' . str_replace('*', '/', $row_data['photo']));
// 使用 Intervention/Image 套件進行壓縮
$image = Image::make($filePath);
// 確認圖片寬度是否超過 1920
if ($image->getWidth() > 1920) {
// 計算等比例縮放後的寬度和高度
$width = 1920;
$height = round($image->getHeight() * $width / $image->getWidth());
// 縮放圖片
$image->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});
}
// 壓縮圖片
$image->encode('jpg', 40);
// 將圖片轉換為 base64 編碼
$base64 = base64_encode($image->encoded);
$post_data = [
'data' => $base64
];
Log::notice($post_data);
// $client = new Client(['base_uri' => 'http://220.128.232.102:5000']);
$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('violationparking_id: ' . $row_data->id);
Log::error($e->getMessage());
}
}
}
Log::notice('違停入案-佐證資料送往入案系統,共' . count($data) . '筆');
}
#endregion
#region 區間入案
public static function interval_entry($days = 7)
{
$records = IntervalDataBack::query();
// $records->where('datacheck', 1);
$records->where('postcheck', 0);
$records->whereNotNull('viocode');
$records->whereNotNull('jsoncheck');
// $records->where('starttime', '>', Carbon::now('Asia/Taipei')->subDays($days));
$records->where('starttime', '>', '2025-01-15');
$data = $records->select('id as intervaldataback_id', 'startpicture', 'endpicture')->get()->toArray();
// dd($data);
$values = [];
foreach ($data as $row) {
$row['photo'] = str_replace('-merge.jpg', '~', str_replace('Merge', 'Interval', $row['startpicture'])) . str_replace('-merge.jpg', '-interval.png', explode('*', $row['endpicture'])[4]);
unset($row['startpicture']);
unset($row['endpicture']);
$values[] = '(\'' . implode('\',\'', $row) . '\')';
}
// dd($values);
if (count($values) > 0) {
DB::transaction(function () use ($values, $records) {
// 在這裡執行需要交易的資料庫操作,例如新增、修改、刪除等等
DB::insert('INSERT IGNORE INTO interval_entry (intervaldataback_id, photo) VALUES' . implode(',', $values));
$records->update(['postcheck' => 1]);
});
}
Log::notice('區間入案-資料收集 近' . $days . '天,共' . count($values) . '筆');
}
public static function get_interval_entry_data($days = 1, $status = [0])
{
$ids = IntervalEntry::whereIn('status', $status)->where('created_at', '>', Carbon::now('Asia/Taipei')->subDays($days))->pluck('intervaldataback_id');
$data = IntervalDataBack::whereIn('id', $ids)->get();
// dd($data);
$values = [];
foreach ($data as $row) {
$s_time = $row->start_time;
$s_date = Carbon::parse($s_time)->toDateString();
$s_twYear = Carbon::parse($s_date)->format('Y') - 1911; // 取得民國年,即西元年減去1911
$s_twDate = $s_twYear . Carbon::parse($s_date)->format('md'); // 將民國年與日期合併
$s_time = Carbon::parse($s_time)->format('His');
$e_time = $row->end_time;
$e_date = Carbon::parse($e_time)->toDateString();
$e_twYear = Carbon::parse($e_date)->format('Y') - 1911; // 取得民國年,即西元年減去1911
$e_twDate = $e_twYear . Carbon::parse($e_date)->format('md'); // 將民國年與日期合併
$e_time = Carbon::parse($e_time)->format('His');
$cartype = $row->cartype;
if ($row->carkind == '汽車' || $row->carkind == '大型車') {
$cartype = 1;
} else if ($row->carkind == '機車' || $row->carkind == '大型重型機車') {
$cartype = 3;
} else {
continue;
}
$row_data = [
'datatime' => $row->start_time,
'SN' => $row->id,
'ViolationDate' => $e_twDate,
'ViolationTime' => $e_time,
'UnitID' => 'A000',
'PoliceName' => DB::table('operator')->where('account', $row->jsoncheck)->first()->name ?? '',
'LicensePlate' => $row->carnumber,
'VehicleType' => $cartype,
'RuleId' => $row->violationcode,
'Road' => $row->location,
'Speed' => intval(floor($row->speed)),
'LimitSpeed' => intval(floor($row->limit_speed)),
'Distance' => $row->distance,
'Duration' => $row->diff,
'InDate' => $s_twDate,
'InTime' => $s_time,
'OutDate' => $e_twDate,
'OutTime' => $e_time,
];
array_push($values, $row_data);
}
return $values;
}
// 組合資料並建檔備存
public static function interval_entry_local($data = [])
{
if (count($data) > 0) {
foreach ($data as $row_data) {
$datetime = $row_data['datatime'];
unset($row_data['datatime']);
// 存放檔案的目錄路徑
$directory = 'entry/interval/' . 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 = 'ITL_' . $row_date . '_' . $row_data['SN'] . '.json';
Storage::put($directory . "/$fileName", $jsonData);
}
}
Log::notice('區間入案-資料組合備存,共' . count($data) . '筆');
}
public static function interval_entry_post($data = [])
{
set_time_limit(0);
if (count($data) > 0) {
foreach ($data as $row_data) {
unset($row_data['datatime']);
$id = $row_data['SN'];
$row_data['SN'] = 'ITL' . $row_data['SN'];
Log::notice($row_data);
try {
// $client = new Client(['base_uri' => 'http://220.128.232.102:8000']);
$response = $client->request('POST', '/HX/sunhouse', ['form_params' => $row_data, 'connect_timeout' => 10]);
// Here the code for successful request
if ($response->getStatusCode() == "200") {
$resp = $response->getBody();
try {
$res = json_decode($resp, true);
if ($res['code'] == "200") {
IntervalEntry::where('intervaldataback_id', $id)->first()->update(['response' => $resp, 'status' => 1]);
} else {
IntervalEntry::where('intervaldataback_id', $id)->first()->update(['response' => $resp, 'status' => 4]);
Log::error('intervaldataback_id: ' . $id . "雖然response->getStatusCode() == 200,但不成功");
}
} catch (\Throwable $th) {
IntervalEntry::where('intervaldataback_id', $id)->first()->update(['response' => $resp, 'status' => 5]);
Log::error('intervaldataback_id: ' . $id);
Log::error($th);
}
} else {
$resp = $response->getBody();
IntervalEntry::where('intervaldataback_id', $id)->first()->update(['response' => $resp, 'status' => 6]);
Log::error('intervaldataback_id: ' . $id . "response->getStatusCode() != 200");
}
// if ($response->getStatusCode() == "200") {
// $resp = $response->getBody();
// IntervalEntry::where('intervaldataback_id', $id)->first()->update(['response' => $resp, 'status' => 1]);
// } else {
// $resp = $response->getBody();
// IntervalEntry::where('intervaldataback_id', $id)->first()->update(['response' => $resp, 'status' => 3]);
// }
} catch (\Exception $e) {
// There was another exception.
Log::error('intervaldataback_id: ' . $id);
Log::error($e->getMessage());
}
// break;
}
}
Log::notice('區間入案-資料送往入案系統,共' . count($data) . '筆');
}
// 送出佐證圖片
public static function interval_entry_post_img()
{
$re_arr = [];
$data = IntervalEntry::where('status', 1)
->where('created_at', '>', Carbon::now('Asia/Taipei')->subDays(10))
// ->whereIn('interval_id', $re_arr)
->get();
if (count($data) > 0) {
foreach ($data as $row_data) {
try {
// 使用 intervention/image 將檔案壓縮至500K以下
// 將圖片轉換為 base64 格式 不儲存
$image = Image::make(public_path('ParsingFiles/interval/' . str_replace('*', '/', $row_data->photo)));
// 確認圖片寬度是否超過 1920
if ($image->getWidth() > 1920) {
// 計算等比例縮放後的寬度和高度
$width = 1920;
$height = round($image->getHeight() * $width / $image->getWidth());
// 縮放圖片
$image->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});
}
// 壓縮圖片
$image->encode('jpg', 40);
// 將圖片轉換為 base64 編碼
$base64 = base64_encode($image->encoded);
$post_data = [
'data' => $base64
];
// Log::channel('entry')->notice($post_data);
$client = new Client(['base_uri' => 'http://220.128.232.102:8000']);
$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::channel('entry')->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('intervaldataback_id(postimg): ' . $row_data->id);
Log::error($e->getMessage());
}
}
}
Log::channel('entry')->notice('區間入案-佐證資料送往入案系統,共' . count($data) . '筆');
}
#endregion
}