update
This commit is contained in:
parent
037e396190
commit
6703869bfb
@ -42,6 +42,7 @@ from app.models.config import ConfigDB
|
|||||||
from app.core.mpmessage import sent_order_status_change_message
|
from app.core.mpmessage import sent_order_status_change_message
|
||||||
from fastapi import BackgroundTasks
|
from fastapi import BackgroundTasks
|
||||||
from app.core.coupon_manager import CouponManager
|
from app.core.coupon_manager import CouponManager
|
||||||
|
from app.core.redis_client import redis_client
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@ -331,6 +332,10 @@ async def create_order(
|
|||||||
else:
|
else:
|
||||||
success_text = settings.ORDER_SUCCESS_TODAY_TEXT
|
success_text = settings.ORDER_SUCCESS_TODAY_TEXT
|
||||||
|
|
||||||
|
# 添加到新订单队列
|
||||||
|
if db_order.address_community_id:
|
||||||
|
redis_client.push_order_to_queue(db_order.address_community_id, db_order.orderid)
|
||||||
|
|
||||||
return success_response(
|
return success_response(
|
||||||
message="订单创建成功",
|
message="订单创建成功",
|
||||||
data={
|
data={
|
||||||
@ -1356,4 +1361,18 @@ async def get_deliveryman_order_summary(
|
|||||||
"total_count": total,
|
"total_count": total,
|
||||||
"yesterday_count": yesterday_total,
|
"yesterday_count": yesterday_total,
|
||||||
"today_count": today_total
|
"today_count": today_total
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@router.get("/deliveryman/check_new/orders", response_model=ResponseModel)
|
||||||
|
async def check_new_orders(
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
deliveryman: UserDB = Depends(get_deliveryman_user)
|
||||||
|
):
|
||||||
|
"""检查新订单"""
|
||||||
|
|
||||||
|
# 从Redis获取新订单ID列表
|
||||||
|
order_ids = redis_client.pop_orders_from_queue(deliveryman.community_id, 10)
|
||||||
|
|
||||||
|
return success_response(data={
|
||||||
|
"has_new_order": len(order_ids) > 0
|
||||||
|
})
|
||||||
|
|||||||
@ -124,18 +124,19 @@ async def wechat_corp_callback(
|
|||||||
change_type = msg_root.find('ChangeType').text
|
change_type = msg_root.find('ChangeType').text
|
||||||
|
|
||||||
if change_type == 'add_member':
|
if change_type == 'add_member':
|
||||||
# 获取数据库会话
|
|
||||||
|
# # 获取数据库会话
|
||||||
db = next(get_db())
|
db = next(get_db())
|
||||||
|
|
||||||
# 查找对应的小区
|
# # 查找对应的小区
|
||||||
community = db.query(CommunityDB).filter(
|
# community = db.query(CommunityDB).filter(
|
||||||
CommunityDB.webot_webhook.isnot(None)
|
# CommunityDB.webot_webhook.isnot(None)
|
||||||
).first()
|
# ).first()
|
||||||
|
|
||||||
if community and community.webot_webhook:
|
# if community and community.webot_webhook:
|
||||||
# 发送欢迎消息
|
# # 发送欢迎消息
|
||||||
wecom_bot = WecomBot()
|
# wecom_bot = WecomBot()
|
||||||
await wecom_bot.send_welcome_message(community.webot_webhook)
|
# await wecom_bot.send_welcome_message(community.webot_webhook)
|
||||||
|
|
||||||
return Response(content="success", media_type="text/plain")
|
return Response(content="success", media_type="text/plain")
|
||||||
|
|
||||||
|
|||||||
98
app/core/redis_client.py
Normal file
98
app/core/redis_client.py
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
import redis
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from app.core.config import settings
|
||||||
|
from typing import List, Dict, Any, Optional
|
||||||
|
|
||||||
|
class RedisClient:
|
||||||
|
"""Redis 客户端"""
|
||||||
|
|
||||||
|
_instance = None
|
||||||
|
|
||||||
|
def __new__(cls):
|
||||||
|
if cls._instance is None:
|
||||||
|
cls._instance = super(RedisClient, cls).__new__(cls)
|
||||||
|
try:
|
||||||
|
cls._instance.client = redis.Redis(
|
||||||
|
host=settings.REDIS_HOST,
|
||||||
|
port=settings.REDIS_PORT,
|
||||||
|
db=settings.REDIS_DB,
|
||||||
|
password=settings.REDIS_PASSWORD,
|
||||||
|
decode_responses=True # 自动解码为字符串
|
||||||
|
)
|
||||||
|
# 测试连接
|
||||||
|
cls._instance.client.ping()
|
||||||
|
logging.info("Redis 连接成功")
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Redis 连接失败: {str(e)}")
|
||||||
|
cls._instance.client = None
|
||||||
|
return cls._instance
|
||||||
|
|
||||||
|
def get_client(self) -> redis.Redis:
|
||||||
|
"""获取 Redis 客户端"""
|
||||||
|
return self.client
|
||||||
|
|
||||||
|
def push_order_to_queue(self, community_id: int, order_id: str) -> bool:
|
||||||
|
"""
|
||||||
|
添加新订单到社区队列
|
||||||
|
|
||||||
|
Args:
|
||||||
|
community_id: 社区ID
|
||||||
|
order_id: 订单ID
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 是否添加成功
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
key = f"community:{community_id}:new_orders"
|
||||||
|
# 使用 LPUSH 将订单ID添加到列表头部
|
||||||
|
self.client.lpush(key, order_id)
|
||||||
|
# 设置过期时间为24小时
|
||||||
|
self.client.expire(key, 86400)
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"添加新订单到队列失败: {str(e)}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def pop_orders_from_queue(self, community_id: int, count: int = 10) -> List[str]:
|
||||||
|
"""
|
||||||
|
获取社区新订单列表
|
||||||
|
|
||||||
|
Args:
|
||||||
|
community_id: 社区ID
|
||||||
|
count: 获取数量,默认10个
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[str]: 订单ID列表
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
key = f"community:{community_id}:new_orders"
|
||||||
|
# 获取订单列表,并移除
|
||||||
|
orders = self.client.lpop(key, count)
|
||||||
|
return orders
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"获取社区新订单列表失败: {str(e)}")
|
||||||
|
return []
|
||||||
|
|
||||||
|
def remove_order(self, community_id: int, order_id: str) -> bool:
|
||||||
|
"""
|
||||||
|
从社区队列中移除订单
|
||||||
|
|
||||||
|
Args:
|
||||||
|
community_id: 社区ID
|
||||||
|
order_id: 订单ID
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: 是否移除成功
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
key = f"community:{community_id}:new_orders"
|
||||||
|
# 使用 LREM 移除列表中的元素
|
||||||
|
self.client.lrem(key, 0, order_id)
|
||||||
|
return True
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"从队列中移除订单失败: {str(e)}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
# 创建全局实例
|
||||||
|
redis_client = RedisClient()
|
||||||
Loading…
Reference in New Issue
Block a user