product.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from sqlalchemy import Column, Integer, String, Text, DateTime, Float, create_engine
  2. from sqlalchemy.ext.declarative import declarative_base
  3. from sqlalchemy.orm import sessionmaker
  4. from datetime import datetime
  5. from config.settings import DATABASE_CONFIG
  6. Base = declarative_base()
  7. class Product(Base):
  8. __tablename__ = 'products'
  9. id = Column(Integer, primary_key=True, autoincrement=True)
  10. product_id = Column(String(100), unique=True, nullable=False, comment='来源平台的商品ID')
  11. name = Column(String(500), nullable=False, comment='商品名称')
  12. url = Column(Text, comment='商品链接')
  13. image_url = Column(Text, comment='商品图片链接')
  14. shop_name = Column(String(200), comment='店铺名称')
  15. platform = Column(String(50), nullable=False, comment='来源平台: taobao, jd, alibaba1688')
  16. category = Column(String(200), comment='商品分类')
  17. brand = Column(String(100), comment='品牌')
  18. is_wholesale = Column(Integer, default=0, comment='是否为批发商品: 0-否, 1-是')
  19. min_order_quantity = Column(Integer, comment='最小起订量(批发)')
  20. unit = Column(String(50), comment='单位')
  21. current_price = Column(Float, comment='当前价格')
  22. original_price = Column(Float, comment='原价')
  23. currency = Column(String(10), default='CNY', comment='货币单位')
  24. sales_volume = Column(Integer, comment='销量')
  25. rating = Column(Float, comment='评分')
  26. review_count = Column(Integer, comment='评论数')
  27. stock = Column(Integer, comment='库存')
  28. description = Column(Text, comment='商品描述')
  29. create_time = Column(DateTime, default=datetime.now, comment='创建时间')
  30. update_time = Column(DateTime, default=datetime.now, onupdate=datetime.now, comment='更新时间')
  31. crawl_time = Column(DateTime, comment='最后爬取时间')
  32. def __repr__(self):
  33. return f"<Product(product_id='{self.product_id}', name='{self.name}', platform='{self.platform}')>"
  34. def to_dict(self):
  35. return {
  36. 'id': self.id,
  37. 'product_id': self.product_id,
  38. 'name': self.name,
  39. 'url': self.url,
  40. 'image_url': self.image_url,
  41. 'shop_name': self.shop_name,
  42. 'platform': self.platform,
  43. 'category': self.category,
  44. 'brand': self.brand,
  45. 'is_wholesale': self.is_wholesale,
  46. 'min_order_quantity': self.min_order_quantity,
  47. 'unit': self.unit,
  48. 'current_price': self.current_price,
  49. 'original_price': self.original_price,
  50. 'currency': self.currency,
  51. 'sales_volume': self.sales_volume,
  52. 'rating': self.rating,
  53. 'review_count': self.review_count,
  54. 'stock': self.stock,
  55. 'description': self.description,
  56. 'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
  57. 'update_time': self.update_time.strftime('%Y-%m-%d %H:%M:%S') if self.update_time else None,
  58. 'crawl_time': self.crawl_time.strftime('%Y-%m-%d %H:%M:%S') if self.crawl_time else None
  59. }
  60. class PriceHistory(Base):
  61. __tablename__ = 'price_history'
  62. id = Column(Integer, primary_key=True, autoincrement=True)
  63. product_id = Column(String(100), nullable=False, comment='商品ID')
  64. price = Column(Float, nullable=False, comment='价格')
  65. original_price = Column(Float, comment='原价')
  66. currency = Column(String(10), default='CNY', comment='货币单位')
  67. platform = Column(String(50), nullable=False, comment='来源平台')
  68. source_url = Column(Text, comment='来源页面URL')
  69. crawl_time = Column(DateTime, default=datetime.now, comment='爬取时间')
  70. price_type = Column(String(50), default='retail', comment='价格类型: retail(零售), wholesale(批发)')
  71. min_quantity = Column(Integer, comment='最小起订量(批发价格)')
  72. max_quantity = Column(Integer, comment='最大起订量(批发价格)')
  73. def __repr__(self):
  74. return f"<PriceHistory(product_id='{self.product_id}', price={self.price}, crawl_time='{self.crawl_time}')>"
  75. def to_dict(self):
  76. return {
  77. 'id': self.id,
  78. 'product_id': self.product_id,
  79. 'price': self.price,
  80. 'original_price': self.original_price,
  81. 'currency': self.currency,
  82. 'platform': self.platform,
  83. 'source_url': self.source_url,
  84. 'crawl_time': self.crawl_time.strftime('%Y-%m-%d %H:%M:%S') if self.crawl_time else None,
  85. 'price_type': self.price_type,
  86. 'min_quantity': self.min_quantity,
  87. 'max_quantity': self.max_quantity
  88. }
  89. def get_engine():
  90. db_config = DATABASE_CONFIG
  91. connection_string = (
  92. f"mysql+pymysql://{db_config['user']}:{db_config['password']}"
  93. f"@{db_config['host']}:{db_config['port']}/{db_config['database']}"
  94. f"?charset={db_config['charset']}"
  95. )
  96. return create_engine(connection_string, echo=False)
  97. def init_db():
  98. engine = get_engine()
  99. Base.metadata.create_all(engine)
  100. print("数据库初始化完成")
  101. def get_session():
  102. engine = get_engine()
  103. Session = sessionmaker(bind=engine)
  104. return Session()