1.优化渠道删除失败问题

This commit is contained in:
2026-06-03 15:41:56 +08:00
parent 9fb98dee3f
commit 136c18e413
15 changed files with 626 additions and 18 deletions

View File

@@ -54,6 +54,22 @@ class DiceChannelConfigService
'dice_reward_config_record' => ['label' => '权重测试记录', 'group' => 'records'],
];
/** 关联数据删除顺序:先删流水/记录,再删玩家,最后删配置 */
private const DELETE_TABLE_ORDER = [
'dice_play_record',
'dice_play_record_test',
'dice_player_wallet_record',
'dice_player_ticket_record',
'dice_reward_config_record',
'dice_player',
'dice_reward',
'dice_reward_config',
'dice_config',
'dice_ante_config',
'dice_lottery_pool_config',
'dice_game',
];
/**
* 默认模板 dept_id 统一为 0并为固定 id 的配置表建立 (dept_id, id) 唯一约束
*/
@@ -417,21 +433,54 @@ class DiceChannelConfigService
if ($userCount > 0) {
throw new ApiException('This channel has users, please delete or transfer them first');
}
$allowed = array_keys(self::RELATION_TABLES);
foreach ($deleteTables as $table) {
if (!in_array($table, $allowed, true)) {
continue;
$tablesToDelete = $this->sortTablesForDelete($deleteTables);
Db::startTrans();
try {
foreach ($tablesToDelete as $table) {
if (!$this->tableHasColumn($table, 'dept_id')) {
continue;
}
Db::table($table)->where('dept_id', $deptId)->delete();
}
if (!$this->tableHasColumn($table, 'dept_id')) {
continue;
}
Db::table($table)->where('dept_id', $deptId)->delete();
Db::name('sa_system_role_dept')->where('dept_id', $deptId)->delete();
SystemDept::destroy($deptId, true);
Db::commit();
} catch (\Throwable $e) {
Db::rollback();
throw new ApiException('Channel delete failed: ' . $e->getMessage());
}
SystemDept::destroy($deptId, true);
DiceRewardConfig::refreshCache($deptId);
DiceReward::refreshCache($deptId);
}
/**
* 按依赖顺序排列待删表(勾选顺序无关)
*
* @param array<int, string> $deleteTables
* @return array<int, string>
*/
private function sortTablesForDelete(array $deleteTables): array
{
$allowed = array_keys(self::RELATION_TABLES);
$picked = [];
foreach ($deleteTables as $table) {
if (is_string($table) && in_array($table, $allowed, true)) {
$picked[$table] = true;
}
}
$ordered = [];
foreach (self::DELETE_TABLE_ORDER as $table) {
if (isset($picked[$table])) {
$ordered[] = $table;
unset($picked[$table]);
}
}
foreach (array_keys($picked) as $table) {
$ordered[] = $table;
}
return $ordered;
}
/**
* @return array<int, array<string, mixed>>
*/
@@ -477,7 +526,9 @@ class DiceChannelConfigService
if ($deptId === null || $deptId === AdminScopeHelper::DEFAULT_TEMPLATE_DEPT) {
$templateId = AdminScopeHelper::DEFAULT_TEMPLATE_DEPT;
$query->where(function ($q) use ($templateId) {
$q->where('dept_id', $templateId)->whereOr('dept_id', 'null');
$q->where('dept_id', $templateId)->whereOr(function ($sub) {
$sub->whereNull('dept_id');
});
});
} else {
$query->where('dept_id', $deptId);