refactor: optimize settings and validation tabs for User users

Material Validation Tab:
- Remove file selection area (file_group and related frames)
- Move output file path configuration to settings
- For User users: hide data source selection, only show shared production IDs

Settings Tab:
- Add "validation output file" configuration to path settings group
- Show "save settings" button for User users
- Add User-only mode to only show path settings group

Changes:
- material_validation_tab.py: Restructure control panel, update export_paths
- settings_tab.py: Add validation output filename, add save button for users

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-02-11 22:40:07 +08:00
parent 28d7db26db
commit 3b491d7b36
2 changed files with 99 additions and 98 deletions

View File

@@ -233,53 +233,13 @@ class MaterialValidationTab(ttk.Frame):
# 普通用户:默认使用 database_filtered 模式
self.source_mode = tk.StringVar(value="database_filtered")
# 文件选择
file_group = ttk.LabelFrame(parent, text="文件选择", padding=10)
file_group.pack(fill=tk.X, pady=5)
# 数据库全表模式 - 无需输入文件
self.db_full_frame = ttk.Frame(file_group)
ttk.Label(
self.db_full_frame,
text="数据库全表模式:将查询 DiscreteMaterialPlanData 表中的所有材料",
foreground="gray"
).pack(anchor="w")
# 数据库过滤模式 - 需要 ProductionID 文件
self.db_filtered_frame = ttk.Frame(file_group)
# 判断用户类型
is_user_only = not self.session_manager.is_admin()
if is_user_only:
# ========== User 模式:直接使用共享的 Production ID ==========
# 自动设置为使用共享数据源
self.production_id_source_var = tk.StringVar(value="shared")
# 创建共享 Production ID 显示标签(但不显示任何内容,只是用于更新)
self.shared_ids_info_label = ttk.Label(
self.db_filtered_frame,
text="",
foreground="gray"
)
# 不 pack保持隐藏状态
else:
# ========== Admin 模式:可选择文件或共享数据 ==========
# 共享 Production ID 提示标签
self.shared_ids_label = ttk.Label(
self.db_filtered_frame,
text="💡 数据提取页面输入了 Production ID可直接使用",
foreground="green",
font=("", 9)
)
# 初始时不显示,等待 Production ID 更新
# Production ID 数据源选择
self.production_id_source_var = tk.StringVar(value="file")
source_frame = ttk.Frame(self.db_filtered_frame)
# Production ID 数据源(仅 Admin
if self.session_manager.is_admin():
source_frame = ttk.Frame(parent)
source_frame.pack(fill=tk.X, pady=5)
# Production ID 数据源选择
self.production_id_source_var = tk.StringVar(value="shared")
ttk.Radiobutton(
source_frame,
text="使用文件:",
@@ -298,7 +258,7 @@ class MaterialValidationTab(ttk.Frame):
# 文件选择器
self.db_filtered_production_id_selector = FileSelector(
self.db_filtered_frame,
parent,
label_text="",
file_type="file",
file_types=[("文本文件", "*.txt"), ("所有文件", "*.*")],
@@ -306,31 +266,14 @@ class MaterialValidationTab(ttk.Frame):
)
self.db_filtered_production_id_selector.pack(fill=tk.X)
# 共享 Production ID 示标签
# 共享 Production ID 示标签
self.shared_ids_info_label = ttk.Label(
self.db_filtered_frame,
parent,
text="",
foreground="blue"
)
# 初始时不显示
# 输出文件(对于所有用户都可见)
self.output_file_selector = FileSelector(
file_group,
label_text="输出文件:",
file_type="file",
file_types=[("Excel 文件", "*.xlsx"), ("所有文件", "*.*")],
initial_dir=self.config.get("paths.data_dir", "data/"),
)
self.output_file_selector.pack(fill=tk.X, pady=(10, 0))
# 设置默认输出
default_output = os.path.join(
self.config.get("paths.data_dir", "data/"),
self.config.get("paths.validation_output", "物料状态校验结果.xlsx"),
)
self.output_file_selector.set(default_output)
# 控制按钮
button_frame = ttk.Frame(parent)
button_frame.pack(fill=tk.X, pady=10)
@@ -438,24 +381,21 @@ class MaterialValidationTab(ttk.Frame):
def _on_source_mode_change(self):
"""数据源模式切换"""
# PERMISSION CHECK: 普通用户固定为 database_filtered 模式
# PERMISSION CHECK: 仅管理员用户可以选择数据源模式
if not self.session_manager.is_admin():
# 对于普通用户pack 数据库过滤模式框架
self.db_full_frame.pack_forget()
self.db_filtered_frame.pack(fill=tk.X, pady=(0, 5))
return
mode = self.source_mode.get()
# 隐藏所有文件选择框架
self.db_full_frame.pack_forget()
self.db_filtered_frame.pack_forget()
# 根据模式显示对应的文件选择器
# 根据模式显示 Production ID 数据源选择(仅 Admin
if mode == "database_full":
self.db_full_frame.pack(fill=tk.X, pady=(0, 5))
# 全表模式:隐藏 Production ID 选择区域
if hasattr(self, 'source_frame'):
self.source_frame.pack_forget()
elif mode == "database_filtered":
self.db_filtered_frame.pack(fill=tk.X, pady=(0, 5))
# 过滤模式:显示 Production ID 选择区域
if hasattr(self, 'source_frame'):
self.source_frame.pack(fill=tk.X, pady=5)
def _on_production_id_source_changed(self):
"""Production ID 数据源切换回调(仅 Admin 模式)"""
@@ -818,10 +758,13 @@ class MaterialValidationTab(ttk.Frame):
def start_validation(self):
"""开始校验"""
# 验证输入
output_file = self.output_file_selector.get()
# 从配置获取输出文件路径
output_file = os.path.join(
self.config.get("paths.data_dir", "data/"),
self.config.get("paths.validation_output", "物料状态校验结果.xlsx"),
)
if not output_file:
messagebox.showerror("错误", "指定输出文件路径")
messagebox.showerror("错误", "先在设置页面配置输出文件路径")
return
mode = self.source_mode.get()
@@ -1232,17 +1175,29 @@ class MaterialValidationTab(ttk.Frame):
def export_results(self):
"""导出结果到 Excel"""
output_file = self.output_file_selector.get()
# 从配置获取输出文件路径
data_dir = self.config.get("paths.data_dir", "data/")
validation_filename = self.config.get("paths.validation_output", "物料状态校验结果.xlsx")
output_file = os.path.join(data_dir, validation_filename)
if not output_file:
output_file = filedialog.asksaveasfilename(
title="保存结果",
defaultextension=".xlsx",
filetypes=[("Excel 文件", "*.xlsx"), ("所有文件", "*.*")],
# 如果配置的文件不存在,提示用户选择位置
if not os.path.exists(data_dir):
output_dir = filedialog.askdirectory(
title="选择输出目录",
initialdir=data_dir
)
if output_dir:
self.config.set("paths.data_dir", output_dir)
# 重新读取配置并构建输出文件路径
data_dir = output_dir
output_file = os.path.join(data_dir, validation_filename)
else:
return
if not output_file:
return
# 如果文件已存在,提示用户覆盖
if os.path.exists(output_file):
if not messagebox.askyesno("确认覆盖", f"文件已存在:{output_file}\n是否覆盖?"):
return
try:
# 收集表格数据