Files
shanxia2.0/backend/app/crud/category.py
T
2026-05-12 16:35:07 +08:00

113 lines
4.3 KiB
Python

"""商品分类CRUD"""
from typing import Optional, List
from sqlalchemy.orm import Session
from sqlalchemy import and_
from app.models.models import BasProductCategory, BasProductCategoryHis
from app.crud.base import CRUDBase
class CategoryCRUD(CRUDBase[BasProductCategory]):
"""商品分类CRUD操作"""
def get_by_name(self, db: Session, name: str, parent_id: str = None) -> Optional[BasProductCategory]:
"""根据名称获取分类"""
query = db.query(BasProductCategory).filter(
and_(BasProductCategory.name == name, BasProductCategory.is_del == 0)
)
if parent_id:
query = query.filter(BasProductCategory.parent_id == parent_id)
else:
query = query.filter(BasProductCategory.parent_id.is_(None))
return query.first()
def get_active_all(self, db: Session) -> List[BasProductCategory]:
"""获取所有启用的分类"""
return db.query(BasProductCategory).filter(
and_(BasProductCategory.is_del == 0, BasProductCategory.is_active == True)
).order_by(BasProductCategory.sort_order.asc()).all()
def get_children(self, db: Session, parent_id: str) -> List[BasProductCategory]:
"""获取子分类"""
return db.query(BasProductCategory).filter(
and_(BasProductCategory.parent_id == parent_id, BasProductCategory.is_del == 0)
).order_by(BasProductCategory.sort_order.asc()).all()
def get_tree(self, db: Session) -> List[dict]:
"""获取分类树"""
all_categories = self.get_active_all(db)
return self._build_tree(all_categories, None)
def _build_tree(self, categories: List[BasProductCategory], parent_id: str = None) -> List[dict]:
"""构建树形结构"""
tree = []
for cat in categories:
if cat.parent_id == parent_id:
pc = getattr(cat, "price_coefficient", None)
node = {
"id": cat.id,
"name": cat.name,
"parentId": cat.parent_id,
"sortOrder": cat.sort_order,
"priceCoefficient": float(pc) if pc is not None else 0.0,
"isActive": cat.is_active,
"children": self._build_tree(categories, cat.id)
}
tree.append(node)
return tree
def update_with_history(self, db: Session, id: str, obj_in: dict, updater: dict = None) -> Optional[BasProductCategory]:
"""更新分类并记录历史"""
original = self.get(db, id)
if not original:
return None
pc_hist = getattr(original, "price_coefficient", None)
his_data = {
"id": original.id,
"name": original.name,
"image_url": getattr(original, "image_url", None),
"parent_id": original.parent_id,
"sort_order": original.sort_order,
"price_coefficient": pc_hist if pc_hist is not None else 0,
"is_active": original.is_active,
"create_at": original.create_at,
"create_by": original.create_by,
"create_name": original.create_name,
"update_at": original.update_at,
"update_by": original.update_by,
}
his = BasProductCategoryHis(**his_data)
db.add(his)
return self.update(db, id, obj_in, updater)
def soft_delete_with_history(self, db: Session, id: str) -> bool:
"""删除分类并记录历史"""
original = self.get(db, id)
if not original:
return False
pc_hist = getattr(original, "price_coefficient", None)
his_data = {
"id": original.id,
"name": original.name,
"image_url": getattr(original, "image_url", None),
"parent_id": original.parent_id,
"sort_order": original.sort_order,
"price_coefficient": pc_hist if pc_hist is not None else 0,
"is_active": original.is_active,
"create_at": original.create_at,
"create_by": original.create_by,
"create_name": original.create_name,
"update_at": original.update_at,
"update_by": original.update_by,
}
his = BasProductCategoryHis(**his_data)
db.add(his)
return self.delete(db, id)
category_crud = CategoryCRUD(BasProductCategory)