where('processcheck', 1); $records->where('postcheck', 0); $records->whereNotNull('violationcode'); $records->whereNotNull('jsoncheck'); // $records->where('starttime', '>', Carbon::now('Asia/Taipei')->subDays($days)); $records->where(DB::Raw('date(start_time)'), '>=', '2025-01-01'); $data = $records->select('id as interval_id', 'merge_picture as photo')->get()->toArray(); // dd($data); $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 interval_entry (interval_id, photo) VALUES' . implode(',', $values)); $records->update(['postcheck' => 1]); }); } Log::channel('entry')->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('interval_id'); $data = Interval::whereIn('id', $ids)->get(); // Log::channel('entry')->notice($ids); // Log::channel('entry')->notice($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->cartype == '98') { $cartype = 1; } else if ($row->cartype == '99') { $cartype = 3; } else{ $cartype = $row->cartype; } $row_data = [ 'datatime' => $row->start_time, 'SN' => $row->id, 'ViolationDate' => $e_twDate, 'ViolationTime' => $e_time, 'UnitID' => User::where('account', $row->jsoncheck)->first()->leader, 'PoliceName' => User::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::channel('entry')->notice('區間入案-資料組合備存,共' . count($data) . '筆'); } // 送出入案,紀錄回傳no public static function interval_entry_post($data = []) { if (count($data) > 0) { foreach ($data as $row_data) { $interval_id = $row_data['SN']; $row_data['SN'] = 'ITL'.$row_data['SN']; unset($row_data['datatime']); try { // Log::channel('entry')->notice($row_data); $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'] == '新增完成'){ IntervalEntry::where('interval_id', $interval_id)->first()->update(['no' => $no, 'response' => $resp, 'status' => 1]); }else{ IntervalEntry::where('interval_id', $interval_id)->first()->update(['no' => $no, 'response' => $resp, 'status' => 3]); } } else { $resp = $response->getBody(); $no = json_decode($resp, true)['No']; IntervalEntry::where('interval_id', $interval_id)->first()->update(['no' => $no, 'response' => $resp, 'status' => 3]); } } catch (\Exception $e) { // There was another exception. Log::error('interval_id: ' . $interval_id); Log::error($e->getMessage()); } } } Log::channel('entry')->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 格式 不儲存 // Log::channel('entry')->notice($row_data->photo); $img = Image::read(public_path('ParsingFiles/interval/' . 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(1920, 1920 * $height / $width); } $base64 = base64_encode($img->encode()); $post_data = [ 'data' => $base64 ]; // Log::channel('entry')->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::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('interval_id(postimg): ' . $row_data->id); Log::error($e->getMessage()); } } } Log::channel('entry')->notice('區間入案-佐證資料送往入案系統,共' . count($data) . '筆'); } #endregion }