package monthlyusage import ( "context" "fmt" "strings" "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/os/gtime" "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/dao" "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/model/do" ) // Repository applies the current usage result to the card information table. // It deliberately does not write TDengine history for this full-sync flow. type Repository struct{} type UsageResult struct { CardID int64 UsageMB float64 } func NewRepository() *Repository { return &Repository{} } func (r *Repository) ApplyBatch(ctx context.Context, results []UsageResult) error { if len(results) == 0 { return nil } cardIDs := make([]int64, 0, len(results)) var statement strings.Builder statement.WriteString("CASE " + dao.TSimCardInfo.Columns().Id + " ") for _, result := range results { if result.CardID <= 0 { return fmt.Errorf("monthly usage result has invalid cardId %d", result.CardID) } fmt.Fprintf(&statement, "WHEN %d THEN %f ", result.CardID, result.UsageMB) cardIDs = append(cardIDs, result.CardID) } statement.WriteString("END") _, err := dao.TSimCardInfo.Ctx(ctx). WhereIn(dao.TSimCardInfo.Columns().Id, cardIDs). Data(do.TSimCardInfo{ UsedFlowMb: gdb.Raw(statement.String()), FlowSyncTime: gtime.Now(), }). Update() return err }