Browse Source

捐獎功能

maa3606 2 years ago
parent
commit
f6419cad13

+ 17 - 3
app/Http/Controllers/DrawController.php

@@ -26,6 +26,7 @@ class DrawController extends Controller
         $activity = Activity::where('id', $request->activity_id)->first();
         $users = CheckIn::where('activity_id', $request->activity_id)
             ->where('is_checked_in', 'true')
+            ->where('is_awarded', 'false')
             ->whereTime('check_ins.updated_at', '<=', $activity->deadline)
             ->whereIn('region', $request->region)
             ->inRandomOrder()
@@ -53,9 +54,11 @@ class DrawController extends Controller
                 'count' => $prize->count - $request->number,
                 'updated_at' => now(),
             ]);
-            $data = [];
+
+            CheckIn::whereIn('user_id', $request->users)->update(['is_awarded' => true]);
+            $recipientsData = [];
             foreach ($request->users as $user_id) {
-                array_push($data, [
+                array_push($recipientsData, [
                     'prize_id' => $request->prize_id,
                     'user_id' => $user_id,
                     'is_claimed' => false,
@@ -63,9 +66,20 @@ class DrawController extends Controller
                     'updated_at' => now(),
                 ]);
             }
-            Recipients::insert($data);
+            Recipients::insert($recipientsData);
             return true;
         }
         return false;
     }
+
+    public function donate(Request $request)
+    {
+        CheckIn::whereIn('user_id', $request->users)->update(['is_awarded' => true]);
+        return 'success';
+    }
+
+    public function test(Request $request)
+    {
+        return 'success';
+    }
 }

+ 1 - 0
app/Models/CheckIn.php

@@ -15,6 +15,7 @@ class CheckIn extends Model
         'department_id',
         'region',
         'is_checked_in',
+        'is_awarded',
         'activity_id',
     ];
 }

+ 1 - 0
database/migrations/2023_02_06_030248_create_check_ins_table.php

@@ -20,6 +20,7 @@ return new class extends Migration
             $table->string('department_id')->nullable();
             $table->string('region');
             $table->boolean('is_checked_in');
+            $table->boolean('is_awarded');
             $table->string('activity_id');
             $table->timestamps();
         });

+ 3 - 1
database/seeders/CheckInSeeder.php

@@ -38,6 +38,7 @@ class CheckInSeeder extends Seeder
                     'department_id' => $value->department_id,
                     'region' => $region,
                     'is_checked_in' => false,
+                    'is_awarded' => false,
                     'activity_id' => 1,
                     'created_at' => now(),
                     'updated_at' => now(),
@@ -46,10 +47,11 @@ class CheckInSeeder extends Seeder
         }
         CheckIn::create([
             'user_id' => 'A100',
-            'name' => '來賓1000',
+            'name' => '來賓XXX',
             'department_id' => null,
             'region' => '來賓',
             'is_checked_in' => true,
+            'is_awarded' => false,
             'activity_id' => 1,
             'created_at' => now(),
             'updated_at' => now(),

+ 1 - 1
database/seeders/DatabaseSeeder.php

@@ -48,7 +48,7 @@ class DatabaseSeeder extends Seeder
         $this->call([
             ActivitySeeder::class,
             CheckInSeeder::class,
-            // PrizeSeeder::class,
+            PrizeSeeder::class,
             RecipientsSeeder::class,
             DepartmentSeeder::class,
         ]);

+ 24 - 3
resources/js/src/views/lottery/Draw.vue

@@ -28,7 +28,7 @@
           @click="hideModal">
           Close
         </b-button>
-        <b-button v-ripple.400="'rgba(255, 255, 255, 0.15)'" class="mt-2" variant="outline-primary" block @click="draw">
+        <b-button v-ripple.400="'rgba(255, 255, 255, 0.15)'" class="mt-2" variant="primary" block @click="draw">
           Accept
         </b-button>
 
@@ -36,7 +36,7 @@
 
       <b-modal ref="draw-error" centered title="抽獎" no-stacking hide-footer>
         <b-card-text class="my-1 text-center">
-          獎錯誤
+          獎錯誤
         </b-card-text>
         <b-button v-ripple.400="'rgba(255, 255, 255, 0.15)'" class="mt-3" variant="outline-secondary" block
           @click="hideModal">
@@ -57,7 +57,14 @@
           </div>
         </div>
 
-        <b-button v-ripple.400="'rgba(255, 255, 255, 0.15)'" class="mt-2" variant="outline-primary" block
+        <div v-if="onlyOne(drawNumber)">
+          <b-button v-ripple.400="'rgba(255, 255, 255, 0.15)'" class="mt-2" variant="outline-primary" block
+            @click="donate">
+            捐出
+          </b-button>
+        </div>
+
+        <b-button v-ripple.400="'rgba(255, 255, 255, 0.15)'" class="mt-2" variant="primary" block
           @click="store">
           Accept
         </b-button>
@@ -297,6 +304,20 @@ export default {
         this.hideModal();
       });
     },
+    donate() {
+      var idList = [];
+      this.drawList.forEach(element => {
+        idList.push(element.user_id);
+      });
+      useJwt.postData('/api/draw/donate', { users: idList }).then(res => {
+        if (res.data) {
+          console.log("success");
+        } else {
+          console.log("donate error");
+        }
+        this.hideModal();
+      });
+    },
     count() {
       var departments = this.departments;
       var numbers = "0123456789"

+ 2 - 0
routes/api.php

@@ -107,6 +107,8 @@ Route::middleware(['auth:sanctum', 'abilities:Admin'])->group(function () {
     // Draw
     Route::post('/draw/draw', [DrawController::class, 'draw']);
     Route::post('/draw/store', [DrawController::class, 'store']);
+    Route::post('/draw/donate', [DrawController::class, 'donate']);
+    Route::post('/draw/test', [DrawController::class, 'test']);
 
     // Prize