|
|
|
@ -332,14 +332,23 @@ async def query_flow(request: Request, spot: str) -> str: |
|
|
|
|
pool = request.app.state.mysql_pool |
|
|
|
|
async with pool.acquire() as conn: |
|
|
|
|
async with conn.cursor() as cur: |
|
|
|
|
search_spot = f"%{spot}%" |
|
|
|
|
# 查询景区基本信息以获取ID |
|
|
|
|
id_query = "SELECT id FROM cyjcpt_bd.zhly_scenic_basic WHERE `name` LIKE %s LIMIT 1" |
|
|
|
|
await cur.execute(id_query, (search_spot,)) |
|
|
|
|
id_row = await cur.fetchone() |
|
|
|
|
scenic_id = id_row[0] if id_row else None |
|
|
|
|
|
|
|
|
|
# 查询景区客流信息 |
|
|
|
|
query = """SELECT ABS(SUM(t1.init_num)-SUM(t1.out_num)) AS in_num , t3.realtime_load_capacity AS capacity |
|
|
|
|
FROM equipment_passenger_flow.flow_current_video t1 |
|
|
|
|
LEFT JOIN cyjcpt_bd.zhly_video_manage t2 ON t1.mac_address = t2.mac_address |
|
|
|
|
LEFT JOIN cyjcpt_bd.zhly_scenic_basic t3 ON t2.video_scenic_id = t3.id |
|
|
|
|
WHERE DATE_FORMAT(t1.create_time,'%%Y%%m%%d') = DATE_FORMAT(NOW(),'%%Y%%m%%d') AND t3.`name` LIKE %s""" |
|
|
|
|
search_spot = f"%{spot}%" |
|
|
|
|
await cur.execute(query, (search_spot,)) |
|
|
|
|
WHERE DATE_FORMAT(t1.create_time,'%%Y%%m%%d') = DATE_FORMAT(NOW(),'%%Y%%m%%d') AND t3.id = {scenic_id_condition}""" |
|
|
|
|
|
|
|
|
|
formatted_flow_query = query.format(scenic_id_condition=scenic_id) |
|
|
|
|
|
|
|
|
|
await cur.execute(formatted_flow_query) |
|
|
|
|
row = await cur.fetchone() |
|
|
|
|
|
|
|
|
|
# 查询停车场信息 |
|
|
|
@ -348,7 +357,7 @@ async def query_flow(request: Request, spot: str) -> str: |
|
|
|
|
LEFT JOIN cyjcpt_bd.zhly_scenic_basic t2 ON t1.scenic_id = t2.id |
|
|
|
|
LEFT JOIN cyjcpt_bd.park_info t3 ON t1.park_code = t3.park_code |
|
|
|
|
LEFT JOIN equipment_passenger_flow.park_current t4 ON t1.park_code = t4.park_code |
|
|
|
|
WHERE t2.`name` LIKE %s AND t1.distance_value <= 1000 AND t3.total_count != 0 {status_condition} ORDER BY t1.distance_value ASC LIMIT 2""" |
|
|
|
|
WHERE t2.id = {scenic_id_condition} AND t1.distance_value <= 1000 AND t3.total_count != 0 {status_condition} ORDER BY t1.distance_value ASC LIMIT 2""" |
|
|
|
|
#获取当前日期 |
|
|
|
|
now = datetime.now() |
|
|
|
|
# 根据是否为节假日设置状态条件 |
|
|
|
@ -360,17 +369,11 @@ async def query_flow(request: Request, spot: str) -> str: |
|
|
|
|
status_condition = "AND t3.`status` = '0'" |
|
|
|
|
|
|
|
|
|
# 格式化SQL查询,插入状态条件 |
|
|
|
|
formatted_query = park_query.format(status_condition=status_condition) |
|
|
|
|
formatted_park_query = park_query.format(scenic_id_condition=scenic_id, status_condition=status_condition) |
|
|
|
|
|
|
|
|
|
park_search_spot = f"%{spot}%" |
|
|
|
|
await cur.execute(formatted_query, (park_search_spot,)) |
|
|
|
|
await cur.execute(formatted_park_query) |
|
|
|
|
park_rows = await cur.fetchall() |
|
|
|
|
|
|
|
|
|
# 先查询景区基本信息以获取ID |
|
|
|
|
id_query = "SELECT id FROM cyjcpt_bd.zhly_scenic_basic WHERE `name` LIKE %s LIMIT 1" |
|
|
|
|
await cur.execute(id_query, (search_spot,)) |
|
|
|
|
id_row = await cur.fetchone() |
|
|
|
|
scenic_id = id_row[0] if id_row else None |
|
|
|
|
except Exception as e: |
|
|
|
|
print(f"[MySQL] 查询失败: {e}") |
|
|
|
|
return f"**未找到景区【{spot}】的信息,请检查名称是否正确。\n\n(内容仅供参考)" |
|
|
|
@ -387,14 +390,14 @@ async def query_flow(request: Request, spot: str) -> str: |
|
|
|
|
0.7-0.9:较拥挤 |
|
|
|
|
大于0.9:拥挤""" |
|
|
|
|
hold_level = "舒适" if hold_rate < 0.3 else "较舒适" if hold_rate < 0.5 else "一般" if hold_rate < 0.7 else "较拥挤" if hold_rate < 0.9 else "拥挤" |
|
|
|
|
result = f"{spot} 客流\n\n在园人数: {in_num}\n\n舒适度:{hold_level};" |
|
|
|
|
result = f"{spot} 客流:在园人数: {in_num};舒适度:{hold_level};" |
|
|
|
|
else: |
|
|
|
|
result = f"未找到景区【{spot}】的客流相关信息,在园人数和舒适度未知;" |
|
|
|
|
if park_rows: |
|
|
|
|
for park_row in park_rows: |
|
|
|
|
# 使用变量名访问停车场数据 |
|
|
|
|
park_name, rate_info, total_count, space, distance_value = park_row |
|
|
|
|
result += f"停车场名称:{park_name} ,距离:{distance_value}米,空余车位:{space if space is not None else '未知'},总车位:{total_count},收费标准:{rate_info if rate_info else '暂无收费标准信息'}。" |
|
|
|
|
result += f"\n停车场信息:名称:{park_name} ,距离:{distance_value}米,空余车位:{space if space is not None else '未知'},总车位:{total_count},收费标准:{rate_info if rate_info else '暂无收费标准信息'}。" |
|
|
|
|
else: |
|
|
|
|
result += "停车场信息:暂无数据。" |
|
|
|
|
|
|
|
|
@ -859,7 +862,7 @@ async def extract_multi_scenic(msg) -> list: |
|
|
|
|
print(f"Error in multi scenic extraction: {e}") |
|
|
|
|
return [] |
|
|
|
|
|
|
|
|
|
async def query_multi_scenic_flow(request: Request, scenics: list) -> str: |
|
|
|
|
async def query_multi_scenic_flow(request: Request, scenics: list,msg: str) -> str: |
|
|
|
|
if not scenics: |
|
|
|
|
print("No scenics found, returning default message.") |
|
|
|
|
return "**未找到景区信息,请检查名称是否正确。**\n\n(内容由AI生成,仅供参考)" |
|
|
|
@ -878,10 +881,10 @@ async def query_multi_scenic_flow(request: Request, scenics: list) -> str: |
|
|
|
|
return results[0]["data"] |
|
|
|
|
elif len(results) >= 2: |
|
|
|
|
# 构造比较提示词 |
|
|
|
|
comparison_prompt = f"请比较以下景区的客流情况,并回答哪个景区人更少:\n\n" |
|
|
|
|
comparison_prompt = f"请结合数据,基于用户的问题:{ msg} 给于简短的建议,注意:需要将原始数据返回\n\n" |
|
|
|
|
for result in results: |
|
|
|
|
comparison_prompt += f"**{result['scenic']}**: {result['data']}\n\n" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
|
|
response = client.chat.completions.create( |
|
|
|
|
model="deepseek-chat", |
|
|
|
|