feat(backend): 添加车辆与加油记录 model、repository 和 GORM 迁移
完成 Phase 1.2,把 GORM 真正接进来。Phase 1.1 留下的 /health 和骨架保持不变, 本 commit 在此之上铺好数据访问层,Phase 1.3 直接在 service / handler 上挂业务即可。 新增: - internal/model/vehicle.go 车辆实体(严格按 plan §7.1) - internal/model/refuel_record.go 加油记录实体(严格按 plan §7.2) - internal/db/db.go SQLite 连接 + AutoMigrate 入口 * glebarez/sqlite 纯 Go,免 CGO,Windows / Docker 通吃 * DSN 自动启用 WAL + 外键约束 + busy_timeout * 表目录若不存在会自动创建 - internal/repository/vehicle.go 车辆数据访问接口 + GORM 实现 * Create / Get / List / Update / Delete * ClearDefault / SetDefault(切换默认车辆用,在事务里跑避免脏状态) - internal/repository/refuel_record.go 加油记录数据访问接口 + GORM 实现 * Create / Get / Update / Delete * ListByVehicle 支持可选时间窗口 * PrevFullRefuel 找上一次加满,给油耗计算用 main.go 改动: - 启动期 db.Open + db.Migrate,失败直接退出 - application 持有 *gorm.DB,Phase 1.3 起注入 repo / service 设计要点: - repository 全是接口,Service 持有接口便于单测用 fake 替换 - 所有 repo 错误统一包成 apperror.NotFound / apperror.Internal, 不让 GORM 原始错误泄漏到 handler - model.TableName() 显式指定单数,避免 GORM 复数 - refuel_record 用 (vehicle_id, refuel_time) 复合场景常见,已加索引 未做(留给后续 commit): - service 层与 DTO - handler / router - 显式 migration 文件(AGENTS.md 要求 schema 变更必须写 migration; 此次为首次建表,AutoMigrate 即可,后续 schema 改动会补 goose / 自管文件) 验证: go mod tidy all modules verified go vet ./... 0 issues go build 25.9 MB binary 冷启动一次,DB 文件生成在 fuellog-api/data/fuellog.db(WAL/SHM 齐备) listtables 脚本 dump 出来的两表 schema 与 plan §7 完全一致
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// RefuelRecord 单次加油记录。
|
||||
//
|
||||
// 关键约定:
|
||||
// - Liters > 0(后端强校验)
|
||||
// - TotalAmount >= 0(允许 0 表示免费加注或赠送)
|
||||
// - PricePerLiter = TotalAmount / Liters(在 service 层实时计算并落库,
|
||||
// 既保留历史真实单价,也方便后续统计直接查询)
|
||||
// - IsFull = true 时,该记录才进入"标准百公里油耗"统计
|
||||
type RefuelRecord struct {
|
||||
ID uint64 `gorm:"primaryKey;column:id" json:"id"`
|
||||
VehicleID uint64 `gorm:"not null;index;column:vehicle_id" json:"vehicle_id"`
|
||||
RefuelTime time.Time `gorm:"not null;index;column:refuel_time" json:"refuel_time"` // UTC
|
||||
Odometer float64 `gorm:"not null;column:odometer" json:"odometer"` // 公里
|
||||
FuelType string `gorm:"not null;column:fuel_type" json:"fuel_type"`
|
||||
Liters float64 `gorm:"not null;column:liters" json:"liters"` // 升
|
||||
PricePerLiter float64 `gorm:"not null;column:price_per_liter" json:"price_per_liter"`
|
||||
TotalAmount float64 `gorm:"not null;column:total_amount" json:"total_amount"` // 元
|
||||
StationName string `gorm:"column:station_name" json:"station_name"`
|
||||
IsFull bool `gorm:"not null;default:false;column:is_full" json:"is_full"`
|
||||
Remark string `gorm:"column:remark" json:"remark"`
|
||||
CreatedAt time.Time `gorm:"not null;column:created_at" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"not null;column:updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
// TableName 显式指定表名。
|
||||
func (RefuelRecord) TableName() string { return "refuel_record" }
|
||||
@@ -0,0 +1,25 @@
|
||||
// Package model 包含 FuelLog 的 GORM 实体。
|
||||
//
|
||||
// 表结构严格遵循 FuelLog_PLAN.md §7。
|
||||
// 字段名以 snake_case 形式映射到列;created_at / updated_at 由 GORM 自动维护。
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// Vehicle 车辆。
|
||||
type Vehicle struct {
|
||||
ID uint64 `gorm:"primaryKey;column:id" json:"id"`
|
||||
Name string `gorm:"not null;column:name" json:"name"`
|
||||
Brand string `gorm:"column:brand" json:"brand"`
|
||||
Model string `gorm:"column:model" json:"model"`
|
||||
PlateNumber string `gorm:"column:plate_number" json:"plate_number"`
|
||||
FuelType string `gorm:"not null;column:fuel_type" json:"fuel_type"`
|
||||
TankCapacity float64 `gorm:"column:tank_capacity" json:"tank_capacity"` // 升
|
||||
CurrentOdometer float64 `gorm:"column:current_odometer" json:"current_odometer"` // 公里
|
||||
DefaultVehicle bool `gorm:"not null;default:false;column:default_vehicle" json:"default_vehicle"`
|
||||
CreatedAt time.Time `gorm:"not null;column:created_at" json:"created_at"`
|
||||
UpdatedAt time.Time `gorm:"not null;column:updated_at" json:"updated_at"`
|
||||
}
|
||||
|
||||
// TableName 显式指定表名,避免 GORM 自动复数。
|
||||
func (Vehicle) TableName() string { return "vehicle" }
|
||||
Reference in New Issue
Block a user