Pārlūkot izejas kodu

Merge branch 'master' of http://10.1.1.202:3030/steve07s/lottery

maa3520 2 gadi atpakaļ
vecāks
revīzija
4ce230a93b

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

@@ -156,11 +156,12 @@ class ActivityController extends Controller
         $spreadsheet = $reader->load($inputFileName);
         $worksheet = $spreadsheet->getActiveSheet();
         $highestRow = $worksheet->getHighestDataRow();
-
+        date_default_timezone_set('Asia/Taipei');
         if ($option == 'prize') {
             if ($worksheet->getCell([1, 1])->getValue() == '獎品') {
-                DB::table('prizes')->where('activity_id', '=', $activity_id)->delete();
-                date_default_timezone_set('Asia/Taipei');
+                if(DB::table('prizes')->where('activity_id', '=', $activity_id)->exists()){
+                    DB::table('prizes')->where('activity_id', '=', $activity_id)->delete();
+                }
                 for ($row = 2; $row <= $highestRow; $row++) {
                     DB::table('prizes')->insert([
                         'activity_id' => $activity_id,
@@ -177,8 +178,9 @@ class ActivityController extends Controller
             }
         } else if ($option == 'guest') {
             if ($worksheet->getCell([1, 1])->getValue() == '來賓編號') {
-                DB::table('check_ins')->where('activity_id', '=', $activity_id)->where('region', '=', '來賓')->delete();
-                date_default_timezone_set('Asia/Taipei');
+                if(DB::table('check_ins')->where('activity_id', '=', $activity_id)->where('region', '=', '來賓')->exists()){
+                    DB::table('check_ins')->where('activity_id', '=', $activity_id)->where('region', '=', '來賓')->delete();
+                }
                 for ($row = 2; $row <= $highestRow; $row++) {
                     DB::table('check_ins')->insert([
                         'user_id' => $worksheet->getCell([1, $row])->getValue(),
@@ -186,6 +188,7 @@ class ActivityController extends Controller
                         'department_id' => 'guest',
                         'region' => '來賓',
                         'is_checked_in' => false,
+                        'is_awarded' => false,
                         'activity_id' => $activity_id,
                         'created_at' => now(),
                         'updated_at' => now(),

+ 3 - 2
app/Http/Controllers/CheckInController.php

@@ -28,9 +28,10 @@ class CheckInController extends Controller
         //
     }
 
-    public function index_by_activity(UpdateCheckInRequest $request)
+    public function index_by_activity(Request $request)
     {
-        return CheckIn::where('activity_id', $request->activityId)->get();
+        $user = CheckIn::where('activity_id', $request->activityId)->pluck('name', 'user_id');
+        return $user;
     }
 
     public function draw_user_when_check_in(StoreCheckInRequest $request)

+ 38 - 16
app/Http/Controllers/DrawController.php

@@ -22,15 +22,32 @@ class DrawController extends Controller
 
     public function draw(Request $request)
     {
+        $prize = Prize::where('id', $request->prize_id)->first();
         $activity = Activity::where('id', $request->activity_id)->first();
-        $users = CheckIn::where('check_ins.activity_id', $request->activity_id)
+        $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()
-            ->get()
-            ->random($request->number);
+            ->get();
+        if ($prize->count >= $request->number && count($users) >= $request->number) {
+            $output = [];
+            $draw_user = $users->random($request->number);
+            foreach ($draw_user as $user) {
+                array_push($output, [
+                    'user_id' => $user->user_id,
+                    'user_name' => $user->name,
+                    'department_id' => $user->department_id,
+                ]);
+            }
+            return $output;
+        }
+        return false;
+    }
 
+    public function store(Request $request)
+    {
         $prize = Prize::where('id', $request->prize_id)->first();
         if ($prize->count >= $request->number) {
             $prize->update([
@@ -38,26 +55,31 @@ class DrawController extends Controller
                 'updated_at' => now(),
             ]);
 
-            $data = [];
-            $output = [];
-            foreach ($users as $user) {
-                array_push($data, [
+            CheckIn::whereIn('user_id', $request->users)->update(['is_awarded' => true]);
+            $recipientsData = [];
+            foreach ($request->users as $user_id) {
+                array_push($recipientsData, [
                     'prize_id' => $request->prize_id,
-                    'user_id' => $user->user_id,
+                    'user_id' => $user_id,
                     'is_claimed' => false,
                     'created_at' => now(),
                     'updated_at' => now(),
                 ]);
-                array_push($output, [
-                    'user_id' => $user->user_id,
-                    'user_name' => $user->name,
-                    'department_id' => $user->department_id,
-                ]);
             }
-
-            Recipients::insert($data);
-            return $output;
+            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';
+    }
 }

+ 9 - 1
app/Http/Controllers/PrizeController.php

@@ -51,7 +51,15 @@ class PrizeController extends Controller
      */
     public function store(StorePrizeRequest $request)
     {
-        //
+        Prize::insert([
+            'activity_id' => $request->activity_id,
+            'name' => $request->name,
+            'provider' => $request->provider,
+            'count' => $request->count,
+            'created_at' => now(),
+            'updated_at' => now(),
+        ]);
+        return true;
     }
 
     /**

+ 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();
         });

+ 4 - 2
database/seeders/CheckInSeeder.php

@@ -37,7 +37,8 @@ class CheckInSeeder extends Seeder
                     'name' => $value->name,
                     'department_id' => $value->department_id,
                     'region' => $region,
-                    'is_checked_in' => true,
+                    '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(),

+ 2 - 2
database/seeders/DatabaseSeeder.php

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

+ 4 - 3
resources/js/src/views/lottery/Activities/ActivityStore.vue

@@ -1,7 +1,7 @@
 <template>
     <div>
-        <form-wizard color="#7367F0" :title="null" :subtitle="null" layout="vertical" finish-button-text="Submit"
-            back-button-text="Previous" class="wizard-vertical mb-3" @on-complete="formSubmitted">
+        <form-wizard color="#7367F0" :title="null" :subtitle="null" layout="vertical" finish-button-text="完成" next-button-text="下一頁"
+            back-button-text="上一頁" class="wizard-vertical mb-3" @on-complete="formSubmitted">
 
             <tab-content title="新增活動">
 
@@ -165,16 +165,17 @@ export default {
                 this.isSuccess = response.data.isSuccess;
                 if (this.isSuccess == true) {
                     this.$refs['insert-success-modal'].toggle();
-                    this.$refs[refOption].reset();
                 } else if (this.isSuccess == false) {
                     console.log('issuccess false')
                     this.errorText = '匯入Excel檔案錯誤';
                     this.$refs['insert-fail-modal'].toggle();
                 }
+                this.$refs[refOption].reset();
             }).catch(error => {
                 console.log('error: ' + error);
                 this.errorText = error;
                 this.$refs['insert-fail-modal'].toggle();
+                this.$refs[refOption].reset();
             });
         },
         formSubmitted() {

+ 3 - 3
resources/js/src/views/lottery/Activities/ActivityUpdate.vue

@@ -1,7 +1,7 @@
 <template>
     <div>
-        <form-wizard color="#7367F0" :title="null" :subtitle="null" layout="vertical" finish-button-text="Submit"
-            back-button-text="Previous" class="wizard-vertical mb-3" @on-complete="formSubmitted">
+        <form-wizard color="#7367F0" :title="null" :subtitle="null" layout="vertical" finish-button-text="完成" next-button-text="下一頁"
+            back-button-text="前一頁" class="wizard-vertical mb-3" @on-complete="formSubmitted">
 
             <tab-content title="新增活動">
 
@@ -188,7 +188,7 @@ export default {
             this.$toast({
                 component: ToastificationContent,
                 props: {
-                    title: 'Form Submitted',
+                    title: '資料上傳完成',
                     icon: 'EditIcon',
                     variant: 'success',
                 },

+ 158 - 22
resources/js/src/views/lottery/Draw.vue

@@ -3,14 +3,18 @@
     <b-row class="prize-list-wrapper">
       <b-col md="6">
         <b-card title="獎項列表" style="height:75vh;">
-          <b-table responsive hover :items="getPrizeList(prizeList)" bordered :fields="fields" @row-clicked="click"
-            style="max-height: 65vh; overflow: auto;">
+          <b-table responsive hover sticky-header=true :items="getPrizeList(prizeList)" bordered :fields="fields" @row-clicked="click"
+            style="max-height: 57vh; overflow: auto;">
             <template #cell(ICON)="data" class="text-center">
               <div class="text-center">
                 <feather-icon :icon="data.value" />
               </div>
             </template>
           </b-table>
+          <b-button v-ripple.400="'rgba(255, 255, 255, 0.15)'" class="mt-3" variant="outline-primary" block
+            @click="showAddPrize">
+            加碼
+          </b-button>
         </b-card>
       </b-col>
 
@@ -28,16 +32,14 @@
           @click="hideModal">
           Close
         </b-button>
-        <b-button v-ripple.400="'rgba(255, 255, 255, 0.15)'" class="mt-2" variant="outline-primary" block
-          v-b-modal.draw-animation @click="draw">
+        <b-button v-ripple.400="'rgba(255, 255, 255, 0.15)'" class="mt-2" variant="primary" block @click="draw">
           Accept
         </b-button>
-
       </b-modal>
 
-      <b-modal ref="draw-error" centered title="抽獎" no-stacking hide-footer>
+      <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">
@@ -45,7 +47,7 @@
         </b-button>
       </b-modal>
 
-      <b-modal ref="draw-animation" id="draw-animation" title="抽獎" centered no-stacking hide-footer>
+      <b-modal ref="draw-animation" title="得獎者是......" centered no-stacking hide-footer>
         <br>
         <div :key="index" v-for="(item, index) in drawList" :class=counterId(index)>
           <i :datafinal=item.department>部門</i>
@@ -58,15 +60,40 @@
           </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>
+      </b-modal>
+
+      <b-modal ref="add-prize" centered title="加碼" no-stacking hide-footer>
+        <label>獎品:</label>
+        <b-form-input v-model="addPrizeName" />
+        <br>
+        <label>加碼者: </label>
+        <b-form-input v-model="addPrizeUser" />
+        <br>
+        <label>數量: </label>
+        <b-form-spinbutton v-model="addPrizeNumber" min="1" />
+
+        <b-button v-ripple.400="'rgba(255, 255, 255, 0.15)'" class="mt-3" variant="outline-secondary" block
           @click="hideModal">
+          Close
+        </b-button>
+        <b-button v-ripple.400="'rgba(255, 255, 255, 0.15)'" class="mt-2" variant="primary" block @click="addPrize">
           Accept
         </b-button>
       </b-modal>
 
       <b-col md="6">
         <b-card title="抽獎紀錄" style="height:75vh;">
-          <b-table responsive :items="getRecipientsList(recipientsList)" bordered
+          <b-table responsive sticky-header=true :items="getRecipientsList(recipientsList)" bordered
             style="max-height: 65vh; overflow: auto;">
             <template #cell(ICON)="data" class="text-center">
               <div class="text-center">
@@ -111,7 +138,6 @@ export default {
       activityId: this.$route.params.activity_id,
       activity: {},
       userList: [],
-      userIdList: [],
       departmentList: [],
       prizeList: [],
       recipientsList: [],
@@ -126,7 +152,55 @@ export default {
       prizeId: '',
       drawList: [],
 
-      departments: ["行政管理部", "行政管理部庶務科", "人力資源科", "行政管理部文書科", "南區辦事處", "中區辦事處", "工程管理一部", "工程管理二部", "c3工地", "發展與企畫部", "工程製圖科", "工務部", "大地工程部", "財務部", "六張犁工地", "中華郵政工地", "法務部調查局工地", "三峽國光工地", "河濱高中工地", "運輸及土木工程部", "城鄉發展部", "軌道工程部", "董事長室", "總經理室", "企業發展中心", "成本中心", "資訊中心", "環境及水資源工程部", "環境水務部", "環境永續部", "機電系統工程部", "結構工程部", "工程設計群", "工程監理群", "總管理處", "建築及設施群", "數位工程發展部", "淡海輕軌計畫監造專案", "安坑輕軌計畫監造專案", "先進技術中心", "塭仔圳市地重劃公共工程專案管理與監造專案", "萬大果菜市場專案"]
+      addPrizeName: '',
+      addPrizeUser: '',
+      addPrizeNumber: 1,
+
+      departments: [
+        "行政管理部",
+        "行政管理部庶務科",
+        "人力資源科",
+        "行政管理部文書科",
+        "南區辦事處",
+        "中區辦事處",
+        "工程管理一部",
+        "工程管理二部",
+        "c3工地",
+        "發展與企畫部",
+        "工程製圖科",
+        "工務部",
+        "大地工程部",
+        "財務部",
+        "六張犁工地",
+        "中華郵政工地",
+        "法務部調查局工地",
+        "三峽國光工地",
+        "河濱高中工地",
+        "運輸及土木工程部",
+        "城鄉發展部",
+        "軌道工程部",
+        "董事長室",
+        "總經理室",
+        "企業發展中心",
+        "成本中心",
+        "資訊中心",
+        "環境及水資源工程部",
+        "環境水務部",
+        "環境永續部",
+        "機電系統工程部",
+        "結構工程部",
+        "工程設計群",
+        "工程監理群",
+        "總管理處",
+        "建築及設施群",
+        "數位工程發展部",
+        "淡海輕軌計畫監造專案",
+        "安坑輕軌計畫監造專案",
+        "先進技術中心",
+        "塭仔圳市地重劃公共工程專案管理與監造專案",
+        "萬大果菜市場專案",
+        "來賓"
+      ]
     }
   },
   created() {
@@ -138,10 +212,6 @@ export default {
       this.userList = res.data;
     });
 
-    useJwt.postData('/api/user/index').then(res => {
-      this.userIdList = res.data;
-    });
-
     useJwt.postData('/api/prize/index_by_activity', { activityId: this.activityId }).then(res => {
       this.prizeList = res.data;
     });
@@ -183,16 +253,28 @@ export default {
     getRecipientsList(recipientsList) {
       var output = [];
       recipientsList.forEach(element => {
-        output.push({ '獎項': this.getPrizeName(element.prize_id), '獲獎人': this.userIdList[element.user_id], '時間': this.dateFormat(element.created_at) });
+        output.push({ '獎項': this.getPrizeName(element.prize_id), '獲獎人': this.userList[element.user_id], '時間': this.dateFormat(element.created_at) });
       });
       return output;
     },
     counterId(id) {
       return "counter_" + id;
     },
+    onlyOne(number) {
+      if (number == 1) {
+        return true;
+      }
+      return false;
+    },
     showDraw() {
       this.$refs['draw-modal'].show();
     },
+    showDrawAnimation() {
+      this.$refs['draw-animation'].show();
+    },
+    showAddPrize() {
+      this.$refs['add-prize'].show();
+    },
     showDrawError() {
       this.$refs['draw-error'].show();
     },
@@ -200,6 +282,7 @@ export default {
       this.$refs['draw-modal'].hide();
       this.$refs['draw-error'].hide();
       this.$refs['draw-animation'].hide();
+      this.$refs['add-prize'].hide();
       this.updateDataset();
     },
     updateDataset() {
@@ -223,16 +306,69 @@ export default {
       }
     },
     draw() {
-      useJwt.postData('/api/draw/draw', { activity_id: this.activityId, prize_id: this.prizeId, region: this.region, number: this.drawNumber }).then(res => {
-        if(res.data){
+      useJwt.postData('/api/draw/draw', { 
+        activity_id: this.activityId, 
+        prize_id: this.prizeId, 
+        region: this.region, 
+        number: this.drawNumber 
+      }).then(res => {
+        if (res.data) {
           this.drawList = res.data;
           setTimeout(() => {
             this.count();
-          }, 100);
+          }, 500);
+          this.showDrawAnimation();
+        } else {
+          this.showDrawError();
+          console.log("draw error");
+        }
+      });
+    },
+    store() {
+      var idList = [];
+      this.drawList.forEach(element => {
+        idList.push(element.user_id);
+      });
+      useJwt.postData('/api/draw/store', { 
+        users: idList, 
+        prize_id: String(this.prizeId), 
+        number: this.drawNumber 
+      }).then(res => {
+        if (res.data) {
+          console.log("store success");
+        } else {
+          console.log("store error");
+        }
+        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("donate success");
+        } else {
+          console.log("donate error");
+        }
+        this.hideModal();
+      });
+    },
+    addPrize() {
+      useJwt.postData('/api/prize/store', {
+        activity_id: this.activityId,
+        name: this.addPrizeName,
+        provider: this.addPrizeUser,
+        count: this.addPrizeNumber
+      }).then(res => {
+        if (res.data) {
+          console.log("add prize success");
         } else {
-          this.hideModal();
-          console.log("error");
+          console.log("add prize error");
         }
+        this.hideModal();
       });
     },
     count() {

+ 4 - 0
routes/api.php

@@ -108,8 +108,12 @@ 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
+    Route::post('/prize/store', [PrizeController::class, 'store']);
 
     // Recipients
     Route::post('/recipient/claim', [RecipientsController::class, 'claim']);