|
|
|
|
import io
|
|
|
|
|
import json
|
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
from typing import Optional, List
|
|
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
from fastapi import UploadFile
|
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
from module_admin.entity.vo.common_vo import CrudResponseModel
|
|
|
|
|
from module_admin.entity.vo.user_vo import CurrentUserModel
|
|
|
|
|
from module_admin.entity.vo.dataint_vo import TsmcbPageObject, SaveTsmcbModel
|
|
|
|
|
from module_admin.entity.do.dataint_do import SysTsmcb
|
|
|
|
|
from module_admin.dao.tsmcb_dao import TsmcbDao
|
|
|
|
|
from exceptions.exception import ServiceException, ServiceWarning
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from utils.common_util import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TsmcbService:
|
|
|
|
|
"""
|
|
|
|
|
智能问答服务层
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
async def get_tsmcb_list_services(cls, result_db: AsyncSession, tsmcb_query: TsmcbPageObject, current_user: CurrentUserModel):
|
|
|
|
|
result = await TsmcbDao.get_tsmcb_list(result_db, tsmcb_query)
|
|
|
|
|
return CamelCaseUtil.transform_result(result)
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
async def save_tsmcb(cls, result_db: AsyncSession, saveTsmcbModel: SaveTsmcbModel, current_user: CurrentUserModel):
|
|
|
|
|
if saveTsmcbModel.onum is None:
|
|
|
|
|
# add
|
|
|
|
|
saveTsmcbModel.onum = uuid.uuid4()
|
|
|
|
|
addObj = SysTsmcb(**saveTsmcbModel.model_dump())
|
|
|
|
|
addObj.create_by = current_user.user.user_name
|
|
|
|
|
addObj.create_time = datetime.now()
|
|
|
|
|
addObj.update_by = current_user.user.user_name
|
|
|
|
|
addObj.update_time = datetime.now()
|
|
|
|
|
await TsmcbDao.insert_tsmcb(result_db, addObj)
|
|
|
|
|
else:
|
|
|
|
|
# update
|
|
|
|
|
oldObj = await TsmcbDao.get_tsmcb_by_id(result_db, saveTsmcbModel.onum)
|
|
|
|
|
if oldObj is None:
|
|
|
|
|
raise ServiceException(message='所改对象不存在')
|
|
|
|
|
saveObj = saveTsmcbModel.model_dump(exclude_unset=True)
|
|
|
|
|
saveObj['update_by'] = current_user.user.user_name
|
|
|
|
|
saveObj['update_time'] = datetime.now()
|
|
|
|
|
await TsmcbDao.update_tsmcb(result_db, saveObj)
|
|
|
|
|
await result_db.commit()
|
|
|
|
|
return CrudResponseModel(is_success=True, message='操作成功')
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
async def delete_tsmcb(cls, db: AsyncSession, array: List[str]):
|
|
|
|
|
await TsmcbDao.delete_tsmcb(db, array)
|
|
|
|
|
await db.commit()
|
|
|
|
|
return CrudResponseModel(is_success=True, message='操作成功')
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
async def get_import_template_services():
|
|
|
|
|
"""
|
|
|
|
|
获取元数据导入模板service
|
|
|
|
|
:return: 元数据导入模板excel的二进制数据
|
|
|
|
|
"""
|
|
|
|
|
table_header_list = ['词性', '类型', '标准化替换字符', '前缀补充字符', '后缀补充字符', '状态']
|
|
|
|
|
selector_header_list = ['状态']
|
|
|
|
|
option_list = [{'状态': ['正常', '停用']}]
|
|
|
|
|
|
|
|
|
|
sheet_config1 = dict(
|
|
|
|
|
sheet_name="特殊名词补充", header_list=table_header_list, data_list=[], selector_header_list=selector_header_list
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
sheet_configs = [sheet_config1]
|
|
|
|
|
binary_data = get_excel_template_with_sheets(
|
|
|
|
|
sheet_configs, # 每个Sheet的配置(包含表头、选择器等)
|
|
|
|
|
option_list
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return binary_data
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
async def batch_import_tsmcb_data(cls,
|
|
|
|
|
result_db: AsyncSession,
|
|
|
|
|
file: UploadFile,
|
|
|
|
|
overWrite: bool,
|
|
|
|
|
current_user: CurrentUserModel):
|
|
|
|
|
table_header_dict = {
|
|
|
|
|
'词性': 'pos',
|
|
|
|
|
'类型': 'type',
|
|
|
|
|
'标准化替换字符': 'std_rpl_str',
|
|
|
|
|
'前缀补充字符': 'prefix_supp_str',
|
|
|
|
|
'后缀补充字符': 'suffix_supp_str',
|
|
|
|
|
'状态': 'status'
|
|
|
|
|
}
|
|
|
|
|
contents = await file.read()
|
|
|
|
|
excel_file = pd.ExcelFile(io.BytesIO(contents))
|
|
|
|
|
await file.close()
|
|
|
|
|
# 获取所有sheet名称
|
|
|
|
|
sheet_names = excel_file.sheet_names
|
|
|
|
|
# 逐个读取
|
|
|
|
|
tableSheet = sheet_names[0]
|
|
|
|
|
result_list = {
|
|
|
|
|
"rows": [],
|
|
|
|
|
"successCount": 0
|
|
|
|
|
}
|
|
|
|
|
if tableSheet == '特殊名词补充':
|
|
|
|
|
df = excel_file.parse(sheet_name=tableSheet, dtype=str, keep_default_na=False, na_values=[])
|
|
|
|
|
df.rename(columns=table_header_dict, inplace=True)
|
|
|
|
|
for index, row in df.iterrows():
|
|
|
|
|
noneValid = ''
|
|
|
|
|
if row['pos'] is None or len(row['pos']) == 0:
|
|
|
|
|
noneValid += "词性不能为空"
|
|
|
|
|
if row['type'] is None or len(row['type']) == 0:
|
|
|
|
|
if len(noneValid) > 0:
|
|
|
|
|
noneValid += ",类型不能为空"
|
|
|
|
|
else:
|
|
|
|
|
noneValid += "类型不能为空"
|
|
|
|
|
if row['std_rpl_str'] is None or len(row['std_rpl_str']) == 0:
|
|
|
|
|
if len(noneValid) > 0:
|
|
|
|
|
noneValid += ",标准化替换字符不能为空"
|
|
|
|
|
else:
|
|
|
|
|
noneValid += "标准化替换字符不能为空"
|
|
|
|
|
if row['prefix_supp_str'] is None or len(row['prefix_supp_str']) == 0:
|
|
|
|
|
if len(noneValid) > 0:
|
|
|
|
|
noneValid += ",前缀补充字符不能为空"
|
|
|
|
|
else:
|
|
|
|
|
noneValid += "前缀补充字符不能为空"
|
|
|
|
|
if row['suffix_supp_str'] is None or len(row['suffix_supp_str']) == 0:
|
|
|
|
|
if len(noneValid) > 0:
|
|
|
|
|
noneValid += ",后缀补充字符名称不能为空"
|
|
|
|
|
else:
|
|
|
|
|
noneValid += "后缀补充字符名称不能为空"
|
|
|
|
|
if row['status'] is None or len(row['status']) == 0:
|
|
|
|
|
if len(noneValid) > 0:
|
|
|
|
|
noneValid += ",状态不能为空"
|
|
|
|
|
else:
|
|
|
|
|
noneValid += "状态不能为空"
|
|
|
|
|
if len(noneValid) > 0:
|
|
|
|
|
result_list['rows'].append({
|
|
|
|
|
"row": index + 2,
|
|
|
|
|
"errorInfo": noneValid
|
|
|
|
|
})
|
|
|
|
|
continue
|
|
|
|
|
tsmcb = SaveTsmcbModel()
|
|
|
|
|
tsmcb.pos = row['pos']
|
|
|
|
|
tsmcb.type = row['type']
|
|
|
|
|
tsmcb.std_rpl_str = row['std_rpl_str']
|
|
|
|
|
tsmcb.prefix_supp_str = row['prefix_supp_str']
|
|
|
|
|
tsmcb.suffix_supp_str = row['suffix_supp_str']
|
|
|
|
|
tsmcb.status = '1' if row['status'] == '正常' else '0'
|
|
|
|
|
await cls.save_tsmcb(result_db, tsmcb, current_user)
|
|
|
|
|
result_list['successCount'] += 1
|
|
|
|
|
return result_list
|
|
|
|
|
|