product.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 BasProduct, BasProductHis
  6. from app.crud.base import CRUDBase
  7. class ProductCRUD(CRUDBase[BasProduct]):
  8. """商品CRUD操作"""
  9. def get_by_name(self, db: Session, name: str) -> Optional[BasProduct]:
  10. """根据名称获取商品"""
  11. return db.query(BasProduct).filter(
  12. and_(BasProduct.name == name, BasProduct.is_del == 0)
  13. ).first()
  14. def get_active_all(self, db: Session) -> List[BasProduct]:
  15. """获取所有启用的商品"""
  16. return db.query(BasProduct).filter(
  17. and_(BasProduct.is_del == 0, BasProduct.is_active == True)
  18. ).order_by(BasProduct.create_at.desc()).all()
  19. def get_multi_by_category(self, db: Session, category_id: str) -> List[BasProduct]:
  20. """根据分类获取商品"""
  21. return db.query(BasProduct).filter(
  22. and_(BasProduct.category_id == category_id, BasProduct.is_del == 0, BasProduct.is_active == True)
  23. ).order_by(BasProduct.create_at.desc()).all()
  24. def get_multi_with_filters(
  25. self,
  26. db: Session,
  27. category_id: str = None,
  28. keyword: str = None,
  29. is_active: bool = None,
  30. skip: int = 0,
  31. limit: int = 10
  32. ) -> tuple[List[BasProduct], int]:
  33. """多条件查询商品"""
  34. query = db.query(BasProduct).filter(BasProduct.is_del == 0)
  35. if category_id:
  36. query = query.filter(BasProduct.category_id == category_id)
  37. if is_active is not None:
  38. query = query.filter(BasProduct.is_active == is_active)
  39. if keyword:
  40. query = query.filter(
  41. (BasProduct.name.like(f"%{keyword}%")) |
  42. (BasProduct.description.like(f"%{keyword}%"))
  43. )
  44. total = query.count()
  45. products = query.order_by(BasProduct.create_at.desc()).offset(skip).limit(limit).all()
  46. return products, total
  47. def update_with_history(self, db: Session, id: str, obj_in: dict, updater: dict = None) -> Optional[BasProduct]:
  48. """更新商品并记录历史"""
  49. original = self.get(db, id)
  50. if not original:
  51. return None
  52. his_data = {
  53. "id": original.id,
  54. "name": original.name,
  55. "category_id": original.category_id,
  56. "unit_id": original.unit_id,
  57. "measure_value": getattr(original, "measure_value", None),
  58. "description": original.description,
  59. "image_url": original.image_url,
  60. "is_active": original.is_active,
  61. "create_at": original.create_at,
  62. "create_by": original.create_by,
  63. "create_name": original.create_name,
  64. "update_at": original.update_at,
  65. "update_by": original.update_by,
  66. }
  67. his = BasProductHis(**his_data)
  68. db.add(his)
  69. return self.update(db, id, obj_in, updater)
  70. def soft_delete_with_history(self, db: Session, id: str) -> bool:
  71. """删除商品并记录历史"""
  72. original = self.get(db, id)
  73. if not original:
  74. return False
  75. his_data = {
  76. "id": original.id,
  77. "name": original.name,
  78. "category_id": original.category_id,
  79. "unit_id": original.unit_id,
  80. "measure_value": getattr(original, "measure_value", None),
  81. "description": original.description,
  82. "image_url": original.image_url,
  83. "is_active": original.is_active,
  84. "create_at": original.create_at,
  85. "create_by": original.create_by,
  86. "create_name": original.create_name,
  87. "update_at": original.update_at,
  88. "update_by": original.update_by,
  89. }
  90. his = BasProductHis(**his_data)
  91. db.add(his)
  92. return self.delete(db, id)
  93. product_crud = ProductCRUD(BasProduct)