138 lines
4.5 KiB
Python
138 lines
4.5 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query
|
||
from sqlalchemy.orm import Session
|
||
from sqlalchemy import and_, desc, func
|
||
from typing import Optional, List
|
||
from app.core.response import success_response
|
||
from app.api.deps import get_db, get_current_user, get_admin_user
|
||
from app.models.merchant_complaint import MerchantComplaintDB, MerchantComplaintCreate, MerchantComplaintInfo, MerchantComplaintUpdate, ComplaintStatus
|
||
from app.models.user import UserDB
|
||
from app.models.merchant import MerchantDB
|
||
from datetime import datetime
|
||
from app.core.wecombot import WecomBot
|
||
|
||
router = APIRouter()
|
||
|
||
@router.get("/get_reason_list")
|
||
async def get_reason_list(
|
||
db: Session = Depends(get_db)
|
||
):
|
||
"""
|
||
获取投诉原因列表
|
||
"""
|
||
# 获取投诉原因列表
|
||
reason_list = [
|
||
"商家不让使用快捷买单",
|
||
"商家引导用户使用其他支付方式",
|
||
"商家存在食品安全问题",
|
||
"商家涉及违法违规行为"
|
||
]
|
||
|
||
return success_response(data={"items": reason_list}, message="投诉原因列表获取成功")
|
||
|
||
@router.post("/")
|
||
async def create_complaint(
|
||
complaint: MerchantComplaintCreate,
|
||
db: Session = Depends(get_db),
|
||
current_user: UserDB = Depends(get_current_user)
|
||
):
|
||
"""
|
||
创建商家投诉
|
||
"""
|
||
try:
|
||
# 检查商家是否存在
|
||
merchant = db.query(MerchantDB).filter(MerchantDB.id == complaint.merchant_id).first()
|
||
if not merchant:
|
||
raise HTTPException(status_code=404, detail="商家不存在")
|
||
|
||
# 创建投诉记录
|
||
db_complaint = MerchantComplaintDB(
|
||
user_id=current_user.userid, # 使用当前用户ID,而不是请求中的user_id
|
||
merchant_id=complaint.merchant_id,
|
||
content=complaint.content,
|
||
image_urls=complaint.image_urls,
|
||
status=ComplaintStatus.PENDING # 默认状态为等待处理
|
||
)
|
||
|
||
db.add(db_complaint)
|
||
db.commit()
|
||
db.refresh(db_complaint)
|
||
|
||
# 发送企业微信消息
|
||
await WecomBot().send_merchant_complaint(db_complaint)
|
||
|
||
return success_response(data={"id": db_complaint.id}, message="投诉提交成功")
|
||
except Exception as e:
|
||
raise HTTPException(status_code=500, detail=str(e))
|
||
|
||
@router.get("/")
|
||
async def get_complaints(
|
||
status: Optional[ComplaintStatus] = None,
|
||
merchant_id: Optional[int] = None,
|
||
start_time: Optional[datetime] = None,
|
||
end_time: Optional[datetime] = None,
|
||
skip: int = Query(0, ge=0),
|
||
limit: int = Query(10, ge=1, le=100),
|
||
db: Session = Depends(get_db),
|
||
current_user: UserDB = Depends(get_current_user)
|
||
):
|
||
"""
|
||
获取当前用户的投诉列表
|
||
"""
|
||
# 构建查询条件
|
||
conditions = [MerchantComplaintDB.user_id == current_user.userid]
|
||
|
||
if status:
|
||
conditions.append(MerchantComplaintDB.status == status)
|
||
|
||
if merchant_id:
|
||
conditions.append(MerchantComplaintDB.merchant_id == merchant_id)
|
||
|
||
if start_time:
|
||
conditions.append(MerchantComplaintDB.create_time >= start_time)
|
||
|
||
if end_time:
|
||
conditions.append(MerchantComplaintDB.create_time <= end_time)
|
||
|
||
# 查询总数
|
||
total = db.query(func.count(MerchantComplaintDB.id)).filter(and_(*conditions)).scalar()
|
||
|
||
# 查询数据
|
||
complaints = db.query(MerchantComplaintDB).filter(and_(*conditions)).order_by(
|
||
desc(MerchantComplaintDB.create_time)
|
||
).offset(skip).limit(limit).all()
|
||
|
||
# 获取商家名称
|
||
result = []
|
||
for complaint in complaints:
|
||
complaint_info = MerchantComplaintInfo.model_validate(complaint)
|
||
complaint_info.merchant_name = complaint.merchant.name
|
||
complaint_info.user_name = complaint.user.nickname
|
||
|
||
result.append(complaint_info)
|
||
|
||
return success_response(data={
|
||
"total": total,
|
||
"items": result
|
||
})
|
||
|
||
@router.put("/{complaint_id}")
|
||
async def update_complaint_status(
|
||
complaint_id: int,
|
||
complaint_update: MerchantComplaintUpdate,
|
||
db: Session = Depends(get_db),
|
||
_: UserDB = Depends(get_admin_user) # 确保只有管理员可以修改状态
|
||
):
|
||
"""
|
||
更新投诉状态(仅管理员可用)
|
||
"""
|
||
# 查询投诉记录
|
||
complaint = db.query(MerchantComplaintDB).filter(MerchantComplaintDB.id == complaint_id).first()
|
||
if not complaint:
|
||
raise HTTPException(status_code=404, detail="投诉记录不存在")
|
||
|
||
# 更新状态
|
||
complaint.status = complaint_update.status
|
||
db.commit()
|
||
db.refresh(complaint)
|
||
|
||
return success_response(message="投诉状态更新成功") |