ActivityController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Activity;
  4. use App\Models\User;
  5. use App\Models\Recipients;
  6. use App\Models\Prize;
  7. use App\Models\CheckIn;
  8. use App\Http\Requests\StoreActivityRequest;
  9. use App\Http\Requests\UpdateActivityRequest;
  10. use App\Http\Controllers\Controller;
  11. use Illuminate\Http\Request;
  12. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  13. use PhpOffice\PhpSpreadsheet\Reader\IReader;
  14. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  15. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  16. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  17. use PhpOffice\PhpSpreadsheet\IOFactory;
  18. use Illuminate\Support\Facades\DB;
  19. use Illuminate\Support\Facades\File;
  20. class ActivityController extends Controller
  21. {
  22. /**
  23. * Display a listing of the resource.
  24. *
  25. * @return \Illuminate\Http\Response
  26. */
  27. public function index()
  28. {
  29. //
  30. $activities = Activity::all();
  31. return $activities;
  32. }
  33. public function get_activity_by_id(Request $request)
  34. {
  35. return Activity::where('id', $request->id)->first();
  36. }
  37. /**
  38. * Show the form for creating a new resource.
  39. *
  40. * @return \Illuminate\Http\Response
  41. */
  42. public function create()
  43. {
  44. //
  45. }
  46. /**
  47. * Store a newly created resource in storage.
  48. *
  49. * @param \App\Http\Requests\StoreActivityRequest $request
  50. * @return \Illuminate\Http\Response
  51. */
  52. public function store(StoreActivityRequest $request)
  53. {
  54. //
  55. $activity = new Activity;
  56. $activity->activity_name = $request->activity_name;
  57. $activity->date = $request->date;
  58. $activity->place = $request->place;
  59. $activity->user_id = $request->user_id;
  60. $activity->deadline = $request->deadline;
  61. $activity->save();
  62. }
  63. /**
  64. * Display the specified resource.
  65. *
  66. * @param \App\Models\Activity $activity
  67. * @return \Illuminate\Http\Response
  68. */
  69. public function show(Request $request, Activity $activity)
  70. {
  71. //
  72. $activity = Activity::where('id', '=', $request->activity_id)->first();
  73. return $activity;
  74. }
  75. /**
  76. * Show the form for editing the specified resource.
  77. *
  78. * @param \App\Models\Activity $activity
  79. * @return \Illuminate\Http\Response
  80. */
  81. public function edit(Activity $activity)
  82. {
  83. //
  84. }
  85. /**
  86. * Update the specified resource in storage.
  87. *
  88. * @param \App\Http\Requests\UpdateActivityRequest $request
  89. * @param \App\Models\Activity $activity
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function update(UpdateActivityRequest $request, Activity $activity)
  93. {
  94. //
  95. Activity::where('id', '=', $request->activity_id)->update([
  96. 'activity_name' => $request->activity_name,
  97. 'date' => $request->date,
  98. 'place' => $request->place,
  99. 'user_id' => $request->user_id,
  100. 'deadline' => $request->deadline,
  101. ]);
  102. }
  103. /**
  104. * Remove the specified resource from storage.
  105. *
  106. * @param \App\Models\Activity $activity
  107. * @return \Illuminate\Http\Response
  108. */
  109. public function destroy(Activity $activity)
  110. {
  111. //
  112. }
  113. //這邊有兩種insert,要怎麼處理StoreRequest?
  114. public function uploadFile(Request $request)
  115. {
  116. $option = $request->option;
  117. $activity_id = $request->activity_id;
  118. if ($activity_id == 'undefined') {
  119. $activity_id = Activity::orderBy('id', 'DESC')->first()->id;
  120. }
  121. $upload_path = public_path('upload');
  122. $file_name = $request->file->getClientOriginalName();
  123. $file_path = $upload_path . '/' . $file_name;
  124. if (File::exists($file_path)) {
  125. File::delete($file_path);
  126. }
  127. $request->file->move($upload_path, $file_name);
  128. $extension = pathinfo($file_path, PATHINFO_EXTENSION);
  129. if ('csv' == $extension) {
  130. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
  131. } else if ('xls' == $extension) {
  132. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
  133. } else if ('xlsx' == $extension) {
  134. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  135. }
  136. $reader->setReadDataOnly(true);
  137. $reader->setReadEmptyCells(false);
  138. $inputFileName = public_path('upload') . '\\' . $file_name;
  139. $spreadsheet = $reader->load($inputFileName);
  140. $worksheet = $spreadsheet->getActiveSheet();
  141. $highestRow = $worksheet->getHighestDataRow();
  142. date_default_timezone_set('Asia/Taipei');
  143. if ($option == 'prize') {
  144. if ($worksheet->getCell([1, 1])->getValue() == '獎品') {
  145. if(DB::table('prizes')->where('activity_id', '=', $activity_id)->exists()){
  146. DB::table('prizes')->where('activity_id', '=', $activity_id)->delete();
  147. }
  148. for ($row = 2; $row <= $highestRow; $row++) {
  149. DB::table('prizes')->insert([
  150. 'activity_id' => $activity_id,
  151. 'name' => $worksheet->getCell([1, $row])->getValue(),
  152. 'count' => $worksheet->getCell([2, $row])->getValue(),
  153. 'provider' => $worksheet->getCell([3, $row])->getValue(),
  154. 'created_at' => now(),
  155. 'updated_at' => now(),
  156. ]);
  157. }
  158. $isSuccess = true;
  159. } else {
  160. $isSuccess = false;
  161. }
  162. } else if ($option == 'guest') {
  163. if ($worksheet->getCell([1, 1])->getValue() == '來賓編號') {
  164. if(DB::table('check_ins')->where('activity_id', '=', $activity_id)->where('region', '=', '來賓')->exists()){
  165. DB::table('check_ins')->where('activity_id', '=', $activity_id)->where('region', '=', '來賓')->delete();
  166. }
  167. for ($row = 2; $row <= $highestRow; $row++) {
  168. DB::table('check_ins')->insert([
  169. 'user_id' => $worksheet->getCell([1, $row])->getValue(),
  170. 'name' => $worksheet->getCell([2, $row])->getValue(),
  171. 'department_id' => 'guest',
  172. 'region' => '來賓',
  173. 'is_checked_in' => false,
  174. 'is_awarded' => false,
  175. 'activity_id' => $activity_id,
  176. 'created_at' => now(),
  177. 'updated_at' => now(),
  178. ]);
  179. }
  180. $isSuccess = true;
  181. } else {
  182. $isSuccess = false;
  183. }
  184. }
  185. $response = [
  186. 'isSuccess' => $isSuccess,
  187. ];
  188. return response($response, 201);
  189. }
  190. }