12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- import tools.utils as utils, core.configs as configs
- from core.dtos import ProjectTaskDto, ProjectQuotaDto, QuotaInputDto
- from core.enum import SendStatusEnum
- from stores import ProjectQuotaStore, ProjectTaskStore, QuotaInputStore
- class TaskSender:
- def __init__(self):
- self._logger = utils.get_logger()
- self._task_store = ProjectTaskStore()
- self._quota_store = ProjectQuotaStore()
- self._quota_input_store = QuotaInputStore()
- def send_task(self, task: ProjectTaskDto):
- try:
- self._logger.info(f"开始推送任务:{task.task_name}")
- self._task_store.update_send_status(task.id, SendStatusEnum.SUCCESS.value)
- error_count = 0
- data_list = self._quota_store.get_quotas_by_task_id(task.id, True)
- for data in data_list:
- msg = self._quota_store.update_send_status(
- data.id, SendStatusEnum.SUCCESS.value
- )
- if msg:
- error_count += 1
- continue
- self._task_store.update_send_status(task.id, SendStatusEnum.SUCCESS.value)
- self._logger.info(
- f"推送任务:{task.task_name}完成,{error_count}项错误/共{len(data_list)}项"
- )
- return None
- except Exception as e:
- msg = f"任务推送失败,原因:{e}"
- self._logger.error(f"推送任务:{task.task_name},{msg}")
- self._task_store.update_send_status(
- task.id, SendStatusEnum.FAILURE.value, msg
- )
- return msg
- def send_quota(self, quota: ProjectQuotaDto):
- try:
- self._logger.debug(f"开始推送定额输入[{quota.id}]")
- self._quota_store.update_send_status(quota.id, SendStatusEnum.SUCCESS.value)
- quota_input = QuotaInputDto.from_quota_dto(quota)
- self._save_quota(quota_input, quota.project_id)
- # self._quota_store.update_send_status(quota.id, SendStatusEnum.SUCCESS.value)
- self._logger.debug(f"推送定额输入[{quota.id}]完成")
- return None
- except Exception as e:
- msg = f"定额输入[{quota.id}]推送失败,原因:{e}"
- self._logger.error(msg)
- self._quota_store.update_send_status(
- quota.id, SendStatusEnum.FAILURE.value, msg
- )
- return msg
- def _save_quota(self, quota: QuotaInputDto, project_id: str):
- try:
- # data = self._quota_input_store.get_quota(project_id, quota.budget_id,quota.item_id,quota.quota_code)
- quota.project_name = f"{quota.project_name}{configs.app.ai_flag}"
- if quota.quota_id and quota.quota_id > 0:
- self._logger.debug(
- f"修改定额输入[{quota.quota_id}]:{quota.project_name} {quota.quota_code} {quota.quota_id}"
- )
- self._quota_input_store.update_quota(project_id, quota)
- else:
- self._quota_input_store.create_quota(project_id, quota)
- self._logger.debug(
- f"新增定额输入[{quota.quota_id}]:{quota.project_name} {quota.quota_code} {quota.quota_id}"
- )
- except Exception as e:
- msg = f"保存定额[{quota.quota_id}]输入失败,原因:{e}"
- self._logger.error(msg)
- raise Exception(msg)
|