refactor(repo): 收敛 base_model 双轨,删除函数统一走 repo.batch_delete_r
- shared/base_repository.py 新增 batch_delete_r(返回 R,等价原 base_model 语义) - 20 个模块删除函数从 Model.batch_delete_r 改为 x_repo.batch_delete_r - shared/base_model.py 移除 classmethod(get_by_id/get_by_ids/get_all_active/exists/batch_delete_r),只留字段 + to_dict - 清理 base_model 多余 imports - 实测 dept 删除、批量删除及多模块读接口正常 Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+4
-80
@@ -6,20 +6,15 @@
|
||||
# +======================================================================
|
||||
# 由 apps/models/base_model.py 迁移而来。
|
||||
|
||||
"""数据库模型基类:通用字段、序列化与通用查询方法。"""
|
||||
"""数据库模型基类:通用字段与序列化方法(数据访问统一走 Repository)。"""
|
||||
|
||||
import logging
|
||||
from datetime import date, datetime, timezone
|
||||
from typing import Optional, List, Dict, Any
|
||||
from typing import Dict, Any
|
||||
|
||||
from sqlalchemy import Column, Integer, DATETIME, and_
|
||||
from sqlalchemy import Column, Integer, DATETIME
|
||||
|
||||
from core import R
|
||||
from core.database import Base, get_db_session
|
||||
from core.database import Base
|
||||
from shared.constants import SERIALIZE_EXCLUDE_FIELDS
|
||||
from utils.request import parse_id_list
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
_UTC_NOW = lambda: datetime.now(tz=timezone.utc)
|
||||
|
||||
@@ -76,74 +71,3 @@ class base_model(Base):
|
||||
value = value.strftime('%Y-%m-%d')
|
||||
result[column.name] = value
|
||||
return result
|
||||
|
||||
# ============================================================
|
||||
# 通用查询方法
|
||||
# ============================================================
|
||||
|
||||
@classmethod
|
||||
def get_by_id(cls, id: Optional[int]) -> Optional['base_model']:
|
||||
"""根据ID获取未删除的记录,不存在返回 None"""
|
||||
if id is None or id <= 0:
|
||||
return None
|
||||
db = get_db_session()
|
||||
return db.query(cls).filter(cls.id == id, cls.is_delete == 0).first()
|
||||
|
||||
@classmethod
|
||||
def get_by_ids(cls, ids: List[int]) -> List['base_model']:
|
||||
"""根据ID列表批量获取未删除的记录"""
|
||||
if not ids:
|
||||
return []
|
||||
valid_ids = [i for i in ids if i and i > 0]
|
||||
if not valid_ids:
|
||||
return []
|
||||
db = get_db_session()
|
||||
return db.query(cls).filter(cls.id.in_(valid_ids), cls.is_delete == 0).all()
|
||||
|
||||
@classmethod
|
||||
def get_all_active(cls) -> List['base_model']:
|
||||
"""获取所有未删除的记录"""
|
||||
db = get_db_session()
|
||||
return db.query(cls).filter(cls.is_delete == 0).all()
|
||||
|
||||
@classmethod
|
||||
def exists(cls, id: Optional[int]) -> bool:
|
||||
"""检查未删除记录是否存在"""
|
||||
if id is None or id <= 0:
|
||||
return False
|
||||
db = get_db_session()
|
||||
return db.query(cls).filter(cls.id == id, cls.is_delete == 0).count() > 0
|
||||
|
||||
# ============================================================
|
||||
# 删除方法
|
||||
# ============================================================
|
||||
|
||||
@classmethod
|
||||
def batch_delete_r(cls, ids_str: Optional[str]):
|
||||
"""
|
||||
批量软删除并返回R对象
|
||||
|
||||
封装ID校验、解析、软删除的完整流程。
|
||||
|
||||
Args:
|
||||
ids_str: 逗号分隔的ID字符串,如 "1,2,3"
|
||||
|
||||
Returns:
|
||||
R: 操作结果
|
||||
"""
|
||||
if not ids_str:
|
||||
return R.failed("记录ID不存在")
|
||||
|
||||
id_list = parse_id_list(ids_str)
|
||||
if not id_list:
|
||||
return R.failed("记录ID不存在")
|
||||
|
||||
db = get_db_session()
|
||||
records = db.query(cls).filter(and_(cls.id.in_(id_list), cls.is_delete == 0)).all()
|
||||
if len(records) != len(id_list):
|
||||
return R.failed("记录不存在")
|
||||
|
||||
for record in records:
|
||||
record.is_delete = 1
|
||||
|
||||
return R.ok(msg="本次共删除{0}条数据".format(len(records)))
|
||||
|
||||
Reference in New Issue
Block a user