From 6ed12d2ec6f2802b2cc74b5214e0661ee3153f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E9=92=8A?= Date: Tue, 2 Sep 2025 15:08:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B7=A1=E4=B9=90=E4=BC=9A=E6=8A=BD=E5=A5=96?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=8F=90=E4=BA=A4.-=E8=81=94=E8=B0=83-?= =?UTF-8?q?=E6=8A=BD=E5=A5=96=E9=80=BB=E8=BE=91=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/model/BlindBoxTurntableGift.php | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/application/api/model/BlindBoxTurntableGift.php b/application/api/model/BlindBoxTurntableGift.php index 6b9c262..799f0aa 100644 --- a/application/api/model/BlindBoxTurntableGift.php +++ b/application/api/model/BlindBoxTurntableGift.php @@ -196,13 +196,20 @@ class BlindBoxTurntableGift extends Model * 单人单次抽奖 */ public function draw_gift_one($gift_bag_id, $user_id, $gift_user_id,$gift_price,$periods,$room_id=0,$box_turntable_log=0){ + //奖池总的抽奖次数 + $total_quantity = db::name("vs_gift_bag_detail")->where(['gift_bag_id' => $gift_bag_id])->sum('quantity'); + $total_remaining = db::name("vs_room_pan")->where(['room_id'=>$room_id,'gift_bag_id'=>$gift_bag_id])->value('remaining_number'); + //本期当前第多少次后抽奖 总的抽奖次数- 剩余数量 + $total_draw_times = $total_quantity - $total_remaining; //随机获取一个礼物 $where = [ 'a.gift_bag_id' => $gift_bag_id, 'a.quantity' => ['>',0], 'b.remaining_number' => ['>',0], - 'b.room_id' => $room_id + 'b.room_id' => $room_id, ]; + // 使用闭包条件来处理复杂的 weight 逻辑 + $where['a.weight'] = ['exp', Db::raw('= 0 OR a.weight < '.$total_draw_times)]; // 优化:基于剩余数量的加权随机选择 $gift_bag_details = db::name("vs_gift_bag_detail") ->field('a.id,a.quantity,b.remaining_number,a.weight,a.foreign_id') @@ -213,26 +220,18 @@ class BlindBoxTurntableGift extends Model if (empty($gift_bag_details)) { return ['code' => 0, 'msg' => '当前盲盒无可用礼物', 'data' => []]; } - //奖池总的抽奖次数 - $total_quantity = db::name("vs_gift_bag_detail")->where(['gift_bag_id' => $gift_bag_id])->sum('quantity'); // 实现加权随机算法:剩余数量越多,被抽中的概率越大 - $total_remaining = 0; + $remaining = 0; foreach ($gift_bag_details as $gift) { - $total_remaining += $gift['remaining_number']; + $remaining += $gift['remaining_number']; } - //本期当前第多少次后抽奖 总的抽奖次数- 剩余数量 - $total_draw_times = $total_quantity - $total_remaining; - - $rand_value = mt_rand(1, $total_remaining); + $rand_value = mt_rand(1, $remaining); $current_sum = 0; $gift_bag_detail = null; foreach ($gift_bag_details as $gift) { if($gift['remaining_number'] <= 0){ continue; } - if($total_draw_times < $gift['weight']){ - continue; - } $current_sum += $gift['remaining_number']; if ($rand_value <= $current_sum) { $gift_bag_detail = $gift; @@ -494,16 +493,24 @@ class BlindBoxTurntableGift extends Model } $is_zhong_jiang = 0; $pan_xlh_num = $pan_xlh['num']; + //奖池总的抽奖次数 + $total_quantity = db::name("vs_gift_bag_detail")->where(['gift_bag_id' => $gift_bag_id])->sum('quantity'); + db::startTrans(); try{ for($i = 0; $i < $num; $i++){ + //本期当前第多少次后抽奖 总的抽奖次数- 剩余数量 + $total_remaining = db::name("vs_room_pan")->where(['room_id'=>$room_id,'gift_bag_id'=>$gift_bag_id])->value('remaining_number'); + $total_draw_times = $total_quantity - $total_remaining; //随机获取一个礼物 $where = [ 'a.gift_bag_id' => $gift_bag_id, 'a.quantity' => ['>',0], 'b.remaining_number' => ['>',0], - 'b.room_id' => $room_id + 'b.room_id' => $room_id, ]; + // 使用闭包条件来处理复杂的 weight 逻辑 + $where['a.weight'] = ['exp', Db::raw('= 0 OR a.weight < '.$total_draw_times)]; // 优化:基于剩余数量的加权随机选择 $gift_bag_details = db::name("vs_gift_bag_detail") ->field('a.id,a.quantity,b.remaining_number,a.weight,a.foreign_id') @@ -511,29 +518,23 @@ class BlindBoxTurntableGift extends Model ->join('vs_room_pan b','b.gift_bag_detail_id = a.id','left') ->where($where) ->select(); + print_r($gift_bag_details);die; if (empty($gift_bag_details)) { return ['code' => 0, 'msg' => '当前盲盒无可用礼物', 'data' => []]; } - //奖池总的抽奖次数 - $total_quantity = db::name("vs_gift_bag_detail")->where(['gift_bag_id' => $gift_bag_id])->sum('quantity'); - // 实现加权随机算法:剩余数量越多,被抽中的概率越大 - $total_remaining = 0; - foreach ($gift_bag_details as $gift) { - $total_remaining += $gift['remaining_number']; - } - //本期当前第多少次后抽奖 总的抽奖次数- 剩余数量 - $total_draw_times = $total_quantity - $total_remaining; - $rand_value = mt_rand(1, $total_remaining); + // 实现加权随机算法:剩余数量越多,被抽中的概率越大 + $remaining = 0; + foreach ($gift_bag_details as $gift) { + $remaining += $gift['remaining_number']; + } + $rand_value = mt_rand(1, $remaining); $current_sum = 0; $gift_bag_detail = null; foreach ($gift_bag_details as $gift) { if($gift['remaining_number'] <= 0){ continue; } - if($total_draw_times < $gift['weight']){ - continue; - } $current_sum += $gift['remaining_number']; if ($rand_value <= $current_sum) { $gift_bag_detail = $gift;