feat: 一键整理强制对齐分组逻辑,移除旧托管分组并优化统计消息区分

This commit is contained in:
SmartUp Developer
2026-07-02 10:42:18 +08:00
parent c797aff773
commit fe3f9f4a4c
2 changed files with 202 additions and 25 deletions
+41 -19
View File
@@ -1300,6 +1300,22 @@ def organize_website_groups(
.all()
)
smartup_managed_group_ids = {str(b.target_group_id) for b in bindings if b.target_group_id}
# 合并已导入 Key 上历史记录的 imported_target_group_id,以确保变更绑定后能清理旧分组
hist_group_ids = (
db.query(UpstreamGeneratedKey.imported_target_group_id)
.filter(
UpstreamGeneratedKey.imported_website_id == wid,
UpstreamGeneratedKey.imported_target_group_id.isnot(None),
UpstreamGeneratedKey.imported_target_group_id != "",
)
.distinct()
.all()
)
for row in hist_group_ids:
smartup_managed_group_ids.add(str(row[0]))
items: list[OrganizeGroupsItem] = []
# 2. 收集所有的 upstream_id 并执行对账,以保证本地 Key 的状态最新
@@ -1474,28 +1490,30 @@ def organize_website_groups(
remote_acc = remote_account_map.get(str(old_account_id))
if remote_acc is not None:
# 目标账号存在:跳过或补齐目标分组记录
# 目标账号存在:检测并对齐目标分组,移除其余已启用的 SmartUp 托管分组,保留非 SmartUp 托管分组
try:
current_group_ids = remote_acc.get("group_ids") or []
if not isinstance(current_group_ids, list):
current_group_ids = [current_group_ids]
str_group_ids = [str(g) for g in current_group_ids]
if str(target_group_id) not in str_group_ids:
# 需要补齐绑定关系
numeric_target = _numeric_group_id(target_group_id)
target_val = numeric_target if numeric_target is not None else target_group_id
numeric_target = _numeric_group_id(target_group_id)
target_val = numeric_target if numeric_target is not None else target_group_id
new_group_ids = list(current_group_ids)
if target_val not in new_group_ids:
new_group_ids.append(target_val)
# 过滤出非 SmartUp 托管的 group_ids
new_group_ids = [g for g in current_group_ids if str(g) not in smartup_managed_group_ids]
# 追加当前目标分组
new_group_ids.append(target_val)
old_set = {str(g) for g in current_group_ids}
new_set = {str(g) for g in new_group_ids}
if old_set != new_set:
c.update_account(old_account_id, {"group_ids": new_group_ids})
# 顺便更新下 remote_account_map 中的数据,防止同个账号后续在其他绑定中重复补齐
# 顺便更新下 remote_account_map 中的数据,防止同个账号后续在其他绑定中重复处理
remote_acc["group_ids"] = new_group_ids
msg = "账号已存在,已补齐目标分组绑定"
msg = "已存在,已迁移到目标分组"
else:
msg = "已存在且已绑定,已跳过"
msg = "已存在且已对齐,已跳过"
# 补齐本地 DB 的目标分组标记
if row.imported_target_group_id != target_group_id:
@@ -1638,17 +1656,21 @@ def organize_website_groups(
)
created_count = sum(1 for item in items if item.status == "created")
exists_count = sum(1 for item in items if item.status == "exists")
missing_key_count = sum(1 for item in items if item.status == "missing_key")
recreated_count = sum(1 for item in items if item.status == "recreated")
aligned_count = sum(1 for item in items if item.status == "exists" and "已对齐" in item.message)
migrated_count = sum(1 for item in items if item.status == "exists" and "已迁移" in item.message)
missing_key_count = sum(1 for item in items if item.status == "missing_key")
failed_count = sum(1 for item in items if item.status == "failed")
parts = []
total_created = created_count + recreated_count
if total_created:
parts.append(f"创建账号 {total_created}")
if exists_count:
parts.append(f"已存在 {exists_count}")
if created_count:
parts.append(f"已创建 {created_count}")
if recreated_count:
parts.append(f"已重建 {recreated_count}")
if aligned_count:
parts.append(f"已存在且已对齐 {aligned_count}")
if migrated_count:
parts.append(f"已存在已迁移 {migrated_count}")
if missing_key_count:
parts.append(f"缺少 Key {missing_key_count}")
if failed_count: