142 lines
5.0 KiB
Python
142 lines
5.0 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from app.models.database import get_db
|
|
from app.models.user import UserDB
|
|
from app.api.deps import get_current_user
|
|
from app.core.response import success_response, error_response, ResponseModel
|
|
from app.models.feedback import (
|
|
FeedbackCommunityApplyDB,
|
|
FeedbackPartnerApplyDB,
|
|
CommunityApplyCreate,
|
|
CommunityApplyInfo,
|
|
PartnerApplyCreate,
|
|
PartnerApplyInfo
|
|
)
|
|
from typing import List
|
|
import logging
|
|
from app.core.config import settings
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/community-apply", response_model=ResponseModel)
|
|
async def apply_community(
|
|
apply_data: CommunityApplyCreate,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""申请小区开通"""
|
|
try:
|
|
|
|
if apply_data.user_id>0:
|
|
current_user = db.query(UserDB).filter(UserDB.userid == apply_data.user_id).first()
|
|
else:
|
|
current_user = get_current_user(db)
|
|
|
|
# 创建申请记录
|
|
new_apply = FeedbackCommunityApplyDB(
|
|
user_id=current_user.userid,
|
|
community_name=apply_data.community_name,
|
|
community_address=apply_data.community_address
|
|
)
|
|
|
|
db.add(new_apply)
|
|
db.commit()
|
|
db.refresh(new_apply)
|
|
|
|
# 发送企业微信通知
|
|
try:
|
|
from app.core.wecombot import WecomBot
|
|
wecom_bot = WecomBot()
|
|
message = f"""📢 新的小区开通申请
|
|
|
|
> 申请人: {current_user.nickname}
|
|
> 联系电话: {current_user.phone}
|
|
> 小区名称: {apply_data.community_name}
|
|
> 小区地址: {apply_data.community_address}
|
|
> 申请时间: {new_apply.create_time}
|
|
"""
|
|
await wecom_bot.send_markdown(message,webhook_url=settings.FEEDBACK_NEED_WECOM_BOT_WEBHOOK_URL)
|
|
except Exception as e:
|
|
logging.error(f"发送企业微信通知失败: {str(e)}")
|
|
|
|
return success_response(message="申请提交成功,我们会尽快处理", data=CommunityApplyInfo.model_validate(new_apply))
|
|
|
|
except Exception as e:
|
|
db.rollback()
|
|
logging.exception(f"申请小区开通失败: {str(e)}")
|
|
return error_response(code=500, message="申请提交失败,请稍后重试")
|
|
|
|
@router.post("/partner-apply", response_model=ResponseModel)
|
|
async def apply_partner(
|
|
apply_data: PartnerApplyCreate,
|
|
db: Session = Depends(get_db),
|
|
):
|
|
"""申请成为合伙人"""
|
|
if apply_data.user_id>0:
|
|
current_user = db.query(UserDB).filter(UserDB.userid == apply_data.user_id).first()
|
|
else:
|
|
current_user = get_current_user(db)
|
|
|
|
try:
|
|
# 创建申请记录
|
|
new_apply = FeedbackPartnerApplyDB(
|
|
user_id=current_user.userid,
|
|
name=apply_data.name,
|
|
phone=apply_data.phone,
|
|
type=apply_data.type_display,
|
|
service_target=apply_data.service_target
|
|
)
|
|
|
|
db.add(new_apply)
|
|
db.commit()
|
|
db.refresh(new_apply)
|
|
|
|
# 发送企业微信通知
|
|
try:
|
|
from app.core.wecombot import WecomBot
|
|
wecom_bot = WecomBot()
|
|
message = f"""📢 新的合伙人申请
|
|
|
|
> 申请类型: {apply_data.type_display}
|
|
> 申请人: {apply_data.name}
|
|
> 联系电话: {apply_data.phone}
|
|
> 服务对象/区域: {apply_data.service_target}
|
|
> 申请时间: {new_apply.create_time}
|
|
"""
|
|
await wecom_bot.send_markdown(message,webhook_url=settings.FEEDBACK_NEED_WECOM_BOT_WEBHOOK_URL)
|
|
except Exception as e:
|
|
logging.error(f"发送企业微信通知失败: {str(e)}")
|
|
|
|
return success_response(message="申请提交成功,我们会尽快联系您", data=PartnerApplyInfo.model_validate(new_apply))
|
|
|
|
except Exception as e:
|
|
db.rollback()
|
|
logging.exception(f"申请合伙人失败: {str(e)}")
|
|
return error_response(code=500, message="申请提交失败,请稍后重试")
|
|
|
|
@router.get("/my-community-applies", response_model=ResponseModel)
|
|
async def get_my_community_applies(
|
|
db: Session = Depends(get_db),
|
|
current_user: UserDB = Depends(get_current_user)
|
|
):
|
|
"""获取我的小区开通申请列表"""
|
|
applies = db.query(FeedbackCommunityApplyDB).filter(
|
|
FeedbackCommunityApplyDB.user_id == current_user.userid
|
|
).order_by(FeedbackCommunityApplyDB.create_time.desc()).all()
|
|
|
|
return success_response(data={
|
|
"items": [CommunityApplyInfo.model_validate(apply) for apply in applies]
|
|
})
|
|
|
|
@router.get("/my-partner-applies", response_model=ResponseModel)
|
|
async def get_my_partner_applies(
|
|
db: Session = Depends(get_db),
|
|
current_user: UserDB = Depends(get_current_user)
|
|
):
|
|
"""获取我的合伙人申请列表"""
|
|
applies = db.query(FeedbackPartnerApplyDB).filter(
|
|
FeedbackPartnerApplyDB.user_id == current_user.userid
|
|
).order_by(FeedbackPartnerApplyDB.create_time.desc()).all()
|
|
|
|
return success_response(data={
|
|
"items": [PartnerApplyInfo.model_validate(apply) for apply in applies]
|
|
}) |