批量新增用户和部门处理脚本
This commit is contained in:
1 parent
63dfe1687a
commit
e72889acdb
6 files changed
+4597
No files matched your search
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Generate batch INSERT SQL for user_agent_permissions.
|
||||
|
||||
Default behavior matches current request:
|
||||
- user_id: 6..312
|
||||
- agent_id: 1,3,4,5,6,7,8,9,10,12
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def parse_agent_ids(raw: str) -> list[int]:
|
||||
ids: list[int] = []
|
||||
for part in raw.split(","):
|
||||
part = part.strip()
|
||||
if not part:
|
||||
continue
|
||||
ids.append(int(part))
|
||||
if not ids:
|
||||
raise ValueError("agent_ids cannot be empty")
|
||||
return ids
|
||||
|
||||
|
||||
def build_sql(user_start: int, user_end: int, agent_ids: list[int]) -> str:
|
||||
values: list[str] = []
|
||||
for user_id in range(user_start, user_end + 1):
|
||||
for agent_id in agent_ids:
|
||||
values.append(f"({user_id}, {agent_id})")
|
||||
|
||||
if not values:
|
||||
raise ValueError("No rows generated")
|
||||
|
||||
sql = (
|
||||
"BEGIN;\n\n"
|
||||
"INSERT INTO user_agent_permissions (user_id, agent_id)\nVALUES\n "
|
||||
+ ",\n ".join(values)
|
||||
+ "\nON CONFLICT DO NOTHING;\n\n"
|
||||
"COMMIT;\n"
|
||||
)
|
||||
return sql
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Generate SQL for user_agent_permissions batch insert."
|
||||
)
|
||||
parser.add_argument("--user-start", type=int, default=6, help="Start user_id (inclusive).")
|
||||
parser.add_argument("--user-end", type=int, default=312, help="End user_id (inclusive).")
|
||||
parser.add_argument(
|
||||
"--agent-ids",
|
||||
default="1,3,4,5,6,7,8,9,10,12",
|
||||
help="Comma-separated agent ids.",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--output",
|
||||
default="backend/scripts/generated_user_agent_permissions.sql",
|
||||
help="Output SQL file path.",
|
||||
)
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.user_start > args.user_end:
|
||||
raise ValueError("--user-start cannot be greater than --user-end")
|
||||
|
||||
agent_ids = parse_agent_ids(args.agent_ids)
|
||||
sql = build_sql(args.user_start, args.user_end, agent_ids)
|
||||
|
||||
output_path = Path(args.output)
|
||||
output_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
output_path.write_text(sql, encoding="utf-8")
|
||||
|
||||
total_users = args.user_end - args.user_start + 1
|
||||
total_rows = total_users * len(agent_ids)
|
||||
print(f"Generated SQL: {output_path}")
|
||||
print(f"Users: {total_users}, agents/user: {len(agent_ids)}, rows: {total_rows}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
raise SystemExit(main())
|
||||
Reference in new issue
Block a user