carcost/api/models.py
yuany3721 71e11eaf30 feat: Add OIDC authentication with Authentik and refactor project structure
Backend:
- Add auth.py for JWT token verification
- Update main.py to protect all routes with auth middleware
- Remove dashboard router (frontend handles aggregation)
- Add Docker support with Dockerfile and docker-compose.yml

Frontend:
- Add OIDC authentication using oidc-client-ts with PKCE flow
- Create router.js with auth guards for automatic login/logout
- Add api.js for unified Axios instance with auth headers
- Add composables: useAuth.js, useVehicleData.js for caching
- Add views/Main.vue as main application page
- Simplify App.vue to router-view container
- Add deploy-web.sh deployment script

Documentation:
- Update AGENTS.md with new architecture and auth flow
2026-04-12 13:31:27 +08:00

87 lines
2.9 KiB
Python

"""
SQLAlchemy 数据库模型定义
"""
from sqlalchemy import (
Column,
Integer,
String,
DateTime,
Boolean,
Text,
Numeric,
Date,
ForeignKey,
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from datetime import datetime
# 定义基类
Base = declarative_base()
class Vehicle(Base):
"""车辆表"""
__tablename__ = "vehicles"
id = Column(Integer, primary_key=True, autoincrement=True)
name = Column(String(50), nullable=False)
purchase_date = Column(Date, nullable=True)
initial_mileage = Column(Integer, default=0, nullable=False)
is_deleted = Column(Boolean, default=False) # 软删除标记
created_at = Column(DateTime, default=datetime.now)
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
# 关联关系
fuel_records = relationship("FuelRecord", back_populates="vehicle")
costs = relationship("Cost", back_populates="vehicle")
class FuelRecord(Base):
"""加油记录表"""
__tablename__ = "fuel_records"
id = Column(Integer, primary_key=True, autoincrement=True)
vehicle_id = Column(Integer, ForeignKey("vehicles.id"), nullable=False)
date = Column(Date, nullable=False)
mileage = Column(Integer, nullable=False)
fuel_amount = Column(Numeric(10, 2), nullable=False)
fuel_price = Column(Numeric(10, 2), nullable=True)
display_cost = Column(Numeric(10, 2), nullable=True) # 机显总价(加油机显示金额)
actual_cost = Column(Numeric(10, 2), nullable=False) # 实付金额(优惠后实际支付)
is_full_tank = Column(Boolean, default=True)
notes = Column(Text, default="")
is_deleted = Column(Boolean, default=False) # 软删除标记
created_at = Column(DateTime, default=datetime.now)
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
# 关联关系
vehicle = relationship("Vehicle", back_populates="fuel_records")
class Cost(Base):
"""费用记录表"""
__tablename__ = "costs"
id = Column(Integer, primary_key=True, autoincrement=True)
vehicle_id = Column(Integer, ForeignKey("vehicles.id"), nullable=False)
date = Column(Date, nullable=False)
type = Column(
String(20), nullable=False
) # 保养/维修/保险/停车/洗车/违章/过路费/其他
amount = Column(Numeric(10, 2), nullable=False)
mileage = Column(Integer, nullable=True)
notes = Column(Text, default="")
is_installment = Column(Boolean, default=False) # 是否分期/分摊
installment_months = Column(Integer, default=12) # 分摊月数
is_deleted = Column(Boolean, default=False) # 软删除标记
created_at = Column(DateTime, default=datetime.now)
updated_at = Column(DateTime, default=datetime.now, onupdate=datetime.now)
# 关联关系
vehicle = relationship("Vehicle", back_populates="costs")