Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
# 定义用户表
class User(Base):
__tablename__ = 'users'
user_id = Column(Integer, primary_key=True, autoincrement=True)
username = Column(String, nullable=False, unique=True)
password = Column(String, nullable=False)
address = Column(String)
favorites = relationship("UserFavorite", back_populates="user")
browsing_history = relationship("UserBrowsingHistory", back_populates="user")
# 定义收藏商品表
class UserFavorite(Base):
__tablename__ = 'user_favorites'
favorite_id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(Integer, ForeignKey('users.user_id'), nullable=False)
product_id = Column(Integer, nullable=False)
user = relationship("User", back_populates="favorites")
# 定义浏览商品表
class UserBrowsingHistory(Base):
__tablename__ = 'user_browsing_history'
history_id = Column(Integer, primary_key=True, autoincrement=True)
user_id = Column(Integer, ForeignKey('users.user_id'), nullable=False)
product_id = Column(Integer, nullable=False)
browse_time = Column(DateTime, default=datetime.utcnow)
user = relationship("User", back_populates="browsing_history")
# 在用户数据库中创建表
Base.metadata.create_all(engine_user)
# 添加用户
def add_user(username, password, address=None):
new_user = User(username=username, password=password, address=address)
session_user.add(new_user)
session_user.commit()
# 添加用户收藏的商品
def add_favorite(user_id, product_id):
new_favorite = UserFavorite(user_id=user_id, product_id=product_id)
session_user.add(new_favorite)
session_user.commit()
# 更新商品信息
def update_product(product_id, name=None, image=None, description=None, category=None, store=None, price=None, positive_feedback_content=None, negative_feedback_content=None):
product = session_product.query(Product).filter(Product.product_id == product_id).one()
if name:
product.product_name = name
if image:
product.product_image = image
if description:
product.product_description = description
if category:
product.category = category
if store:
product.store = store
if price is not None:
product.price = price
if positive_feedback_content is not None:
product.positive_feedback_content = positive_feedback_content
if negative_feedback_content is not None:
product.negative_feedback_content = negative_feedback_content
session_product.commit()
# 增加商品好评数量
def increment_positive_feedback(product_id, content=None):
product = session_product.query(Product).filter(Product.product_id == product_id).one()
product.positive_feedback += 1
if content:
product.positive_feedback_content = content
session_product.commit()
# 增加商品差评数量
def increment_negative_feedback(product_id, content=None):
product = session_product.query(Product).filter(Product.product_id == product_id).one()
product.negative_feedback += 1
if content:
product.negative_feedback_content = content
session_product.commit()
#控制面板上半部分在database2中
elif choice == '5':
product_id = int(input("商品ID: "))
name = input("商品名称: ")
image = input("商品图片: ")
description = input("商品描述: ")
category = input("类别: ")
store = input("店铺: ")
price = float(input("价格: "))
positive_feedback_content = input("好评内容: ")
negative_feedback_content = input("差评内容: ")
update_product(product_id, name, image, description, category, store, price, positive_feedback_content, negative_feedback_content)
elif choice == '6':
product_id = int(input("商品ID: "))
delete_product(product_id)
elif choice == '7':
product_id = int(input("商品ID: "))
increment_click_count(product_id)
elif choice == '8':
product_id = int(input("商品ID: "))
increment_favorite_count(product_id)
elif choice == '9':
product_id = int(input("商品ID: "))
content = input("好评内容: ")
increment_positive_feedback(product_id, content)
elif choice == '10':
product_id = int(input("商品ID: "))
content = input("差评内容: ")
increment_negative_feedback(product_id, content)
elif choice == '0':
break
else:
print("无效的选择,请重新输入。")
if __name__ == "__main__":
menu()