product_service.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # app/services/product_service.py
  2. from typing import Optional
  3. from sqlalchemy.ext.asyncio import AsyncSession
  4. from sqlalchemy import select
  5. from app.utils.search import apply_keyword_search_stmt
  6. from app.utils.pagination import paginate
  7. from app.core.biz_exception import NotFoundError
  8. from app.models.product import VasProduct
  9. from app.schemas.product import VasProductCreate, VasProductUpdate
  10. class ProductService:
  11. @staticmethod
  12. async def create(
  13. db: AsyncSession,
  14. data: VasProductCreate,
  15. ) -> VasProduct:
  16. rec = VasProduct(**data.dict())
  17. db.add(rec)
  18. await db.commit()
  19. await db.refresh(rec)
  20. return rec
  21. @staticmethod
  22. async def get(
  23. db: AsyncSession,
  24. id: int,
  25. ) -> VasProduct:
  26. stmt = select(VasProduct).where(VasProduct.id == id)
  27. obj = (await db.execute(stmt)).scalar_one_or_none()
  28. if not obj:
  29. raise NotFoundError("Product not exist")
  30. return obj
  31. @staticmethod
  32. async def update(
  33. db: AsyncSession,
  34. id: int,
  35. data: VasProductUpdate,
  36. ) -> VasProduct:
  37. stmt = select(VasProduct).where(VasProduct.id == id)
  38. rec = (await db.execute(stmt)).scalar_one_or_none()
  39. if not rec:
  40. raise NotFoundError("Product not exist")
  41. for k, v in data.dict(exclude_unset=True).items():
  42. setattr(rec, k, v)
  43. await db.commit()
  44. await db.refresh(rec)
  45. return rec
  46. @staticmethod
  47. async def delete(
  48. db: AsyncSession,
  49. id: int,
  50. ) -> bool:
  51. stmt = select(VasProduct).where(VasProduct.id == id)
  52. rec = (await db.execute(stmt)).scalar_one_or_none()
  53. if not rec:
  54. raise NotFoundError("Product not exist")
  55. await db.delete(rec)
  56. await db.commit()
  57. return True
  58. @staticmethod
  59. async def list_enable_product(
  60. db: AsyncSession,
  61. country: str = None,
  62. visa_type: str = None,
  63. page: int = 0,
  64. size: int = 10,
  65. keyword: str = None,
  66. ):
  67. # ⚠️ paginate / apply_keyword_search 仍然基于 Query
  68. # 如果你当前 paginate 是同步实现,这里保持与你原项目一致
  69. stmt = select(VasProduct)
  70. stmt = stmt.where(VasProduct.enabled == 1)
  71. if country:
  72. stmt = stmt.where(VasProduct.country == country)
  73. if visa_type:
  74. stmt = stmt.where(VasProduct.visa_type == visa_type)
  75. stmt = apply_keyword_search_stmt(
  76. stmt=stmt,
  77. model=VasProduct,
  78. keyword=keyword,
  79. fields=["title", "provider", "description", "country", "city"],
  80. )
  81. stmt = stmt.order_by(
  82. VasProduct.recommend_score.desc(),
  83. VasProduct.created_at.desc(),
  84. )
  85. return await paginate(db, stmt, page, size)
  86. @staticmethod
  87. async def list_product(
  88. db: AsyncSession,
  89. country: str = None,
  90. visa_type: str = None,
  91. page: int = 0,
  92. size: int = 10,
  93. keyword: str = None,
  94. ):
  95. # ⚠️ paginate / apply_keyword_search 仍然基于 Query
  96. # 如果你当前 paginate 是同步实现,这里保持与你原项目一致
  97. stmt = select(VasProduct)
  98. if country:
  99. stmt = stmt.where(VasProduct.country == country)
  100. if visa_type:
  101. stmt = stmt.where(VasProduct.visa_type == visa_type)
  102. stmt = apply_keyword_search_stmt(
  103. stmt=stmt,
  104. model=VasProduct,
  105. keyword=keyword,
  106. fields=["title", "provider", "description", "country", "city"],
  107. )
  108. stmt = stmt.order_by(
  109. VasProduct.recommend_score.desc(),
  110. VasProduct.created_at.desc(),
  111. )
  112. return await paginate(db, stmt, page, size)