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.
200 lines
7.0 KiB
200 lines
7.0 KiB
<?php |
|
|
|
namespace App\Http\Controllers\System; |
|
|
|
use App\Exports\ArrayExport; |
|
use Illuminate\Http\Request; |
|
use App\Http\Controllers\Controller; |
|
use App\Models\ExportFiles; |
|
use App\Models\UserLog; |
|
use Carbon\Carbon; |
|
use Maatwebsite\Excel\Facades\Excel; |
|
use Illuminate\Support\Str; |
|
|
|
class LogController extends Controller |
|
{ |
|
public function __construct() |
|
{ |
|
} |
|
|
|
// index |
|
public function index(Request $request) |
|
{ |
|
$actionDetails = UserLog::cachedDistinctActionDetail(); |
|
$users = UserLog::cachedDistinctUser(); |
|
return view('system.setting.log') |
|
->with('actionDetails', $actionDetails) |
|
->with('users', $users); |
|
} |
|
|
|
// get data |
|
#region 取得資料 |
|
function getData($request) |
|
{ |
|
#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 |
|
$records = UserLog::query(); |
|
$records->whereNotIn('user_id', [1]); |
|
if (isset($columnName)) |
|
$records->orderBy($columnName, $columnSortOrder); |
|
if (isset($searchValue)) { |
|
$records->where(function ($query) use ($searchValue) { |
|
$query->where('user_name', 'like', '%' . $searchValue . '%') |
|
->orwhere('action_detail', 'like', '%' . $searchValue . '%') |
|
->orwhere('creator_id', 'like', '%' . $searchValue . '%') |
|
->orwhere('remark', 'like', '%' . $searchValue . '%'); |
|
// ->orwhere('outlet_id', 'like', '%' . $searchValue . '%') |
|
// ->orwhere('created_at', 'like', '%' . $searchValue . '%') |
|
}); |
|
} |
|
|
|
//時間限制 |
|
if (isset($searchByFromdate)) |
|
$records->where('created_at', ">", $searchByFromdate . ' 00:00:00'); |
|
if (isset($searchByTodate)) |
|
$records->where('created_at', "<", $searchByTodate . ' 23:59:59'); |
|
|
|
if (isset($request->action_detail)) |
|
$records->where('action_detail', $request->action_detail); |
|
if (isset($request->username)) |
|
$records->whereIn('user_name', $request->username); |
|
|
|
$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( |
|
"id" => $sno++, // $record->id, |
|
"user_name" => $record->user_name, |
|
"action_detail" => $record->action_detail, |
|
"ip" => $record->ip, |
|
"remark" => $record->remark, |
|
"created_at" => Carbon::parse($record->created_at)->format('Y-m-d H:i:s'), |
|
"count" => $count, |
|
"period" => $period, |
|
"last_count" => $record->last_count ?? 0, |
|
"carnumber" => $record->carnumber, |
|
"location" => $record->location, |
|
); |
|
} |
|
#endregion |
|
return [$draw, $totalRecords, $totalRecordswithFilter, $data_arr]; |
|
} |
|
#endregion |
|
|
|
// get datatable |
|
#region DataTable (AJAX刷新用 參數 processcheckStatus 0未審 1已審 2不舉發 "99清冊用")使用 |
|
public function getDataTable(Request $request) |
|
{ |
|
|
|
[$draw, $totalRecords, $totalRecordswithFilter, $data_arr] = $this->getData($request); |
|
|
|
$response = array( |
|
"draw" => intval($draw), |
|
"iTotalRecords" => $totalRecords, |
|
"iTotalDisplayRecords" => $totalRecordswithFilter, |
|
"aaData" => $data_arr |
|
); |
|
|
|
|
|
// dd($response); |
|
echo json_encode($response); |
|
exit; |
|
} |
|
#endregion |
|
|
|
public function getLogDataExport(Request $request) |
|
{ |
|
|
|
$remark = '操作紀錄清冊'; |
|
$columns = ['id', 'created_at', 'user_name', 'ip', 'action_detail', 'remark', 'carnumber', 'location']; |
|
$columnTitle = [ |
|
["【表單名稱】:", $remark], |
|
["【違規日期區間】:", $request->searchByFromdate, " ~ ", $request->searchByTodate], |
|
['編號', '操作時間', '使用者名稱', 'ip', '動作', '操作說明', '車號', '地點'] |
|
]; |
|
|
|
|
|
if (isset($request->searchByFromdate) && isset($request->searchByTodate)) { |
|
$remark = $remark . ' ' . $request->searchByFromdate . ' ~ ' . $request->searchByTodate; |
|
} |
|
|
|
[$draw, $totalRecords, $totalRecordswithFilter, $data_arr] = $this->getData($request); |
|
$data = array_map(function ($row) use ($columns) { |
|
$data = array_merge(array_flip($columns), array_intersect_key($row, array_flip($columns))); |
|
return $data; |
|
}, $data_arr); |
|
|
|
$fileName = '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('ms-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' => '檔案不存在']); |
|
} |
|
} |
|
}
|
|
|