| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- # app/services/agent/tools/builtin/product_tools.py
- from typing import Optional, Dict, Any, List
- from sqlalchemy.ext.asyncio import AsyncSession
- from sqlalchemy import select, or_, desc
- from app.models.product import VasProduct
- from app.services.agent.tools.registry import agent_tool
- @agent_tool(
- name="search_products",
- description="检索可用的增值服务/签证/产品列表,支持按国家、签证类型或关键词搜索",
- )
- async def search_products(
- keyword: Optional[str] = None,
- country: Optional[str] = None,
- visa_type: Optional[str] = None,
- limit: int = 10,
- *,
- db: AsyncSession,
- ) -> Dict[str, Any]:
- """搜索并列出可用的产品"""
- limit = max(1, min(limit, 30))
- stmt = select(VasProduct).where(VasProduct.enabled == 1)
- if country and country.strip():
- stmt = stmt.where(VasProduct.country.ilike(f"%{country.strip()}%"))
- if visa_type and visa_type.strip():
- stmt = stmt.where(VasProduct.visa_type.ilike(f"%{visa_type.strip()}%"))
- if keyword and keyword.strip():
- kw = f"%{keyword.strip()}%"
- stmt = stmt.where(or_(VasProduct.title.ilike(kw), VasProduct.description.ilike(kw)))
- stmt = stmt.order_by(desc(VasProduct.recommend_score), desc(VasProduct.id)).limit(limit)
- res = await db.execute(stmt)
- products = res.scalars().all()
- items = []
- for p in products:
- items.append({
- "product_id": p.id,
- "title": p.title,
- "country": p.country,
- "city": p.city,
- "visa_type": p.visa_type,
- "price_amount": p.price_amount,
- "currency": p.price_currency,
- "description": p.description[:200] if p.description else "",
- })
- return {
- "count": len(items),
- "products": items,
- }
- @agent_tool(
- name="get_product_detail",
- description="获取指定商品ID的详细信息与配置说明",
- )
- async def get_product_detail(
- product_id: int,
- *,
- db: AsyncSession,
- ) -> Dict[str, Any]:
- """获取单个产品的详细信息"""
- stmt = select(VasProduct).where(VasProduct.id == product_id)
- res = await db.execute(stmt)
- p = res.scalar_one_or_none()
- if not p:
- return {"error": f"Product ID {product_id} does not exist"}
- return {
- "product_id": p.id,
- "title": p.title,
- "country": p.country,
- "city": p.city,
- "visa_type": p.visa_type,
- "provider": p.provider,
- "price_amount": p.price_amount,
- "currency": p.price_currency,
- "enabled": bool(p.enabled),
- "description": p.description,
- "extra_fields": p.extra_fields,
- "schema_id": p.schema_id,
- }
- @agent_tool(
- name="admin_list_all_products",
- description="管理员查看系统所有签证商品(包含已启用与已下架的商品),支持按国家、服务商筛选",
- )
- async def admin_list_all_products(
- country: Optional[str] = None,
- provider: Optional[str] = None,
- enabled: Optional[bool] = None,
- limit: int = 20,
- *,
- db: AsyncSession,
- ) -> Dict[str, Any]:
- """管理员全景查询商品列表"""
- stmt = select(VasProduct)
- if enabled is not None:
- stmt = stmt.where(VasProduct.enabled == (1 if enabled else 0))
- if country and country.strip():
- stmt = stmt.where(VasProduct.country.ilike(f"%{country.strip()}%"))
- if provider and provider.strip():
- stmt = stmt.where(VasProduct.provider.ilike(f"%{provider.strip()}%"))
- stmt = stmt.order_by(desc(VasProduct.id)).limit(max(1, min(limit, 50)))
- res = await db.execute(stmt)
- products = res.scalars().all()
- items = []
- for p in products:
- items.append({
- "product_id": p.id,
- "title": p.title,
- "country": p.country,
- "city": p.city,
- "visa_type": p.visa_type,
- "provider": p.provider,
- "price_amount": p.price_amount,
- "currency": p.price_currency,
- "enabled": bool(p.enabled),
- "recommend_score": p.recommend_score,
- })
- return {
- "count": len(items),
- "products": items,
- }
- @agent_tool(
- name="admin_toggle_product_status",
- description="上架或下架指定商品(enabled: true-上架启用, false-下架停用)",
- is_dangerous=True,
- )
- async def admin_toggle_product_status(
- product_id: int,
- enabled: bool,
- *,
- db: AsyncSession,
- ) -> Dict[str, Any]:
- """更新商品上下架状态"""
- stmt = select(VasProduct).where(VasProduct.id == product_id)
- res = await db.execute(stmt)
- p = res.scalar_one_or_none()
- if not p:
- return {"error": f"Product ID {product_id} does not exist"}
- old_status = bool(p.enabled)
- p.enabled = 1 if enabled else 0
- await db.commit()
- await db.refresh(p)
- return {
- "success": True,
- "product_id": p.id,
- "title": p.title,
- "previous_status": "enabled" if old_status else "disabled",
- "new_status": "enabled" if enabled else "disabled",
- }
- @agent_tool(
- name="admin_update_product_price",
- description="修改指定商品的服务费用单价(price_amount 单位为分,如 35000 代表 350 元)",
- is_dangerous=True,
- )
- async def admin_update_product_price(
- product_id: int,
- price_amount: int,
- *,
- db: AsyncSession,
- ) -> Dict[str, Any]:
- """修改商品单价"""
- if price_amount <= 0:
- return {"error": "Price amount must be greater than 0"}
- stmt = select(VasProduct).where(VasProduct.id == product_id)
- res = await db.execute(stmt)
- p = res.scalar_one_or_none()
- if not p:
- return {"error": f"Product ID {product_id} does not exist"}
- old_price = p.price_amount
- p.price_amount = price_amount
- await db.commit()
- await db.refresh(p)
- return {
- "success": True,
- "product_id": p.id,
- "title": p.title,
- "previous_price": old_price,
- "new_price": p.price_amount,
- "currency": p.price_currency,
- }
|