modelRT/database/create_topologic.go

43 lines
1.2 KiB
Go

// Package database define database operation functions
package database
import (
"context"
"fmt"
"time"
"modelRT/common/errcode"
"modelRT/network"
"modelRT/orm"
"gorm.io/gorm"
)
// CreateTopologicIntoDB define create topologic info of the circuit diagram query by pageID and topologic info
func CreateTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, topologicInfos []network.TopologicUUIDCreateInfo) error {
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
var topologicSlice []orm.Topologic
for _, info := range topologicInfos {
topologicInfo := orm.Topologic{
UUIDFrom: info.UUIDFrom,
UUIDTo: info.UUIDTo,
Flag: info.Flag,
Comment: info.Comment,
}
topologicSlice = append(topologicSlice, topologicInfo)
}
result := tx.WithContext(cancelCtx).Create(&topologicSlice)
if result.Error != nil || result.RowsAffected != int64(len(topologicSlice)) {
err := result.Error
if result.RowsAffected != int64(len(topologicSlice)) {
err = fmt.Errorf("%w:please check insert topologic slice", errcode.ErrInsertRowUnexpected)
}
return fmt.Errorf("insert topologic link failed:%w", err)
}
return nil
}