<%
    model_name = entity.name.capitalize()
    router_name = entity.name.lower() + '_router'
    schema_name = model_name + 'Schema'
    table_name = entity.name.lower() + 's'
%>
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from typing import List

from ..database import get_db
from ..models.${entity.name.lower()} import ${model_name}
from ..schemas.${entity.name.lower()} import ${schema_name}

${router_name} = APIRouter(
    prefix="/api/${entity.name.lower()}s",
    tags=["${entity.name.capitalize()}管理"]
)

@${router_name}.get("/", response_model=List[${schema_name}])
def get_${entity.name.lower()}_list(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
    """
    获取${entity.name.capitalize()}列表
    """
    return db.query(${model_name}).offset(skip).limit(limit).all()

@${router_name}.post("/", response_model=${schema_name})
def create_${entity.name.lower()}(${entity.name.lower()}: ${schema_name}, db: Session = Depends(get_db)):
    """
    创建${entity.name.capitalize()}
    """
    db_${entity.name.lower()} = ${model_name}(**${entity.name.lower()}.dict())
    db.add(db_${entity.name.lower()})
    db.commit()
    db.refresh(db_${entity.name.lower()})
    return db_${entity.name.lower()}

@${router_name}.get("/{${entity.name.lower()}_id}", response_model=${schema_name})
def get_${entity.name.lower()}(${entity.name.lower()}_id: int, db: Session = Depends(get_db)):
    """
    获取单个${entity.name.capitalize()}
    """
    ${entity.name.lower()} = db.query(${model_name}).filter(${model_name}.id == ${entity.name.lower()}_id).first()
    if not ${entity.name.lower()}:
        raise HTTPException(status_code=404, detail="${entity.name.capitalize()} not found")
    return ${entity.name.lower()}

@${router_name}.put("/{${entity.name.lower()}_id}", response_model=${schema_name})
def update_${entity.name.lower()}(${entity.name.lower()}_id: int, ${entity.name.lower()}: ${schema_name}, db: Session = Depends(get_db)):
    """
    更新${entity.name.capitalize()}
    """
    db_${entity.name.lower()} = db.query(${model_name}).filter(${model_name}.id == ${entity.name.lower()}_id).first()
    if not db_${entity.name.lower()}:
        raise HTTPException(status_code=404, detail="${entity.name.capitalize()} not found")
    % for field in entity.fields:
    setattr(db_${entity.name.lower()}, "${field['name']}", ${entity.name.lower()}.${field['name']})
    % endfor
    db.commit()
    db.refresh(db_${entity.name.lower()})
    return db_${entity.name.lower()}

@${router_name}.delete("/{${entity.name.lower()}_id}")
def delete_${entity.name.lower()}(${entity.name.lower()}_id: int, db: Session = Depends(get_db)):
    """
    删除${entity.name.capitalize()}
    """
    ${entity.name.lower()} = db.query(${model_name}).filter(${model_name}.id == ${entity.name.lower()}_id).first()
    if not ${entity.name.lower()}:
        raise HTTPException(status_code=404, detail="${entity.name.capitalize()} not found")
    db.delete(${entity.name.lower()})
    db.commit()
    return {"message": "${entity.name.capitalize()} deleted successfully"}