Files
shanxia2.0/backend/app/crud/crm/supplier_info.py
T
wuzilongandCursor 6a8cd90b98 feat: cascade column facet options by current list context
Make dynamic column-facet values respect other active filters (Excel-style), with backend facet context helpers, list-page wiring, slip print/sidebar fixes, and refreshed graphify graphs.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-11 22:04:20 +08:00

353 lines
11 KiB
Python

"""CRM 供方信息 CRUD — 对照 CrmSupplierInfoServiceImpl.java"""
from __future__ import annotations
import math
from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple
from sqlalchemy.orm import Session
from app.core.delete_guard import assert_row_deletable_by_creator
from app.core.utils import generate_uuid
from app.crud.crm.data_scope_helpers import (
apply_crm_create_by_scope,
ensure_crm_create_by_row_in_scope,
)
from app.models.crm_models import CrmSupplierInfo
_FMT = "%Y-%m-%d %H:%M:%S"
_WRITABLE_FIELDS = (
"supplier_name",
"short_name",
"supplier_code",
"supplier_address",
"legal_person",
"agent",
"phone_fax",
"bank_name",
"bank_account",
"allow_toxic",
"allow_explosive",
"status",
"order_num",
"remark",
)
def total_pages(total: int, page_size: int) -> int:
if page_size <= 0:
return 0
return math.ceil(total / page_size)
def _row_to_dict(row: CrmSupplierInfo) -> Dict[str, Any]:
"""ORM → camelCase(单点真相)"""
return {
"id": row.id,
"supplierName": row.supplier_name or "",
"shortName": row.short_name or "",
"supplierCode": row.supplier_code or "",
"supplierAddress": row.supplier_address or "",
"legalPerson": row.legal_person or "",
"agent": row.agent or "",
"phoneFax": row.phone_fax or "",
"bankName": row.bank_name or "",
"bankAccount": row.bank_account or "",
"allowToxic": int(row.allow_toxic or 0),
"allowExplosive": int(row.allow_explosive or 0),
"status": row.status or "0",
"orderNum": row.order_num if row.order_num is not None else 0,
"remark": row.remark or "",
"createBy": row.create_by or "",
"createTime": row.create_time.strftime(_FMT) if row.create_time else "",
"updateBy": row.update_by or "",
"updateTime": row.update_time.strftime(_FMT) if row.update_time else "",
}
def _split_csv(raw) -> list:
if not raw or not str(raw).strip():
return []
return [s.strip() for s in str(raw).split(",") if s.strip()]
def _apply_list_filters(
q,
*,
supplier_name: str = None,
supplier_code: str = None,
supplier_name_in: str = None,
supplier_code_in: str = None,
status: str = None,
allow_toxic=None,
allow_explosive=None,
):
"""Java: buildQueryWrapper + 列头漏斗扩展"""
supplier_code_values = _split_csv(supplier_code_in)
if supplier_code_values:
q = q.filter(CrmSupplierInfo.supplier_code.in_(supplier_code_values))
elif supplier_code and str(supplier_code).strip():
q = q.filter(CrmSupplierInfo.supplier_code.like(f"%{str(supplier_code).strip()}%"))
supplier_name_values = _split_csv(supplier_name_in)
if supplier_name_values:
q = q.filter(CrmSupplierInfo.supplier_name.in_(supplier_name_values))
elif supplier_name and supplier_name.strip():
q = q.filter(CrmSupplierInfo.supplier_name.like(f"%{supplier_name.strip()}%"))
if status is not None and str(status).strip() != "":
q = q.filter(CrmSupplierInfo.status == str(status).strip())
if allow_toxic is not None and str(allow_toxic).strip() != "":
values = [int(x) for x in str(allow_toxic).split(",") if str(x).strip() != ""]
if values:
q = q.filter(CrmSupplierInfo.allow_toxic.in_(values))
if allow_explosive is not None and str(allow_explosive).strip() != "":
values = [int(x) for x in str(allow_explosive).split(",") if str(x).strip() != ""]
if values:
q = q.filter(CrmSupplierInfo.allow_explosive.in_(values))
return q
def list_supplier_info(
db: Session,
tenant_no: str,
*,
page: int = 1,
page_size: int = 10,
supplier_name: str = None,
supplier_code: str = None,
supplier_name_in: str = None,
supplier_code_in: str = None,
status: str = None,
allow_toxic=None,
allow_explosive=None,
user_info=None,
) -> Tuple[List[Dict[str, Any]], int]:
"""Java: queryPageList"""
q = db.query(CrmSupplierInfo).filter(
CrmSupplierInfo.tenant_no == tenant_no,
CrmSupplierInfo.del_flag == 0,
)
if user_info:
q = apply_crm_create_by_scope(q, CrmSupplierInfo, user_info, db, tenant_no)
q = _apply_list_filters(
q,
supplier_name=supplier_name,
supplier_code=supplier_code,
supplier_name_in=supplier_name_in,
supplier_code_in=supplier_code_in,
status=status,
allow_toxic=allow_toxic,
allow_explosive=allow_explosive,
)
total = q.count()
rows = (
q.order_by(CrmSupplierInfo.create_time.desc())
.offset((page - 1) * page_size)
.limit(page_size)
.all()
)
return [_row_to_dict(r) for r in rows], total
def list_all_supplier_info(
db: Session,
tenant_no: str,
*,
supplier_name: str = None,
supplier_code: str = None,
supplier_name_in: str = None,
supplier_code_in: str = None,
status: str = None,
allow_toxic=None,
allow_explosive=None,
user_info=None,
) -> List[Dict[str, Any]]:
"""Java: queryList(不分页,报价单下拉)"""
q = db.query(CrmSupplierInfo).filter(
CrmSupplierInfo.tenant_no == tenant_no,
CrmSupplierInfo.del_flag == 0,
)
if user_info:
q = apply_crm_create_by_scope(q, CrmSupplierInfo, user_info, db, tenant_no)
q = _apply_list_filters(
q,
supplier_name=supplier_name,
supplier_code=supplier_code,
supplier_name_in=supplier_name_in,
supplier_code_in=supplier_code_in,
status=status,
allow_toxic=allow_toxic,
allow_explosive=allow_explosive,
)
rows = q.order_by(CrmSupplierInfo.create_time.desc()).all()
return [_row_to_dict(r) for r in rows]
def get_supplier_info(
db: Session,
supplier_id: str,
*,
tenant_no: str = None,
) -> Optional[CrmSupplierInfo]:
"""Java: queryById"""
q = db.query(CrmSupplierInfo).filter(
CrmSupplierInfo.id == supplier_id,
CrmSupplierInfo.del_flag == 0,
)
if tenant_no:
q = q.filter(CrmSupplierInfo.tenant_no == tenant_no)
return q.first()
def create_supplier_info(
db: Session,
*,
tenant_no: str,
user_id: str = None,
dept_id: str = "",
**kwargs,
) -> CrmSupplierInfo:
"""Java: insertByBo"""
supplier_name = (kwargs.get("supplier_name") or "").strip()
if not supplier_name:
raise ValueError("供方名称不能为空")
obj = CrmSupplierInfo(
id=generate_uuid(),
supplier_name=supplier_name,
short_name=kwargs.get("short_name") or "",
supplier_code=kwargs.get("supplier_code") or "",
supplier_address=kwargs.get("supplier_address") or "",
legal_person=kwargs.get("legal_person") or "",
agent=kwargs.get("agent") or "",
phone_fax=kwargs.get("phone_fax") or "",
bank_name=kwargs.get("bank_name") or "",
bank_account=kwargs.get("bank_account") or "",
allow_toxic=kwargs.get("allow_toxic", 0) or 0,
allow_explosive=kwargs.get("allow_explosive", 0) or 0,
status=kwargs.get("status") or "0",
tenant_no=tenant_no,
order_num=kwargs.get("order_num"),
remark=kwargs.get("remark"),
create_by=user_id,
create_dept_id=dept_id or "",
del_flag=0,
)
db.add(obj)
db.commit()
db.refresh(obj)
return obj
def update_supplier_info(
db: Session,
supplier_id: str,
*,
tenant_no: str = None,
user_id: str = None,
user_info=None,
**kwargs,
) -> CrmSupplierInfo:
"""Java: updateByBo"""
obj = get_supplier_info(db, supplier_id, tenant_no=tenant_no)
if not obj:
raise ValueError("供方信息不存在")
ensure_crm_create_by_row_in_scope(
db, obj, user_info, obj.tenant_no, message="无权访问该供方信息"
)
if "supplier_name" in kwargs and kwargs["supplier_name"] is not None:
name = (kwargs["supplier_name"] or "").strip()
if not name:
raise ValueError("供方名称不能为空")
obj.supplier_name = name
for field in _WRITABLE_FIELDS:
if field == "supplier_name":
continue
if field in kwargs and kwargs[field] is not None:
setattr(obj, field, kwargs[field])
obj.update_by = user_id
obj.update_time = datetime.now()
db.commit()
db.refresh(obj)
return obj
def soft_delete_supplier_info(
db: Session,
supplier_id: str,
*,
tenant_no: str = None,
user_id: str = None,
user_info=None,
) -> bool:
"""Java: deleteWithValidByIds(门户软删)"""
obj = get_supplier_info(db, supplier_id, tenant_no=tenant_no)
if not obj:
raise ValueError("供方信息不存在")
ensure_crm_create_by_row_in_scope(
db, obj, user_info, obj.tenant_no, message="无权访问该供方信息"
)
assert_row_deletable_by_creator(obj, user_info, user_id=user_id)
obj.del_flag = 1
obj.update_by = user_id
obj.update_time = datetime.now()
db.commit()
return True
_SUPPLIER_COLUMN_FACET_FIELDS = {
"supplierCode": CrmSupplierInfo.supplier_code,
"supplierName": CrmSupplierInfo.supplier_name,
}
def list_supplier_column_facet_values(
db: Session,
tenant_no: str,
*,
field: str,
keyword: str = None,
limit: int = 500,
user_info=None,
status: str = None,
filter_kwargs: Optional[dict] = None,
) -> List[Dict[str, Any]]:
"""主体危化品列表列头可选值:supplierCode / supplierName(可带列表上下文)。"""
from app.services.column_facet import list_column_facet_values
col = _SUPPLIER_COLUMN_FACET_FIELDS.get(str(field or "").strip())
if col is None:
raise ValueError("field 仅支持 supplierCode 或 supplierName")
fk = filter_kwargs or {}
st = (status or fk.get("status") or "").strip() or None
def _apply(q):
q = _apply_list_filters(
q,
supplier_name=fk.get("supplier_name"),
supplier_code=fk.get("supplier_code"),
supplier_name_in=fk.get("supplier_name_in"),
supplier_code_in=fk.get("supplier_code_in"),
status=st,
allow_toxic=fk.get("allow_toxic_in") or fk.get("allow_toxic"),
allow_explosive=fk.get("allow_explosive_in") or fk.get("allow_explosive"),
)
if user_info:
return apply_crm_create_by_scope(q, CrmSupplierInfo, user_info, db, tenant_no)
return q
return list_column_facet_values(
db,
CrmSupplierInfo,
col,
tenant_no=tenant_no,
keyword=keyword,
limit=limit,
apply_scope=_apply,
)