1.修改彩金池配置中的playerDefault时自动修改绑定该配置的用户
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\dice\logic\lottery_pool_config;
|
||||
|
||||
use app\api\cache\UserCache;
|
||||
use app\api\service\LotteryService;
|
||||
use app\dice\helper\AdminScopeHelper;
|
||||
use app\dice\helper\ConfigScopeEditHelper;
|
||||
@@ -15,6 +16,7 @@ use app\dice\basic\DiceBaseLogic;
|
||||
use plugin\saiadmin\exception\ApiException;
|
||||
use plugin\saiadmin\utils\Helper;
|
||||
use support\think\Cache;
|
||||
use support\think\Db;
|
||||
|
||||
/**
|
||||
* 色子奖池配置逻辑层
|
||||
@@ -51,7 +53,7 @@ class DiceLotteryPoolConfigLogic extends DiceBaseLogic
|
||||
$pickedDeptId
|
||||
);
|
||||
if ($result) {
|
||||
$pool = DiceLotteryPoolConfig::find($id);
|
||||
$pool = DiceLotteryPoolConfig::where('id', $id)->find();
|
||||
if ($pool && $pool->isPlayerDefaultTemplate()) {
|
||||
$this->syncPlayersBoundToPlayerDefaultPool($pool);
|
||||
}
|
||||
@@ -60,7 +62,7 @@ class DiceLotteryPoolConfigLogic extends DiceBaseLogic
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改 playerDefault 后:同步同渠道所有 lottery_config_id 指向该池的玩家 T1–T5 权重,并刷新 Redis 彩金池快照
|
||||
* 修改 playerDefault 后:同步同渠道所有绑定该池(及应跟随 playerDefault 的玩家)T1–T5 权重,并刷新 Redis
|
||||
*
|
||||
* @return int 已同步玩家数量
|
||||
*/
|
||||
@@ -70,38 +72,86 @@ class DiceLotteryPoolConfigLogic extends DiceBaseLogic
|
||||
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);
|
||||
$fresh = DiceLotteryPoolConfig::where('id', $poolId)->find();
|
||||
if (!$fresh || !$fresh->isPlayerDefaultTemplate()) {
|
||||
return 0;
|
||||
}
|
||||
$pool = $fresh;
|
||||
|
||||
$query = DicePlayer::where('lottery_config_id', $poolId);
|
||||
if (AdminScopeHelper::isTemplateDeptId($poolDeptId)) {
|
||||
$weights = $this->extractPoolTierWeights($pool);
|
||||
$poolDeptId = AdminScopeHelper::normalizeRecordDeptId($pool->dept_id ?? null);
|
||||
$legacyDefaultPoolId = $this->findDefaultPoolIdForDept($poolDeptId);
|
||||
|
||||
$idsMap = [];
|
||||
|
||||
$boundPlayers = DicePlayer::where('lottery_config_id', $poolId)->select();
|
||||
foreach ($boundPlayers as $player) {
|
||||
$idsMap[(int) $player->id] = true;
|
||||
}
|
||||
|
||||
if ($legacyDefaultPoolId > 0 && $legacyDefaultPoolId !== $poolId) {
|
||||
$legacyPlayers = DicePlayer::where('lottery_config_id', $legacyDefaultPoolId)->select();
|
||||
foreach ($legacyPlayers as $player) {
|
||||
if (AdminScopeHelper::resolvePlayerConfigDeptId($player) === $poolDeptId) {
|
||||
$idsMap[(int) $player->id] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$unboundPlayers = DicePlayer::where(function ($q) {
|
||||
$q->where('lottery_config_id', 0)->whereOr('lottery_config_id', null);
|
||||
})->select();
|
||||
foreach ($unboundPlayers as $player) {
|
||||
if (AdminScopeHelper::resolvePlayerConfigDeptId($player) === $poolDeptId) {
|
||||
$idsMap[(int) $player->id] = true;
|
||||
}
|
||||
}
|
||||
|
||||
if ($idsMap === []) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$ids = array_keys($idsMap);
|
||||
$update = array_merge($weights, ['lottery_config_id' => $poolId]);
|
||||
Db::table('dice_player')->whereIn('id', $ids)->update($update);
|
||||
|
||||
foreach ($ids as $playerId) {
|
||||
LotteryService::patchPlayerWeightsCache($playerId, $weights);
|
||||
LotteryService::invalidatePlayerLotteryCache($playerId);
|
||||
UserCache::deleteUser($playerId);
|
||||
}
|
||||
|
||||
return count($ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{t1_weight:float,t2_weight:float,t3_weight:float,t4_weight:float,t5_weight:float}
|
||||
*/
|
||||
private function extractPoolTierWeights(DiceLotteryPoolConfig $pool): array
|
||||
{
|
||||
$data = $pool->getData();
|
||||
return [
|
||||
't1_weight' => (float) ($data['t1_weight'] ?? 0),
|
||||
't2_weight' => (float) ($data['t2_weight'] ?? 0),
|
||||
't3_weight' => (float) ($data['t3_weight'] ?? 0),
|
||||
't4_weight' => (float) ($data['t4_weight'] ?? 0),
|
||||
't5_weight' => (float) ($data['t5_weight'] ?? 0),
|
||||
];
|
||||
}
|
||||
|
||||
private function findDefaultPoolIdForDept(int $deptId): int
|
||||
{
|
||||
$query = DiceLotteryPoolConfig::where('name', 'default');
|
||||
if (AdminScopeHelper::isTemplateDeptId($deptId)) {
|
||||
$query->where(function ($q) {
|
||||
$q->where('dept_id', AdminScopeHelper::DEFAULT_TEMPLATE_DEPT)
|
||||
->whereOr('dept_id', null);
|
||||
});
|
||||
} else {
|
||||
$query->where('dept_id', $poolDeptId);
|
||||
$query->where('dept_id', $deptId);
|
||||
}
|
||||
|
||||
$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);
|
||||
$row = $query->find();
|
||||
return $row ? (int) $row->id : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user