product_service.py 4.7 KB

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