129 lines
4.7 KiB
Python
129 lines
4.7 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""Legacy delete and human-readable list CLI commands."""
|
||
from db.accounts_repo import get_account_by_id, find_accounts
|
||
from db.connection import get_conn, init_db
|
||
from service._svc_common import (
|
||
_remove_profile_dir,
|
||
_resolve_or_fail,
|
||
_say,
|
||
_say_err,
|
||
)
|
||
from util.logging_config import get_skill_logger
|
||
from util.platforms import get_platform_spec
|
||
from util.runtime_paths import _runtime_paths_debug_text
|
||
|
||
|
||
def cmd_delete_by_id(account_id) -> None:
|
||
"""delete id <id> — legacy"""
|
||
log = get_skill_logger()
|
||
log.info("delete_by_id id=%r", account_id)
|
||
acc = get_account_by_id(account_id)
|
||
if not acc:
|
||
_say(f"ERROR:ACCOUNT_NOT_FOUND 账号 id={account_id} 不存在。")
|
||
_say_err(_runtime_paths_debug_text())
|
||
return
|
||
profile_dir = acc.get("profile_dir", "")
|
||
conn = get_conn()
|
||
try:
|
||
cur = conn.cursor()
|
||
cur.execute("DELETE FROM accounts WHERE id = ?", (int(account_id),))
|
||
cur.execute("DELETE FROM credentials WHERE account_id = ?", (int(account_id),))
|
||
conn.commit()
|
||
finally:
|
||
conn.close()
|
||
_remove_profile_dir(profile_dir)
|
||
log.info("delete_by_id_done id=%s", account_id)
|
||
_say(f"✅ 已删除账号 ID {account_id}。")
|
||
|
||
|
||
def cmd_delete_by_platform(platform_input: str) -> None:
|
||
"""delete platform <platform> — legacy"""
|
||
log = get_skill_logger()
|
||
log.info("delete_by_platform platform=%r", platform_input)
|
||
key = _resolve_or_fail(platform_input)
|
||
if not key:
|
||
return
|
||
conn = get_conn()
|
||
try:
|
||
cur = conn.cursor()
|
||
cur.execute("SELECT id, profile_dir FROM accounts WHERE platform_key = ?", (key,))
|
||
rows = cur.fetchall()
|
||
if not rows:
|
||
_say("ERROR:NO_ACCOUNTS_FOUND 该平台下没有账号记录。")
|
||
return
|
||
for rid, pdir in rows:
|
||
_remove_profile_dir(pdir)
|
||
cur.execute("DELETE FROM accounts WHERE platform_key = ?", (key,))
|
||
cur.execute(
|
||
"DELETE FROM credentials WHERE platform_key = ? AND account_id IS NOT NULL",
|
||
(key,),
|
||
)
|
||
conn.commit()
|
||
log.info("delete_by_platform_done platform=%s count=%s", key, len(rows))
|
||
display = get_platform_spec(key, conn=conn).get("display_name", key)
|
||
_say(f"✅ 已删除 {display} 下共 {len(rows)} 条账号。")
|
||
finally:
|
||
conn.close()
|
||
|
||
|
||
def cmd_delete_by_platform_phone(platform_input: str, phone: str) -> None:
|
||
"""Legacy: delete by platform+phone — now uses login_id matching"""
|
||
log = get_skill_logger()
|
||
log.info("delete_by_platform_phone platform=%r", platform_input)
|
||
key = _resolve_or_fail(platform_input)
|
||
if not key:
|
||
return
|
||
phone_norm = "".join(c for c in phone if c.isdigit())
|
||
conn = get_conn()
|
||
try:
|
||
cur = conn.cursor()
|
||
cur.execute(
|
||
"SELECT id, profile_dir FROM accounts WHERE platform_key = ? AND login_id = ?",
|
||
(key, phone_norm),
|
||
)
|
||
row = cur.fetchone()
|
||
if not row:
|
||
_say("ERROR:ACCOUNT_NOT_FOUND 该平台下无此手机号。")
|
||
return
|
||
rid, pdir = row
|
||
_remove_profile_dir(pdir)
|
||
cur.execute("DELETE FROM accounts WHERE id = ?", (rid,))
|
||
cur.execute("DELETE FROM credentials WHERE account_id = ?", (rid,))
|
||
conn.commit()
|
||
log.info("delete_by_platform_phone_done id=%s", rid)
|
||
_say(f"✅ 已删除账号 ID {rid}。")
|
||
finally:
|
||
conn.close()
|
||
|
||
|
||
def cmd_list(platform="all", limit: int = 10) -> None:
|
||
"""Legacy: human-readable list output."""
|
||
log = get_skill_logger()
|
||
log.info("list filter=%r", platform)
|
||
init_db()
|
||
accounts = find_accounts(
|
||
platform_key=None if (not platform or platform in ("all", "全部")) else platform,
|
||
limit=limit,
|
||
)
|
||
if not accounts:
|
||
_say("ERROR:NO_ACCOUNTS_FOUND")
|
||
_say_err(_runtime_paths_debug_text())
|
||
return
|
||
log.info("list_ok rows=%s", len(accounts))
|
||
for i, acc in enumerate(accounts):
|
||
_say(f"id:{acc['id']}")
|
||
_say(f"label:{acc['account_label']}")
|
||
_say(f"platform:{acc['platform_key']}")
|
||
_say(f"login_id:{acc['login_id']}")
|
||
_say(f"env:{acc['environment']} role:{acc['role']}")
|
||
_say(f"status:{acc['status']} session:{acc['session_status']}")
|
||
_say(f"profile_dir:{acc['profile_dir']}")
|
||
_say(f"url:{acc['url']}")
|
||
_say(f"created_at:{acc['created_at']}")
|
||
_say(f"updated_at:{acc['updated_at']}")
|
||
if acc.get("last_error_code"):
|
||
_say(f"last_error:{acc['last_error_code']} {acc.get('last_error_message', '')}")
|
||
if i < len(accounts) - 1:
|
||
_say("_______________________________________")
|
||
_say("")
|