CheckInController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 index_by_activity(Request $request)
  27. {
  28. $user = CheckIn::where('activity_id', $request->activity_id)->pluck('name', 'user_id');
  29. return $user;
  30. }
  31. public function draw_user_when_check_in(StoreCheckInRequest $request)
  32. {
  33. return CheckIn::where('is_checked_in', 'true')->get()->random($request->number);
  34. }
  35. public function draw_user_by_region(StoreCheckInRequest $request)
  36. {
  37. return CheckIn::where('is_checked_in', 'true')
  38. ->whereTime('check_ins.updated_at', '<=', $request->deadline)
  39. ->whereIn('region', $request->region)
  40. ->get()
  41. ->random($request->number);
  42. }
  43. /**
  44. * Show the form for creating a new resource.
  45. *
  46. * @return \Illuminate\Http\Response
  47. */
  48. public function create()
  49. {
  50. //
  51. }
  52. /**
  53. * Store a newly created resource in storage.
  54. *
  55. * @param \App\Http\Requests\StoreCheckInRequest $request
  56. * @return \Illuminate\Http\Response
  57. */
  58. public function store(StoreCheckInRequest $request)
  59. {
  60. //
  61. }
  62. /**
  63. * Display the specified resource.
  64. *
  65. * @param \App\Models\CheckIn $checkIn
  66. * @return \Illuminate\Http\Response
  67. */
  68. public function show(Request $request, CheckIn $checkIn)
  69. {
  70. //
  71. $checkIn = $request->activity_id ?
  72. CheckIn::where('user_id', '=', $request->user_id)->where('activity_id', '=', $request->activity_id)->first()
  73. : CheckIn::where('user_id', '=', $request->user_id)->first();
  74. if (!$checkIn) {
  75. $response = [
  76. 'user' => $checkIn,
  77. ];
  78. return response($response, 201);
  79. }
  80. $department = Department::where('department_id', '=', $checkIn->department_id)->first();
  81. $checkIn->department = $department->department_name ?? '暫無部門';
  82. $response = [
  83. 'user' => $checkIn,
  84. ];
  85. return response($response, 201);
  86. }
  87. /**
  88. * Show the form for editing the specified resource.
  89. *
  90. * @param \App\Models\CheckIn $checkIn
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function edit(CheckIn $checkIn)
  94. {
  95. //
  96. }
  97. /**
  98. * Update the specified resource in storage.
  99. *
  100. * @param \App\Http\Requests\UpdateCheckInRequest $request
  101. * @param \App\Models\CheckIn $checkIn
  102. * @return \Illuminate\Http\Response
  103. */
  104. public function update(UpdateCheckInRequest $request, CheckIn $checkIn)
  105. {
  106. CheckIn::where('user_id', $request->user_id)->update(['is_checked_in' => true]);
  107. return 'success';
  108. }
  109. public function check_out(UpdateCheckInRequest $request, CheckIn $checkIn)
  110. {
  111. CheckIn::where('user_id', $request->user_id)->update(['is_checked_in' => false]);
  112. return 'success';
  113. }
  114. /**
  115. * Remove the specified resource from storage.
  116. *
  117. * @param \App\Models\CheckIn $checkIn
  118. * @return \Illuminate\Http\Response
  119. */
  120. public function destroy(CheckIn $checkIn)
  121. {
  122. //
  123. }
  124. public function export(Request $request)
  125. {
  126. $users = CheckIn::where('activity_id', '=', $request->activity_id)->get();
  127. $filename = "check_in_list_" . date('Y_m_d_H_i_s',time()) . ".csv";
  128. $filepath = "./upload/".$filename;
  129. $handle = fopen($filepath, 'w+');
  130. fputcsv($handle, array('user_id', 'is_checked_in', 'updated_at'));
  131. foreach ($users as $user) {
  132. fputcsv($handle, array($user->user_id, $user->is_checked_in, $user->updated_at));
  133. }
  134. fclose($handle);
  135. $headers = array(
  136. 'Content-Type' => 'text/csv',
  137. );
  138. return response()->download($filepath, $filename, $headers);
  139. }
  140. }