|
@@ -1,9 +1,9 @@
|
|
|
|
+import data_process,data_send
|
|
import threading,utils,os
|
|
import threading,utils,os
|
|
|
|
|
|
from models.project_data import ProjectModel,SubProjectModel, SubProjectItemModel
|
|
from models.project_data import ProjectModel,SubProjectModel, SubProjectItemModel
|
|
from stores.project_store import ProjectStore
|
|
from stores.project_store import ProjectStore
|
|
-import data_process,data_send
|
|
|
|
-
|
|
|
|
|
|
+from services.log_helper import LogHelper,OperationType,OperationModule
|
|
|
|
|
|
class ProjectService:
|
|
class ProjectService:
|
|
|
|
|
|
@@ -20,25 +20,31 @@ class ProjectService:
|
|
|
|
|
|
def add_project(self, project:ProjectModel):
|
|
def add_project(self, project:ProjectModel):
|
|
try:
|
|
try:
|
|
- project.create_by = 'admin' if project.create_by is None else project.create_by
|
|
|
|
self._project_store.insert_project(project)
|
|
self._project_store.insert_project(project)
|
|
|
|
+ LogHelper.log_success(OperationType.CREATE,OperationModule.PROJECT, f"添加项目成功",utils.to_str(project.to_dict()))
|
|
return True, ''
|
|
return True, ''
|
|
except Exception as e:
|
|
except Exception as e:
|
|
self._logger.error(f"添加项目失败:{e}")
|
|
self._logger.error(f"添加项目失败:{e}")
|
|
|
|
+ LogHelper.log_fail(OperationType.CREATE, OperationModule.PROJECT, f"添加项目失败 错误:{project.project_name} {str(e)}",utils.to_str(project.to_dict()))
|
|
return False, '添加项目失败'
|
|
return False, '添加项目失败'
|
|
def update_project(self, project:ProjectModel):
|
|
def update_project(self, project:ProjectModel):
|
|
project = self._project_store.query_project_by_id(project.id)
|
|
project = self._project_store.query_project_by_id(project.id)
|
|
|
|
+ log_data = utils.to_str(project.to_dict())
|
|
if project:
|
|
if project:
|
|
self._project_store.update_project(project)
|
|
self._project_store.update_project(project)
|
|
|
|
+ LogHelper.log_success(OperationType.UPDATE, OperationModule.PROJECT, f"更新项目成功", log_data, utils.to_str(project.to_dict()))
|
|
return True, ''
|
|
return True, ''
|
|
else:
|
|
else:
|
|
|
|
+ LogHelper.log_fail( OperationType.UPDATE, OperationModule.PROJECT, f"更新项目失败 错误:{project.project_name}项目不存在", log_data)
|
|
return False, '项目不存在'
|
|
return False, '项目不存在'
|
|
def delete_project(self, project_id:int):
|
|
def delete_project(self, project_id:int):
|
|
project = self._project_store.query_project_by_id(project_id)
|
|
project = self._project_store.query_project_by_id(project_id)
|
|
if project:
|
|
if project:
|
|
self._project_store.delete_project(project_id)
|
|
self._project_store.delete_project(project_id)
|
|
|
|
+ LogHelper.log_success(OperationType.DELETE, OperationModule.PROJECT, f"删除项目成功", utils.to_str(project.to_dict()))
|
|
return True,''
|
|
return True,''
|
|
else:
|
|
else:
|
|
|
|
+ LogHelper.log_fail(OperationType.DELETE, OperationModule.PROJECT, f"删除项目失败 错误:{project.project_name}项目不存在",utils.to_str(project.to_dict()))
|
|
return False, '项目不存在'
|
|
return False, '项目不存在'
|
|
|
|
|
|
def get_all_sub_project_paginated(self, project_id: int, page: int, per_page: int, keyword: str = None, status: int = None) ->(list[SubProjectModel], int):
|
|
def get_all_sub_project_paginated(self, project_id: int, page: int, per_page: int, keyword: str = None, status: int = None) ->(list[SubProjectModel], int):
|
|
@@ -54,16 +60,19 @@ class ProjectService:
|
|
return project
|
|
return project
|
|
|
|
|
|
def save_sub_project(self, sub_project:SubProjectModel, project_files: list, delete_old: bool):
|
|
def save_sub_project(self, sub_project:SubProjectModel, project_files: list, delete_old: bool):
|
|
|
|
+ log_data=""
|
|
if sub_project.id:
|
|
if sub_project.id:
|
|
sub_data = self._project_store.query_sub_project(sub_project.id)
|
|
sub_data = self._project_store.query_sub_project(sub_project.id)
|
|
|
|
+ log_data = utils.to_str(sub_data.to_dict())
|
|
if not sub_data:
|
|
if not sub_data:
|
|
|
|
+ LogHelper.log_fail(OperationType.UPDATE, OperationModule.SUB_PROJECT, f"修改工程失败 错误:{sub_project.sub_project_name} 工程不存在", log_data)
|
|
return False, '子项目不存在'
|
|
return False, '子项目不存在'
|
|
sub_data.sub_project_name = sub_project.sub_project_name
|
|
sub_data.sub_project_name = sub_project.sub_project_name
|
|
sub_data.standard_version = sub_project.standard_version
|
|
sub_data.standard_version = sub_project.standard_version
|
|
sub_data.work_catalog = sub_project.work_catalog
|
|
sub_data.work_catalog = sub_project.work_catalog
|
|
sub_data.work_content = sub_project.work_content
|
|
sub_data.work_content = sub_project.work_content
|
|
sub_data.status = 0
|
|
sub_data.status = 0
|
|
- self.update_sub_project(sub_data)
|
|
|
|
|
|
+ self._project_store.update_sub_project(sub_data)
|
|
else:
|
|
else:
|
|
sub_data = SubProjectModel(
|
|
sub_data = SubProjectModel(
|
|
project_id=sub_project.project_id,
|
|
project_id=sub_project.project_id,
|
|
@@ -74,19 +83,26 @@ class ProjectService:
|
|
file_paths="",
|
|
file_paths="",
|
|
status=0
|
|
status=0
|
|
)
|
|
)
|
|
- new_id = self.add_sub_project(sub_data)
|
|
|
|
|
|
+ new_id = self._project_store.insert_sub_project(sub_data)
|
|
sub_data.id = new_id
|
|
sub_data.id = new_id
|
|
try:
|
|
try:
|
|
# 处理文件上传逻辑
|
|
# 处理文件上传逻辑
|
|
if project_files:
|
|
if project_files:
|
|
file_paths = self._process_file_upload(sub_data, project_files, delete_old)
|
|
file_paths = self._process_file_upload(sub_data, project_files, delete_old)
|
|
- self.update_sub_project_file_paths(sub_data.id, file_paths)
|
|
|
|
|
|
+ self._update_sub_project_file_paths(sub_data.id, file_paths)
|
|
|
|
+ sub_data.file_paths = file_paths
|
|
elif delete_old:
|
|
elif delete_old:
|
|
return False,"请上传项目数据文件"
|
|
return False,"请上传项目数据文件"
|
|
|
|
+ if sub_project.id:
|
|
|
|
+ LogHelper.log_success(OperationType.UPDATE ,OperationModule.SUB_PROJECT, f"修改工程成功", log_data, utils.to_str(sub_data.to_dict()))
|
|
|
|
+ else:
|
|
|
|
+ LogHelper.log_success(OperationType.CREATE ,OperationModule.SUB_PROJECT, f"添加工程成功", utils.to_str(sub_data.to_dict()))
|
|
return True, ''
|
|
return True, ''
|
|
except ValueError as ve:
|
|
except ValueError as ve:
|
|
|
|
+ LogHelper.log_fail(OperationType.UPDATE, OperationModule.SUB_PROJECT, f"{'修改' if sub_project.id else '添加'}工程失败 错误:{sub_project.sub_project_name} {str(ve)}", utils.to_str(sub_data.to_dict()))
|
|
return False,str(ve)
|
|
return False,str(ve)
|
|
except Exception as e:
|
|
except Exception as e:
|
|
|
|
+ LogHelper.log_fail(OperationType.UPDATE, OperationModule.SUB_PROJECT, f"{'修改' if sub_project.id else '添加'}工程失败 错误:{sub_project.sub_project_name} {str(e)}", utils.to_str(sub_data.to_dict()))
|
|
return False,f'服务器错误: {str(e)}'
|
|
return False,f'服务器错误: {str(e)}'
|
|
|
|
|
|
def _process_file_upload(self,sub_project: SubProjectModel, files: list, delete_old: bool) -> str:
|
|
def _process_file_upload(self,sub_project: SubProjectModel, files: list, delete_old: bool) -> str:
|
|
@@ -97,10 +113,24 @@ class ProjectService:
|
|
self._logger.info(f"保存处理文件,项目ID:{sub_project.project_id},子项目ID:{sub_project.id}")
|
|
self._logger.info(f"保存处理文件,项目ID:{sub_project.project_id},子项目ID:{sub_project.id}")
|
|
if delete_old:
|
|
if delete_old:
|
|
if os.path.exists(sub_project_dir):
|
|
if os.path.exists(sub_project_dir):
|
|
|
|
+ delete_paths = []
|
|
for filename in os.listdir(sub_project_dir):
|
|
for filename in os.listdir(sub_project_dir):
|
|
file_path = os.path.join(sub_project_dir, filename)
|
|
file_path = os.path.join(sub_project_dir, filename)
|
|
if os.path.isfile(file_path):
|
|
if os.path.isfile(file_path):
|
|
- os.remove(file_path)
|
|
|
|
|
|
+ delete_dir = os.path.join(sub_project_dir, 'delete')
|
|
|
|
+ os.makedirs(delete_dir, exist_ok=True)
|
|
|
|
+ # 处理文件名冲突
|
|
|
|
+ base_name = os.path.basename(file_path)
|
|
|
|
+ target_path = os.path.join(delete_dir, base_name)
|
|
|
|
+ counter = 1
|
|
|
|
+ while os.path.exists(target_path):
|
|
|
|
+ name, ext = os.path.splitext(base_name)
|
|
|
|
+ target_path = os.path.join(delete_dir, f"{name}_{counter}{ext}")
|
|
|
|
+ counter += 1
|
|
|
|
+ os.rename(file_path, target_path)
|
|
|
|
+ delete_paths.append(target_path)
|
|
|
|
+ if len(delete_paths) > 0:
|
|
|
|
+ LogHelper.log_success(OperationType.DELETE, OperationModule.SUB_PROJECT, f"删除子工程文件:{sub_project.sub_project_name}", utils.to_str(delete_paths))
|
|
file_paths =[] if delete_old or not sub_project.file_paths else sub_project.file_paths.split(',')
|
|
file_paths =[] if delete_old or not sub_project.file_paths else sub_project.file_paths.split(',')
|
|
for file in files:
|
|
for file in files:
|
|
if not file.filename:
|
|
if not file.filename:
|
|
@@ -115,13 +145,7 @@ class ProjectService:
|
|
file_paths.append(file_path)
|
|
file_paths.append(file_path)
|
|
return ','.join(file_paths)
|
|
return ','.join(file_paths)
|
|
|
|
|
|
- def add_sub_project(self, sub_project:SubProjectModel):
|
|
|
|
- return self._project_store.insert_sub_project(sub_project)
|
|
|
|
-
|
|
|
|
- def update_sub_project(self, sub_project:SubProjectModel):
|
|
|
|
- self._project_store.update_sub_project(sub_project)
|
|
|
|
-
|
|
|
|
- def update_sub_project_file_paths(self, sub_project_id: int, paths: str):
|
|
|
|
|
|
+ def _update_sub_project_file_paths(self, sub_project_id: int, paths: str):
|
|
self._project_store.update_sub_project_file_path(sub_project_id, paths)
|
|
self._project_store.update_sub_project_file_path(sub_project_id, paths)
|
|
def start_sub_project_task(self, sub_project_id: int) -> (bool, str):
|
|
def start_sub_project_task(self, sub_project_id: int) -> (bool, str):
|
|
data = self._project_store.query_sub_project(sub_project_id)
|
|
data = self._project_store.query_sub_project(sub_project_id)
|
|
@@ -132,6 +156,7 @@ class ProjectService:
|
|
return False, '正在分析处理中'
|
|
return False, '正在分析处理中'
|
|
if data.status == 32 or data.status == 33:
|
|
if data.status == 32 or data.status == 33:
|
|
return False, '正在上传数据中'
|
|
return False, '正在上传数据中'
|
|
|
|
+ LogHelper.log_success(OperationType.PROCESS_TASK, OperationModule.SUB_PROJECT, f"启动工程任务", utils.to_str(data.to_dict()))
|
|
thread = threading.Thread(target = self._process_and_send_sub_project, args=(data,))
|
|
thread = threading.Thread(target = self._process_and_send_sub_project, args=(data,))
|
|
thread.start()
|
|
thread.start()
|
|
return True, ''
|
|
return True, ''
|
|
@@ -153,6 +178,7 @@ class ProjectService:
|
|
return False, '正在分析处理中'
|
|
return False, '正在分析处理中'
|
|
if data.status == 32 or data.status == 33:
|
|
if data.status == 32 or data.status == 33:
|
|
return False, '正在上传数据中'
|
|
return False, '正在上传数据中'
|
|
|
|
+ LogHelper.log_success(OperationType.PROCESS, OperationModule.SUB_PROJECT, f"处理工程数据", utils.to_str(data.to_dict()))
|
|
thread = threading.Thread(target=self._process_sub_project, args=(data,))
|
|
thread = threading.Thread(target=self._process_sub_project, args=(data,))
|
|
thread.start()
|
|
thread.start()
|
|
return True, ''
|
|
return True, ''
|
|
@@ -174,6 +200,7 @@ class ProjectService:
|
|
return False, '正在分析处理中'
|
|
return False, '正在分析处理中'
|
|
if data.status == 32 or data.status == 33:
|
|
if data.status == 32 or data.status == 33:
|
|
return False, '正在上传数据中'
|
|
return False, '正在上传数据中'
|
|
|
|
+ LogHelper.log_success(OperationType.SEND, OperationModule.SUB_PROJECT, f"发送工程数据", utils.to_str(data.to_dict()))
|
|
thread = threading.Thread(target=self._send_sub_project_data, args=(data,))
|
|
thread = threading.Thread(target=self._send_sub_project_data, args=(data,))
|
|
thread.start()
|
|
thread.start()
|
|
|
|
|
|
@@ -188,8 +215,10 @@ class ProjectService:
|
|
project = self._project_store.query_sub_project(sub_project_id)
|
|
project = self._project_store.query_sub_project(sub_project_id)
|
|
if project:
|
|
if project:
|
|
self._project_store.delete_sub_project(sub_project_id)
|
|
self._project_store.delete_sub_project(sub_project_id)
|
|
|
|
+ LogHelper.log_success(OperationType.DELETE, OperationModule.SUB_PROJECT, f"删除工程成功", utils.to_str(project.to_dict()))
|
|
return True,''
|
|
return True,''
|
|
else:
|
|
else:
|
|
|
|
+ LogHelper.log_fail(OperationType.DELETE, OperationModule.SUB_PROJECT, f"删除工程", utils.to_str(project.to_dict()))
|
|
return False, '项目不存在'
|
|
return False, '项目不存在'
|
|
|
|
|
|
def get_sub_project_item_list_by_sub_project_paginated(self, project_id: int, page: int, per_page: int, keyword: str = None, process_status:int = None, send_status:int=None) -> (list[SubProjectItemModel], int):
|
|
def get_sub_project_item_list_by_sub_project_paginated(self, project_id: int, page: int, per_page: int, keyword: str = None, process_status:int = None, send_status:int=None) -> (list[SubProjectItemModel], int):
|
|
@@ -202,41 +231,62 @@ class ProjectService:
|
|
|
|
|
|
def add_sub_project_item(self, item:SubProjectItemModel):
|
|
def add_sub_project_item(self, item:SubProjectItemModel):
|
|
project = self._project_store.query_sub_project(item.sub_project_id)
|
|
project = self._project_store.query_sub_project(item.sub_project_id)
|
|
- project_item = SubProjectItemModel(
|
|
|
|
- project_id=project.project_id,
|
|
|
|
- sub_project_id=item.sub_project_id,
|
|
|
|
- device_name=item.device_name,
|
|
|
|
- device_model=item.device_model,
|
|
|
|
- device_unit=item.device_unit,
|
|
|
|
- standard_version=project.standard_version,
|
|
|
|
- standard_no=item.standard_no,
|
|
|
|
- )
|
|
|
|
- return self._project_store.insert_sub_project_item(project_item)
|
|
|
|
|
|
+ try:
|
|
|
|
+ project_item = SubProjectItemModel(
|
|
|
|
+ project_id=project.project_id,
|
|
|
|
+ sub_project_id=item.sub_project_id,
|
|
|
|
+ device_name=item.device_name,
|
|
|
|
+ device_model=item.device_model,
|
|
|
|
+ device_unit=item.device_unit,
|
|
|
|
+ standard_version=project.standard_version,
|
|
|
|
+ standard_no=item.standard_no,
|
|
|
|
+ )
|
|
|
|
+ new_id= self._project_store.insert_sub_project_item(project_item)
|
|
|
|
+ if new_id:
|
|
|
|
+ LogHelper.log_success(OperationType.CREATE, OperationModule.SUB_PROJECT_DETAIL, f"新增工程明细成功", utils.to_str(project_item.to_dict()))
|
|
|
|
+ return new_id
|
|
|
|
+ except Exception as e:
|
|
|
|
+ LogHelper.log_fail(OperationType.CREATE, OperationModule.SUB_PROJECT_DETAIL, f"新增工程明细失败 错误:{str(e)}", utils.to_str(item.to_dict()))
|
|
|
|
+ return None
|
|
|
|
|
|
def update_sub_project_item(self, item:SubProjectItemModel):
|
|
def update_sub_project_item(self, item:SubProjectItemModel):
|
|
project_item = self._project_store.query_sub_project_item_by_id(item.id)
|
|
project_item = self._project_store.query_sub_project_item_by_id(item.id)
|
|
if project_item:
|
|
if project_item:
|
|
- project_item.device_name = item.device_name
|
|
|
|
- project_item.device_model = item.device_model
|
|
|
|
- project_item.device_unit = item.device_unit
|
|
|
|
- project_item.device_count = float(item.device_count)
|
|
|
|
- project_item.standard_no = item.standard_no
|
|
|
|
- self._project_store.update_sub_project_item(project_item)
|
|
|
|
- return True
|
|
|
|
|
|
+ try:
|
|
|
|
+ log_data = utils.to_str(project_item.to_dict())
|
|
|
|
+ project_item.device_name = item.device_name
|
|
|
|
+ project_item.device_model = item.device_model
|
|
|
|
+ project_item.device_unit = item.device_unit
|
|
|
|
+ project_item.device_count = float(item.device_count)
|
|
|
|
+ project_item.standard_no = item.standard_no
|
|
|
|
+ self._project_store.update_sub_project_item(project_item)
|
|
|
|
+ LogHelper.log_success(OperationType.UPDATE, OperationModule.SUB_PROJECT_DETAIL, f"修改工程明细成功", log_data, utils.to_str(project_item.to_dict()))
|
|
|
|
+ return True
|
|
|
|
+ except Exception as e:
|
|
|
|
+ LogHelper.log_fail(OperationType.UPDATE, OperationModule.SUB_PROJECT_DETAIL, f"修改工程明细失败 错误:{str(e)}", utils.to_str(item.to_dict()))
|
|
|
|
+ return False
|
|
else:
|
|
else:
|
|
|
|
+ LogHelper.log_fail(OperationType.UPDATE, OperationModule.SUB_PROJECT_DETAIL, f"修改工程明细失败 错误:明细不存在", utils.to_str(item.to_dict()))
|
|
return False
|
|
return False
|
|
def delete_sub_project_item(self, item_id:int):
|
|
def delete_sub_project_item(self, item_id:int):
|
|
project_item = self._project_store.query_sub_project_item_by_id(item_id)
|
|
project_item = self._project_store.query_sub_project_item_by_id(item_id)
|
|
if project_item:
|
|
if project_item:
|
|
- self._project_store.delete_sub_project_item_by_id(project_item.id)
|
|
|
|
- return True
|
|
|
|
|
|
+ try:
|
|
|
|
+ self._project_store.delete_sub_project_item_by_id(project_item.id)
|
|
|
|
+ LogHelper.log_success(OperationType.DELETE, OperationModule.SUB_PROJECT_DETAIL, f"删除工程明细成功", utils.to_str(project_item.to_dict()))
|
|
|
|
+ return True
|
|
|
|
+ except Exception as e:
|
|
|
|
+ LogHelper.log_fail(OperationType.DELETE, OperationModule.SUB_PROJECT_DETAIL, f"删除工程明细失败 错误:{str(e)}", str(item_id))
|
|
else:
|
|
else:
|
|
|
|
+ LogHelper.log_fail(OperationType.DELETE, OperationModule.SUB_PROJECT_DETAIL, f"删除工程明细失败 错误:明细不存在", str(item_id))
|
|
return False
|
|
return False
|
|
|
|
|
|
def start_process_item(self, item_id:int):
|
|
def start_process_item(self, item_id:int):
|
|
project_item = self._project_store.query_sub_project_item_by_id(item_id)
|
|
project_item = self._project_store.query_sub_project_item_by_id(item_id)
|
|
if not project_item:
|
|
if not project_item:
|
|
|
|
+ LogHelper.log_fail(OperationType.PROCESS, OperationModule.SUB_PROJECT_DETAIL, f"处理工程明细失败 错误:明细不存在", str(item_id))
|
|
return False, '项目不存在'
|
|
return False, '项目不存在'
|
|
|
|
+ LogHelper.log_success(OperationType.PROCESS, OperationModule.SUB_PROJECT_DETAIL, f"开始处理工程明细", utils.to_str(project_item.to_dict()))
|
|
thread = threading.Thread(target=self._process_sub_project_item, args=(project_item,))
|
|
thread = threading.Thread(target=self._process_sub_project_item, args=(project_item,))
|
|
thread.start()
|
|
thread.start()
|
|
return True,""
|
|
return True,""
|
|
@@ -251,7 +301,9 @@ class ProjectService:
|
|
def start_send_item(self, item_id:int):
|
|
def start_send_item(self, item_id:int):
|
|
project_item = self._project_store.query_sub_project_item_by_id(item_id)
|
|
project_item = self._project_store.query_sub_project_item_by_id(item_id)
|
|
if not project_item:
|
|
if not project_item:
|
|
|
|
+ LogHelper.log_fail(OperationType.SEND, OperationModule.SUB_PROJECT_DETAIL, f"发送工程明细失败 错误:明细不存在", str(item_id))
|
|
return False, '项目不存在'
|
|
return False, '项目不存在'
|
|
|
|
+ LogHelper.log_success(OperationType.SEND, OperationModule.SUB_PROJECT_DETAIL, f"开始发送工程明细", utils.to_str(project_item.to_dict()))
|
|
thread = threading.Thread(target=self._send_sub_project_item, args=(project_item,))
|
|
thread = threading.Thread(target=self._send_sub_project_item, args=(project_item,))
|
|
thread.start()
|
|
thread.start()
|
|
return True,""
|
|
return True,""
|