61 lines
1.9 KiB
Go
61 lines
1.9 KiB
Go
// Package database define database operation functions
|
|
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"modelRT/common/errcode"
|
|
"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, componentInfo network.ComponentUpdateInfo) (string, error) {
|
|
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
defer cancel()
|
|
|
|
globalUUID, err := uuid.FromString(componentInfo.UUID)
|
|
if err != nil {
|
|
return "", fmt.Errorf("format uuid from string type failed:%w", err)
|
|
}
|
|
|
|
var component orm.Component
|
|
result := tx.Model(&orm.Component{}).WithContext(cancelCtx).Where("global_uuid = ?", globalUUID).Find(&component)
|
|
if result.Error != nil || result.RowsAffected == 0 {
|
|
err := result.Error
|
|
if result.RowsAffected == 0 {
|
|
err = fmt.Errorf("%w:please check update component conditions", errcode.ErrUpdateRowZero)
|
|
}
|
|
return "", fmt.Errorf("query component info failed:%w", err)
|
|
}
|
|
|
|
updateParams := orm.Component{
|
|
GlobalUUID: globalUUID,
|
|
GridTag: strconv.FormatInt(componentInfo.GridID, 10),
|
|
ZoneTag: strconv.FormatInt(componentInfo.ZoneID, 10),
|
|
StationTag: strconv.FormatInt(componentInfo.StationID, 10),
|
|
Tag: componentInfo.Tag,
|
|
Name: componentInfo.Name,
|
|
Context: componentInfo.Context,
|
|
Op: componentInfo.Op,
|
|
Ts: time.Now(),
|
|
}
|
|
|
|
result = tx.Model(&orm.Component{}).WithContext(cancelCtx).Where("GLOBAL_UUID = ?", component.GlobalUUID).Updates(&updateParams)
|
|
|
|
if result.Error != nil || result.RowsAffected == 0 {
|
|
err := result.Error
|
|
if result.RowsAffected == 0 {
|
|
err = fmt.Errorf("%w:please check update component conditions", errcode.ErrUpdateRowZero)
|
|
}
|
|
return "", fmt.Errorf("update component info failed:%w", err)
|
|
}
|
|
return component.GlobalUUID.String(), nil
|
|
}
|