75 lines
2.2 KiB
Python
75 lines
2.2 KiB
Python
"""
|
|
Product业务逻辑服务层
|
|
"""
|
|
from typing import Optional, Tuple, List
|
|
from flask import current_app
|
|
from sqlalchemy import func, or_
|
|
from app import db
|
|
from app.models import Product, Version
|
|
from app.utils.logger import log_operation
|
|
|
|
|
|
class ProductService:
|
|
"""产品业务逻辑服务"""
|
|
|
|
@staticmethod
|
|
def get_products(
|
|
page: int = 1,
|
|
per_page: int = 20,
|
|
keyword: str = '',
|
|
product_type: Optional[str] = None,
|
|
is_paid: Optional[int] = None
|
|
) -> Tuple[List[Product], int]:
|
|
"""
|
|
获取产品列表
|
|
:param page: 页码
|
|
:param per_page: 每页数量
|
|
:param keyword: 关键词
|
|
:param product_type: 产品类型
|
|
:param is_paid: 是否付费
|
|
:return: (产品列表, 总数)
|
|
"""
|
|
query = Product.query.filter_by(status=1) # 只显示启用的产品
|
|
|
|
# 关键词搜索
|
|
if keyword:
|
|
escaped_keyword = keyword.replace('%', '\\%').replace('_', '\\_')
|
|
pattern = f'%{escaped_keyword}%'
|
|
query = query.filter(
|
|
or_(
|
|
Product.product_name.like(pattern, escape='\\'),
|
|
Product.description.like(pattern, escape='\\')
|
|
)
|
|
)
|
|
|
|
# 筛选条件
|
|
if product_type:
|
|
query = query.filter(Product.product_type == product_type)
|
|
|
|
query = query.order_by(Product.create_time.desc())
|
|
|
|
# 分页
|
|
pagination = query.paginate(page=page, per_page=per_page, error_out=False)
|
|
return pagination.items, pagination.total
|
|
|
|
@staticmethod
|
|
def get_product(product_id: str) -> Optional[Product]:
|
|
"""
|
|
获取产品详情
|
|
:param product_id: 产品ID
|
|
:return: 产品对象
|
|
"""
|
|
return Product.query.filter_by(product_id=product_id, status=1).first()
|
|
|
|
@staticmethod
|
|
def get_latest_version(product_id: str) -> Optional[Version]:
|
|
"""
|
|
获取产品最新版本
|
|
:param product_id: 产品ID
|
|
:return: 版本对象
|
|
"""
|
|
return Version.query.filter_by(
|
|
product_id=product_id,
|
|
publish_status=1
|
|
).order_by(Version.update_time.desc(), Version.create_time.desc()).first()
|