200 lines
5.8 KiB
Python
200 lines
5.8 KiB
Python
import logging
|
||
from dao.inspection_dao import InspectionDAO
|
||
|
||
class InspectionConfigManager:
|
||
"""检验配置管理器,用于管理检验项目配置"""
|
||
|
||
_instance = None
|
||
|
||
@classmethod
|
||
def get_instance(cls):
|
||
"""获取单例实例"""
|
||
if cls._instance is None:
|
||
cls._instance = InspectionConfigManager()
|
||
return cls._instance
|
||
|
||
def __init__(self):
|
||
"""初始化检验配置管理器"""
|
||
self.dao = InspectionDAO()
|
||
self.configs = []
|
||
self.reload_configs()
|
||
|
||
def reload_configs(self):
|
||
"""重新加载检验配置"""
|
||
try:
|
||
self.configs = self.dao.get_all_inspection_configs(include_disabled=True)
|
||
logging.info(f"已加载{len(self.configs)}个检验项目配置")
|
||
return True
|
||
except Exception as e:
|
||
logging.error(f"加载检验配置失败: {str(e)}")
|
||
return False
|
||
|
||
def save_configs(self, configs, username='system'):
|
||
"""保存检验配置列表
|
||
|
||
Args:
|
||
configs: 检验配置列表
|
||
username: 操作用户
|
||
|
||
Returns:
|
||
bool: 保存是否成功
|
||
"""
|
||
try:
|
||
# 获取当前所有配置
|
||
current_configs = self.get_configs(include_disabled=True)
|
||
|
||
# 创建位置到配置ID的映射
|
||
position_to_id = {}
|
||
for config in current_configs:
|
||
position = config.get('position')
|
||
if position:
|
||
position_to_id[position] = config.get('id')
|
||
|
||
# 更新每个配置
|
||
for config in configs:
|
||
position = config.get('position')
|
||
if position in position_to_id:
|
||
# 更新已有配置
|
||
config_id = position_to_id[position]
|
||
self.update_config(config_id, config, username)
|
||
|
||
# 重新加载配置
|
||
self.reload_configs()
|
||
return True
|
||
except Exception as e:
|
||
logging.error(f"保存检验配置失败: {str(e)}")
|
||
return False
|
||
|
||
def get_configs(self, include_disabled=False):
|
||
"""获取检验配置列表
|
||
|
||
Args:
|
||
include_disabled: 是否包含禁用的项目
|
||
|
||
Returns:
|
||
list: 检验配置列表
|
||
"""
|
||
if include_disabled:
|
||
return self.configs
|
||
else:
|
||
return [config for config in self.configs if config['enabled']]
|
||
|
||
def get_enabled_configs(self):
|
||
"""获取已启用的检验配置列表
|
||
|
||
Returns:
|
||
list: 已启用的检验配置列表
|
||
"""
|
||
return self.get_configs(include_disabled=False)
|
||
|
||
def get_enabled_count(self):
|
||
"""获取已启用的检验项目数量
|
||
|
||
Returns:
|
||
int: 已启用的检验项目数量
|
||
"""
|
||
return len(self.get_enabled_configs())
|
||
|
||
def get_config_by_position(self, position):
|
||
"""根据位置获取检验配置
|
||
|
||
Args:
|
||
position: 位置序号 (1-6)
|
||
|
||
Returns:
|
||
dict: 检验配置,未找到则返回None
|
||
"""
|
||
for config in self.configs:
|
||
if config['position'] == position:
|
||
return config
|
||
return None
|
||
|
||
def toggle_config(self, position, enabled, username='system'):
|
||
"""启用或禁用检验配置
|
||
|
||
Args:
|
||
position: 位置序号 (1-6)
|
||
enabled: 是否启用
|
||
username: 操作用户
|
||
|
||
Returns:
|
||
bool: 操作是否成功
|
||
"""
|
||
result = self.dao.toggle_inspection_config(position, enabled, username)
|
||
if result:
|
||
self.reload_configs()
|
||
return result
|
||
|
||
def update_config(self, config_id, data, username='system'):
|
||
"""更新检验配置
|
||
|
||
Args:
|
||
config_id: 配置ID
|
||
data: 更新数据
|
||
username: 操作用户
|
||
|
||
Returns:
|
||
bool: 更新是否成功
|
||
"""
|
||
result = self.dao.update_inspection_config(config_id, data, username)
|
||
if result:
|
||
self.reload_configs()
|
||
return result
|
||
|
||
def get_inspection_headers(self):
|
||
"""获取检验表头,用于UI显示
|
||
|
||
Returns:
|
||
list: 检验表头列表
|
||
"""
|
||
enabled_configs = self.get_enabled_configs()
|
||
headers = []
|
||
|
||
# 按位置排序
|
||
enabled_configs.sort(key=lambda x: x['position'])
|
||
|
||
for config in enabled_configs:
|
||
headers.append(config['display_name'])
|
||
|
||
return headers
|
||
|
||
def get_inspection_column_count(self):
|
||
"""获取检验列数量
|
||
|
||
Returns:
|
||
int: 检验列数量
|
||
"""
|
||
return len(self.get_enabled_configs())
|
||
|
||
def save_inspection_data(self, order_id, data, username='system'):
|
||
"""保存检验数据
|
||
|
||
Args:
|
||
order_id: 工程号
|
||
data: 检验数据列表
|
||
username: 操作用户
|
||
|
||
Returns:
|
||
bool: 保存是否成功
|
||
"""
|
||
return self.dao.save_inspection_data(order_id, data, username)
|
||
|
||
def get_inspection_data(self, order_id):
|
||
"""获取检验数据
|
||
|
||
Args:
|
||
order_id: 工程号
|
||
|
||
Returns:
|
||
list: 检验数据列表
|
||
"""
|
||
return self.dao.get_inspection_data_by_order(order_id)
|
||
|
||
def delete_inspection_data(self, order_id, tray_id):
|
||
"""删除检验数据
|
||
|
||
Args:
|
||
order_id: 工程号
|
||
tray_id: 托盘号
|
||
"""
|
||
return self.dao.delete_inspection_data(order_id, tray_id) |