Reuse list filters with ids/columns support and ListExportDropdown UX. Co-authored-by: Cursor <cursoragent@cursor.com>
203 lines
6.5 KiB
Python
203 lines
6.5 KiB
Python
"""站点 CRUD(sys_site)"""
|
||
from __future__ import annotations
|
||
|
||
import math
|
||
from datetime import datetime
|
||
from typing import Any, Dict, List, Optional, Tuple
|
||
|
||
from sqlalchemy import and_, func, or_
|
||
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.models.models import SysSite
|
||
|
||
|
||
def _row_to_dict(s: SysSite) -> Dict[str, Any]:
|
||
return {
|
||
"id": s.id,
|
||
"siteName": s.site_name,
|
||
"siteCode": s.site_code,
|
||
"siteType": s.site_type or "",
|
||
"status": s.status,
|
||
"address": s.address or "",
|
||
"contactName": s.contact_name or "",
|
||
"contactPhone": s.contact_phone or "",
|
||
"businessScope": s.business_scope or "",
|
||
"tenantNo": s.tenant_no,
|
||
"orderNum": s.order_num if s.order_num is not None else 0,
|
||
"remark": s.remark or "",
|
||
"createBy": s.create_by or "",
|
||
"createTime": s.create_time.strftime("%Y-%m-%d %H:%M:%S") if s.create_time else "",
|
||
"updateTime": s.update_time.strftime("%Y-%m-%d %H:%M:%S") if s.update_time else "",
|
||
}
|
||
|
||
|
||
def list_sites(
|
||
db: Session,
|
||
*,
|
||
page: int = 1,
|
||
page_size: int = 10,
|
||
keyword: Optional[str] = None,
|
||
tenant_no: Optional[str] = None,
|
||
ids: Optional[List[str]] = None,
|
||
max_page_size: Optional[int] = None,
|
||
user_info=None,
|
||
) -> Tuple[List[Dict[str, Any]], int]:
|
||
def _filtered():
|
||
q0 = db.query(SysSite).filter(SysSite.del_flag == 0)
|
||
if tenant_no:
|
||
q0 = q0.filter(SysSite.tenant_no == tenant_no)
|
||
kw = (keyword or "").strip()
|
||
if kw:
|
||
like = f"%{kw}%"
|
||
q0 = q0.filter(or_(SysSite.site_name.like(like), SysSite.site_code.like(like)))
|
||
if ids:
|
||
q0 = q0.filter(SysSite.id.in_(ids))
|
||
# 站点为租户主数据,不做 create_by 数据范围过滤
|
||
return q0
|
||
|
||
total = _filtered().order_by(None).with_entities(func.count(SysSite.id)).scalar() or 0
|
||
page = max(1, page)
|
||
cap = int(max_page_size) if max_page_size else 500
|
||
page_size = min(max(1, page_size), cap)
|
||
rows = (
|
||
_filtered()
|
||
.order_by(SysSite.create_time.desc())
|
||
.offset((page - 1) * page_size)
|
||
.limit(page_size)
|
||
.all()
|
||
)
|
||
return [_row_to_dict(x) for x in rows], int(total)
|
||
|
||
|
||
def get_site(db: Session, site_id: str) -> Optional[SysSite]:
|
||
return (
|
||
db.query(SysSite)
|
||
.filter(and_(SysSite.id == site_id, SysSite.del_flag == 0))
|
||
.first()
|
||
)
|
||
|
||
|
||
def site_code_exists(db: Session, code: str, tenant_no: str, exclude_id: Optional[str] = None) -> bool:
|
||
q = db.query(SysSite).filter(and_(SysSite.site_code == code.strip(), SysSite.tenant_no == tenant_no, SysSite.del_flag == 0))
|
||
if exclude_id:
|
||
q = q.filter(SysSite.id != exclude_id)
|
||
return q.first() is not None
|
||
|
||
|
||
def create_site(
|
||
db: Session,
|
||
*,
|
||
site_name: str,
|
||
site_code: str,
|
||
site_type: Optional[str] = None,
|
||
status: str = "1",
|
||
address: Optional[str] = None,
|
||
contact_name: Optional[str] = None,
|
||
contact_phone: Optional[str] = None,
|
||
business_scope: Optional[str] = None,
|
||
tenant_no: str = "shanxia2.0",
|
||
order_num: Optional[int] = None,
|
||
remark: Optional[str] = None,
|
||
user_id: Optional[str] = None,
|
||
) -> SysSite:
|
||
sc = (site_code or "").strip()
|
||
if not sc:
|
||
raise ValueError("站点编码不能为空")
|
||
if site_code_exists(db, sc, tenant_no):
|
||
raise ValueError("站点编码已存在")
|
||
obj = SysSite(
|
||
id=generate_uuid(),
|
||
site_name=(site_name or "").strip() or sc,
|
||
site_code=sc,
|
||
site_type=(site_type or "").strip() or None,
|
||
status=status or "1",
|
||
address=(address or "").strip() or None,
|
||
contact_name=(contact_name or "").strip() or None,
|
||
contact_phone=(contact_phone or "").strip() or None,
|
||
business_scope=(business_scope or "").strip() or None,
|
||
tenant_no=tenant_no,
|
||
order_num=order_num if order_num is not None else 0,
|
||
remark=(remark or "").strip() or None,
|
||
create_by=user_id,
|
||
create_dept_id="",
|
||
create_time=datetime.now(),
|
||
update_by=user_id,
|
||
update_time=datetime.now(),
|
||
del_flag=0,
|
||
)
|
||
db.add(obj)
|
||
db.commit()
|
||
db.refresh(obj)
|
||
return obj
|
||
|
||
|
||
def update_site(
|
||
db: Session,
|
||
site_id: str,
|
||
*,
|
||
site_name: Optional[str] = None,
|
||
site_code: Optional[str] = None,
|
||
site_type: Optional[str] = None,
|
||
status: Optional[str] = None,
|
||
address: Optional[str] = None,
|
||
contact_name: Optional[str] = None,
|
||
contact_phone: Optional[str] = None,
|
||
business_scope: Optional[str] = None,
|
||
order_num: Optional[int] = None,
|
||
remark: Optional[str] = None,
|
||
user_id: Optional[str] = None,
|
||
) -> SysSite:
|
||
obj = get_site(db, site_id)
|
||
if not obj:
|
||
raise ValueError("站点不存在")
|
||
if site_code is not None:
|
||
new_code = site_code.strip()
|
||
if not new_code:
|
||
raise ValueError("站点编码不能为空")
|
||
if new_code != obj.site_code and site_code_exists(db, new_code, obj.tenant_no, exclude_id=site_id):
|
||
raise ValueError("站点编码已存在")
|
||
obj.site_code = new_code
|
||
if site_name is not None:
|
||
obj.site_name = (site_name or "").strip() or obj.site_name
|
||
if site_type is not None:
|
||
obj.site_type = (site_type or "").strip() or None
|
||
if status is not None:
|
||
obj.status = status
|
||
if address is not None:
|
||
obj.address = (address or "").strip() or None
|
||
if contact_name is not None:
|
||
obj.contact_name = (contact_name or "").strip() or None
|
||
if contact_phone is not None:
|
||
obj.contact_phone = (contact_phone or "").strip() or None
|
||
if business_scope is not None:
|
||
obj.business_scope = (business_scope or "").strip() or None
|
||
if order_num is not None:
|
||
obj.order_num = order_num
|
||
if remark is not None:
|
||
obj.remark = (remark or "").strip() or None
|
||
obj.update_by = user_id
|
||
obj.update_time = datetime.now()
|
||
db.add(obj)
|
||
db.commit()
|
||
db.refresh(obj)
|
||
return obj
|
||
|
||
|
||
def soft_delete_site(db: Session, site_id: str, *, user_info: dict = None) -> None:
|
||
obj = get_site(db, site_id)
|
||
if not obj:
|
||
raise ValueError("站点不存在")
|
||
assert_row_deletable_by_creator(obj, user_info)
|
||
obj.del_flag = 1
|
||
obj.update_time = datetime.now()
|
||
db.add(obj)
|
||
db.commit()
|
||
|
||
|
||
def total_pages(total: int, page_size: int) -> int:
|
||
if total <= 0:
|
||
return 0
|
||
return int(math.ceil(total / float(page_size)))
|