service.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. from db import MongoDao
  2. from data import BrandInfo, ProductInfo
  3. import gradio as gr
  4. product_dao = MongoDao("vbrand-ec")
  5. class Service:
  6. product_map = {}
  7. @staticmethod
  8. def get_brand_list():
  9. """获取数据库中存储的品牌列表"""
  10. brand_list = product_dao.get_one_field_data("brandName")
  11. brand_list = list(dict.fromkeys(brand_list))
  12. return brand_list
  13. @staticmethod
  14. def get_init_state_product_list(brand_name):
  15. """获取初始状态下的产品列表"""
  16. brand_info = BrandInfo(brand_name)
  17. prodct_titles = [p.title for p in brand_info.product_list]
  18. Service.product_map[brand_name] = brand_info.product_list
  19. return prodct_titles
  20. @staticmethod
  21. def on_brand_change(brand_name):
  22. """加载选定品牌的所有商品"""
  23. brand_info = BrandInfo(brand_name)
  24. produt_titles = [p.title for p in brand_info.product_list]
  25. Service.product_map[brand_name] = brand_info.product_list
  26. return gr.update(choices=produt_titles, value=produt_titles[0])
  27. @staticmethod
  28. def on_product_change(title, brand_name):
  29. product_list = Service.product_map.get(brand_name, [])
  30. product = next((p for p in product_list if p.title == title), None)
  31. if not product:
  32. return [], [["商品未找到", ""]]
  33. # 加载图片
  34. images = [image_url for image_url in product.iamges]
  35. # 产品信息
  36. info = [
  37. ["商品链接", product.url],
  38. ["平台", product.platFormName],
  39. ["商户名称", product.shopTitle],
  40. ["颜色", product.colors],
  41. ["尺寸", product.sizes],
  42. ["价格", product.price],
  43. ]
  44. return images, info
  45. if __name__ == "__main__":
  46. brand_list = Service.get_brand_list()
  47. print(brand_list)