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.
300 lines
10 KiB
300 lines
10 KiB
<?php |
|
|
|
namespace App\Http\Controllers\System; |
|
|
|
use App\Class\LogWriter; |
|
use App\Exports\ArrayExport; |
|
use App\Http\Controllers\Controller; |
|
use App\Models\Clientlog; |
|
use App\Models\Clientlogsecond; |
|
use App\Models\Serverlog; |
|
use Carbon\Carbon; |
|
use Illuminate\Http\Request; |
|
use App\Models\ExportFiles; |
|
use Illuminate\Support\Facades\DB; |
|
use Illuminate\Support\Facades\Storage; |
|
use Maatwebsite\Excel\Facades\Excel; |
|
use Illuminate\Support\Str; |
|
|
|
class TimeLogController extends Controller |
|
{ |
|
#region view |
|
public function Timelog() |
|
{ |
|
$equipment = Clientlog::cachedDistinctName(); |
|
return view('system.interval.timelog', compact('equipment')); |
|
} |
|
|
|
public function TimelogSecond() |
|
{ |
|
$equipment = Clientlogsecond::cachedDistinctName(); |
|
return view('system.interval.timelogsecond', compact('equipment')); |
|
} |
|
#endregion |
|
|
|
#region api |
|
|
|
// 強制校時 |
|
#region forceTime |
|
public function ForceTime(Request $request) |
|
{ |
|
DB::beginTransaction(); |
|
try { |
|
$client = Clientlog::cachedDistinctThisIP(); |
|
|
|
$txtstring = ""; |
|
for ($i = 0; $i < count($client); $i++) { |
|
$txtstring .= $client[$i]->this_ip . "\n"; |
|
} |
|
Storage::put( |
|
"\\Timelog\\Flag\\" . "check.txt", |
|
$txtstring |
|
); |
|
$logData = [ |
|
'action' => 'forceTime', |
|
'action_detail' => '強制校時', |
|
'ip' => request()->ip(), |
|
'remark' => "強制校時", |
|
]; |
|
LogWriter::writeLog($logData, 'api'); |
|
DB::commit(); |
|
} catch (\Exception $e) { |
|
DB::rollBack(); |
|
return response()->json(['message' => 'error'], 500); |
|
} |
|
|
|
return response()->json(['message' => 'success'], 200); |
|
} |
|
#endregion |
|
|
|
// 取得最新的時間紀錄 |
|
#region getLatestTimeLog |
|
public function getLatestTimeLog(Request $request) |
|
{ |
|
$data = []; |
|
#region DataTable 搜尋屬性 |
|
if (!isset($request->export)) { |
|
$draw = $request->get('draw'); |
|
} else { |
|
$draw = 0; |
|
} |
|
#endregion |
|
if ($request->type == 'client') { |
|
// 取得 serverLog 的最新時間紀錄 |
|
$server_log = Serverlog::orderBy('this_time', 'desc')->first(); |
|
if ($server_log) { |
|
$data[] = $server_log->toArray(); |
|
} |
|
// 取得 clientLog 的最新時間紀錄 |
|
|
|
$distinct_name = Clientlog::cachedDistinctName(); |
|
// dd($distinct_name); |
|
foreach ($distinct_name as $name) { |
|
$client_log = Clientlog::where('name', $name->name)->orderBy('this_time', 'desc')->first(); |
|
if ($client_log) { |
|
$data[] = $client_log->toArray(); |
|
} |
|
} |
|
} |
|
if ($request->type == 'clientsecond') { |
|
// 取得 serverLog 的最新時間紀錄 |
|
$server_log = Serverlog::orderBy('this_time', 'desc')->first(); |
|
if ($server_log) { |
|
$data[] = $server_log->toArray(); |
|
} |
|
// 取得 clientLog 的最新時間紀錄 |
|
|
|
$distinct_name = Clientlogsecond::cachedDistinctName(); |
|
foreach ($distinct_name as $name) { |
|
$client_log = Clientlogsecond::where('name', $name->name)->orderBy('this_time', 'desc')->first(); |
|
if ($client_log) { |
|
$data[] = $client_log->toArray(); |
|
} |
|
} |
|
} |
|
$totalRecords = count($data); |
|
$totalRecordswithFilter = count($data); |
|
|
|
|
|
$response = array( |
|
"draw" => intval($draw), |
|
"iTotalRecords" => $totalRecords, |
|
"iTotalDisplayRecords" => $totalRecordswithFilter, |
|
"aaData" => $data |
|
); |
|
echo json_encode($response); |
|
exit; |
|
} |
|
|
|
#endregion |
|
|
|
// 取得歷史時間紀錄 |
|
#region getServerLog |
|
public function getLogHistory(Request $request) |
|
{ |
|
// ini set memory limit |
|
ini_set('memory_limit', '1024M'); |
|
// ini set time limit |
|
set_time_limit(0); |
|
|
|
#region Request 搜尋值 |
|
if (isset($request->searchByFromdate)) |
|
$searchByFromdate = $request->searchByFromdate; |
|
if (isset($request->searchByTodate)) |
|
$searchByTodate = $request->searchByTodate; |
|
#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 |
|
if ($request->type == 'client') { |
|
$records = Clientlog::query(); |
|
} |
|
if ($request->type == 'clientsecond') { |
|
$records = Clientlogsecond::query(); |
|
} |
|
if ($request->type == 'server') { |
|
$records = Serverlog::query(); |
|
} |
|
|
|
if (isset($columnName)) |
|
$records->orderBy($columnName, $columnSortOrder); |
|
if (isset($searchValue)) { |
|
$records->where(function ($query) use ($searchValue) { |
|
$query->where('name', 'like', '%' . $searchValue . '%') |
|
->orwhere('to_ip', 'like', '%' . $searchValue . '%') |
|
->orwhere('this_ip', 'like', '%' . $searchValue . '%') |
|
->orwhere('this_time', 'like', '%' . $searchValue . '%') |
|
->orwhere('diffsecond', 'like', '%' . $searchValue . '%') |
|
->orwhere('to_time', 'like', '%' . $searchValue . '%'); |
|
}); |
|
} |
|
if (isset($request->name)){ |
|
$records->where('name', $request->name); |
|
} |
|
|
|
//時間限制 |
|
if (isset($searchByFromdate)) |
|
$records->where('this_time', ">", $searchByFromdate . ' 00:00:00'); |
|
if (isset($searchByTodate)) |
|
$records->where('this_time', "<", $searchByTodate . ' 23:59:59'); |
|
|
|
|
|
$totalRecords = $records->count(); |
|
$totalRecordswithFilter = $records->count(); |
|
|
|
#region DataTable 分頁(報表不分頁) |
|
if (!isset($request->export)) { |
|
if (isset($start)) |
|
$records->skip($start); |
|
if (isset($rowperpage)) |
|
$records->take($rowperpage); |
|
} |
|
#endregion |
|
|
|
$records = $records->get(); |
|
|
|
#region 資料處理 |
|
$data_arr = array(); |
|
$sno = 1 + ($request->get('start') ?? 0); |
|
foreach ($records as $record) { |
|
//還原用id |
|
|
|
//報表用的欄位,避免前端錯誤,給預設值 |
|
$count = $record->count ?? 0; |
|
$period = $record->period ?? ""; |
|
|
|
$data_arr[] = array( |
|
// 校時對象時間 校時對象IP before_time本機校時前(本機校時後 - 時間誤差 毫秒三位數) 本機校時後 時間誤差(ms) 本機IP 本機名稱 |
|
// "sno" => $sno++, |
|
"to_time" => $record->to_time, |
|
"to_ip" => $record->to_ip, |
|
"before_time" => $record->before_time, |
|
"diffsecond" => $record->diffsecond, |
|
"this_time" => $record->this_time, |
|
"this_ip" => $record->this_ip, |
|
"name" => $record->name, |
|
"pushed" => $record->pushed ?? "", |
|
"force_time" => $record->force_time ?? "0", |
|
|
|
); |
|
} |
|
if (isset($request->export)) { |
|
if ($request->type == 'client') |
|
$remark = '設備端校時資料'; |
|
if ($request->type == 'clientsecond') |
|
$remark = '設備端計時資料'; |
|
if ($request->type == 'server') |
|
$remark = '伺服器端校時資料'; |
|
// if ($request->type != 'server') |
|
// $columns = ['to_time', 'to_ip', 'before_time', 'this_time', 'diffsecond', 'this_ip', 'name', 'pushed']; |
|
// else |
|
$columns = ['to_time', 'to_ip', 'before_time', 'this_time', 'diffsecond', 'this_ip', 'name']; |
|
|
|
$columnTitle = [ |
|
["【表單名稱】:", $remark], |
|
["【查詢日期區間】:", $request->searchByFromdate, " ~ ", $request->searchByTodate], |
|
['校時對象時間', '校時對象IP', '本機校時前', '本機校時後', '時間誤差(ms)', '本機IP', '本機名稱'] |
|
]; |
|
|
|
|
|
if (isset($request->searchByFromdate) && isset($request->searchByTodate)) { |
|
$remark = $remark . ' ' . $request->searchByFromdate . ' ~ ' . $request->searchByTodate; |
|
} |
|
|
|
|
|
$data = array_map(function ($row) use ($columns) { |
|
// 如果欄位diffsecond為0 顯示0 |
|
$data = array_merge(array_flip($columns), array_intersect_key($row, array_flip($columns))); |
|
$data['diffsecond'] = $data['diffsecond'] == 0 ? "0" : $data['diffsecond']; |
|
return $data; |
|
}, $data_arr); |
|
|
|
$fileName = $request->type . 'log-' . 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('system.filedownload', ['path' => $fileName])], 200); |
|
} |
|
$response = array( |
|
"draw" => intval($draw), |
|
"iTotalRecords" => $totalRecords, |
|
"iTotalDisplayRecords" => $totalRecordswithFilter, |
|
"aaData" => $data_arr |
|
); |
|
echo json_encode($response); |
|
exit; |
|
} |
|
|
|
#endregion |
|
|
|
|
|
#endregion |
|
}
|
|
|