chore: 并入 iotlcm-worker-svr 历史到 worker-svr/(原提交 96f0f4e)
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package simstatus
|
||||
|
||||
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"
|
||||
)
|
||||
|
||||
// Result is the database projection of a successful status query.
|
||||
type Result struct {
|
||||
CardID int64
|
||||
CardStatus int
|
||||
}
|
||||
|
||||
type Repository struct{}
|
||||
|
||||
func NewRepository() *Repository { return &Repository{} }
|
||||
|
||||
// ApplyBatch updates all successful results in one SQL statement. The status
|
||||
// timestamp is written only for cards whose upstream query succeeded.
|
||||
func (r *Repository) ApplyBatch(ctx context.Context, results []Result) error {
|
||||
if len(results) == 0 {
|
||||
return nil
|
||||
}
|
||||
ids := 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("status result has invalid cardId %d", result.CardID)
|
||||
}
|
||||
fmt.Fprintf(&statement, "WHEN %d THEN %d ", result.CardID, result.CardStatus)
|
||||
ids = append(ids, result.CardID)
|
||||
}
|
||||
statement.WriteString("END")
|
||||
_, err := dao.TSimCardInfo.Ctx(ctx).
|
||||
WhereIn(dao.TSimCardInfo.Columns().Id, ids).
|
||||
Data(do.TSimCardInfo{
|
||||
BossCardStatus: gdb.Raw(statement.String()),
|
||||
StatusSyncTime: gtime.Now(),
|
||||
}).Update()
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user