ActivityController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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->year = $request->year;
  58. $activity->date = $request->date;
  59. $activity->place = $request->place;
  60. $activity->user_id = $request->user_id;
  61. $activity->deadline = $request->deadline;
  62. $activity->save();
  63. }
  64. /**
  65. * Display the specified resource.
  66. *
  67. * @param \App\Models\Activity $activity
  68. * @return \Illuminate\Http\Response
  69. */
  70. public function show(Request $request, Activity $activity)
  71. {
  72. //
  73. $activity = Activity::where('id', '=', $request->activity_id)->first();
  74. return $activity;
  75. }
  76. /**
  77. * Show the form for editing the specified resource.
  78. *
  79. * @param \App\Models\Activity $activity
  80. * @return \Illuminate\Http\Response
  81. */
  82. public function edit(Activity $activity)
  83. {
  84. //
  85. }
  86. /**
  87. * Update the specified resource in storage.
  88. *
  89. * @param \App\Http\Requests\UpdateActivityRequest $request
  90. * @param \App\Models\Activity $activity
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function update(UpdateActivityRequest $request, Activity $activity)
  94. {
  95. //
  96. Activity::where('id', '=', $request->activity_id)->update([
  97. 'activity_name' => $request->activity_name,
  98. 'year' => $request->year,
  99. 'date' => $request->date,
  100. 'place' => $request->place,
  101. 'user_id' => $request->user_id,
  102. 'deadline' => $request->deadline,
  103. ]);
  104. }
  105. /**
  106. * Remove the specified resource from storage.
  107. *
  108. * @param \App\Models\Activity $activity
  109. * @return \Illuminate\Http\Response
  110. */
  111. public function destroy(Activity $activity)
  112. {
  113. //
  114. }
  115. //這邊有兩種insert,要怎麼處理StoreRequest?
  116. public function uploadFile(Request $request)
  117. {
  118. $option = $request->option;
  119. $activity_id = $request->activity_id;
  120. if ($activity_id == 'undefined') {
  121. $activity_id = Activity::orderBy('id', 'DESC')->first()->id;
  122. }
  123. $upload_path = public_path('upload');
  124. $file_name = $request->file->getClientOriginalName();
  125. $file_path = $upload_path . '/' . $file_name;
  126. if (File::exists($file_path)) {
  127. File::delete($file_path);
  128. }
  129. $request->file->move($upload_path, $file_name);
  130. $extension = pathinfo($file_path, PATHINFO_EXTENSION);
  131. if ('csv' == $extension) {
  132. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
  133. } else if ('xls' == $extension) {
  134. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
  135. } else if ('xlsx' == $extension) {
  136. $reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
  137. }
  138. $reader->setReadDataOnly(true);
  139. $reader->setReadEmptyCells(false);
  140. $inputFileName = public_path('upload') . '\\' . $file_name;
  141. $spreadsheet = $reader->load($inputFileName);
  142. $worksheet = $spreadsheet->getActiveSheet();
  143. $highestRow = $worksheet->getHighestDataRow();
  144. date_default_timezone_set('Asia/Taipei');
  145. if ($option == 'prize') {
  146. if ($worksheet->getCell([1, 1])->getValue() == '獎品') {
  147. if(DB::table('prizes')->where('activity_id', '=', $activity_id)->exists()){
  148. DB::table('prizes')->where('activity_id', '=', $activity_id)->delete();
  149. }
  150. for ($row = 2; $row <= $highestRow; $row++) {
  151. DB::table('prizes')->insert([
  152. 'activity_id' => $activity_id,
  153. 'name' => $worksheet->getCell([1, $row])->getValue(),
  154. 'count' => $worksheet->getCell([2, $row])->getValue(),
  155. 'provider' => $worksheet->getCell([3, $row])->getValue(),
  156. 'created_at' => now(),
  157. 'updated_at' => now(),
  158. ]);
  159. }
  160. $isSuccess = true;
  161. } else {
  162. $isSuccess = false;
  163. }
  164. } else if ($option == 'guest') {
  165. if ($worksheet->getCell([1, 1])->getValue() == '來賓編號') {
  166. if(DB::table('check_ins')->where('activity_id', '=', $activity_id)->where('region', '=', '來賓')->exists()){
  167. DB::table('check_ins')->where('activity_id', '=', $activity_id)->where('region', '=', '來賓')->delete();
  168. }
  169. for ($row = 2; $row <= $highestRow; $row++) {
  170. DB::table('check_ins')->insert([
  171. 'user_id' => $worksheet->getCell([1, $row])->getValue(),
  172. 'name' => $worksheet->getCell([2, $row])->getValue(),
  173. 'department_id' => 'guest',
  174. 'region' => '來賓',
  175. 'is_checked_in' => false,
  176. 'is_awarded' => false,
  177. 'activity_id' => $activity_id,
  178. 'created_at' => now(),
  179. 'updated_at' => now(),
  180. ]);
  181. }
  182. $isSuccess = true;
  183. } else {
  184. $isSuccess = false;
  185. }
  186. }
  187. $response = [
  188. 'isSuccess' => $isSuccess,
  189. ];
  190. return response($response, 201);
  191. }
  192. }