import os import shutil import pymysql import tkinter as tk from tkinter import filedialog, messagebox, StringVar, BooleanVar import configparser # 执行 SQL 文件 def execute_sql_file(sql_file_path, db_config): connection = None cursor = None try: # 连接 MySQL 数据库 connection = pymysql.connect( host=db_config["host"], user=db_config["user"], password=db_config["password"], database=db_config["database"], port=int(db_config["port"]), ) cursor = connection.cursor() # 读取并处理 SQL 文件 with open(sql_file_path, "r", encoding="utf-8") as file: sql_script = file.read() # 拆分 SQL 语句 statements = sql_script.split(";") for statement in statements: # 去除空的语句 if statement.strip(): cursor.execute(statement) print(f"执行 SQL 语句: {statement.strip()}") # 提交事务 connection.commit() messagebox.showinfo("SQL执行成功", f"SQL文件 {sql_file_path} 已成功执行") except Exception as e: messagebox.showerror("SQL执行失败", f"执行SQL文件时出错: {e}") if connection: connection.rollback() finally: if cursor: cursor.close() if connection: connection.close() # 处理解压后的文件 def handle_extracted_files( temp_extract_dir, output_dir, ui_dir, db_config, execute_sql ): for root_dir, dirs, files in os.walk(temp_extract_dir): for file in files: src_file_path = os.path.join(root_dir, file) relative_path = os.path.relpath(root_dir, temp_extract_dir) top_level_dir = relative_path.split(os.sep)[0] # 获取一级目录名 if "module" in top_level_dir: dest_file_path = os.path.join(output_dir, relative_path, file) os.makedirs(os.path.dirname(dest_file_path), exist_ok=True) shutil.copy2(src_file_path, dest_file_path) print(f"复制文件 {src_file_path} 到 {dest_file_path}") elif "sql" in top_level_dir and file == "sql.sql" and execute_sql: execute_sql_file(src_file_path, db_config) print(f"执行SQL文件: {src_file_path}") elif "vue" in top_level_dir: dest_file_path = os.path.join(ui_dir, relative_path, file) os.makedirs(os.path.dirname(dest_file_path), exist_ok=True) shutil.copy2(src_file_path, dest_file_path) print(f"复制文件 {src_file_path} 到 {dest_file_path}") # 选择zip文件 def select_zip_file(): zip_file_path.set(filedialog.askopenfilename(filetypes=[("ZIP files", "*.zip")])) # 选择输出目录 def select_output_dir(): output_dir.set(filedialog.askdirectory()) # 选择UI目录 def select_ui_dir(): ui_dir.set(filedialog.askdirectory()) # 保存配置信息到 .ini 文件 def save_config_to_ini(): config = configparser.ConfigParser() config["DATABASE"] = { "host": host_var.get(), "user": user_var.get(), "password": password_var.get(), "database": db_var.get(), "port": port_var.get(), } with open("config.ini", "w") as configfile: config.write(configfile) messagebox.showinfo("保存成功", "数据库配置信息已保存到 config.ini 文件中") # 解压并处理文件 def extract_and_process_files(): if not zip_file_path.get() or not output_dir.get() or not ui_dir.get(): messagebox.showwarning("警告", "请确保选择了ZIP文件和目标目录") return # 设置数据库连接参数 db_config = { "host": host_var.get() or "localhost", "user": user_var.get() or "root", "password": password_var.get() or "root", "database": db_var.get() or "cwet", "port": port_var.get() or 3306, } # 检查是否选择了执行 SQL if execute_sql_var.get(): # 解压ZIP文件并执行 SQL temp_extract_dir = os.path.join(output_dir.get(), "temp_extracted") shutil.unpack_archive(zip_file_path.get(), temp_extract_dir) # 处理解压后的文件 handle_extracted_files( temp_extract_dir, output_dir.get(), ui_dir.get(), db_config, True ) # 清理临时文件夹 shutil.rmtree(temp_extract_dir) messagebox.showinfo("完成", "文件处理完成!") else: # 保存配置到 ini 文件 save_config_to_ini() # 创建主窗口 root = tk.Tk() root.title("代码生成工具") # 创建变量 zip_file_path = StringVar() output_dir = StringVar() ui_dir = StringVar() # 数据库连接参数 host_var = StringVar(value="localhost") user_var = StringVar(value="root") password_var = StringVar(value="root") db_var = StringVar(value="cwet") port_var = StringVar(value="3306") # 是否执行 SQL 的选择 execute_sql_var = BooleanVar(value=True) # GUI布局 tk.Label(root, text="选择ZIP文件:").grid(row=0, column=0, padx=10, pady=5) tk.Entry(root, textvariable=zip_file_path, width=50).grid( row=0, column=1, padx=10, pady=5 ) tk.Button(root, text="选择ZIP文件", command=select_zip_file).grid( row=0, column=2, padx=10, pady=5 ) tk.Label(root, text="选择输出目录:").grid(row=1, column=0, padx=10, pady=5) tk.Entry(root, textvariable=output_dir, width=50).grid(row=1, column=1, padx=10, pady=5) tk.Button(root, text="选择输出目录", command=select_output_dir).grid( row=1, column=2, padx=10, pady=5 ) tk.Label(root, text="选择UI目录:").grid(row=2, column=0, padx=10, pady=5) tk.Entry(root, textvariable=ui_dir, width=50).grid(row=2, column=1, padx=10, pady=5) tk.Button(root, text="选择UI目录", command=select_ui_dir).grid( row=2, column=2, padx=10, pady=5 ) # 数据库设置 tk.Label(root, text="数据库主机:").grid(row=3, column=0, padx=10, pady=5) tk.Entry(root, textvariable=host_var).grid(row=3, column=1, padx=10, pady=5) tk.Label(root, text="用户名:").grid(row=4, column=0, padx=10, pady=5) tk.Entry(root, textvariable=user_var).grid(row=4, column=1, padx=10, pady=5) tk.Label(root, text="密码:").grid(row=5, column=0, padx=10, pady=5) tk.Entry(root, textvariable=password_var).grid(row=5, column=1, padx=10, pady=5) tk.Label(root, text="数据库名:").grid(row=6, column=0, padx=10, pady=5) tk.Entry(root, textvariable=db_var).grid(row=6, column=1, padx=10, pady=5) tk.Label(root, text="端口:").grid(row=7, column=0, padx=10, pady=5) tk.Entry(root, textvariable=port_var).grid(row=7, column=1, padx=10, pady=5) # 执行 SQL 的复选框 tk.Checkbutton(root, text="执行SQL语句", variable=execute_sql_var).grid( row=8, column=0, columnspan=2, padx=10, pady=5 ) # 开始处理按钮 tk.Button(root, text="开始处理", command=extract_and_process_files).grid( row=9, column=1, padx=10, pady=20 ) # 运行应用 root.mainloop()