盲盒转盘优化-重构-调试
This commit is contained in:
@@ -37,6 +37,7 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
||||
*/
|
||||
public function draw_gift($gift_bag_id, $user_id, $gift_user_ids, $num = 1, $room_id = 0, $heart_id = 0,$auction_id = 0)
|
||||
{
|
||||
try {
|
||||
// 收礼人
|
||||
$gift_user_ids = explode(',', $gift_user_ids);
|
||||
$total_num = $num * count($gift_user_ids); //总数量
|
||||
@@ -114,6 +115,22 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
||||
);
|
||||
// 6. 构建并返回结果
|
||||
return $this->buildDrawResult($boxTurntableLog, $giftCounts);
|
||||
} catch (\Exception $e) {
|
||||
$key = 'blind_box_draw_errors_' . date('Y-m-d-H-i-s');
|
||||
$errorData = [
|
||||
'gift_bag_id' => $gift_bag_id,
|
||||
'user_id' => $user_id,
|
||||
'gift_user_ids' => $gift_user_ids,
|
||||
'num' => $num,
|
||||
'room_id' => $room_id,
|
||||
'heart_id' => $heart_id,
|
||||
'auction_id' => $auction_id,
|
||||
];
|
||||
if ($this->redis) {
|
||||
$this->redis->setex($key, 86400 * 7, $e->getMessage() . ' ' . json_encode($errorData));
|
||||
}
|
||||
return ['code' => 0, 'msg' => "网络加载失败,请重试!", 'data' => null];
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 预计算抽奖结果
|
||||
@@ -221,6 +238,7 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
||||
$this->getCachedXlhPeriodsNum("set");
|
||||
// 更新Alias表
|
||||
$this->updateAliasTable($aliasTableForNew, $selectedGift['id']);
|
||||
$this->updateCachedGiftBagDetail($gift_bag_id, $selectedGift['id']); //更新缓存中礼物的数量
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -235,8 +253,6 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
||||
{
|
||||
// 查找礼物在Alias表中的位置
|
||||
$gifts = &$aliasTable['gifts'];
|
||||
$indexMap = &$aliasTable['index_map'];
|
||||
|
||||
foreach ($gifts as &$gift) {
|
||||
if ($gift['id'] == $giftId) {
|
||||
$gift['remaining_number']--;
|
||||
@@ -377,15 +393,26 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
||||
{
|
||||
if (!$aliasTable) return null;
|
||||
|
||||
// 添加重试机制,最多尝试5次
|
||||
$maxAttempts = 5;
|
||||
$attempt = 0;
|
||||
while ($attempt < $maxAttempts) {
|
||||
$n = $aliasTable['n'];
|
||||
$k = mt_rand(0, $n - 1);
|
||||
|
||||
// 随机选择
|
||||
if (mt_rand() / mt_getrandmax() < $aliasTable['prob'][$k]) {
|
||||
return $aliasTable['index_map'][$k];
|
||||
$selectedGift = $aliasTable['index_map'][$k];
|
||||
} else {
|
||||
return $aliasTable['index_map'][$aliasTable['alias'][$k]] ?? null;
|
||||
$selectedGift = $aliasTable['index_map'][$aliasTable['alias'][$k]] ?? null;
|
||||
}
|
||||
// 检查选中的礼物是否还有库存
|
||||
if ($selectedGift && $selectedGift['remaining_number'] > 0) {
|
||||
return $selectedGift;
|
||||
}
|
||||
$attempt++;
|
||||
}
|
||||
// 如果重试次数用完仍未抽中有效礼物,返回null
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* 构建Alias表(O(n)复杂度,只执行一次)
|
||||
@@ -468,6 +495,21 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
||||
Cache::set($cacheKey, $gift_bag_detail_data, $this->cache_time);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 获取缓存中的奖池单个礼物信息
|
||||
*/
|
||||
private function getCachedGiftBagDetailItem($gift_bag_id, $detail_id) {
|
||||
$cacheKey = "pan_gift_bag_detail".$gift_bag_id;
|
||||
$gift_bag_detail_item = Cache::get($cacheKey);
|
||||
if($gift_bag_detail_item) {
|
||||
foreach ($gift_bag_detail_item as $item) {
|
||||
if ($item['id'] == $detail_id) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
/**
|
||||
* 获取奖池总库存
|
||||
*/
|
||||
@@ -572,7 +614,7 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
||||
throw new \Exception('添加盲盒转盘记录失败');
|
||||
}
|
||||
// 3. 批量更新库存(按ID排序避免死锁)
|
||||
$this->batchUpdateGiftInventory($availableGiftss);
|
||||
$this->batchUpdateGiftInventory($bag_data['gift_bag_id'],$availableGiftss);
|
||||
|
||||
// 4. 批量插入礼包发放记录
|
||||
$this->batchInsertGiftBagReceiveLog($bag_data,$user_id, $boxTurntableLog,$room_id, $precomputedResults);
|
||||
@@ -638,7 +680,7 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
||||
/**
|
||||
* 批量更新礼物库存
|
||||
*/
|
||||
private function batchUpdateGiftInventory($precomputedResults)
|
||||
private function batchUpdateGiftInventory($gift_bag_id,$precomputedResults)
|
||||
{
|
||||
// 按礼物ID分组统计需要减少的数量
|
||||
$inventoryUpdates = [];
|
||||
@@ -655,6 +697,7 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
||||
$giftBagDetail = Db::name("vs_gift_bag_detail")
|
||||
->where('id', $giftId)
|
||||
->find();
|
||||
$giftBagDetailCached = $this->getCachedGiftBagDetailItem($gift_bag_id, $giftId);
|
||||
if (!$giftBagDetail) {
|
||||
throw new \Exception("礼物详情不存在,ID: " . $giftId);
|
||||
}
|
||||
@@ -662,14 +705,17 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
||||
if ($giftBagDetail['remaining_number'] < $count) {
|
||||
throw new \Exception("礼物库存不足,ID: " . $giftId);
|
||||
}
|
||||
$ret = db::name("vs_gift_bag_detail")->where('id',$giftId)
|
||||
->setDec('remaining_number', $count);
|
||||
$upRemainingNumber = $giftBagDetail['remaining_number'] - $count;
|
||||
if($giftBagDetail['remaining_number']!=$giftBagDetailCached['remaining_number']){
|
||||
$this->redis->setex( 'blind_box_draw_errors_' . date('Y-m-d-H-i-s'), 86400 * 7, "有并发:礼物数量不一致,礼物ID: " . $giftId . ' ' .json_encode($giftBagDetail) . ' ' .json_encode($giftBagDetailCached));
|
||||
$upRemainingNumber = $giftBagDetailCached['remaining_number'];
|
||||
}
|
||||
$ret = db::name("vs_gift_bag_detail")->where('id',$giftId)->update([
|
||||
'remaining_number' => $upRemainingNumber
|
||||
]);
|
||||
if (!$ret) {
|
||||
throw new \Exception('更新礼物剩余数量失败');
|
||||
}
|
||||
|
||||
// 同时更新缓存中的库存信息
|
||||
$this->updateCachedGiftBagDetail($giftBagDetail['gift_bag_id'], $giftId, $count);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user