Browse Source

完成亂數機率分布

maa3520 2 years ago
parent
commit
ac1aacb5d4

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

@@ -28,8 +28,7 @@ class DrawController extends Controller
         $prize = Prize::where('id', $request->prize_id)->first();
         if ($prize->count > 0) {
             $activity = Activity::where('id', $request->activity_id)->first();
-            $users = CheckIn::
-            where('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)
@@ -122,29 +121,22 @@ class DrawController extends Controller
 
     public function draw_test(Request $request)
     {
-
-        $prize = Prize::where('id', $request->prize_id)->first();
-        $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()
-            ->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;
+        
+        $output = [];
+        $draw_count = $request->number;
+        //去掉判斷只保留邏輯
+        for($i = 0; $i < $draw_count ;$i++) {
+            $users = CheckIn::where('activity_id', $request->activity_id)
+                // ->where('is_checked_in', 'true')
+                // ->where('is_awarded', 'false')
+                ->whereIn('region', $request->region)
+                ->join('departments', 'check_ins.department_id', '=', 'departments.department_id')
+                ->inRandomOrder()
+                ->get();
+    
+            $draw_user = $users->random(1);
+            array_push($output,$draw_user[0]->user_id);
         }
-        return false;
+        return $output;
     }
 }

+ 21 - 11
resources/js/src/views/lottery/RandomTest.vue

@@ -9,7 +9,7 @@
 					</b-card-title>
 				</div>
 				<div class="d-flex align-items-center">
-					<b-button variant="info" @click="draw">抽獎管理頁面</b-button>
+					<b-button variant="info" @click="draw">隨機抽100次</b-button>
 				</div>
 			</b-card-header>
 
@@ -85,10 +85,10 @@ export default {
 			chartjsData,
 			rangePicker: ['2019-05-01', '2019-05-10'],
 			randomData: {
-				labels: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140],
+				labels: [],
 				datasets: [
 					{
-						data: [80, 150, 180, 270, 210, 160, 160, 202, 265, 210, 270, 255, 290, 360, 375],
+						data: [],
 						label: '中獎次數',
 						borderColor: chartColors.lineChartDanger,
 						lineTension: 0.5,
@@ -108,17 +108,19 @@ export default {
 					}
 				],
 			},
+			data: {},
 		}
 	},
 	created() {
 		this.loaded = false
-
 		useJwt.postData('/api/check_in/index_by_activity', { activity_id: this.activityId }).then(res => {
+			console.log(res.data)
 
 			this.userList = res.data;
 			let array = Object.keys(res.data);
 			this.randomData.labels = array;
 			this.randomData.datasets[0].data = Array(array.length).fill(0)
+			this.data = array.reduce((acc, curr) => (acc[curr] = 0, acc), {});
 			this.loaded = true
 		});
 
@@ -128,27 +130,35 @@ export default {
 		});
 
 	},
-	mounted() {
-		console.log("mounted")
-	},
 	computed: {
 
 	},
 	methods: {
 		draw() {
-			useJwt.postData('/api/draw/draw', {
+			useJwt.postData('/api/randomTest/draw', {
 				activity_id: this.activityId,
 				prize_id: this.prizeList[2].id,
 				region: ['北區', '中區', '南區', '來賓'],
 				number: 100
 			}).then(res => {
 				this.drawList = res.data;
+				let result = this.countOccurrences(this.data, res.data);
+				this.randomData.datasets[0].data = result;
 				console.log(res.data);
+				console.log(result);
 			});
 		},
-	},
-	watch: {
-
+		countOccurrences(obj1, array1) {
+			const obj = obj1;
+			array1.forEach((item) => {
+				if (obj.hasOwnProperty(item)) {
+					obj[item]++;
+				}
+			});
+			this.data = obj;
+			let array2 = Object.values(obj);
+			return array2;
+		}
 	},
 	mounted() {
 

+ 14 - 1
resources/js/src/views/lottery/charts-components/ChartjsComponentLineChart.vue

@@ -22,7 +22,20 @@ export default {
     },
   },
   mounted() {
-    this.renderChart(this.data, this.options, this.plugins, this.styles)
+    this.render();
+  },
+  methods: {
+    render() {
+      this.renderChart(this.data, this.options, this.plugins, this.styles)
+    },
+  },
+  watch: {
+    data: {
+      handler: function (newVal, oldVal) {
+        this.render();
+      },
+      deep: true
+    }
   },
 }
 </script>

+ 2 - 2
resources/js/src/views/lottery/charts-components/chartjsData.js

@@ -67,9 +67,9 @@ export default {
                         display: true,
                     },
                     ticks: {
-                        stepSize: 10,
+                        stepSize: 2,
                         min: 0,
-                        max: 50,
+                        max: 10,
                         fontColor: chartColors.labelColor,
                     },
                     gridLines: {

+ 3 - 0
routes/api.php

@@ -98,6 +98,9 @@ Route::middleware(['auth:sanctum', 'abilities:Admin'])->group(function () {
 
     //DrawManager
     Route::post('/drawManager', [DrawController::class, 'send']);
+
+    //Tester
+    Route::post('/randomTest/draw', [DrawController::class, 'draw_test']);
 });
 
 Route::middleware(['auth:sanctum', 'ability:Draw,Admin'])->group(function () {