盲盒转盘优化-重构-修改调试
This commit is contained in:
@@ -62,17 +62,14 @@ class BlindBox extends adminApi
|
|||||||
$lists[$key]['gift_id'] = $value['foreign_id'];
|
$lists[$key]['gift_id'] = $value['foreign_id'];
|
||||||
$lists[$key]['gift_name'] = $value['name'];
|
$lists[$key]['gift_name'] = $value['name'];
|
||||||
$gift_data = db::name('vs_gift')->where(['gid'=>$value['foreign_id']])->find();
|
$gift_data = db::name('vs_gift')->where(['gid'=>$value['foreign_id']])->find();
|
||||||
if(empty($gift_data)){
|
$lists[$key]['base_image'] = $gift_data['base_image'] ?? "";
|
||||||
continue;
|
$lists[$key]['gift_price'] = $gift_data['gift_price'] ?? 0;
|
||||||
}
|
$lists[$key]['quantity'] = $value['quantity'] ?? 0;
|
||||||
$lists[$key]['base_image'] = $gift_data['base_image'];
|
$lists[$key]['remaining_number'] = $value['remaining_number'] ?? 0;
|
||||||
$lists[$key]['gift_price'] = $gift_data['gift_price'];
|
$lists[$key]['is_public_screen'] = $gift_data['is_public_screen'] ?? 0;
|
||||||
$lists[$key]['quantity'] = $value['quantity'];
|
$lists[$key]['is_public_server'] = $gift_data['is_public_server'] ?? 0;
|
||||||
$lists[$key]['remaining_number'] = $value['remaining_number'];
|
$lists[$key]['is_world_show'] = $value['is_world_show'] ?? 0;
|
||||||
$lists[$key]['is_public_screen'] = $gift_data['is_public_screen'];
|
$lists[$key]['weight'] = $value['weight'] ?? 0;
|
||||||
$lists[$key]['is_public_server'] = $gift_data['is_public_server'];
|
|
||||||
$lists[$key]['is_world_show'] = $value['is_world_show'];
|
|
||||||
$lists[$key]['weight'] = $value['weight'];
|
|
||||||
$lists[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
|
$lists[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -354,6 +351,14 @@ class BlindBox extends adminApi
|
|||||||
foreach ($gift_bag_detail as $k=>$v){
|
foreach ($gift_bag_detail as $k=>$v){
|
||||||
$bag_data = db::name($this->table)->where(['id'=>$v['id']])->update(['remaining_number'=>$v['quantity']]);
|
$bag_data = db::name($this->table)->where(['id'=>$v['id']])->update(['remaining_number'=>$v['quantity']]);
|
||||||
}
|
}
|
||||||
|
// 清除缓存
|
||||||
|
Cache::rm("pan_gift_bag".$gift_bag_id);
|
||||||
|
Cache::rm("pan_gift_bag_detail".$gift_bag_id);
|
||||||
|
Cache::rm("pan_total_quantity".$gift_bag_id);
|
||||||
|
Cache::rm("pan_total_remaining".$gift_bag_id);
|
||||||
|
Cache::rm("pan_total_draw".$gift_bag_id);
|
||||||
|
Cache::rm("pan_gift_info_map".$gift_bag_id);
|
||||||
|
|
||||||
return V(1,"成功");
|
return V(1,"成功");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
|||||||
$this->redis->connect(config('redis.host'), config('redis.port')); // 根据实际配置调整主机和端口
|
$this->redis->connect(config('redis.host'), config('redis.port')); // 根据实际配置调整主机和端口
|
||||||
// 选择数据库1
|
// 选择数据库1
|
||||||
$this->redis->select(1);
|
$this->redis->select(1);
|
||||||
$this->cache_time = 60 *24;
|
$this->cache_time = 60*60; //缓存一小时
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
Log::record('Redis连接失败: ' . $e->getMessage(), 'error');
|
Log::record('Redis连接失败: ' . $e->getMessage(), 'error');
|
||||||
$this->redis = null;
|
$this->redis = null;
|
||||||
@@ -336,7 +336,7 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
|||||||
Cache::set($cacheKey, $pan_gift_bag_detail, $this->cache_time);
|
Cache::set($cacheKey, $pan_gift_bag_detail, $this->cache_time);
|
||||||
Cache::set("pan_total_quantity".$gift_bag_id, $total_quantity, $this->cache_time);
|
Cache::set("pan_total_quantity".$gift_bag_id, $total_quantity, $this->cache_time);
|
||||||
Cache::set("pan_total_remaining".$gift_bag_id, $total_remaining, $this->cache_time);
|
Cache::set("pan_total_remaining".$gift_bag_id, $total_remaining, $this->cache_time);
|
||||||
Cache::set("pan_total_draw_times".$gift_bag_id, $total_draw_times, $this->cache_time);
|
Cache::set("pan_total_draw".$gift_bag_id, $total_draw_times, $this->cache_time);
|
||||||
}
|
}
|
||||||
return $pan_gift_bag_detail;
|
return $pan_gift_bag_detail;
|
||||||
}
|
}
|
||||||
@@ -708,30 +708,18 @@ class BlindBoxTurntableGiftDrawWorldNew extends Model
|
|||||||
|
|
||||||
// 批量更新
|
// 批量更新
|
||||||
foreach ($inventoryUpdates as $giftId => $count) {
|
foreach ($inventoryUpdates as $giftId => $count) {
|
||||||
$giftBagDetail = Db::name("vs_gift_bag_detail")
|
|
||||||
->where('id', $giftId)
|
|
||||||
->find();
|
|
||||||
$giftBagDetailCached = $this->getCachedGiftBagDetailItem($gift_bag_id, $giftId);
|
$giftBagDetailCached = $this->getCachedGiftBagDetailItem($gift_bag_id, $giftId);
|
||||||
if (!$giftBagDetail) {
|
if(empty($giftBagDetailCached)){
|
||||||
throw new \Exception("礼物详情不存在,ID: " . $giftId);
|
$giftBagDetail = db::name("vs_gift_bag_detail")->where('id',$giftId)->find();
|
||||||
}
|
$upRemainingNumber = $giftBagDetail['remaining_number'] - $count;
|
||||||
$upRemainingNumber = $giftBagDetail['remaining_number'] - $count;
|
}else{
|
||||||
if($upRemainingNumber!=$giftBagDetailCached['remaining_number']){
|
|
||||||
$this->redis->setex( 'blind_box_draw_errors_' . date('Y-m-d-H-i-s'), 86400 * 7, "有并发:礼物数量不一致,礼物ID: " . $giftId . '数据库数量: '.$upRemainingNumber. '缓存数量: '.$giftBagDetailCached['remaining_number']. ' ' .json_encode($giftBagDetail) . ' ' .json_encode($giftBagDetailCached));
|
|
||||||
$upRemainingNumber = $giftBagDetailCached['remaining_number'];
|
$upRemainingNumber = $giftBagDetailCached['remaining_number'];
|
||||||
}
|
}
|
||||||
// 检查库存是否足够
|
|
||||||
if ($upRemainingNumber < 0) {
|
|
||||||
throw new \Exception("礼物库存不足,ID: " . $giftId);
|
|
||||||
}
|
|
||||||
if($upRemainingNumber = $giftBagDetail['remaining_number']){
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$ret = db::name("vs_gift_bag_detail")->where('id',$giftId)->update([
|
$ret = db::name("vs_gift_bag_detail")->where('id',$giftId)->update([
|
||||||
'remaining_number' => $upRemainingNumber
|
'remaining_number' => $upRemainingNumber
|
||||||
]);
|
]);
|
||||||
if (!$ret) {
|
if (!$ret) {
|
||||||
throw new \Exception('更新礼物剩余数量失败');
|
throw new \Exception('更新礼物剩余数量失败 ID: ' . $giftId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user