CheckInController.php 4.3 KB

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