Files
d3i-szct/apps/api/dcm/apply_postpone.py
2026-06-02 17:46:38 +08:00

103 lines
3.8 KiB
Python

"""
接受OA请求,操作数字城管工单延期
"""
import logging
from typing import Optional
from apps.api import dcm
from apps.app_handler import AppHandler
from dock.dcm import dcm_push_apply_postpone
from models.dcm_apply_delay import DcmApplyPostpone
from models.dcm_task import DcmTask
from paste.core import aio_pool
from paste.core.logging import echo_log
from paste.web.decorators import route
@route(f'{dcm.ApiPrefix}/applyDelay')
class ApplyPostponeHandler(AppHandler):
"""
申请延期接口。
对接数字城管系统的申请延期接口,请求后本接口先将数据保存本地,然后响应客户端,然后开始后台启动推送。
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.dcm_task: Optional[DcmTask] = None
self.dcm_apply_postpone: Optional[DcmApplyPostpone] = None
def _params_for_db(self, **kwargs: dict) -> dict:
"""
提取数据库所需参数。
"""
return {
DcmApplyPostpone.flow_token.key: kwargs.get('flowToken', ''),
DcmApplyPostpone.dcm_task_id.key: kwargs.get('gdId', ''),
DcmApplyPostpone.task_number.key: kwargs.get('taskNumber', ''),
DcmApplyPostpone.apply_act_id.key: self.dcm_task.act_id,
DcmApplyPostpone.reply_part_id.key: kwargs.get('replyPartID', 39),
DcmApplyPostpone.ard_level.key: kwargs.get('ardLevel', 0),
DcmApplyPostpone.ard_type_id.key: kwargs.get('ardTypeId', 12),
DcmApplyPostpone.apply_memo.key: kwargs.get('opinion', ''),
DcmApplyPostpone.apply_type.key: kwargs.get('applyType', '延期'),
DcmApplyPostpone.attachments.key: kwargs.get('attachments', ''),
DcmApplyPostpone.delay_multiple.key: kwargs.get('delayMultiple', 2),
DcmApplyPostpone.time_num.key: kwargs.get('timeNum', 48),
DcmApplyPostpone.time_unit.key: kwargs.get('timeUnit', 4),
DcmApplyPostpone.postpone_date.key: kwargs.get('postponeDate', ''),
}
async def apply_postpone(self, **kwargs) -> dict:
# 必填参数校验
required_keys = [
'gdId', 'taskNumber', 'applyType', 'opinion', 'delayMultiple', 'flowToken'
]
missing = [
k for k in required_keys
if k not in kwargs or kwargs[k] is None
]
if missing:
raise ValueError(f"缺少必要参数: {missing}")
if kwargs.get('delayMultiple') not in (1, 2, '1', '2'):
raise ValueError('延期倍数只能为1或2')
# 读取待办任务对象
dcm_task_id = kwargs.get('gdId', '')
self.dcm_task = await DcmTask.async_find_by_id(dcm_task_id)
# 保存请求数据
params = self._params_for_db(**kwargs)
self.dcm_apply_postpone = DcmApplyPostpone().copy_from_dict(params)
self.dcm_apply_postpone.status = 0
await self.dcm_apply_postpone.async_save()
# 后台执行提交申请延期请求到数字城管
await aio_pool.run_background_task(
dcm_push_apply_postpone.push_apply_postpone(self.dcm_apply_postpone, self.dcm_task)
)
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.apply_postpone(**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)