48 lines
1.7 KiB
Go
48 lines
1.7 KiB
Go
// Package database define database operation functions
|
||
package database
|
||
|
||
import (
|
||
"context"
|
||
"fmt"
|
||
"time"
|
||
|
||
"modelRT/constant"
|
||
"modelRT/network"
|
||
"modelRT/orm"
|
||
|
||
"gorm.io/gorm"
|
||
)
|
||
|
||
// UpdateTopologicIntoDB define update topologic info of the circuit diagram query by pageID and topologic info
|
||
func UpdateTopologicIntoDB(ctx context.Context, tx *gorm.DB, pageID int64, changeInfo network.TopologicUUIDChangeInfos) error {
|
||
var result *gorm.DB
|
||
|
||
cancelCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||
defer cancel()
|
||
|
||
switch changeInfo.ChangeType {
|
||
case constant.UUIDFromChangeType:
|
||
result = tx.WithContext(cancelCtx).Model(orm.Topologic{}).Where("page_id = ? nad uuid_from = ? and uuid_to = ?", pageID, changeInfo.OldUUIDFrom, changeInfo.OldUUIDTo).Updates(orm.Topologic{UUIDFrom: changeInfo.NewUUIDFrom})
|
||
case constant.UUIDToChangeType:
|
||
result = tx.WithContext(cancelCtx).Model(orm.Topologic{}).Where("page_id = ? nad uuid_from = ? and uuid_to = ?", pageID, changeInfo.OldUUIDFrom, changeInfo.OldUUIDTo).Updates(orm.Topologic{UUIDTo: changeInfo.NewUUIDTo})
|
||
if result.Error != nil || result.RowsAffected == 0 {
|
||
return fmt.Errorf("insert new topologic link failed:%w", result.Error)
|
||
}
|
||
case constant.UUIDAddChangeType:
|
||
topologic := orm.Topologic{
|
||
PageID: pageID,
|
||
Flag: changeInfo.Flag,
|
||
UUIDFrom: changeInfo.NewUUIDFrom,
|
||
UUIDTo: changeInfo.OldUUIDFrom,
|
||
Comment: changeInfo.Comment,
|
||
}
|
||
result = tx.WithContext(cancelCtx).Create(&topologic)
|
||
}
|
||
|
||
// TODO 测试下RowsAffected==0时候,result.Error是否有值,是否会带来空指针问题
|
||
if result.Error != nil || result.RowsAffected == 0 {
|
||
return fmt.Errorf("insert or update topologic link failed:%w", result.Error)
|
||
}
|
||
return nil
|
||
}
|