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

This commit is contained in:
2026-06-04 13:56:33 +08:00
parent eb9ade6d16
commit 16a59c28d4
3 changed files with 124 additions and 33 deletions

View File

@@ -336,6 +336,13 @@
return v == null || v === 0 return v == null || v === 0
} }
/** 将彩金池配置的 T1T5 写入表单(绑定彩金池时展示与提交均以池为准) */
function applyPoolWeightsToForm(cfg: LotteryPoolConfigOption) {
WEIGHT_FIELDS.forEach((key) => {
;(formData as Record<string, number>)[key] = Number(cfg[key] ?? 0)
})
}
/** 根据当前 lottery_config_id 加载 DiceLotteryConfig并将五个权重写入当前 player.*_weight */ /** 根据当前 lottery_config_id 加载 DiceLotteryConfig并将五个权重写入当前 player.*_weight */
async function loadCurrentLotteryConfig() { async function loadCurrentLotteryConfig() {
const id = formData.lottery_config_id const id = formData.lottery_config_id
@@ -349,9 +356,7 @@
if (payload) { if (payload) {
const row = parseLotteryPoolConfigOption(payload) const row = parseLotteryPoolConfigOption(payload)
currentLotteryConfig.value = row currentLotteryConfig.value = row
WEIGHT_FIELDS.forEach((key) => { applyPoolWeightsToForm(row)
;(formData as any)[key] = Number(row[key] ?? 0)
})
} else { } else {
currentLotteryConfig.value = null currentLotteryConfig.value = null
} }
@@ -360,6 +365,15 @@
} }
} }
watch(
() => currentLotteryConfig.value,
(cfg) => {
if (cfg && !isLotteryConfigEmpty()) {
applyPoolWeightsToForm(cfg)
}
}
)
watch( watch(
() => props.modelValue, () => props.modelValue,
(newVal) => { (newVal) => {
@@ -378,9 +392,7 @@
const payload = extractReadPayload(res) const payload = extractReadPayload(res)
if (payload) { if (payload) {
const row = parseLotteryPoolConfigOption(payload) const row = parseLotteryPoolConfigOption(payload)
WEIGHT_FIELDS.forEach((key) => { applyPoolWeightsToForm(row)
;(formData as any)[key] = Number(row[key] ?? 0)
})
currentLotteryConfig.value = row currentLotteryConfig.value = row
} else { } else {
currentLotteryConfig.value = null currentLotteryConfig.value = null
@@ -513,6 +525,13 @@
const payload = { ...formData } const payload = { ...formData }
if (isLotteryConfigEmpty()) { if (isLotteryConfigEmpty()) {
;(payload as any).lottery_config_id = null ;(payload as any).lottery_config_id = null
} else if (currentLotteryConfig.value) {
applyPoolWeightsToForm(currentLotteryConfig.value)
WEIGHT_FIELDS.forEach((key) => {
;(payload as Record<string, number>)[key] = Number(
(formData as Record<string, number>)[key] ?? 0
)
})
} }
if (props.dialogType === 'edit' && !payload.password) { if (props.dialogType === 'edit' && !payload.password) {
delete (payload as any).password delete (payload as any).password

View File

@@ -6,6 +6,7 @@
// +---------------------------------------------------------------------- // +----------------------------------------------------------------------
namespace app\dice\logic\lottery_pool_config; namespace app\dice\logic\lottery_pool_config;
use app\api\cache\UserCache;
use app\api\service\LotteryService; use app\api\service\LotteryService;
use app\dice\helper\AdminScopeHelper; use app\dice\helper\AdminScopeHelper;
use app\dice\helper\ConfigScopeEditHelper; use app\dice\helper\ConfigScopeEditHelper;
@@ -15,6 +16,7 @@ use app\dice\basic\DiceBaseLogic;
use plugin\saiadmin\exception\ApiException; use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper; use plugin\saiadmin\utils\Helper;
use support\think\Cache; use support\think\Cache;
use support\think\Db;
/** /**
* 色子奖池配置逻辑层 * 色子奖池配置逻辑层
@@ -51,7 +53,7 @@ class DiceLotteryPoolConfigLogic extends DiceBaseLogic
$pickedDeptId $pickedDeptId
); );
if ($result) { if ($result) {
$pool = DiceLotteryPoolConfig::find($id); $pool = DiceLotteryPoolConfig::where('id', $id)->find();
if ($pool && $pool->isPlayerDefaultTemplate()) { if ($pool && $pool->isPlayerDefaultTemplate()) {
$this->syncPlayersBoundToPlayerDefaultPool($pool); $this->syncPlayersBoundToPlayerDefaultPool($pool);
} }
@@ -60,7 +62,7 @@ class DiceLotteryPoolConfigLogic extends DiceBaseLogic
} }
/** /**
* 修改 playerDefault 后:同步同渠道所有 lottery_config_id 指向该池的玩家 T1T5 权重,并刷新 Redis 彩金池快照 * 修改 playerDefault 后:同步同渠道所有绑定该池(及应跟随 playerDefault 的玩家T1T5 权重,并刷新 Redis
* *
* @return int 已同步玩家数量 * @return int 已同步玩家数量
*/ */
@@ -70,38 +72,86 @@ class DiceLotteryPoolConfigLogic extends DiceBaseLogic
if ($poolId <= 0) { if ($poolId <= 0) {
return 0; return 0;
} }
$weights = [ $fresh = DiceLotteryPoolConfig::where('id', $poolId)->find();
't1_weight' => (int) ($pool->t1_weight ?? 0), if (!$fresh || !$fresh->isPlayerDefaultTemplate()) {
't2_weight' => (int) ($pool->t2_weight ?? 0), return 0;
't3_weight' => (int) ($pool->t3_weight ?? 0), }
't4_weight' => (int) ($pool->t4_weight ?? 0), $pool = $fresh;
't5_weight' => (int) ($pool->t5_weight ?? 0),
];
$poolDeptId = AdminScopeHelper::normalizeRecordDeptId($pool->dept_id ?? null);
$query = DicePlayer::where('lottery_config_id', $poolId); $weights = $this->extractPoolTierWeights($pool);
if (AdminScopeHelper::isTemplateDeptId($poolDeptId)) { $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) { $query->where(function ($q) {
$q->where('dept_id', AdminScopeHelper::DEFAULT_TEMPLATE_DEPT) $q->where('dept_id', AdminScopeHelper::DEFAULT_TEMPLATE_DEPT)
->whereOr('dept_id', null); ->whereOr('dept_id', null);
}); });
} else { } else {
$query->where('dept_id', $poolDeptId); $query->where('dept_id', $deptId);
} }
$row = $query->find();
$playerIds = $query->column('id'); return $row ? (int) $row->id : 0;
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);
} }
/** /**

View File

@@ -7,6 +7,7 @@
namespace app\dice\logic\player; namespace app\dice\logic\player;
use app\dice\basic\DiceBaseLogic; use app\dice\basic\DiceBaseLogic;
use app\dice\model\lottery_pool_config\DiceLotteryPoolConfig;
use plugin\saiadmin\exception\ApiException; use plugin\saiadmin\exception\ApiException;
use plugin\saiadmin\utils\Helper; use plugin\saiadmin\utils\Helper;
use app\dice\model\player\DicePlayer; use app\dice\model\player\DicePlayer;
@@ -48,9 +49,30 @@ class DicePlayerLogic extends DiceBaseLogic
} else { } else {
unset($data['password']); unset($data['password']);
} }
$data = $this->applyLotteryPoolWeightsToPlayerData($data);
return parent::edit($id, $data); return parent::edit($id, $data);
} }
/**
* 已绑定彩金池时:玩家 T1T5 以池配置为准,避免前端提交陈旧权重覆盖同步结果
*/
private function applyLotteryPoolWeightsToPlayerData(array $data): array
{
$configId = isset($data['lottery_config_id']) ? (int) $data['lottery_config_id'] : 0;
if ($configId <= 0) {
return $data;
}
$config = DiceLotteryPoolConfig::find($configId);
if (!$config) {
return $data;
}
$row = $config->getData();
foreach (['t1_weight', 't2_weight', 't3_weight', 't4_weight', 't5_weight'] as $field) {
$data[$field] = $row[$field] ?? 0;
}
return $data;
}
/** /**
* 密码加密md5(salt . password) * 密码加密md5(salt . password)
*/ */