解决超出发放礼物问题-盲盒转盘-加Redis统计
This commit is contained in:
@@ -7,12 +7,22 @@ use think\Log;
|
||||
use think\Model;
|
||||
use think\Db;
|
||||
use think\Session;
|
||||
use think\cache\driver\Redis;
|
||||
|
||||
/*
|
||||
* 盲盒转盘优化后方法
|
||||
*
|
||||
*/
|
||||
class BlindBoxTurntableGiftDraw extends Model
|
||||
{
|
||||
private $redis;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->redis = new Redis(['database' => 1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重构后的抽奖方法 - 优化响应速度
|
||||
*/
|
||||
@@ -48,8 +58,11 @@ class BlindBoxTurntableGiftDraw extends Model
|
||||
$currentXlhPeriodsNum = $precomputeResult['data']['current_xlh_periods_num'];
|
||||
$xlhIsPiaoPing = $precomputeResult['data']['xlh_is_piao_ping'];
|
||||
$expectedCount = count(explode(',', $gift_user_ids)) * $num;
|
||||
|
||||
if(count($precomputedResults) != $expectedCount){
|
||||
Log::record('抽奖结果数量与预期数量不一致,请重试!预期: '.$expectedCount.', 实际: '.count($precomputedResults).': ' . $room_id."【数据】".var_export($precomputedResults, true),"infos-mhzp");
|
||||
// 记录错误到Redis
|
||||
$this->recordDrawErrorToRedis($expectedCount, count($precomputedResults), $room_id, $user_id, $gift_bag_id, $num, $gift_user_ids, $precomputedResults);
|
||||
return ['code' => 0, 'msg' => '抽奖结果数量与预期数量不一致,请重试! ', 'data' => null];
|
||||
}
|
||||
|
||||
@@ -1547,4 +1560,59 @@ class BlindBoxTurntableGiftDraw extends Model
|
||||
return ['code' => 1, 'msg' => '成功', 'data' => $result_list];
|
||||
}
|
||||
|
||||
//错误日志记录-------------------------------------------------------
|
||||
/**
|
||||
* 记录抽奖错误日志到Redis
|
||||
*/
|
||||
private function recordDrawErrorToRedis($expectedCount, $actualCount, $room_id, $user_id, $gift_bag_id, $num, $gift_user_ids, $precomputedResults)
|
||||
{
|
||||
$errorData = [
|
||||
'timestamp' => time(),
|
||||
'expected_count' => $expectedCount,
|
||||
'actual_count' => $actualCount,
|
||||
'room_id' => $room_id,
|
||||
'user_id' => $user_id,
|
||||
'gift_bag_id' => $gift_bag_id,
|
||||
'num' => $num,
|
||||
'gift_user_ids' => $gift_user_ids,
|
||||
'precomputed_results_count' => count($precomputedResults),
|
||||
'error_type' => 'draw_count_mismatch'
|
||||
];
|
||||
|
||||
// 将错误信息推送到Redis列表中
|
||||
$this->redis->lpush('blind_box_draw_errors', json_encode($errorData));
|
||||
|
||||
// 同时记录到哈希结构,便于按房间ID或用户ID查询
|
||||
$errorKey = 'blind_box_error:' . $room_id . ':' . time() . ':' . uniqid();
|
||||
$this->redis->hmset($errorKey, $errorData);
|
||||
$this->redis->expire($errorKey, 86400 * 7); // 设置1天过期时间
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取抽奖错误统计信息
|
||||
*/
|
||||
public function getDrawErrorStats()
|
||||
{
|
||||
$errors = $this->redis->lrange('blind_box_draw_errors', 0, 99); // 获取最近100条错误记录
|
||||
$stats = [
|
||||
'total_errors' => $this->redis->llen('blind_box_draw_errors'),
|
||||
'recent_errors' => [],
|
||||
'error_distribution' => []
|
||||
];
|
||||
|
||||
foreach ($errors as $error) {
|
||||
$errorData = json_decode($error, true);
|
||||
$stats['recent_errors'][] = $errorData;
|
||||
|
||||
// 统计错误分布
|
||||
$errorType = $errorData['error_type'] ?? 'unknown';
|
||||
if (!isset($stats['error_distribution'][$errorType])) {
|
||||
$stats['error_distribution'][$errorType] = 0;
|
||||
}
|
||||
$stats['error_distribution'][$errorType]++;
|
||||
}
|
||||
|
||||
return $stats;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user