1.修改彩金池配置中的playerDefault时自动修改绑定该配置的用户

This commit is contained in:
2026-06-04 13:47:35 +08:00
parent 8c6c122dc2
commit eb9ade6d16
2 changed files with 96 additions and 1 deletions

View File

@@ -83,6 +83,47 @@ class LotteryService
Cache::set($key, json_encode($data), self::EXPIRE);
}
/**
* 使玩家彩金池 Redis 快照失效(下次 getOrCreate 从库重建)
*/
public static function invalidatePlayerLotteryCache(int $playerId): void
{
if ($playerId <= 0) {
return;
}
Cache::delete(self::getRedisKey($playerId));
}
/**
* 若 Redis 中已有该玩家彩金池快照,则仅更新 player_weights避免全量重建
*
* @param array{t1_weight?:int,t2_weight?:int,t3_weight?:int,t4_weight?:int,t5_weight?:int} $weights
*/
public static function patchPlayerWeightsCache(int $playerId, array $weights): void
{
if ($playerId <= 0) {
return;
}
$key = self::getRedisKey($playerId);
$cached = Cache::get($key);
if (!$cached || !is_string($cached)) {
return;
}
$data = json_decode($cached, true);
if (!is_array($data)) {
Cache::delete($key);
return;
}
$data['player_weights'] = [
't1_weight' => (int) ($weights['t1_weight'] ?? 0),
't2_weight' => (int) ($weights['t2_weight'] ?? 0),
't3_weight' => (int) ($weights['t3_weight'] ?? 0),
't4_weight' => (int) ($weights['t4_weight'] ?? 0),
't5_weight' => (int) ($weights['t5_weight'] ?? 0),
];
Cache::set($key, json_encode($data), self::EXPIRE);
}
/** 根据奖池配置的 t1_weight..t5_weight 权重随机抽取档位 T1-T5 */
public static function drawTierByWeights(DiceLotteryPoolConfig $config): string
{

View File

@@ -6,9 +6,11 @@
// +----------------------------------------------------------------------
namespace app\dice\logic\lottery_pool_config;
use app\api\service\LotteryService;
use app\dice\helper\AdminScopeHelper;
use app\dice\helper\ConfigScopeEditHelper;
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
use app\dice\model\player\DicePlayer;
use app\dice\basic\DiceBaseLogic;
use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper;
@@ -39,7 +41,7 @@ class DiceLotteryPoolConfigLogic extends DiceBaseLogic
{
$pickedDeptId = AdminScopeHelper::pickRequestDeptId($requestDeptId, $data);
$deptId = AdminScopeHelper::resolveConfigDeptId($adminInfo, $pickedDeptId);
return ConfigScopeEditHelper::updateByPkAndDept(
$result = ConfigScopeEditHelper::updateByPkAndDept(
$this->model,
$id,
$deptId,
@@ -48,6 +50,58 @@ class DiceLotteryPoolConfigLogic extends DiceBaseLogic
$adminInfo,
$pickedDeptId
);
if ($result) {
$pool = DiceLotteryPoolConfig::find($id);
if ($pool && $pool->isPlayerDefaultTemplate()) {
$this->syncPlayersBoundToPlayerDefaultPool($pool);
}
}
return $result;
}
/**
* 修改 playerDefault 后:同步同渠道所有 lottery_config_id 指向该池的玩家 T1T5 权重,并刷新 Redis 彩金池快照
*
* @return int 已同步玩家数量
*/
public function syncPlayersBoundToPlayerDefaultPool(DiceLotteryPoolConfig $pool): int
{
$poolId = (int) ($pool->id ?? 0);
if ($poolId <= 0) {
return 0;
}
$weights = [
't1_weight' => (int) ($pool->t1_weight ?? 0),
't2_weight' => (int) ($pool->t2_weight ?? 0),
't3_weight' => (int) ($pool->t3_weight ?? 0),
't4_weight' => (int) ($pool->t4_weight ?? 0),
't5_weight' => (int) ($pool->t5_weight ?? 0),
];
$poolDeptId = AdminScopeHelper::normalizeRecordDeptId($pool->dept_id ?? null);
$query = DicePlayer::where('lottery_config_id', $poolId);
if (AdminScopeHelper::isTemplateDeptId($poolDeptId)) {
$query->where(function ($q) {
$q->where('dept_id', AdminScopeHelper::DEFAULT_TEMPLATE_DEPT)
->whereOr('dept_id', null);
});
} else {
$query->where('dept_id', $poolDeptId);
}
$playerIds = $query->column('id');
if ($playerIds === [] || $playerIds === null) {
return 0;
}
$ids = array_map('intval', is_array($playerIds) ? $playerIds : [$playerIds]);
DicePlayer::whereIn('id', $ids)->update($weights);
foreach ($ids as $playerId) {
LotteryService::patchPlayerWeightsCache($playerId, $weights);
}
return count($ids);
}
/**