fix(expenses): unify AI application submission transaction
This commit is contained in:
@@ -15,8 +15,8 @@ from app.schemas.budget import (
|
||||
BudgetAllocationRead,
|
||||
BudgetCheckRead,
|
||||
BudgetCheckRequest,
|
||||
BudgetOperationRequest,
|
||||
BudgetOperationRead,
|
||||
BudgetOperationRequest,
|
||||
BudgetSummaryRead,
|
||||
BudgetTransactionRead,
|
||||
)
|
||||
@@ -31,7 +31,8 @@ class BudgetService(BudgetPaginationMixin, BudgetSupportMixin):
|
||||
self.db = db
|
||||
|
||||
def ensure_budget_ready(self) -> None:
|
||||
Base.metadata.create_all(bind=self.db.get_bind())
|
||||
# 复用当前 Session 连接,避免在业务事务中通过 Engine 隐式提交已 flush 的单据。
|
||||
Base.metadata.create_all(bind=self.db.connection())
|
||||
exists = self.db.scalar(select(BudgetAllocation.id).limit(1))
|
||||
if exists:
|
||||
return
|
||||
|
||||
@@ -781,26 +781,30 @@ class UserAgentApplicationPersistenceMixin:
|
||||
currency="CNY",
|
||||
invoice_count=0,
|
||||
occurred_at=self._parse_application_occurred_at(facts.get("time", "")),
|
||||
submitted_at=datetime.now(UTC) if submit else None,
|
||||
status="submitted" if submit else "draft",
|
||||
approval_stage="直属领导审批" if submit else "待提交",
|
||||
submitted_at=None,
|
||||
status="draft",
|
||||
approval_stage="待提交",
|
||||
risk_flags_json=[self._build_application_detail_flag(facts)],
|
||||
)
|
||||
self.db.add(claim)
|
||||
self.db.flush()
|
||||
if submit:
|
||||
from app.services.expense_claims import ExpenseClaimService
|
||||
if not submit:
|
||||
self.db.commit()
|
||||
self.db.refresh(claim)
|
||||
return claim
|
||||
|
||||
platform_review = ExpenseClaimService(self.db).evaluate_platform_risk_rules(
|
||||
claim,
|
||||
business_stage="expense_application",
|
||||
)
|
||||
platform_flags = list(platform_review.get("flags") or [])
|
||||
if platform_flags:
|
||||
claim.risk_flags_json = [*list(claim.risk_flags_json or []), *platform_flags]
|
||||
self.db.commit()
|
||||
self.db.refresh(claim)
|
||||
return claim
|
||||
from app.services.expense_claims import ExpenseClaimService
|
||||
|
||||
try:
|
||||
submitted = ExpenseClaimService(self.db).submit_claim(claim.id, current_user)
|
||||
except Exception:
|
||||
# 外层编排会记录失败工具调用并提交事务,必须先回滚本次已 flush 的草稿。
|
||||
self.db.rollback()
|
||||
raise
|
||||
if submitted is None:
|
||||
self.db.rollback()
|
||||
raise ValueError("未找到可提交的申请单。")
|
||||
return submitted
|
||||
|
||||
def _find_duplicate_expense_application_record(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user