Przeglądaj źródła

Merge branch 'kevin'

maa3606 2 lat temu
rodzic
commit
823c1d2aa0

+ 5 - 0
app/Http/Controllers/ActivityController.php

@@ -35,6 +35,11 @@ class ActivityController extends Controller
         return $activities;
     }
 
+    public function get_activity_by_id(Request $request)
+    {
+        return Activity::where('id', $request->id)->first();
+    }
+
     /**
      * Show the form for creating a new resource.
      *

+ 11 - 1
app/Http/Controllers/DrawController.php

@@ -4,7 +4,17 @@ namespace App\Http\Controllers;
 
 use Illuminate\Http\Request;
 
+use App\Models\CheckIn;
+use App\Models\Prize;
+
 class DrawController extends Controller
 {
-    //
+    public function draw_user_by_region(Request $request)
+    {
+        return CheckIn::where('is_checked_in', 'true')
+            ->whereTime('check_ins.updated_at', '<=', $request->deadline)
+            ->whereIn('region', $request->region)
+            ->get()
+            ->random($request->number);
+    }
 }

+ 25 - 2
app/Http/Controllers/PrizeController.php

@@ -3,6 +3,7 @@
 namespace App\Http\Controllers;
 
 use App\Models\Prize;
+use Illuminate\Http\Request;
 use App\Http\Requests\StorePrizeRequest;
 use App\Http\Requests\UpdatePrizeRequest;
 
@@ -23,9 +24,13 @@ class PrizeController extends Controller
         return Prize::where('activity_id', $request->activityId)->get();
     }
 
-    public function drawable_prizes()
+    public function prize_is_drawable(Request $request)
     {
-        return Prize::where('count', '>', 0)->get();
+        $prize = Prize::where('id', $request->prizeId)->first();
+        if ($prize->count >= $request->number) {
+            return true;
+        }
+        return false;
     }
 
     /**
@@ -101,6 +106,24 @@ class PrizeController extends Controller
         return false;
     }
 
+    public function draw_patch(UpdatePrizeRequest $request, Prize $prize)
+    {
+        $prize = Prize::where('id', $request->prizeId)->first();
+        if (!$prize) {
+            // return 'prize not found';
+            return false;
+        }
+        if ($prize->count >= $request->number) {
+            $prize->update([
+                'count' => $prize->count - $request->number,
+                'updated_at' => now(),
+            ]);
+            return true;
+        }
+        // return 'prize is empty';
+        return false;
+    }
+
     /**
      * Remove the specified resource from storage.
      *

+ 16 - 0
app/Http/Controllers/RecipientsController.php

@@ -57,6 +57,22 @@ class RecipientsController extends Controller
         return 'success';
     }
 
+    public function store_patch(StoreRecipientsRequest $request)
+    {
+        $data = [];
+        foreach ($request->userIdList as $userId) {
+            array_push($data, [
+                'prize_id' => $request->prizeId,
+                'user_id' => $userId,
+                'is_claimed' => false,
+                'created_at' => now(),
+                'updated_at' => now(),
+            ]);
+        }
+        Recipients::insert($data);
+        return 'success';
+    }
+
     /**
      * Display the specified resource.
      *

+ 22 - 20
database/seeders/CheckInSeeder.php

@@ -21,29 +21,31 @@ class CheckInSeeder extends Seeder
         $countries = json_decode($json);
 
         foreach ($countries as $key => $value) {
-            switch ($value->department_id) { 
-                case 'B00':
-                    $region = '南區';
-                    break; 
-                case 'B10':
-                    $region = '中區';
-                    break;
-                default:
-                    $region = '北區';
+            if ($value->department_id != 'xxx') {
+                switch ($value->department_id) {
+                    case 'B00':
+                        $region = '南區';
+                        break;
+                    case 'B10':
+                        $region = '中區';
+                        break;
+                    default:
+                        $region = '北區';
+                }
+                CheckIn::create([
+                    'user_id' => $value->user_id,
+                    'name' => $value->name,
+                    'department_id' => $value->department_id,
+                    'region' => $region,
+                    'is_checked_in' => true,
+                    'activity_id' => 1,
+                    'created_at' => now(),
+                    'updated_at' => now(),
+                ]);
             }
-            CheckIn::create([
-                'user_id' => $value->user_id,
-                'name' => $value->name,
-                'department_id' => $value->department_id,
-                'region' => $region,
-                'is_checked_in' => true,
-                'activity_id' => 1,
-                'created_at' => now(),
-                'updated_at' => now(),
-            ]);
         }
         CheckIn::create([
-            'user_id' => 'A1000',
+            'user_id' => 'A100',
             'name' => '來賓1000',
             'department_id' => null,
             'region' => '來賓',

+ 1 - 1
database/seeders/DatabaseSeeder.php

@@ -49,7 +49,7 @@ class DatabaseSeeder extends Seeder
             ActivitySeeder::class,
             CheckInSeeder::class,
             PrizeSeeder::class,
-            // RecipientsSeeder::class,
+            RecipientsSeeder::class,
             DepartmentSeeder::class,
         ]);
     }

+ 8 - 8
database/seeders/RecipientsSeeder.php

@@ -18,13 +18,13 @@ class RecipientsSeeder extends Seeder
     public function run()
     {
         Recipients::truncate();
-        $prizes = Prize::all();
-        foreach ($prizes as $prize) {
-            Recipients::create([
-                'prize_id' => $prize->id,
-                'user_id' => User::all()->random()->user_id,
-                'is_claimed' => false
-            ]);
-        }
+        // $prizes = Prize::all();
+        // foreach ($prizes as $prize) {
+        //     Recipients::create([
+        //         'prize_id' => $prize->id,
+        //         'user_id' => User::all()->random()->user_id,
+        //         'is_claimed' => false
+        //     ]);
+        // }
     }
 }

+ 1 - 1
resources/js/src/router/config.js

@@ -151,7 +151,7 @@ export const settings = {
             redirect: 'error-404',
         },
         {
-            path: '/draw',
+            path: '/draw/:activity',
             name: 'draw',
             component: () => import('@/views/Draw.vue'),
             meta: {

+ 71 - 66
resources/js/src/views/Draw.vue

@@ -48,14 +48,14 @@
       <b-modal ref="draw-animation" id="draw-animation" title="抽獎" centered no-stacking hide-footer>
         <br>
         <div v-for="(item, index) in drawList" :class=counterId(index)>
-          <div>
-            <i>部門</i>
+          <i :datafinal=item.department>部門</i>
+          <div :datafinal=item.userId>
+            <i>0</i>
+            <i>0</i>
+            <i>0</i>
+            <i>0</i>
           </div>
-          <i>0</i>
-          <i>0</i>
-          <i>0</i>
-          <i>0</i>
-          <span></span>
+          <span :datafinal=item.userName></span>
         </div>
 
         <b-button v-ripple.400="'rgba(255, 255, 255, 0.15)'" class="mt-2" variant="outline-primary" block
@@ -108,7 +108,8 @@ export default {
   },
   data() {
     return {
-      activity: [],
+      activityId: this.$route.params.activity,
+      activity: {},
       userList: [],
       userIdList: [],
       departmentList: [],
@@ -129,10 +130,11 @@ export default {
     }
   },
   created() {
-    this.activity = this.$route.params.activity;
-    console.log(this.activity);
+    useJwt.postData('/api/activity/getActivity', { id: this.activityId }).then(res => {
+      this.activity = res.data;
+    });
 
-    useJwt.postData('/api/check_in/index_by_activity', { activityId: this.activity.id }).then(res => {
+    useJwt.postData('/api/check_in/index_by_activity', { activityId: this.activityId }).then(res => {
       this.userList = res.data;
     });
 
@@ -140,11 +142,11 @@ export default {
       this.userIdList = res.data;
     });
 
-    useJwt.postData('/api/prize/index_by_activity', { activityId: this.activity.id }).then(res => {
+    useJwt.postData('/api/prize/index_by_activity', { activityId: this.activityId }).then(res => {
       this.prizeList = res.data;
     });
 
-    useJwt.postData('/api/recipients/index_by_activity', { activityId: this.activity.id }).then(res => {
+    useJwt.postData('/api/recipients/index_by_activity', { activityId: this.activityId }).then(res => {
       this.recipientsList = res.data;
     });
 
@@ -153,6 +155,15 @@ export default {
     });
   },
   methods: {
+    dateFormat(date) {
+      var d = new Date(date);
+      return d.getFullYear()
+        + '/' + (d.getMonth() + 1).toString().padStart(2, '0')
+        + '/' + d.getDay().toString().padStart(2, '0')
+        + '/' + d.getHours().toString().padStart(2, '0')
+        + ':' + d.getMinutes().toString().padStart(2, '0')
+        + ':' + d.getSeconds().toString().padStart(2, '0');
+    },
     getPrizeList(prizeList) {
       var output = [];
       prizeList.forEach(element => {
@@ -176,78 +187,73 @@ export default {
       });
       return output;
     },
+    counterId(id) {
+      return "counter_" + id;
+    },
+    showDraw() {
+      this.$refs['draw-modal'].show();
+    },
+    showDrawError() {
+      this.$refs['draw-error'].show();
+    },
+    hideModal() {
+      this.$refs['draw-modal'].hide();
+      this.$refs['draw-error'].hide();
+      this.$refs['draw-animation'].hide();
+      this.updateDataset();
+    },
+    updateDataset() {
+      useJwt.postData('/api/prize/index_by_activity', { activityId: this.activityId }).then(res => {
+        this.prizeList = res.data;
+      });
+      useJwt.postData('/api/recipients/index_by_activity', { activityId: this.activityId }).then(res => {
+        this.recipientsList = res.data;
+      });
+    },
     click(params) {
       this.prize = this.getPrizeName(params.id);
       this.prizeId = params.id;
       this.drawMax = params["數量"];
       this.drawNumber = 1;
+      this.drawList = [];
       if (this.drawMax == 0) {
         this.showDrawError();
       } else {
         this.showDraw();
       }
     },
-    counterId(id) {
-      return "counter_" + id;
-    },
     draw() {
-      useJwt.postData('/api/check_in/draw_by_region', { deadline: this.activity.deadline, region: this.region, number: this.drawNumber }).then(res => {
-        res.data.forEach(element => {
-          this.drawList.push({ userId: element.user_id, userName: element.name, department: this.departmentList[element.department_id] });
-          useJwt.postData('/api/prize/draw', { prizeId: this.prizeId }).then(p => {
-            if (p.data) {
+      useJwt.postData('/api/draw/draw_user', { deadline: this.activity.deadline, region: this.region, number: this.drawNumber }).then(res => {
+        useJwt.postData('/api/prize/prizeDrawable', { prizeId: this.prizeId, number: this.drawNumber }).then(drawable => {
+          if (drawable) {
+            var userIdList=[];
+            res.data.forEach(element => {
+              this.drawList.push({ userId: element.user_id, userName: element.name, department: this.departmentList[element.department_id] });
+              userIdList.push(element.user_id);
+            });
+            useJwt.postData('/api/recipients/storePatch', { prizeId: this.prizeId, userIdList: userIdList }).then(r => {
+              console.log(r.data);
+              useJwt.postData('/api/prize/drawPatch', { prizeId: this.prizeId, number: this.drawNumber });
+            });
+            setTimeout(() => {
               this.count(this.drawList);
-              useJwt.postData('/api/recipients/store', { prizeId: this.prizeId, userId: element.user_id }).then(r => {
-                console.log(r.data);
-              });
-            } else {
-              console.log(p.data);
-            }
-          });
+            });
+          } else {
+            console.log("prize is not available.");
+          }
         });
       });
     },
-    updateDataset() {
-      useJwt.postData('/api/prize/index_by_activity', { activityId: this.activity.id }).then(res => {
-        this.prizeList = res.data;
-      });
-      useJwt.postData('/api/recipients/index_by_activity', { activityId: this.activity.id }).then(res => {
-        this.recipientsList = res.data;
-      });
-    },
-    dateFormat(date) {
-      var d = new Date(date);
-      return d.getFullYear()
-        + '/' + (d.getMonth() + 1).toString().padStart(2, '0')
-        + '/' + d.getDay().toString().padStart(2, '0')
-        + '/' + d.getHours().toString().padStart(2, '0')
-        + ':' + d.getMinutes().toString().padStart(2, '0')
-        + ':' + d.getSeconds().toString().padStart(2, '0');
-    },
-    showDraw() {
-      this.$refs['draw-modal'].show();
-    },
-    showDrawError() {
-      this.$refs['draw-error'].show();
-    },
-    hideModal() {
-      this.$refs['draw-modal'].hide();
-      this.$refs['draw-error'].hide();
-      this.$refs['draw-animation'].hide();
-      this.updateDataset();
-    },
-    count(drawUserList) {
+    count() {
       var departments = this.departments;
       var numbers = "0123456789"
       var string = numbers;
-      
-      drawUserList.forEach((element, index) => {
-        var deps = document.querySelectorAll(".counter_" + index + " > div > i");
-        var allCounters = document.querySelectorAll(".counter_" + index + " > i");
 
-        // TODO: 抽獎時間重疊
+      this.drawList.forEach((element, index) => {
+        var deps = document.querySelectorAll(".counter_" + index + " > i");
+        var allCounters = document.querySelectorAll(".counter_" + index + " > div > i");
         deps.forEach(function (el) {
-          var duration = 2000;
+          var duration = 1000;
           var interval = setInterval(function () {
             el.innerText = departments[Math.floor(Math.random() * departments.length)];
             duration = duration - 50;
@@ -258,7 +264,7 @@ export default {
           }, 50);
         });
         allCounters.forEach((el, i) => {
-          var duration = 4000 + i * 1000;
+          var duration = 2000 + i * 1000;
           var interval = setInterval(e => {
             el.innerText = string.charAt(Math.random() * string.length);
             duration = duration - 50;
@@ -273,7 +279,6 @@ export default {
         });
       });
     },
-
   },
 }
 </script>

+ 8 - 2
routes/api.php

@@ -14,6 +14,7 @@ use App\Http\Controllers\PrizeController;
 use App\Http\Controllers\CheckInController;
 use App\Http\Controllers\RecipientsController;
 use App\Http\Controllers\DepartmentController;
+use App\Http\Controllers\DrawController;
 
 /*
 |--------------------------------------------------------------------------
@@ -96,19 +97,24 @@ Route::middleware(['auth:sanctum', 'abilities:Admin'])->group(function () {
     Route::post('/activity/claim', [ActivityController::class, 'claim']);
     Route::post('/activity/isClaimed', [RecipientsController::class, 'isClaimed']);
     Route::post('/activity/uploadFile', [ActivityController::class, 'uploadFile']);
+    Route::post('/activity/getActivity', [ActivityController::class, 'get_activity_by_id']);
     Route::post('/activity/show', [ActivityController::class, 'show']);
 
     // CheckIn
     Route::post('/check_in/update', [CheckInController::class, 'update']);
     Route::post('/check_in/check_out', [CheckInController::class, 'check_out']);
     Route::post('/check_in/draw', [CheckInController::class, 'draw_user_when_check_in']);
-    Route::post('/check_in/draw_by_region', [CheckInController::class, 'draw_user_by_region']);
+
+    // Draw
+    Route::post('/draw/draw_user', [DrawController::class, 'draw_user_by_region']);
 
     // Prize
     Route::post('/prize/draw', [PrizeController::class, 'draw']);
+    Route::post('/prize/drawPatch', [PrizeController::class, 'draw_patch']);
 
     // Recipients
     Route::post('/recipients/store', [RecipientsController::class, 'store']);
+    Route::post('/recipients/storePatch', [RecipientsController::class, 'store_patch']);
 
     Route::post('/user/show', [UserController::class, 'show']);
 });
@@ -120,4 +126,4 @@ Route::post('/prize/index_by_activity', [PrizeController::class, 'index_by_activ
 Route::post('/recipients/index', [RecipientsController::class, 'index']);
 Route::post('/recipients/index_by_activity', [RecipientsController::class, 'index_by_activity']);
 
-Route::post('/prize/drawable_prizes', [PrizeController::class, 'drawable_prizes']);
+Route::post('/prize/prizeDrawable', [PrizeController::class, 'prize_is_drawable']);