category.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. """商品分类CRUD"""
  2. from typing import Optional, List
  3. from sqlalchemy.orm import Session
  4. from sqlalchemy import and_
  5. from app.models.models import BasProductCategory, BasProductCategoryHis
  6. from app.crud.base import CRUDBase
  7. class CategoryCRUD(CRUDBase[BasProductCategory]):
  8. """商品分类CRUD操作"""
  9. def get_by_name(self, db: Session, name: str, parent_id: str = None) -> Optional[BasProductCategory]:
  10. """根据名称获取分类"""
  11. query = db.query(BasProductCategory).filter(
  12. and_(BasProductCategory.name == name, BasProductCategory.is_del == 0)
  13. )
  14. if parent_id:
  15. query = query.filter(BasProductCategory.parent_id == parent_id)
  16. else:
  17. query = query.filter(BasProductCategory.parent_id.is_(None))
  18. return query.first()
  19. def get_active_all(self, db: Session) -> List[BasProductCategory]:
  20. """获取所有启用的分类"""
  21. return db.query(BasProductCategory).filter(
  22. and_(BasProductCategory.is_del == 0, BasProductCategory.is_active == True)
  23. ).order_by(BasProductCategory.sort_order.asc()).all()
  24. def get_children(self, db: Session, parent_id: str) -> List[BasProductCategory]:
  25. """获取子分类"""
  26. return db.query(BasProductCategory).filter(
  27. and_(BasProductCategory.parent_id == parent_id, BasProductCategory.is_del == 0)
  28. ).order_by(BasProductCategory.sort_order.asc()).all()
  29. def get_tree(self, db: Session) -> List[dict]:
  30. """获取分类树"""
  31. all_categories = self.get_active_all(db)
  32. return self._build_tree(all_categories, None)
  33. def _build_tree(self, categories: List[BasProductCategory], parent_id: str = None) -> List[dict]:
  34. """构建树形结构"""
  35. tree = []
  36. for cat in categories:
  37. if cat.parent_id == parent_id:
  38. pc = getattr(cat, "price_coefficient", None)
  39. node = {
  40. "id": cat.id,
  41. "name": cat.name,
  42. "parentId": cat.parent_id,
  43. "sortOrder": cat.sort_order,
  44. "priceCoefficient": float(pc) if pc is not None else 0.0,
  45. "isActive": cat.is_active,
  46. "children": self._build_tree(categories, cat.id)
  47. }
  48. tree.append(node)
  49. return tree
  50. def update_with_history(self, db: Session, id: str, obj_in: dict, updater: dict = None) -> Optional[BasProductCategory]:
  51. """更新分类并记录历史"""
  52. original = self.get(db, id)
  53. if not original:
  54. return None
  55. pc_hist = getattr(original, "price_coefficient", None)
  56. his_data = {
  57. "id": original.id,
  58. "name": original.name,
  59. "image_url": getattr(original, "image_url", None),
  60. "parent_id": original.parent_id,
  61. "sort_order": original.sort_order,
  62. "price_coefficient": pc_hist if pc_hist is not None else 0,
  63. "is_active": original.is_active,
  64. "create_at": original.create_at,
  65. "create_by": original.create_by,
  66. "create_name": original.create_name,
  67. "update_at": original.update_at,
  68. "update_by": original.update_by,
  69. }
  70. his = BasProductCategoryHis(**his_data)
  71. db.add(his)
  72. return self.update(db, id, obj_in, updater)
  73. def soft_delete_with_history(self, db: Session, id: str) -> bool:
  74. """删除分类并记录历史"""
  75. original = self.get(db, id)
  76. if not original:
  77. return False
  78. pc_hist = getattr(original, "price_coefficient", None)
  79. his_data = {
  80. "id": original.id,
  81. "name": original.name,
  82. "image_url": getattr(original, "image_url", None),
  83. "parent_id": original.parent_id,
  84. "sort_order": original.sort_order,
  85. "price_coefficient": pc_hist if pc_hist is not None else 0,
  86. "is_active": original.is_active,
  87. "create_at": original.create_at,
  88. "create_by": original.create_by,
  89. "create_name": original.create_name,
  90. "update_at": original.update_at,
  91. "update_by": original.update_by,
  92. }
  93. his = BasProductCategoryHis(**his_data)
  94. db.add(his)
  95. return self.delete(db, id)
  96. category_crud = CategoryCRUD(BasProductCategory)