58 lines
1.6 KiB
Go
58 lines
1.6 KiB
Go
// Package database define database operation functions
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"modelRT/constant"
|
|
"modelRT/network"
|
|
"modelRT/orm"
|
|
|
|
"github.com/gofrs/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CreateComponentIntoDB define create component info of the circuit diagram into DB
|
|
func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfos []network.ComponentCreateInfo) error {
|
|
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
|
|
var componentSlice []orm.Component
|
|
for _, info := range componentInfos {
|
|
globalUUID, err := uuid.FromString(info.UUID)
|
|
if err != nil {
|
|
return fmt.Errorf("format uuid from string type failed:%w", err)
|
|
}
|
|
|
|
componentInfo := orm.Component{
|
|
GlobalUUID: globalUUID,
|
|
GridID: info.GridID,
|
|
ZoneID: info.ZoneID,
|
|
StationID: info.StationID,
|
|
ComponentType: info.ComponentType,
|
|
State: info.State,
|
|
ConnectedBus: info.ConnectedBus,
|
|
Name: info.Name,
|
|
VisibleID: info.Name,
|
|
Description: info.Description,
|
|
Context: info.Context,
|
|
Comment: info.Comment,
|
|
InService: info.InService,
|
|
}
|
|
componentSlice = append(componentSlice, componentInfo)
|
|
}
|
|
|
|
result := tx.WithContext(cancelCtx).Create(&componentSlice)
|
|
|
|
if result.Error != nil || result.RowsAffected != int64(len(componentSlice)) {
|
|
err := result.Error
|
|
if result.RowsAffected != int64(len(componentSlice)) {
|
|
err = fmt.Errorf("%w:please check insert component slice", constant.ErrInsertRowUnexpected)
|
|
}
|
|
return fmt.Errorf("insert component info failed:%w", err)
|
|
}
|
|
return nil
|
|
}
|