Mirror the quote approver setting so contract submit dialogs can prefill ordered approvers from a tenant config page while remaining editable. Co-authored-by: Cursor <cursoragent@cursor.com>
96 lines
2.8 KiB
Python
96 lines
2.8 KiB
Python
from __future__ import annotations
|
|
|
|
from datetime import datetime
|
|
from typing import Any, Dict, List, Optional
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.core.utils import generate_uuid
|
|
from app.crud.crm.quote_approver_setting import join_approver_ids, parse_approver_ids
|
|
from app.models.crm_models import CrmContractApproverSetting
|
|
from app.models.models import SysUser
|
|
|
|
|
|
def _row_to_dict(row: CrmContractApproverSetting) -> Dict[str, Any]:
|
|
return {
|
|
"id": row.id,
|
|
"approverIds": parse_approver_ids(row.approver_ids),
|
|
"status": row.status or "0",
|
|
"tenantNo": row.tenant_no,
|
|
"updateTime": row.update_time.isoformat(sep=" ") if row.update_time else None,
|
|
}
|
|
|
|
|
|
def get_setting(db: Session, *, tenant_no: str) -> Optional[Dict[str, Any]]:
|
|
row = (
|
|
db.query(CrmContractApproverSetting)
|
|
.filter(
|
|
CrmContractApproverSetting.tenant_no == tenant_no,
|
|
CrmContractApproverSetting.del_flag == 0,
|
|
)
|
|
.order_by(CrmContractApproverSetting.update_time.desc())
|
|
.first()
|
|
)
|
|
return _row_to_dict(row) if row else None
|
|
|
|
|
|
def _assert_users_exist(db: Session, *, tenant_no: str, approver_ids: List[str]) -> None:
|
|
ids = [str(i).strip() for i in approver_ids if str(i).strip()]
|
|
if not ids:
|
|
return
|
|
rows = (
|
|
db.query(SysUser.id)
|
|
.filter(SysUser.id.in_(ids), SysUser.del_flag == 0)
|
|
.all()
|
|
)
|
|
found = {str(r[0]) for r in rows}
|
|
missing = [i for i in ids if i not in found]
|
|
if missing:
|
|
raise ValueError(f"审批人无效或不存在:{','.join(missing)}")
|
|
|
|
|
|
def save_setting(
|
|
db: Session,
|
|
*,
|
|
tenant_no: str,
|
|
approver_ids: List[str],
|
|
status: str,
|
|
user_id: str = None,
|
|
dept_id: str = "",
|
|
) -> Dict[str, Any]:
|
|
status_norm = "1" if str(status) == "1" else "0"
|
|
ids = [str(i).strip() for i in (approver_ids or []) if str(i).strip()]
|
|
_assert_users_exist(db, tenant_no=tenant_no, approver_ids=ids)
|
|
|
|
row = (
|
|
db.query(CrmContractApproverSetting)
|
|
.filter(
|
|
CrmContractApproverSetting.tenant_no == tenant_no,
|
|
CrmContractApproverSetting.del_flag == 0,
|
|
)
|
|
.first()
|
|
)
|
|
now = datetime.now()
|
|
if row:
|
|
row.approver_ids = join_approver_ids(ids)
|
|
row.status = status_norm
|
|
row.update_by = user_id
|
|
row.update_time = now
|
|
else:
|
|
row = CrmContractApproverSetting(
|
|
id=generate_uuid(),
|
|
approver_ids=join_approver_ids(ids),
|
|
status=status_norm,
|
|
tenant_no=tenant_no,
|
|
create_by=user_id,
|
|
create_dept_id=dept_id or "",
|
|
create_time=now,
|
|
update_by=user_id,
|
|
update_time=now,
|
|
del_flag=0,
|
|
)
|
|
db.add(row)
|
|
db.commit()
|
|
db.refresh(row)
|
|
return _row_to_dict(row)
|