modelRT/database/update_topologic.go

48 lines
1.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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
}