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.
296 lines
13 KiB
296 lines
13 KiB
1 month ago
|
from module_admin.entity.vo.ndstand_vo import *
|
||
|
from module_admin.dao.ndstand_dao import *
|
||
|
from module_admin.entity.vo.user_vo import *
|
||
|
from utils.pwd_util import *
|
||
|
from utils.common_util import *
|
||
|
import traceback
|
||
|
from pydantic.errors import *
|
||
|
from fastapi import HTTPException # 如果你使用的是FastAPI的HTTPException
|
||
|
import httpx
|
||
|
|
||
|
from pydantic import ValidationError
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
class NdstandService:
|
||
|
"""
|
||
|
数据标准管理模块服务层
|
||
|
"""
|
||
|
|
||
|
@classmethod
|
||
|
def get_ndstand_select_option_services(cls, result_db: Session):
|
||
|
"""
|
||
|
获取数据标准列表不分页信息service
|
||
|
:param result_db: orm对象
|
||
|
:return: 数据标准列表不分页信息对象
|
||
|
"""
|
||
|
ndstand_list_result = NdstandDao.get_ndstand_select_option_dao(result_db)
|
||
|
|
||
|
return ndstand_list_result
|
||
|
|
||
|
@classmethod
|
||
|
def get_ndstand_list_services(cls, result_db: Session, query_object: NdstandModel):
|
||
|
"""
|
||
|
获取数据标准列表信息service
|
||
|
:param result_db: orm对象
|
||
|
:param query_object: 查询参数对象
|
||
|
:return: 数据标准列表信息对象
|
||
|
"""
|
||
|
ndstand_list_result = NdstandDao.get_ndstand_list(result_db, query_object)
|
||
|
|
||
|
return ndstand_list_result
|
||
|
|
||
|
@classmethod
|
||
|
def add_ndstand_services(cls, result_db: Session, page_object: NdstandModel):
|
||
|
"""
|
||
|
新增数据标准信息service
|
||
|
:param result_db: orm对象
|
||
|
:param page_object: 新增数据标准对象
|
||
|
:return: 新增数据标准校验结果
|
||
|
"""
|
||
|
ndstand = NdstandDao.get_ndstand_detail_by_info(result_db, NdstandModel(**dict(data_std_cn_name=page_object.data_std_cn_name)))
|
||
|
if ndstand:
|
||
|
result = dict(is_success=False, message='数据标准名称已存在')
|
||
|
else:
|
||
|
try:
|
||
|
NdstandDao.add_ndstand_dao(result_db, page_object)
|
||
|
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 CrudNdstandResponse(**result)
|
||
|
|
||
|
@classmethod
|
||
|
def edit_ndstand_services(cls, result_db: Session, page_object: NdstandModel):
|
||
|
"""
|
||
|
数据标准信息service
|
||
|
:param result_db: orm对象
|
||
|
:param page_object: 数据标准信息对象
|
||
|
:return: 编辑数据标准校验结果
|
||
|
"""
|
||
|
edit_ndstand = page_object.dict(exclude_unset=True)
|
||
|
ndstand_info = cls.detail_ndstand_services(result_db, edit_ndstand.get('onum'))
|
||
|
if ndstand_info:
|
||
|
if ndstand_info.onum != page_object.onum:
|
||
|
ndstand = NdstandDao.get_ndstand_by_info(result_db, NdstandModel(**dict(onum=page_object.onum)))
|
||
|
if ndstand:
|
||
|
result = dict(is_success=False, message='数据标准名称已存在')
|
||
|
return CrudNdstandResponse(**result)
|
||
|
try:
|
||
|
NdstandDao.edit_ndstand_dao(result_db, edit_ndstand)
|
||
|
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 CrudNdstandResponse(**result)
|
||
|
|
||
|
@classmethod
|
||
|
def delete_ndstand_services(cls, result_db: Session, page_object: DeleteNdstandModel):
|
||
|
"""
|
||
|
删除数据标准信息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)
|
||
|
NdstandDao.delete_ndstand_dao(result_db, NdstandModel(**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 CrudNdstandResponse(**result)
|
||
|
|
||
|
@classmethod
|
||
|
def detail_ndstand_services(cls, result_db: Session, onum: int):
|
||
|
"""
|
||
|
获取数据标准详细信息service
|
||
|
:param result_db: orm对象
|
||
|
:param onum: 数据标准id
|
||
|
:return: 数据标准id对应的信息
|
||
|
"""
|
||
|
ndstand = NdstandDao.get_ndstand_detail_by_id(result_db, onum=onum)
|
||
|
|
||
|
return ndstand
|
||
|
|
||
|
@classmethod
|
||
|
def batch_import_ndstand_services(cls, result_db: Session, ndstand_import: ImportNdstandModel, current_user: CurrentUserInfoServiceResponse):
|
||
|
"""
|
||
|
批量导入数据标准service
|
||
|
:param ndstand_import: 数据标准导入参数对象
|
||
|
:param result_db: orm对象
|
||
|
:param current_ndstand: 当前代码表对象
|
||
|
:return: 批量导入数据标准结果
|
||
|
"""
|
||
|
header_dict = {
|
||
|
"序号": "onum",
|
||
|
"数据标准编号": "data_std_no",
|
||
|
"数据标准中文名": "data_std_cn_name",
|
||
|
"数据标准英文": "data_std_eng_name",
|
||
|
"所属范围": "belt_scop",
|
||
|
"业务定义": "busi_defn",
|
||
|
"数据标准主题": "data_std_schm",
|
||
|
"标准一级分类": "std_pri_clas",
|
||
|
"标准二级分类": "std_scd_clas",
|
||
|
"标准三级分类": "std_thre_clas",
|
||
|
"数据安全分级": "data_sec_fifd",
|
||
|
"数据类别": "data_clas",
|
||
|
"取值范围": "val_scop",
|
||
|
"代码编号": "fddict_col_no",
|
||
|
"状态": "status"
|
||
|
}
|
||
|
filepath = get_filepath_from_url(ndstand_import.url)
|
||
|
df = pd.read_excel(filepath)
|
||
|
|
||
|
df.rename(columns=header_dict, inplace=True)
|
||
|
add_error_result = []
|
||
|
count = 0
|
||
|
try:
|
||
|
for index, row in df.iterrows():
|
||
|
count = count + 1
|
||
|
if row['status'] == '正常':
|
||
|
row['status'] = '0'
|
||
|
if row['status'] == '停用':
|
||
|
row['status'] = '1'
|
||
|
add_ndstand = NdstandModel(
|
||
|
**dict(
|
||
|
onum=row['onum'],
|
||
|
data_std_no = str(row['data_std_no']).replace('nan', ''),
|
||
|
data_std_cn_name = str(row['data_std_cn_name']).replace('nan', ''),
|
||
|
data_std_eng_name = str(row['data_std_eng_name']).replace('nan', ''),
|
||
|
belt_scop = str(row['belt_scop']).replace('nan', ''),
|
||
|
busi_defn = str(row['busi_defn']).replace('nan', ''),
|
||
|
data_std_schm = str(row['data_std_schm']).replace('nan', ''),
|
||
|
std_pri_clas = str(row['std_pri_clas']).replace('nan', ''),
|
||
|
std_scd_clas = str(row['std_scd_clas']).replace('nan', ''),
|
||
|
std_thre_clas = str(row['std_thre_clas']).replace('nan', ''),
|
||
|
data_sec_fifd = str(row['data_sec_fifd']).replace('nan', ''),
|
||
|
data_clas = str(row['data_clas']).replace('nan', ''),
|
||
|
val_scop = str(row['val_scop']).replace('nan', ''),
|
||
|
fddict_col_no = str(row['fddict_col_no']).replace('nan', ''),
|
||
|
status = str(row['status']),
|
||
|
create_by=current_user.user.user_name,
|
||
|
update_by=current_user.user.user_name,
|
||
|
)
|
||
|
)
|
||
|
ndstand_info = NdstandDao.get_ndstand_by_info(result_db, NdstandModel(**dict(onum=row['onum'])))
|
||
|
if ndstand_info:
|
||
|
if ndstand_import.is_update:
|
||
|
edit_ndstand = NdstandModel(
|
||
|
**dict(
|
||
|
onum=row['onum'],
|
||
|
data_std_no = str(row['data_std_no']).replace('nan', ''),
|
||
|
data_std_cn_name = str(row['data_std_cn_name']).replace('nan', ''),
|
||
|
data_std_eng_name = str(row['data_std_eng_name']).replace('nan', ''),
|
||
|
belt_scop = str(row['belt_scop']).replace('nan', ''),
|
||
|
busi_defn = str(row['busi_defn']).replace('nan', ''),
|
||
|
data_std_schm = str(row['data_std_schm']).replace('nan', ''),
|
||
|
std_pri_clas = str(row['std_pri_clas']).replace('nan', ''),
|
||
|
std_scd_clas = str(row['std_scd_clas']).replace('nan', ''),
|
||
|
std_thre_clas = str(row['std_thre_clas']).replace('nan', ''),
|
||
|
data_sec_fifd = str(row['data_sec_fifd']).replace('nan', ''),
|
||
|
data_clas = str(row['data_clas']).replace('nan', ''),
|
||
|
val_scop = str(row['val_scop']).replace('nan', ''),
|
||
|
fddict_col_no = str(row['fddict_col_no']).replace('nan', ''),
|
||
|
status = str(row['status']),
|
||
|
update_by=current_user.user.user_name
|
||
|
)
|
||
|
).dict(exclude_unset=True)
|
||
|
NdstandDao.edit_ndstand_dao(result_db, edit_ndstand)
|
||
|
else:
|
||
|
add_error_result.append(f"{count}.用户账号{row['user_name']}已存在")
|
||
|
else:
|
||
|
NdstandDao.add_ndstand_dao(result_db, add_ndstand)
|
||
|
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))
|
||
|
print(f"Caught an exception: {e}")
|
||
|
if isinstance(e, HTTPException):
|
||
|
print(f"HTTPException: {e.status_code} {e.detail}")
|
||
|
else:
|
||
|
if isinstance(e, ValidationError):
|
||
|
print(f"ValidationError: {e.errors()}")
|
||
|
else:
|
||
|
print(f"Exception: {e}")
|
||
|
print("Traceback:")
|
||
|
print(traceback.format_exc())
|
||
|
|
||
|
return CrudNdstandResponse(**result)
|
||
|
|
||
|
@staticmethod
|
||
|
def get_ndstand_import_template_services():
|
||
|
"""
|
||
|
获取数据标准导入模板service
|
||
|
:return: 数据标准导入模板excel的二进制数据
|
||
|
"""
|
||
|
header_list = ["序号","数据标准编号","数据标准中文名","数据标准英文","所属范围","业务定义","数据标准主题","标准一级分类","标准二级分类","标准三级分类","数据安全分级","数据类别","取值范围","代码编号","状态"]
|
||
|
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
|
||
|
|
||
|
|
||
|
@staticmethod
|
||
|
def export_ndstand_list_services(ndstand_list: List):
|
||
|
"""
|
||
|
导出数据标准信息service
|
||
|
:param ndstand_list: 数据标准列表
|
||
|
:return: 数据治理信息对应excel的二进制数据
|
||
|
"""
|
||
|
# 创建一个映射代码,将英文键映射到中文键
|
||
|
mapping_dict = {
|
||
|
"onum": "序号",
|
||
|
"data_std_no": "数据标准编号",
|
||
|
"data_std_cn_name": "数据标准中文名",
|
||
|
"data_std_eng_name": "数据标准英文",
|
||
|
"belt_scop": "所属范围",
|
||
|
"busi_defn": "业务定义",
|
||
|
"data_std_schm": "数据标准主题",
|
||
|
"std_pri_clas": "标准一级分类",
|
||
|
"std_scd_clas": "标准二级分类",
|
||
|
"std_thre_clas": "标准三级分类",
|
||
|
"data_sec_fifd": "数据安全分级",
|
||
|
"data_clas": "数据类别",
|
||
|
"val_scop": "取值范围",
|
||
|
"fddict_col_no": "代码编号",
|
||
|
"status": "状态",
|
||
|
}
|
||
|
|
||
|
data = [NdstandModel(**vars(row)).dict() for row in ndstand_list]
|
||
|
|
||
|
for item in data:
|
||
|
if item.get('status') == '0':
|
||
|
item['status'] = '正常'
|
||
|
else:
|
||
|
item['status'] = '停用'
|
||
|
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 get_all_ndstand_services(cls, result_db: Session):
|
||
|
"""
|
||
|
获取字所有典类型列表信息service
|
||
|
:param result_db: orm对象
|
||
|
:return: 字典类型列表信息对象
|
||
|
"""
|
||
|
ndstand_list_result = NdstandDao.get_all_ndstand(result_db)
|
||
|
|
||
|
return ndstand_list_result
|