2024-12-05 14:57:23 +08:00
|
|
|
// Package database define database operation functions
|
|
|
|
|
package database
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
2025-01-10 16:57:29 +08:00
|
|
|
"strconv"
|
2024-12-05 14:57:23 +08:00
|
|
|
"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
|
2024-12-30 16:39:11 +08:00
|
|
|
func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo network.ComponentUpdateInfo) (int64, error) {
|
2024-12-05 14:57:23 +08:00
|
|
|
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
2024-12-30 16:39:11 +08:00
|
|
|
globalUUID, err := uuid.FromString(componentInfo.UUID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return -1, fmt.Errorf("format uuid from string type failed:%w", err)
|
|
|
|
|
}
|
2024-12-05 14:57:23 +08:00
|
|
|
|
2025-01-10 16:57:29 +08:00
|
|
|
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", constant.ErrUpdateRowZero)
|
|
|
|
|
}
|
|
|
|
|
return -1, fmt.Errorf("query component info failed:%w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateParams := orm.Component{
|
2024-12-30 16:39:11 +08:00
|
|
|
GlobalUUID: globalUUID,
|
2025-01-10 16:57:29 +08:00
|
|
|
GridID: strconv.FormatInt(componentInfo.GridID, 10),
|
|
|
|
|
ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10),
|
|
|
|
|
StationID: strconv.FormatInt(componentInfo.StationID, 10),
|
2024-12-30 16:39:11 +08:00
|
|
|
PageID: componentInfo.PageID,
|
|
|
|
|
Tag: componentInfo.Tag,
|
|
|
|
|
ComponentType: componentInfo.ComponentType,
|
|
|
|
|
Name: componentInfo.Name,
|
|
|
|
|
Context: componentInfo.Context,
|
|
|
|
|
Op: componentInfo.Op,
|
|
|
|
|
Ts: time.Now(),
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-10 16:57:29 +08:00
|
|
|
result = tx.Model(&orm.Component{}).WithContext(cancelCtx).Where("id = ?", component.ID).Updates(&updateParams)
|
|
|
|
|
|
2024-12-30 16:39:11 +08:00
|
|
|
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)
|
2024-12-05 14:57:23 +08:00
|
|
|
}
|
2024-12-30 16:39:11 +08:00
|
|
|
return -1, fmt.Errorf("update component info failed:%w", err)
|
2024-12-05 14:57:23 +08:00
|
|
|
}
|
2024-12-30 16:39:11 +08:00
|
|
|
return component.ID, nil
|
2024-12-05 14:57:23 +08:00
|
|
|
}
|