Lấy ra danh sách giá thấp nhất giờ tôi có bảng tôi muốn lấy ra giá thấp nhất và giá đó phải được gửi qua mail và bên dưới là cách tôi thực hiện với SQLAlchemy
Lấy ra danh sách giá thấp nhất bạn có thể thêm một phần mã để cập nhật cột notify của các sản phẩm đã được truy vấn thành 0.
Dưới đây là đoạn mã đã được chỉnh sửa để thực hiện việc đó:
from sqlalchemy import update def get_min_price(self): subquery = self.session.query( Product.type, func.min(Product.price).label('lowest_price') ).filter( Product.deleted == 0, Product.notify == 1 ).group_by(Product.type).subquery() query_lowest_price_by_type = self.session.query( Product.id, Product.name, Product.url, Product.link, Product.type, Product.price ).join( subquery, (Product.type == subquery.c.type) & ( Product.price == subquery.c.lowest_price) ).filter( Product.deleted == 0, Product.notify == 1 ) # Thực thi câu truy vấn lowest_price_by_type = query_lowest_price_by_type.all() # Lấy danh sách ID của các sản phẩm đã lấy ra product_ids = [product.id for product in lowest_price_by_type] # Thực hiện update lại notify là 0 cho các sản phẩm đã lấy ra self.session.execute( update(Product) .where(Product.id.in_(product_ids)) .values(notify=0) ) self.session.commit() lowest_price_by_type = self.sorted_price_min(lowest_price_by_type) return lowest_price_by_type
Trong đoạn mã này, chúng ta sử dụng hàm update từ SQLAlchemy để cập nhật các sản phẩm đã được lấy ra từ truy vấn ban đầu, đặt cột notify của chúng thành 0 và commit thay đổi vào cơ sở dữ liệu.
Để thực hiện việc chỉ cập nhật trạng thái notify thành 0 cho các sản phẩm đã được thông báo cách đây 3 ngày hoặc lâu hơn, bạn có thể sử dụng điều kiện thêm vào truy vấn update.
Điều này yêu cầu so sánh cột created_at với ngày hiện tại trừ đi 3 ngày.
Trước đó bạn có thể test bằng việc xây dựng chức năng sort
Trở lại với đoạn code lấy giá thấp nhất
Tối ưu code lấy giá thấp nhất
Dưới đây là cách bạn có thể chỉnh sửa đoạn mã để thêm điều kiện này:
from sqlalchemy import update, func from datetime import datetime, timedelta def get_min_price(self): three_days_ago = datetime.now() - timedelta(days=3) subquery = self.session.query( Product.type, func.min(Product.price).label('lowest_price') ).filter( Product.deleted == 0, Product.notify == 1 ).group_by(Product.type).subquery() query_lowest_price_by_type = self.session.query( Product.id, Product.name, Product.url, Product.link, Product.type, Product.price ).join( subquery, (Product.type == subquery.c.type) & ( Product.price == subquery.c.lowest_price) ).filter( Product.deleted == 0, Product.notify == 1 ) # Thực thi câu truy vấn lowest_price_by_type = query_lowest_price_by_type.all() # Lấy danh sách ID của các sản phẩm đã lấy ra product_ids = [product.id for product in lowest_price_by_type] # Thực hiện update lại notify là 0 cho các sản phẩm đã được thông báo ít nhất 3 ngày trước self.session.execute( update(Product) .where( Product.id.in_(product_ids), Product.created_at <= three_days_ago ) .values(notify=0) ) self.session.commit() lowest_price_by_type = self.sorted_price_min(lowest_price_by_type) return lowest_price_by_type
Trong đoạn mã này:
Full Code
Chia mã thành các hàm nhỏ hơn có thể giúp mã dễ đọc hơn và dễ bảo trì hơn.
Dưới đây là cách bạn có thể chia đoạn mã thành các hàm nhỏ hơn:
from sqlalchemy import update, func from datetime import datetime, timedelta class ProductService: def __init__(self, session): self.session = session def get_min_price_subquery(self): return self.session.query( Product.type, func.min(Product.price).label('lowest_price') ).filter( Product.deleted == 0, Product.notify == 1 ).group_by(Product.type).subquery() def get_lowest_price_by_type(self, subquery): return self.session.query( Product.id, Product.name, Product.url, Product.link, Product.type, Product.price ).join( subquery, (Product.type == subquery.c.type) & ( Product.price == subquery.c.lowest_price) ).filter( Product.deleted == 0, Product.notify == 1 ) def get_product_ids(self, query): return [product.id for product in query.all()] def update_notify_status(self, product_ids, three_days_ago): self.session.execute( update(Product) .where( Product.id.in_(product_ids), Product.created_at <= three_days_ago ) .values(notify=0) ) self.session.commit() def get_min_price(self): three_days_ago = datetime.now() - timedelta(days=3) subquery = self.get_min_price_subquery() query_lowest_price_by_type = self.get_lowest_price_by_type(subquery) lowest_price_by_type = query_lowest_price_by_type.all() product_ids = self.get_product_ids(query_lowest_price_by_type) self.update_notify_status(product_ids, three_days_ago) return self.sorted_price_min(lowest_price_by_type) def sorted_price_min(self, lowest_price_by_type): # Implement your sorting logic here return lowest_price_by_type
Giải thích các hàm nhỏ hơn:
Bạn có thể gọi hàm get_min_price từ lớp ProductService để lấy danh sách sản phẩm có giá thấp nhất và cập nhật trạng thái notify.
Tất nhiên! Hãy giải thích từng hàm và quy trình trong mã đã được chia nhỏ cho lấy ra danh sách giá thấp nhất
Đây là một lớp được thiết kế để thao tác với dịch vụ liên quan đến sản phẩm trong cơ sở dữ liệu.
Nó sử dụng SQLAlchemy để tương tác với cơ sở dữ liệu thông qua đối tượng session được cung cấp khi khởi tạo lớp.
def get_min_price_subquery(self): return self.session.query( Product.type, func.min(Product.price).label('lowest_price') ).filter( Product.deleted == 0, Product.notify == 1 ).group_by(Product.type).subquery()
def get_lowest_price_by_type(self, subquery): return self.session.query( Product.id, Product.name, Product.url, Product.link, Product.type, Product.price ).join( subquery, (Product.type == subquery.c.type) & ( Product.price == subquery.c.lowest_price) ).filter( Product.deleted == 0, Product.notify == 1 )
def get_product_ids(self, query): return [product.id for product in query.all()]
def update_notify_status(self, product_ids, three_days_ago): self.session.execute( update(Product) .where( Product.id.in_(product_ids), Product.created_at <= three_days_ago ) .values(notify=0) ) self.session.commit()
def get_min_price(self): three_days_ago = datetime.now() - timedelta(days=3) subquery = self.get_min_price_subquery() query_lowest_price_by_type = self.get_lowest_price_by_type(subquery) lowest_price_by_type = query_lowest_price_by_type.all() product_ids = self.get_product_ids(query_lowest_price_by_type) self.update_notify_status(product_ids, three_days_ago) return self.sorted_price_min(lowest_price_by_type)
def sorted_price_min(self, lowest_price_by_type): # Implement your sorting logic here return lowest_price_by_type
Việc chia nhỏ mã thành các hàm nhỏ giúp dễ bảo trì và mở rộng mã, mỗi hàm thực hiện một nhiệm vụ cụ thể, dễ dàng tái sử dụng và sửa đổi khi cần thiết.
#Mtips5s #Contact
Fanpage: https://www.facebook.com/mtipscoder
Group trao đổi, chia sẻ: https://www.facebook.com/groups/mtipscoder
Website: https://mtips5s.com
Youtube: https://mtips5s.com
Twitter(X): @takagiks99
Instagram: @khuongkara
Threads: @khuongkara
Google Maps: @khuongkara
#Base Code #Souce Code
Bộ công cụ My Self: @tools.mtips5s.com
Github: @github
Npm: @npm
Docker: @docker
Chúc các bạn thành công!
Leave A Comment