54 lines
1.5 KiB
Go
54 lines
1.5 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"
|
|
)
|
|
|
|
// UpdateComponentIntoDB define update component info of the circuit diagram into DB
|
|
func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfos []network.ComponentUpdateInfo) error {
|
|
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
|
|
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,
|
|
}
|
|
result := tx.Model(&orm.Component{}).WithContext(cancelCtx).Updates(&componentInfo)
|
|
if result.Error != nil || result.RowsAffected == 0 {
|
|
err := result.Error
|
|
if result.RowsAffected == 0 {
|
|
err = fmt.Errorf("%w:please check update component conditions", constant.ErrUpdateRowZero)
|
|
}
|
|
return fmt.Errorf("update component info failed:%w", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|