CheckInController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\CheckIn;
  4. use App\Models\User;
  5. use Illuminate\Http\Request;
  6. use App\Http\Requests\StoreCheckInRequest;
  7. use App\Http\Requests\UpdateCheckInRequest;
  8. use App\Models\Department;
  9. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  10. use PhpOffice\PhpSpreadsheet\Reader\IReader;
  11. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  12. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  13. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  14. use PhpOffice\PhpSpreadsheet\IOFactory;
  15. class CheckInController extends Controller
  16. {
  17. /**
  18. * Display a listing of the resource.
  19. *
  20. * @return \Illuminate\Http\Response
  21. */
  22. public function index()
  23. {
  24. //
  25. }
  26. public function searchByName(Request $request)
  27. {
  28. $user = CheckIn::where('name', 'like', '%' . $request->name . '%')->first();
  29. if ($user) {
  30. return response()->json($user->user_id);
  31. } else {
  32. return response()->json('姓名查無資料!請洽詢工作人員!');
  33. }
  34. }
  35. public function index_by_activity(Request $request)
  36. {
  37. $user = CheckIn::where('activity_id', $request->activity_id)->pluck('name', 'user_id');
  38. return $user;
  39. }
  40. public function draw_user_when_check_in(StoreCheckInRequest $request)
  41. {
  42. return CheckIn::where('is_checked_in', 'true')->get()->random($request->number);
  43. }
  44. public function draw_user_by_region(StoreCheckInRequest $request)
  45. {
  46. return CheckIn::where('is_checked_in', 'true')
  47. ->whereTime('check_ins.updated_at', '<=', $request->deadline)
  48. ->whereIn('region', $request->region)
  49. ->get()
  50. ->random($request->number);
  51. }
  52. /**
  53. * Show the form for creating a new resource.
  54. *
  55. * @return \Illuminate\Http\Response
  56. */
  57. public function create()
  58. {
  59. //
  60. }
  61. /**
  62. * Store a newly created resource in storage.
  63. *
  64. * @param \App\Http\Requests\StoreCheckInRequest $request
  65. * @return \Illuminate\Http\Response
  66. */
  67. public function store(StoreCheckInRequest $request)
  68. {
  69. //
  70. }
  71. /**
  72. * Display the specified resource.
  73. *
  74. * @param \App\Models\CheckIn $checkIn
  75. * @return \Illuminate\Http\Response
  76. */
  77. public function show(Request $request, CheckIn $checkIn)
  78. {
  79. //
  80. $checkIn = $request->activity_id ?
  81. CheckIn::where('user_id', '=', $request->user_id)->where('activity_id', '=', $request->activity_id)->first()
  82. : CheckIn::where('user_id', '=', $request->user_id)->first();
  83. if (!$checkIn) {
  84. $response = [
  85. 'user' => $checkIn,
  86. ];
  87. return response($response, 201);
  88. }
  89. $department = Department::where('department_id', '=', $checkIn->department_id)->first();
  90. $checkIn->department = $department->department_name ?? '暫無部門';
  91. $response = [
  92. 'user' => $checkIn,
  93. ];
  94. return response($response, 201);
  95. }
  96. /**
  97. * Show the form for editing the specified resource.
  98. *
  99. * @param \App\Models\CheckIn $checkIn
  100. * @return \Illuminate\Http\Response
  101. */
  102. public function edit(CheckIn $checkIn)
  103. {
  104. //
  105. }
  106. /**
  107. * Update the specified resource in storage.
  108. *
  109. * @param \App\Http\Requests\UpdateCheckInRequest $request
  110. * @param \App\Models\CheckIn $checkIn
  111. * @return \Illuminate\Http\Response
  112. */
  113. public function check_in(UpdateCheckInRequest $request, CheckIn $checkIn)
  114. {
  115. // 首先根據 user_id 查找 CheckIn 實例
  116. $checkIn = CheckIn::where('user_id', $request->user_id)->first();
  117. // 檢查是否找到相應記錄以及 department_id 是否不是 'guest'
  118. if ($checkIn && $checkIn->department_id != 'guest') {
  119. // 更新 is_checked_in 為
  120. $checkIn->update(['is_checked_in' => true]);
  121. }
  122. return $checkIn;
  123. }
  124. public function check_out(UpdateCheckInRequest $request, CheckIn $checkIn)
  125. {
  126. CheckIn::where('user_id', $request->user_id)->update(['is_checked_in' => false]);
  127. return 'success';
  128. }
  129. /**
  130. * Remove the specified resource from storage.
  131. *
  132. * @param \App\Models\CheckIn $checkIn
  133. * @return \Illuminate\Http\Response
  134. */
  135. public function destroy(CheckIn $checkIn)
  136. {
  137. //
  138. }
  139. public function export(Request $request)
  140. {
  141. $users = CheckIn::where('activity_id', '=', $request->activity_id)->get();
  142. $filename = "check_in_list_" . date('Y_m_d_H_i_s', time()) . ".csv";
  143. $filepath = "./upload/" . $filename;
  144. $handle = fopen($filepath, 'w+');
  145. fputcsv($handle, array('user_id', 'is_checked_in', 'updated_at'));
  146. foreach ($users as $user) {
  147. fputcsv($handle, array($user->user_id, $user->is_checked_in, $user->updated_at));
  148. }
  149. fclose($handle);
  150. $headers = array(
  151. 'Content-Type' => 'text/csv',
  152. );
  153. return response()->download($filepath, $filename, $headers);
  154. }
  155. }