deliveryman-api/app/api/endpoints/community_profit_sharing.py
2025-03-10 16:14:13 +08:00

206 lines
8.0 KiB
Python

from fastapi import APIRouter, Depends, HTTPException, Query, Path
from sqlalchemy.orm import Session
from typing import List, Optional
from pydantic import BaseModel, Field, field_validator
from app.models.database import get_db
from app.models.community_profit_sharing import CommunityProfitSharing
from app.models.community import CommunityDB
from app.api.deps import get_current_user, get_admin_user
from app.models.user import UserDB
from datetime import datetime
from decimal import Decimal
from app.core.response import error_response, success_response
router = APIRouter()
# 请求和响应模型
class CommunityProfitSharingCreate(BaseModel):
community_id: int = Field(..., ge=1, description="社区ID")
platform_rate: float = Field(..., ge=0, le=100, description="平台分润比例(%)")
partner_rate: float = Field(..., ge=0, le=100, description="合伙人分润比例(%)")
admin_rate: float = Field(..., ge=0, le=100, description="管理员分润比例(%)")
delivery_rate: float = Field(..., ge=0, le=100, description="配送员分润比例(%)")
class CommunityProfitSharingUpdate(BaseModel):
platform_rate: float = Field(..., ge=0, le=100, description="平台分润比例(%)")
partner_rate: float = Field(..., ge=0, le=100, description="合伙人分润比例(%)")
admin_rate: float = Field(..., ge=0, le=100, description="管理员分润比例(%)")
delivery_rate: float = Field(..., ge=0, le=100, description="配送员分润比例(%)")
class CommunityProfitSharingResponse(BaseModel):
id: int
community_id: int
platform_rate: float
partner_rate: float
admin_rate: float
delivery_rate: float
create_time: datetime
update_time: datetime
# 包含社区名称
community_name: Optional[str] = None
class Config:
from_attributes = True
# 创建社区分润
@router.post("/", response_model=CommunityProfitSharingResponse)
async def create_community_profit_sharing(
profit_sharing: CommunityProfitSharingCreate,
db: Session = Depends(get_db),
current_user: UserDB = Depends(get_admin_user)
):
"""创建社区分润"""
# 检查社区是否存在
community = db.query(CommunityDB).filter(CommunityDB.id == profit_sharing.community_id).first()
if not community:
return error_response(code=404, message="社区不存在")
# 检查该社区是否已有分润记录
existing_profit_sharing = db.query(CommunityProfitSharing).filter(
CommunityProfitSharing.community_id == profit_sharing.community_id
).first()
if existing_profit_sharing:
return error_response(code=400, message="该社区已有分润记录,请使用更新接口")
# 检查分润比例之和是否等于100%
total_rate = profit_sharing.platform_rate + profit_sharing.partner_rate + profit_sharing.admin_rate + profit_sharing.delivery_rate
if total_rate != 100:
return error_response(code=400, message="分润比例之和必须等于100%")
# 创建新分润记录
new_profit_sharing = CommunityProfitSharing(
community_id=profit_sharing.community_id,
platform_rate=profit_sharing.platform_rate,
partner_rate=profit_sharing.partner_rate,
admin_rate=profit_sharing.admin_rate,
delivery_rate=profit_sharing.delivery_rate
)
db.add(new_profit_sharing)
db.commit()
db.refresh(new_profit_sharing)
# 添加社区名称
result = CommunityProfitSharingResponse.model_validate(new_profit_sharing)
result.community_name = community.name
return success_response(data=result)
# 获取所有社区分润
@router.get("/", response_model=List[CommunityProfitSharingResponse])
async def get_community_profit_sharings(
db: Session = Depends(get_db),
current_user: UserDB = Depends(get_admin_user),
skip: int = Query(0, ge=0),
limit: int = Query(100, ge=1, le=100)
):
"""获取所有社区分润"""
profit_sharings = db.query(CommunityProfitSharing).offset(skip).limit(limit).all()
# 添加社区名称
results = []
for profit_sharing in profit_sharings:
community = db.query(CommunityDB).filter(CommunityDB.id == profit_sharing.community_id).first()
result = CommunityProfitSharingResponse.model_validate(profit_sharing)
result.community_name = community.name if community else None
results.append(result)
return success_response(data=results)
# 获取特定社区的分润
@router.get("/community/{community_id}", response_model=CommunityProfitSharingResponse)
async def get_community_profit_sharing(
community_id: int = Path(..., ge=1),
db: Session = Depends(get_db),
current_user: UserDB = Depends(get_admin_user)
):
"""获取特定社区的分润"""
# 检查社区是否存在
community = db.query(CommunityDB).filter(CommunityDB.id == community_id).first()
if not community:
return error_response(code=404, message="社区不存在")
profit_sharing = db.query(CommunityProfitSharing).filter(
CommunityProfitSharing.community_id == community_id
).first()
if not profit_sharing:
return error_response(code=404, message="该社区暂无分润记录")
# 添加社区名称
result = CommunityProfitSharingResponse.model_validate(profit_sharing)
result.community_name = community.name
return success_response(data=result)
# 更新社区分润
@router.put("/community/{community_id}", response_model=CommunityProfitSharingResponse)
async def update_community_profit_sharing(
profit_sharing: CommunityProfitSharingUpdate,
community_id: int = Path(..., ge=1),
db: Session = Depends(get_db),
current_user: UserDB = Depends(get_admin_user)
):
"""更新社区分润"""
# 检查社区是否存在
community = db.query(CommunityDB).filter(CommunityDB.id == community_id).first()
if not community:
return error_response(code=404, message="社区不存在")
# 检查该社区是否有分润记录
db_profit_sharing = db.query(CommunityProfitSharing).filter(
CommunityProfitSharing.community_id == community_id
).first()
if not db_profit_sharing:
return error_response(code=404, message="该社区暂无分润记录,请先创建")
# 检查分润比例之和是否等于100%
total_rate = profit_sharing.platform_rate + profit_sharing.partner_rate + profit_sharing.admin_rate + profit_sharing.delivery_rate
if total_rate != 100:
return error_response(code=400, message="分润比例之和必须等于100%")
# 更新分润记录
db_profit_sharing.platform_rate = profit_sharing.platform_rate
db_profit_sharing.partner_rate = profit_sharing.partner_rate
db_profit_sharing.admin_rate = profit_sharing.admin_rate
db_profit_sharing.delivery_rate = profit_sharing.delivery_rate
db_profit_sharing.update_time = datetime.now()
db.commit()
db.refresh(db_profit_sharing)
# 添加社区名称
result = CommunityProfitSharingResponse.model_validate(db_profit_sharing)
result.community_name = community.name
return success_response(data=result)
# 删除社区分润
@router.delete("/community/{community_id}", response_model=dict)
async def delete_community_profit_sharing(
community_id: int = Path(..., ge=1),
db: Session = Depends(get_db),
current_user: UserDB = Depends(get_admin_user)
):
"""删除社区分润"""
# 检查社区是否存在
community = db.query(CommunityDB).filter(CommunityDB.id == community_id).first()
if not community:
return error_response(code=404, message="社区不存在")
# 检查该社区是否有分润记录
db_profit_sharing = db.query(CommunityProfitSharing).filter(
CommunityProfitSharing.community_id == community_id
).first()
if not db_profit_sharing:
return error_response(code=404, message="该社区暂无分润记录")
# 删除分润记录
db.delete(db_profit_sharing)
db.commit()
return success_response(message="社区分润记录已删除")