| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179 |
- <?php
- namespace App\Http\Controllers;
- use App\Models\CheckIn;
- use App\Models\User;
- use Illuminate\Http\Request;
- use App\Http\Requests\StoreCheckInRequest;
- use App\Http\Requests\UpdateCheckInRequest;
- use App\Models\Department;
- use PhpOffice\PhpSpreadsheet\Spreadsheet;
- use PhpOffice\PhpSpreadsheet\Reader\IReader;
- use PhpOffice\PhpSpreadsheet\Reader\Csv;
- use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
- use PhpOffice\PhpSpreadsheet\Reader\Xls;
- use PhpOffice\PhpSpreadsheet\IOFactory;
- class CheckInController extends Controller
- {
- /**
- * Display a listing of the resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function index()
- {
- //
- }
- public function searchByName(Request $request)
- {
- $user = CheckIn::where('name', 'like', '%' . $request->name . '%')->first();
- if ($user) {
- return response()->json($user->user_id);
- } else {
- return response()->json('姓名查無資料!請洽詢工作人員!');
- }
- }
- public function index_by_activity(Request $request)
- {
- $user = CheckIn::where('activity_id', $request->activity_id)->pluck('name', 'user_id');
- return $user;
- }
- public function draw_user_when_check_in(StoreCheckInRequest $request)
- {
- return CheckIn::where('is_checked_in', 'true')->get()->random($request->number);
- }
- public function draw_user_by_region(StoreCheckInRequest $request)
- {
- return CheckIn::where('is_checked_in', 'true')
- ->whereTime('check_ins.updated_at', '<=', $request->deadline)
- ->whereIn('region', $request->region)
- ->get()
- ->random($request->number);
- }
- /**
- * Show the form for creating a new resource.
- *
- * @return \Illuminate\Http\Response
- */
- public function create()
- {
- //
- }
- /**
- * Store a newly created resource in storage.
- *
- * @param \App\Http\Requests\StoreCheckInRequest $request
- * @return \Illuminate\Http\Response
- */
- public function store(StoreCheckInRequest $request)
- {
- //
- }
- /**
- * Display the specified resource.
- *
- * @param \App\Models\CheckIn $checkIn
- * @return \Illuminate\Http\Response
- */
- public function show(Request $request, CheckIn $checkIn)
- {
- //
- $checkIn = $request->activity_id ?
- CheckIn::where('user_id', '=', $request->user_id)->where('activity_id', '=', $request->activity_id)->first()
- : CheckIn::where('user_id', '=', $request->user_id)->first();
- if (!$checkIn) {
- $response = [
- 'user' => $checkIn,
- ];
- return response($response, 201);
- }
- $department = Department::where('department_id', '=', $checkIn->department_id)->first();
- $checkIn->department = $department->department_name ?? '暫無部門';
- $response = [
- 'user' => $checkIn,
- ];
- return response($response, 201);
- }
- /**
- * Show the form for editing the specified resource.
- *
- * @param \App\Models\CheckIn $checkIn
- * @return \Illuminate\Http\Response
- */
- public function edit(CheckIn $checkIn)
- {
- //
- }
- /**
- * Update the specified resource in storage.
- *
- * @param \App\Http\Requests\UpdateCheckInRequest $request
- * @param \App\Models\CheckIn $checkIn
- * @return \Illuminate\Http\Response
- */
- public function check_in(UpdateCheckInRequest $request, CheckIn $checkIn)
- {
- // 首先根據 user_id 查找 CheckIn 實例
- $checkIn = CheckIn::where('user_id', $request->user_id)->first();
- // 檢查是否找到相應記錄以及 department_id 是否不是 'guest'
- if ($checkIn && $checkIn->department_id != 'guest') {
- // 更新 is_checked_in 為
- $checkIn->update(['is_checked_in' => true]);
- }
- return $checkIn;
- }
- public function check_out(UpdateCheckInRequest $request, CheckIn $checkIn)
- {
- CheckIn::where('user_id', $request->user_id)->update(['is_checked_in' => false]);
- return 'success';
- }
- /**
- * Remove the specified resource from storage.
- *
- * @param \App\Models\CheckIn $checkIn
- * @return \Illuminate\Http\Response
- */
- public function destroy(CheckIn $checkIn)
- {
- //
- }
- public function export(Request $request)
- {
- $users = CheckIn::where('activity_id', '=', $request->activity_id)->get();
- $filename = "check_in_list_" . date('Y_m_d_H_i_s', time()) . ".csv";
- $filepath = "./upload/" . $filename;
- $handle = fopen($filepath, 'w+');
- fputcsv($handle, array('user_id', 'is_checked_in', 'updated_at'));
- foreach ($users as $user) {
- fputcsv($handle, array($user->user_id, $user->is_checked_in, $user->updated_at));
- }
- fclose($handle);
- $headers = array(
- 'Content-Type' => 'text/csv',
- );
- return response()->download($filepath, $filename, $headers);
- }
- }
|