89 lines
2.9 KiB
Python
89 lines
2.9 KiB
Python
from typing import TypedDict
|
|
|
|
from fastapi import FastAPI
|
|
from langgraph.constants import START, END
|
|
from langgraph.graph import StateGraph
|
|
|
|
from app.model.ApproveModel import ApproveModel, ApproveService
|
|
from app.model.ReimburseModel import ReimburseService, ReimburseModel
|
|
from app.utils.db_utils import AsyncSessionDep
|
|
from app.utils.postgres_checkpointer import AsyncPostgresSaverDep
|
|
|
|
|
|
def add_approve_route(app: FastAPI):
|
|
# 提交报销单接口
|
|
@app.post('/submit_reimburse')
|
|
async def submit_reimburse(
|
|
reimburse: dict,
|
|
session: AsyncSessionDep,
|
|
checkpointer: AsyncPostgresSaverDep,
|
|
):
|
|
reimburse_id = reimburse.get('id')
|
|
# 先查一遍报销单信息
|
|
reimburse_cls: ReimburseModel = await ReimburseService.query_item(session=session, row_dict={"id": reimburse_id})
|
|
|
|
# 1. 没有审批单——
|
|
# 2. 审批单为审批中状态——不处理
|
|
# 3. 审批单为已通过状态——不处理
|
|
# 4. 审批单为已驳回状态——
|
|
# 5. 审批单为已撤回状态——
|
|
|
|
# 2,3 的情况下不再提交报销单
|
|
|
|
if reimburse_cls.approve:
|
|
if reimburse_cls.approve.status == 'approving':
|
|
return {"message": "报销单已提交审批,请勿重复提交!"}
|
|
if reimburse_cls.approve.status == 'approved':
|
|
return {"message": "报销单已通过审批,请勿重复提交!"}
|
|
|
|
# 1,4,5 的情况下新建审批单
|
|
# 创建审批单,使用新的审批单走审批流程
|
|
|
|
new_approve_dict = {
|
|
"title": f"{reimburse_cls.user.full_name} - 报销单审批",
|
|
"description": reimburse_cls.title + (f" / {reimburse_cls.remarks}" if reimburse_cls.remarks else ""),
|
|
"status": "approving",
|
|
"amount": reimburse_cls.amount,
|
|
"logs": "[]",
|
|
"user_id": reimburse_cls.user_id, # 先把审批人的id设置为申请人
|
|
"proj_id": reimburse_cls.proj_id,
|
|
}
|
|
insert_approve_cls = await ApproveService.item_insert(session=session, row_dict=new_approve_dict)
|
|
|
|
# 将审批单与报销单管理,设置报销单的approve_id为审批单的id
|
|
await ReimburseService.item_update(session=session, row_dict={"id": reimburse_id, "approve_id": insert_approve_cls.id}, )
|
|
|
|
graph = create_approve_graph(session, checkpointer)
|
|
|
|
await graph.ainvoke({
|
|
"input_approve_dict": insert_approve_cls.model_dump(),
|
|
"input_reimburse_user_id": reimburse_cls.user_id
|
|
})
|
|
|
|
return {"message": "报销单提交成功!"}
|
|
|
|
|
|
# 创建一个图来处理审批流
|
|
def create_approve_graph(
|
|
session: AsyncSessionDep,
|
|
checkpointer: AsyncPostgresSaverDep,
|
|
):
|
|
class StateSchema(TypedDict):
|
|
input_reimburse_user_id: str
|
|
input_approve_dict: dict
|
|
|
|
status: str
|
|
|
|
builder = StateGraph(StateSchema)
|
|
|
|
async def node_create_approve(state: StateSchema):
|
|
pass
|
|
|
|
builder.add_node(node_create_approve)
|
|
builder.add_edge(START, 'node_create_approve')
|
|
builder.add_edge('node_create_approve', END)
|
|
|
|
graph = builder.compile(checkpointer=checkpointer)
|
|
|
|
return graph
|