refactor(go): simplify daily mileage table key

This commit is contained in:
lingniu
2026-07-03 07:16:49 +08:00
parent e89c9fd98f
commit 04bafb4cd6
6 changed files with 24 additions and 19 deletions

View File

@@ -33,7 +33,6 @@ type MetricRow struct {
FirstTotalMileageKM *float64 `json:"first_total_mileage_km,omitempty"`
LatestTotalMileageKM *float64 `json:"latest_total_mileage_km,omitempty"`
SampleCount int64 `json:"sample_count"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
@@ -61,7 +60,6 @@ func (r *MetricRepository) Query(ctx context.Context, query MetricQuery) ([]Metr
for rows.Next() {
var row MetricRow
var statDate scanDate
var createdAt scanDateTime
var updatedAt scanDateTime
var first sql.NullFloat64
var latest sql.NullFloat64
@@ -73,13 +71,11 @@ func (r *MetricRepository) Query(ctx context.Context, query MetricQuery) ([]Metr
&first,
&latest,
&row.SampleCount,
&createdAt,
&updatedAt,
); err != nil {
return nil, err
}
row.StatDate = statDate.String
row.CreatedAt = createdAt.String
row.UpdatedAt = updatedAt.String
if first.Valid {
row.FirstTotalMileageKM = &first.Float64
@@ -123,7 +119,7 @@ func normalizeMetricQuery(query MetricQuery) MetricQuery {
func buildMetricSQL(query MetricQuery) (string, []any) {
where, args := buildMetricWhere(query)
sqlText := `SELECT vin, stat_date, protocol, daily_mileage_km, first_total_mileage_km, latest_total_mileage_km, sample_count, created_at, updated_at FROM vehicle_daily_mileage`
sqlText := `SELECT vin, stat_date, protocol, daily_mileage_km, first_total_mileage_km, latest_total_mileage_km, sample_count, updated_at FROM vehicle_daily_mileage`
if len(where) > 0 {
sqlText += " WHERE " + strings.Join(where, " AND ")
}