You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
282 lines
12 KiB
282 lines
12 KiB
from module_admin.entity.vo.user_vo import *
|
|
from module_admin.entity.vo.vecset_vo import *
|
|
from module_admin.entity.vo.dasset_vo import *
|
|
from module_admin.dao.vecset_dao import *
|
|
from module_admin.dao.dasset_dao import *
|
|
|
|
from utils.pwd_util import *
|
|
from utils.common_util import *
|
|
|
|
|
|
class VecsetService:
|
|
"""
|
|
智能语句配置模块服务层
|
|
"""
|
|
|
|
@classmethod
|
|
def get_vecset_list_services(cls, result_db: Session, query_object: VecsetQueryModel, data_scope_sql: str):
|
|
"""
|
|
获取智能语句配置信息service
|
|
:param result_db: orm对象
|
|
:param query_object: 查询参数对象
|
|
:param data_scope_sql: 数据权限对应的查询sql语句
|
|
:return: 字段列表信息对象
|
|
"""
|
|
vecset_list_result = VecsetDao.get_vecset_list(result_db, query_object, data_scope_sql)
|
|
#test_asset = SysDasset.get_dasset_detail_by_dname(result_db,dasset_name='资讯数据资产')
|
|
#print(test_asset,99999999)
|
|
|
|
return vecset_list_result
|
|
|
|
@classmethod
|
|
def add_vecset_services(cls, result_db: Session, page_object: AddVecsetModel):
|
|
"""
|
|
新增智能语句配置信息service
|
|
:param result_db: orm对象
|
|
:param page_object: 新增智能语句配置对象
|
|
:return: 新增字段校验结果
|
|
"""
|
|
add_vecset = VecsetModel(**page_object.dict())
|
|
vecset = VecsetDao.get_vecset_by_info(result_db, VecsetModel(**dict(stab_name=page_object.stab_name)))
|
|
if vecset:
|
|
result = dict(is_success=False, message='字段名已存在')
|
|
else:
|
|
try:
|
|
add_result = VecsetDao.add_vecset_dao(result_db, add_vecset)
|
|
onum = add_result.onum
|
|
result_db.commit()
|
|
result = dict(is_success=True, message='新增成功')
|
|
except Exception as e:
|
|
result_db.rollback()
|
|
result = dict(is_success=False, message=str(e))
|
|
|
|
return CrudVecsetResponse(**result)
|
|
|
|
@classmethod
|
|
def edit_vecset_services(cls, result_db: Session, page_object: AddVecsetModel):
|
|
"""
|
|
编辑智能语句配置信息service
|
|
:param result_db: orm对象
|
|
:param page_object: 编辑智能语句配置对象
|
|
:return: 编辑智能语句配置校验结果
|
|
"""
|
|
edit_vecset = page_object.dict(exclude_unset=True)
|
|
if page_object.type == 'status' or page_object.type == 'avatar':
|
|
del edit_vecset['type']
|
|
vecset_info = cls.detail_vecset_services(result_db, edit_vecset.get('onum'))
|
|
if vecset_info:
|
|
if page_object.type != 'status' and page_object.type != 'avatar' and vecset_info.vecset.stab_name != page_object.stab_name:
|
|
vecset = VecsetDao.get_vecset_by_info(result_db, VecsetModel(**dict(stab_name=page_object.stab_name)))
|
|
if vecset:
|
|
result = dict(is_success=False, message='字段名已存在')
|
|
return CrudVecsetResponse(**result)
|
|
try:
|
|
VecsetDao.edit_vecset_dao(result_db, edit_vecset)
|
|
if page_object.type != 'status' and page_object.type != 'avatar':
|
|
onum_dict = dict(onum=page_object.onum)
|
|
result_db.commit()
|
|
result = dict(is_success=True, message='更新成功')
|
|
except Exception as e:
|
|
result_db.rollback()
|
|
result = dict(is_success=False, message=str(e))
|
|
else:
|
|
result = dict(is_success=False, message='字段不存在')
|
|
|
|
return CrudVecsetResponse(**result)
|
|
|
|
@classmethod
|
|
def delete_vecset_services(cls, result_db: Session, page_object: DeleteVecsetModel):
|
|
"""
|
|
删除智能语句配置信息service
|
|
:param result_db: orm对象
|
|
:param page_object: 删除智能语句配置对象
|
|
:return: 删除智能语句配置校验结果
|
|
"""
|
|
|
|
if page_object.onums.split(','):
|
|
onum_list = page_object.onums.split(',')
|
|
|
|
try:
|
|
for onum in onum_list:
|
|
onum_dict = dict(onum=onum, update_by=page_object.update_by, update_time=page_object.update_time) #
|
|
VecsetDao.delete_vecset_dao(result_db, VecsetModel(**onum_dict))
|
|
result_db.commit()
|
|
result = dict(is_success=True, message='删除成功')
|
|
except Exception as e:
|
|
result_db.rollback()
|
|
result = dict(is_success=False, message=str(e))
|
|
else:
|
|
result = dict(is_success=False, message='传入智能语句配置id为空')
|
|
return CrudVecsetResponse(**result)
|
|
|
|
@classmethod
|
|
def detail_vecset_services(cls, result_db: Session, onum: int):
|
|
"""
|
|
获取智能语句配置详细信息service
|
|
:param result_db: orm对象
|
|
:param onum: 智能语句配置id
|
|
:return: 智能语句配置id对应的信息
|
|
"""
|
|
vecset = VecsetDao.get_vecset_detail_by_id(result_db, onum=onum)
|
|
|
|
return VecsetDetailModel(
|
|
vecset=vecset.vecset_basic_info,
|
|
dasset=vecset.vecset_dasset_info
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
def export_vecset_list_services(vecset_list: List):
|
|
"""
|
|
导出配置表信息service
|
|
:param vecset_list: 代码信息列表
|
|
:return: 配置表信息对应excel的二进制数据
|
|
"""
|
|
# 创建一个映射代码,将英文键映射到中文键
|
|
mapping_dict = {
|
|
"dasset_id": "数据域名",
|
|
"onum": "序号",
|
|
"stab_name": "表名称",
|
|
"squery": "查询语句",
|
|
"sanal_plan": "分析方法",
|
|
"sintnt_term": "意图词",
|
|
|
|
"ssql": "参考sql",
|
|
"sim_thrsh": "相似阈值",
|
|
"status": "状态",
|
|
# "create_by": "创建者",
|
|
# "create_time": "创建时间",
|
|
# "update_by": "更新者",
|
|
# "update_time": "更新时间",
|
|
}
|
|
|
|
#data = [VecsetModel(**vars(row)).dict() for row in vecset_list]
|
|
data = [VecsetModel(**row).dict() for row in vecset_list]
|
|
|
|
for item in data:
|
|
if item.get('status') == '0':
|
|
item['status'] = '正常'
|
|
else:
|
|
item['status'] = '停用'
|
|
if item.get('dasset_id') == 101:
|
|
item['dasset_id'] = '资讯域'
|
|
elif item.get('dasset_id') == 102:
|
|
item['dasset_id'] = '财务域'
|
|
elif item.get('dasset_id') == 145:
|
|
item['dasset_id'] = '账户域'
|
|
else:
|
|
item['dasset_id'] == '其他域'
|
|
new_data = [{mapping_dict.get(key): value for key, value in item.items() if mapping_dict.get(key)} for item in data]
|
|
binary_data = export_list2excel(new_data)
|
|
|
|
return binary_data
|
|
|
|
@classmethod
|
|
def batch_import_vecset_services(cls, result_db: Session, vecset_import: ImportVecsetModel, current_user: CurrentUserInfoServiceResponse):
|
|
"""
|
|
批量导入智能语句配置表service
|
|
:param vecset_import: 智能语句配置表导入参数对象
|
|
:param result_db: orm对象
|
|
:param current_vecset: 当前智能语句配置表对象
|
|
:return: 批量导入智能语句配置表结果
|
|
"""
|
|
header_dict = {
|
|
"数据域名": "dasset_id",
|
|
#"序号": "onum",
|
|
"表名称": "stab_name",
|
|
"查询语句": "squery",
|
|
"分析方法": "sanal_plan",
|
|
"意图词": "sintnt_term",
|
|
|
|
"参考sql": "ssql",
|
|
"相似阈值": "sim_thrsh",
|
|
"状态": "status"
|
|
}
|
|
filepath = get_filepath_from_url(vecset_import.url)
|
|
df = pd.read_excel(filepath)
|
|
df.rename(columns=header_dict, inplace=True)
|
|
add_error_result = []
|
|
count = 0
|
|
|
|
# VecsetDao.get_vecset_by_info(result_db, VecsetModel(**dict(stab_name=page_object.stab_name)))
|
|
|
|
|
|
|
|
|
|
try:
|
|
for index, row in df.iterrows():
|
|
count = count + 1
|
|
|
|
if row['dasset_id'] == '资讯域':
|
|
row['dasset_id'] = '101'
|
|
if row['dasset_id'] == '财务域':
|
|
row['dasset_id'] = '102'
|
|
if row['dasset_id'] == '其他域':
|
|
row['dasset_id'] = '121'
|
|
if row['status'] == '正常':
|
|
row['status'] = '0'
|
|
if row['status'] == '停用':
|
|
row['status'] = '1'
|
|
add_vecset = VecsetModel(
|
|
**dict(
|
|
dasset_id=row['dasset_id'],
|
|
#onum=row['onum'],
|
|
stab_name = str(row['stab_name']).replace('nan', ''),
|
|
squery = str(row['squery']).replace('nan', ''),
|
|
sanal_plan = str(row['sanal_plan']).replace('nan', ''),
|
|
sintnt_term = str(row['sintnt_term']).replace('nan', ''),
|
|
ssql = str(row['ssql']).replace('nan', ''),
|
|
sim_thrsh = str(row['sim_thrsh']).replace('nan', ''),
|
|
status=row['status'],
|
|
create_by=current_user.user.user_name,
|
|
update_by=current_user.user.user_name,
|
|
)
|
|
)
|
|
vecset_info = VecsetDao.get_vecset_by_info_imp(result_db, VecsetModel(**dict(squery=row['squery'])))
|
|
if vecset_info:
|
|
if vecset_import.is_update:
|
|
edit_vecset = VecsetModel(
|
|
**dict(
|
|
dasset_id=row['dasset_id'],
|
|
#onum=row['onum'],
|
|
stab_name = str(row['stab_name']).replace('nan', ''),
|
|
squery = str(row['squery']).replace('nan', ''),
|
|
sanal_plan = str(row['sanal_plan']).replace('nan', ''),
|
|
sintnt_term = str(row['sintnt_term']).replace('nan', ''),
|
|
|
|
ssql = str(row['ssql']).replace('nan', ''),
|
|
sim_thrsh = str(row['sim_thrsh']).replace('nan', ''),
|
|
status=row['status'],
|
|
update_by=current_user.user.user_name
|
|
)
|
|
).dict(exclude_unset=True)
|
|
VecsetDao.edit_vecset_dao(result_db, edit_vecset)
|
|
else:
|
|
add_error_result.append(f"{count}.用户账号{row['user_name']}已存在")
|
|
else:
|
|
VecsetDao.add_vecset_dao(result_db, add_vecset)
|
|
result_db.commit()
|
|
result = dict(is_success=True, message='\n'.join(add_error_result))
|
|
except Exception as e:
|
|
result_db.rollback()
|
|
result = dict(is_success=False, message=str(e))
|
|
|
|
return CrudVecsetResponse(**result)
|
|
|
|
@staticmethod
|
|
def get_vecset_import_template_services():
|
|
"""
|
|
获取智能语句配置表导入模板service
|
|
:return: 智能语句配置表导入模板excel的二进制数据
|
|
"""
|
|
#header_list = ["数据域名", "序号", "查询语句", "分析方法", "意图词", "表名称", "参考sql", "相似阈值", "状态"]
|
|
header_list = ["数据域名","表名称", "查询语句", "分析方法", "意图词", "参考sql", "相似阈值", "状态"]
|
|
|
|
selector_header_list = ["数据域名","状态"]
|
|
option_list = [{"数据域名": ["资讯域", "财务域", "账户域", "其他域"]},{"状态": ["正常", "停用"]}]
|
|
binary_data = get_excel_template(header_list=header_list, selector_header_list=selector_header_list, option_list=option_list)
|
|
|
|
return binary_data
|