fix bug of create、delete、update handler

This commit is contained in:
douxu 2025-01-10 16:57:29 +08:00
parent c1691d4da2
commit d89bf83f8b
11 changed files with 87 additions and 135 deletions

View File

@ -4,6 +4,7 @@ package database
import ( import (
"context" "context"
"fmt" "fmt"
"strconv"
"time" "time"
"modelRT/constant" "modelRT/constant"
@ -26,9 +27,9 @@ func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo
component := orm.Component{ component := orm.Component{
GlobalUUID: globalUUID, GlobalUUID: globalUUID,
GridID: componentInfo.GridID, GridID: strconv.FormatInt(componentInfo.GridID, 10),
ZoneID: componentInfo.ZoneID, ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10),
StationID: componentInfo.StationID, StationID: strconv.FormatInt(componentInfo.StationID, 10),
PageID: componentInfo.PageID, PageID: componentInfo.PageID,
Tag: componentInfo.Tag, Tag: componentInfo.Tag,
ComponentType: componentInfo.ComponentType, ComponentType: componentInfo.ComponentType,
@ -39,7 +40,6 @@ func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo
} }
result := tx.WithContext(cancelCtx).Create(&component) result := tx.WithContext(cancelCtx).Create(&component)
if result.Error != nil || result.RowsAffected == 0 { if result.Error != nil || result.RowsAffected == 0 {
err := result.Error err := result.Error
if result.RowsAffected == 0 { if result.RowsAffected == 0 {

View File

@ -4,6 +4,7 @@ package database
import ( import (
"context" "context"
"fmt" "fmt"
"strconv"
"time" "time"
"modelRT/constant" "modelRT/constant"
@ -24,11 +25,21 @@ func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo
return -1, fmt.Errorf("format uuid from string type failed:%w", err) return -1, fmt.Errorf("format uuid from string type failed:%w", err)
} }
component := orm.Component{ 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{
GlobalUUID: globalUUID, GlobalUUID: globalUUID,
GridID: componentInfo.GridID, GridID: strconv.FormatInt(componentInfo.GridID, 10),
ZoneID: componentInfo.ZoneID, ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10),
StationID: componentInfo.StationID, StationID: strconv.FormatInt(componentInfo.StationID, 10),
PageID: componentInfo.PageID, PageID: componentInfo.PageID,
Tag: componentInfo.Tag, Tag: componentInfo.Tag,
ComponentType: componentInfo.ComponentType, ComponentType: componentInfo.ComponentType,
@ -38,7 +49,8 @@ func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo
Ts: time.Now(), Ts: time.Now(),
} }
result := tx.Model(&orm.Component{}).WithContext(cancelCtx).Updates(&component) result = tx.Model(&orm.Component{}).WithContext(cancelCtx).Where("id = ?", component.ID).Updates(&updateParams)
if result.Error != nil || result.RowsAffected == 0 { if result.Error != nil || result.RowsAffected == 0 {
err := result.Error err := result.Error
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
@ -46,6 +58,5 @@ func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo
} }
return -1, fmt.Errorf("update component info failed:%w", err) return -1, fmt.Errorf("update component info failed:%w", err)
} }
return component.ID, nil return component.ID, nil
} }

View File

@ -1,52 +1,37 @@
{ {
"page_id":1, "page_id": 1,
"topologics":[ "topologics": [
{ {
"uuid_from":"12311-111", "uuid_from": "70c190f2-8a60-42a9-b143-ec5f87e0aa6b",
"uuid_to":"12311-114" "uuid_to": "70c190f2-8a75-42a9-b166-ec5f87e0aa6b"
}, },
{ {
"uuid_from":"12311-115", "uuid_from": "70c190f2-8a75-42a9-b166-ec5f87e0aa6b",
"uuid_to":"12311-116" "uuid_to": "70c200f2-8a75-42a9-c166-bf5f87e0aa6b"
} }
], ],
"free_vertexs":[ "component_infos": [
"12311-111",
"12311-112",
"12311-115"
],
"component_infos":[
{ {
"grid_id":1, "component_type": 3,
"zone":1, "grid_id": 1,
"station_id":1, "name": "demo21",
"uuid":"12311-114", "page_id": 1,
"name":"母线 1", "station_id": 1,
"component_type":1, "uuid": "70c190f2-8a75-42a9-b166-ec5f87e0aa6b",
"in_service":true, "zone_id": 1,
"connected_bus":1, "context":"{\"top\":\"7.4\",\"left\":\"3.2\",\"right\":\"3.5\",\"buttom\":\"1.2\"}",
"state":1, "params": "{\"anchor_i\":false,\"anchor_v\":true,\"context\":\"{\\\"top\\\": \\\"7.4\\\", \\\"left\\\": \\\"3.2\\\", \\\"right\\\": \\\"3.5\\\", \\\"buttom\\\": \\\"1.2\\\"}\",\"name\":\"demo21\",\"oi_alarm\":110,\"op\":1,\"ov_alarm\":110,\"resistance\":100,\"ts\":\"2021-01-01T08:00:00+08:00\",\"ui_alarm\":90,\"uv_alarm\":90}"
"visible_id":"",
"description":"",
"context":"",
"comment":"",
"params":""
}, },
{ {
"grid_id":1, "component_type": 3,
"zone":1, "grid_id": 1,
"station_id":1, "name": "demo22",
"uuid":"12311-116", "page_id": 1,
"name":"发电机 1", "station_id": 1,
"component_type":1, "uuid": "70c200f2-8a75-42a9-c166-bf5f87e0aa6b",
"in_service":true, "zone_id": 1,
"connected_bus":1, "context":"{\"top\":\"7.4\",\"left\":\"3.2\",\"right\":\"3.5\",\"buttom\":\"1.2\"}",
"state":1, "params": "{\"anchor_i\":false,\"anchor_v\":true,\"context\":\"{\\\"top\\\": \\\"7.4\\\", \\\"left\\\": \\\"3.2\\\", \\\"right\\\": \\\"3.5\\\", \\\"buttom\\\": \\\"1.2\\\"}\",\"name\":\"demo22\",\"oi_alarm\":110,\"op\":1,\"ov_alarm\":110,\"resistance\":100,\"ts\":\"2021-01-01T08:00:00+08:00\",\"ui_alarm\":90,\"uv_alarm\":90}"
"visible_id":"",
"description":"",
"context":"",
"comment":"",
"params":""
} }
] ]
} }

View File

@ -2,27 +2,22 @@
"page_id":1, "page_id":1,
"topologics":[ "topologics":[
{ {
"uuid_from":"12311-111", "uuid_from": "70c190f2-8a75-42a9-b166-ec5f87e0aa6b",
"uuid_to":"12311-114" "uuid_to": "70c200f2-8a75-42a9-c166-bf5f87e0aa6b"
}, },
{ {
"uuid_from":"12311-115", "uuid_from": "70c190f2-8a60-42a9-b143-ec5f87e0aa6b",
"uuid_to":"12311-116" "uuid_to": "70c190f2-8a75-42a9-b166-ec5f87e0aa6b"
} }
], ],
"free_vertexs":[
"12311-111",
"12311-112",
"12311-115"
],
"component_infos":[ "component_infos":[
{ {
"uuid":"12311-114", "uuid":"70c200f2-8a75-42a9-c166-bf5f87e0aa6b",
"component_type":1 "component_type":3
}, },
{ {
"uuid":"12311-116", "uuid":"70c190f2-8a75-42a9-b166-ec5f87e0aa6b",
"component_type":1 "component_type":3
} }
] ]
} }

View File

@ -3,63 +3,23 @@
"topologics":[ "topologics":[
{ {
"change_type":1, "change_type":1,
"old_uuid_from":"12311-111", "old_uuid_from":"70c190f2-8a75-42a9-b166-ec5f87e0aa6b",
"old_uuid_to":"12311-113", "old_uuid_to":"70c200f2-8a75-42a9-c166-bf5f87e0aa6b",
"new_uuid_from":"12311-111", "new_uuid_from":"e32bc0be-67f4-4d79-a5da-eaa40a5bd77d",
"new_uuid_to":"12311-114" "new_uuid_to":"70c200f2-8a75-42a9-c166-bf5f87e0aa6b"
},
{
"change_type":2,
"old_uuid_from":"12311-111",
"old_uuid_to":"12311-113",
"new_uuid_from":"12311-112",
"new_uuid_to":"12311-113"
},
{
"change_type":3,
"old_uuid_from":"",
"old_uuid_to":"",
"new_uuid_from":"12311-115",
"new_uuid_to":"12311-116"
} }
], ],
"free_vertexs":[ "component_infos": [
"12311-111",
"12311-112",
"12311-115"
],
"component_infos":[
{ {
"grid_id":1, "component_type": 3,
"zone":1, "grid_id": 1,
"station_id":1, "name": "demo23",
"uuid":"12311-114", "page_id": 1,
"name":"母线 1", "station_id": 1,
"component_type":1, "uuid": "70c200f2-8a75-42a9-c166-bf5f87e0aa6b",
"in_service":true, "zone_id": 1,
"connected_bus":1, "context":"{\"top\":\"7.4\",\"left\":\"3.2\",\"right\":\"3.5\",\"buttom\":\"1.2\"}",
"state":1, "params": "{\"anchor_i\":false,\"anchor_v\":true,\"context\":\"{\\\"top\\\": \\\"7.4\\\", \\\"left\\\": \\\"3.2\\\", \\\"right\\\": \\\"3.5\\\", \\\"buttom\\\": \\\"1.2\\\"}\",\"name\":\"demo23\",\"oi_alarm\":110,\"op\":1,\"ov_alarm\":110,\"resistance\":100,\"ts\":\"2021-01-01T08:00:00+08:00\",\"ui_alarm\":90,\"uv_alarm\":90}"
"visible_id":"",
"description":"",
"context":"",
"comment":"",
"params":""
},
{
"grid_id":1,
"zone":1,
"station_id":1,
"uuid":"12311-116",
"name":"发电机 1",
"component_type":1,
"in_service":true,
"connected_bus":1,
"state":1,
"visible_id":"",
"description":"",
"context":"",
"comment":"",
"params":""
} }
] ]
} }

View File

@ -141,7 +141,7 @@ func ComponentAnchorReplaceHandler(c *gin.Context) {
anchorParam := config.AnchorParamConfig{ anchorParam := config.AnchorParamConfig{
AnchorParamBaseConfig: config.AnchorParamBaseConfig{ AnchorParamBaseConfig: config.AnchorParamBaseConfig{
StationID: strconv.FormatInt(componentInfo.StationID, 10), StationID: componentInfo.StationID,
ComponentID: strconv.FormatInt(componentInfo.ID, 10), ComponentID: strconv.FormatInt(componentInfo.ID, 10),
UUID: uuid, UUID: uuid,
AnchorName: anchorName, AnchorName: anchorName,

View File

@ -145,7 +145,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
} }
var component orm.Component var component orm.Component
result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Where("uuid = ?", globalUUID).Find(&component) result := tx.WithContext(cancelCtx).Clauses(clause.Locking{Strength: "UPDATE"}).Where("global_uuid = ?", globalUUID).Find(&component)
if result.Error != nil || result.RowsAffected == 0 { if result.Error != nil || result.RowsAffected == 0 {
tx.Rollback() tx.Rollback()
@ -189,7 +189,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) {
modelStruct := model.SelectModelByType(component.ComponentType) modelStruct := model.SelectModelByType(component.ComponentType)
modelStruct.SetComponentID(component.ID) modelStruct.SetComponentID(component.ID)
result = tx.WithContext(cancelCtx).Delete(modelStruct) result = tx.WithContext(cancelCtx).Where("component_id = ?", component.ID).Delete(modelStruct)
if result.Error != nil || result.RowsAffected == 0 { if result.Error != nil || result.RowsAffected == 0 {
tx.Rollback() tx.Rollback()

View File

@ -57,6 +57,7 @@ type CircuitDiagramUpdateRequest struct {
// ParseUUID define parse UUID by change type in topologic change struct // ParseUUID define parse UUID by change type in topologic change struct
func ParseUUID(info TopologicChangeInfo) (TopologicUUIDChangeInfos, error) { func ParseUUID(info TopologicChangeInfo) (TopologicUUIDChangeInfos, error) {
var UUIDChangeInfo TopologicUUIDChangeInfos var UUIDChangeInfo TopologicUUIDChangeInfos
UUIDChangeInfo.ChangeType = info.ChangeType
switch info.ChangeType { switch info.ChangeType {
case constant.UUIDFromChangeType: case constant.UUIDFromChangeType:

View File

@ -12,9 +12,9 @@ type Component struct {
ID int64 `gorm:"column:id;primaryKey"` ID int64 `gorm:"column:id;primaryKey"`
GlobalUUID uuid.UUID `gorm:"column:global_uuid"` GlobalUUID uuid.UUID `gorm:"column:global_uuid"`
Tag string `gorm:"column:tag"` Tag string `gorm:"column:tag"`
GridID int64 `gorm:"column:grid"` GridID string `gorm:"column:grid"`
ZoneID int64 `gorm:"column:zone"` ZoneID string `gorm:"column:zone"`
StationID int64 `gorm:"column:station"` StationID string `gorm:"column:station"`
PageID int64 `gorm:"column:page_id"` PageID int64 `gorm:"column:page_id"`
ComponentType int `gorm:"column:type"` ComponentType int `gorm:"column:type"`
Name string `gorm:"column:name"` Name string `gorm:"column:name"`

View File

@ -4,17 +4,17 @@ package orm
import "time" import "time"
type Demo struct { type Demo struct {
ID int64 `gorm:"column:id"` // 主键 ID ID int64 `gorm:"column:id" json:"id"` // 主键 ID
ComponentID int64 `gorm:"column:component_id"` // compoent表ID ComponentID int64 `gorm:"column:component_id" json:"component_id"` // compoent表ID
Resistance float32 `gorm:"column:resistance"` // 电阻值 Resistance float32 `gorm:"column:resistance" json:"resistance"` // 电阻值
AnchorV bool `gorm:"column:anchor_v"` // 是否锚定电压 AnchorV bool `gorm:"column:anchor_v" json:"anchor_v"` // 是否锚定电压
UVAlarm float32 `gorm:"column:uv_alarm"` // 欠压告警值 UVAlarm float32 `gorm:"column:uv_alarm" json:"uv_alarm"` // 欠压告警值
OVAlarm float32 `gorm:"column:ov_alarm"` // 过压告警值 OVAlarm float32 `gorm:"column:ov_alarm" json:"ov_alarm"` // 过压告警值
AnchorI bool `gorm:"column:anchor_i"` // 是否锚定电流 AnchorI bool `gorm:"column:anchor_i" json:"anchor_i"` // 是否锚定电流
UIAlarm float32 `gorm:"column:ui_alarm"` // 低电流告警值 UIAlarm float32 `gorm:"column:ui_alarm" json:"ui_alarm"` // 低电流告警值
OIAlarm float32 `gorm:"column:oi_alarm"` // 高电流告警值 OIAlarm float32 `gorm:"column:oi_alarm" json:"oi_alarm"` // 高电流告警值
Op int `gorm:"column:op"` // 操作人 ID Op int `gorm:"column:op" json:"op"` // 操作人 ID
Ts time.Time `gorm:"column:ts"` // 操作时间 Ts time.Time `gorm:"column:ts" json:"ts"` // 操作时间
} }
// TableName func respresent return table name of busbar section // TableName func respresent return table name of busbar section

View File

@ -59,7 +59,7 @@ var ParseFunc = func(parseConfig interface{}) {
anchorParam := config.AnchorParamConfig{ anchorParam := config.AnchorParamConfig{
AnchorParamBaseConfig: config.AnchorParamBaseConfig{ AnchorParamBaseConfig: config.AnchorParamBaseConfig{
StationID: strconv.FormatInt(modelParseConfig.ComponentInfo.StationID, 10), StationID: modelParseConfig.ComponentInfo.StationID,
ComponentID: strconv.FormatInt(modelParseConfig.ComponentInfo.ID, 10), ComponentID: strconv.FormatInt(modelParseConfig.ComponentInfo.ID, 10),
UUID: uuid, UUID: uuid,
AnchorName: anchorName, AnchorName: anchorName,