102 lines
4.3 KiB
Python
102 lines
4.3 KiB
Python
from typing import Optional
|
|
import logging
|
|
|
|
from apps.api import govs
|
|
from apps.app_handler import AppHandler
|
|
from paste.web.decorators import route
|
|
from paste.core import aio_pool
|
|
from paste.core.logging import echo_log
|
|
from dock.govs import govs_phase_wise_completion
|
|
from models.govs_order_master import GovsOrderMaster
|
|
from models.govs_phase_wise_completion import GovsPhaseWiseCompletion
|
|
|
|
|
|
@route(f'{govs.ApiPrefix}/phase-wise-completion/create')
|
|
class CreateDelayHandler(AppHandler):
|
|
"""
|
|
阶段性办结接口。
|
|
|
|
对接省12345的阶段性办结接口,请求后本接口先将数据保存本地,然后响应客户端,然后开始后台启动推送。
|
|
"""
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.govs_order: Optional[GovsOrderMaster] = None
|
|
self.phase_wise_completion: Optional[GovsPhaseWiseCompletion] = None
|
|
|
|
def _params_for_db(self, **kwargs: dict) -> dict:
|
|
"""
|
|
提取数据库所需参数。
|
|
"""
|
|
return {
|
|
GovsPhaseWiseCompletion.master_id.key: kwargs.get('gdId', ''),
|
|
GovsPhaseWiseCompletion.flow_token.key: kwargs.get('flowToken', ''),
|
|
GovsPhaseWiseCompletion.gd_id.key: kwargs.get('gdId', ''),
|
|
GovsPhaseWiseCompletion.is_contact.key: kwargs.get('isContact', ''),
|
|
GovsPhaseWiseCompletion.contact_name.key: kwargs.get('contactName', ''),
|
|
GovsPhaseWiseCompletion.contact_time.key: kwargs.get('contactTime', ''),
|
|
GovsPhaseWiseCompletion.contact_type.key: kwargs.get('contactType', ''),
|
|
GovsPhaseWiseCompletion.next_feedback_time.key: kwargs.get('nextFeedbackTime', ''),
|
|
GovsPhaseWiseCompletion.advice.key: kwargs.get('advice', ''),
|
|
GovsPhaseWiseCompletion.reason.key: kwargs.get('reason', ''),
|
|
GovsPhaseWiseCompletion.remark.key: kwargs.get('remark', ''),
|
|
GovsPhaseWiseCompletion.action_name.key: kwargs.get('actionName', ''),
|
|
GovsPhaseWiseCompletion.case_accord_type_one_name.key: kwargs.get('caseAccordTypeOneName', ''),
|
|
GovsPhaseWiseCompletion.case_accord_type_two_name.key: kwargs.get('caseAccordTypeTwoName', ''),
|
|
GovsPhaseWiseCompletion.case_accord_type_three_name.key: kwargs.get('caseAccordTypeThreeName', ''),
|
|
GovsPhaseWiseCompletion.order_id.key: kwargs.get('orderId', ''),
|
|
GovsPhaseWiseCompletion.task_id.key: kwargs.get('taskId', '')
|
|
}
|
|
|
|
async def create_delay(self, **kwargs) -> dict:
|
|
# 必填参数校验
|
|
required_keys = [
|
|
'gdId', 'flowToken', 'isContact', 'contactName', 'contactTime', 'contactType', 'nextFeedbackTime', 'advice',
|
|
'reason'
|
|
]
|
|
missing = [
|
|
k for k in required_keys
|
|
if k not in kwargs or kwargs[k] is None
|
|
]
|
|
if missing:
|
|
raise ValueError(f"缺少必要参数: {missing}")
|
|
|
|
# 读取待办任务对象
|
|
govs_task_id = kwargs.get('gdId', '')
|
|
self.govs_order = await GovsOrderMaster.async_find_by_id(govs_task_id)
|
|
|
|
# 保存请求数据
|
|
params = self._params_for_db(**kwargs)
|
|
self.phase_wise_completion = GovsPhaseWiseCompletion().copy_from_dict(params)
|
|
self.phase_wise_completion.status = 0
|
|
await self.phase_wise_completion.async_save()
|
|
|
|
# 后台执行提交阶段性办结请求到省12345
|
|
await aio_pool.run_background_task(
|
|
govs_phase_wise_completion.create_phase_wise_completion(self.phase_wise_completion, self.govs_order)
|
|
)
|
|
|
|
return {
|
|
'msg': '阶段性办结成功.'
|
|
}
|
|
|
|
# @auth_token
|
|
async def post(self):
|
|
"""
|
|
处理 POST 请求。
|
|
|
|
---
|
|
tags:
|
|
- D3I API
|
|
summary: 阶段性办结接口
|
|
"""
|
|
try:
|
|
echo_log(self.request.body.decode())
|
|
_, params = self.get_request_params()
|
|
_result = await self.create_delay(**params)
|
|
self.response_ok(code=0, data=_result)
|
|
except Exception as e:
|
|
self.response_error(e, status_code=200, api_status_code=500)
|
|
self.log(msg=e, level=logging.ERROR, is_log_exc=True)
|