From d89bf83f8b88ef85eea5117dfb8d3776f8b213de Mon Sep 17 00:00:00 2001 From: douxu Date: Fri, 10 Jan 2025 16:57:29 +0800 Subject: [PATCH] =?UTF-8?q?fix=20bug=20of=20create=E3=80=81delete=E3=80=81?= =?UTF-8?q?update=20handler?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- database/create_component.go | 8 +-- database/update_component.go | 23 ++++++-- example/circuit_diagram_create.json | 65 +++++++++------------- example/circuit_diagram_delete.json | 21 +++---- example/circuit_diagram_update.json | 68 +++++------------------ handler/anchor_point_replace.go | 2 +- handler/circuit_diagram_delete.go | 4 +- network/circuit_diagram_update_request.go | 1 + orm/circuit_diagram_component.go | 6 +- orm/demo.go | 22 ++++---- pool/concurrency_model_parse.go | 2 +- 11 files changed, 87 insertions(+), 135 deletions(-) diff --git a/database/create_component.go b/database/create_component.go index 8cedfa6..cdfd99e 100644 --- a/database/create_component.go +++ b/database/create_component.go @@ -4,6 +4,7 @@ package database import ( "context" "fmt" + "strconv" "time" "modelRT/constant" @@ -26,9 +27,9 @@ func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo component := orm.Component{ GlobalUUID: globalUUID, - GridID: componentInfo.GridID, - ZoneID: componentInfo.ZoneID, - StationID: componentInfo.StationID, + GridID: strconv.FormatInt(componentInfo.GridID, 10), + ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10), + StationID: strconv.FormatInt(componentInfo.StationID, 10), PageID: componentInfo.PageID, Tag: componentInfo.Tag, ComponentType: componentInfo.ComponentType, @@ -39,7 +40,6 @@ func CreateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo } result := tx.WithContext(cancelCtx).Create(&component) - if result.Error != nil || result.RowsAffected == 0 { err := result.Error if result.RowsAffected == 0 { diff --git a/database/update_component.go b/database/update_component.go index 4dcd325..22db5b9 100644 --- a/database/update_component.go +++ b/database/update_component.go @@ -4,6 +4,7 @@ package database import ( "context" "fmt" + "strconv" "time" "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) } - 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, - GridID: componentInfo.GridID, - ZoneID: componentInfo.ZoneID, - StationID: componentInfo.StationID, + GridID: strconv.FormatInt(componentInfo.GridID, 10), + ZoneID: strconv.FormatInt(componentInfo.ZoneID, 10), + StationID: strconv.FormatInt(componentInfo.StationID, 10), PageID: componentInfo.PageID, Tag: componentInfo.Tag, ComponentType: componentInfo.ComponentType, @@ -38,7 +49,8 @@ func UpdateComponentIntoDB(ctx context.Context, tx *gorm.DB, componentInfo netwo 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 { err := result.Error 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 component.ID, nil } diff --git a/example/circuit_diagram_create.json b/example/circuit_diagram_create.json index 004116b..240d2ec 100644 --- a/example/circuit_diagram_create.json +++ b/example/circuit_diagram_create.json @@ -1,52 +1,37 @@ { - "page_id":1, - "topologics":[ + "page_id": 1, + "topologics": [ { - "uuid_from":"12311-111", - "uuid_to":"12311-114" + "uuid_from": "70c190f2-8a60-42a9-b143-ec5f87e0aa6b", + "uuid_to": "70c190f2-8a75-42a9-b166-ec5f87e0aa6b" }, { - "uuid_from":"12311-115", - "uuid_to":"12311-116" + "uuid_from": "70c190f2-8a75-42a9-b166-ec5f87e0aa6b", + "uuid_to": "70c200f2-8a75-42a9-c166-bf5f87e0aa6b" } ], - "free_vertexs":[ - "12311-111", - "12311-112", - "12311-115" - ], - "component_infos":[ + "component_infos": [ { - "grid_id":1, - "zone":1, - "station_id":1, - "uuid":"12311-114", - "name":"母线 1", - "component_type":1, - "in_service":true, - "connected_bus":1, - "state":1, - "visible_id":"", - "description":"", - "context":"", - "comment":"", - "params":"" + "component_type": 3, + "grid_id": 1, + "name": "demo21", + "page_id": 1, + "station_id": 1, + "uuid": "70c190f2-8a75-42a9-b166-ec5f87e0aa6b", + "zone_id": 1, + "context":"{\"top\":\"7.4\",\"left\":\"3.2\",\"right\":\"3.5\",\"buttom\":\"1.2\"}", + "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}" }, { - "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":"" + "component_type": 3, + "grid_id": 1, + "name": "demo22", + "page_id": 1, + "station_id": 1, + "uuid": "70c200f2-8a75-42a9-c166-bf5f87e0aa6b", + "zone_id": 1, + "context":"{\"top\":\"7.4\",\"left\":\"3.2\",\"right\":\"3.5\",\"buttom\":\"1.2\"}", + "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}" } ] } \ No newline at end of file diff --git a/example/circuit_diagram_delete.json b/example/circuit_diagram_delete.json index ae6efe7..f5e8360 100644 --- a/example/circuit_diagram_delete.json +++ b/example/circuit_diagram_delete.json @@ -2,27 +2,22 @@ "page_id":1, "topologics":[ { - "uuid_from":"12311-111", - "uuid_to":"12311-114" + "uuid_from": "70c190f2-8a75-42a9-b166-ec5f87e0aa6b", + "uuid_to": "70c200f2-8a75-42a9-c166-bf5f87e0aa6b" }, { - "uuid_from":"12311-115", - "uuid_to":"12311-116" + "uuid_from": "70c190f2-8a60-42a9-b143-ec5f87e0aa6b", + "uuid_to": "70c190f2-8a75-42a9-b166-ec5f87e0aa6b" } ], - "free_vertexs":[ - "12311-111", - "12311-112", - "12311-115" - ], "component_infos":[ { - "uuid":"12311-114", - "component_type":1 + "uuid":"70c200f2-8a75-42a9-c166-bf5f87e0aa6b", + "component_type":3 }, { - "uuid":"12311-116", - "component_type":1 + "uuid":"70c190f2-8a75-42a9-b166-ec5f87e0aa6b", + "component_type":3 } ] } diff --git a/example/circuit_diagram_update.json b/example/circuit_diagram_update.json index 14d3e94..53d108b 100644 --- a/example/circuit_diagram_update.json +++ b/example/circuit_diagram_update.json @@ -3,63 +3,23 @@ "topologics":[ { "change_type":1, - "old_uuid_from":"12311-111", - "old_uuid_to":"12311-113", - "new_uuid_from":"12311-111", - "new_uuid_to":"12311-114" - }, - { - "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" + "old_uuid_from":"70c190f2-8a75-42a9-b166-ec5f87e0aa6b", + "old_uuid_to":"70c200f2-8a75-42a9-c166-bf5f87e0aa6b", + "new_uuid_from":"e32bc0be-67f4-4d79-a5da-eaa40a5bd77d", + "new_uuid_to":"70c200f2-8a75-42a9-c166-bf5f87e0aa6b" } ], - "free_vertexs":[ - "12311-111", - "12311-112", - "12311-115" - ], - "component_infos":[ + "component_infos": [ { - "grid_id":1, - "zone":1, - "station_id":1, - "uuid":"12311-114", - "name":"母线 1", - "component_type":1, - "in_service":true, - "connected_bus":1, - "state":1, - "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":"" + "component_type": 3, + "grid_id": 1, + "name": "demo23", + "page_id": 1, + "station_id": 1, + "uuid": "70c200f2-8a75-42a9-c166-bf5f87e0aa6b", + "zone_id": 1, + "context":"{\"top\":\"7.4\",\"left\":\"3.2\",\"right\":\"3.5\",\"buttom\":\"1.2\"}", + "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}" } ] } diff --git a/handler/anchor_point_replace.go b/handler/anchor_point_replace.go index 57ee15b..9c92b21 100644 --- a/handler/anchor_point_replace.go +++ b/handler/anchor_point_replace.go @@ -141,7 +141,7 @@ func ComponentAnchorReplaceHandler(c *gin.Context) { anchorParam := config.AnchorParamConfig{ AnchorParamBaseConfig: config.AnchorParamBaseConfig{ - StationID: strconv.FormatInt(componentInfo.StationID, 10), + StationID: componentInfo.StationID, ComponentID: strconv.FormatInt(componentInfo.ID, 10), UUID: uuid, AnchorName: anchorName, diff --git a/handler/circuit_diagram_delete.go b/handler/circuit_diagram_delete.go index b2c8c3e..3bd1030 100644 --- a/handler/circuit_diagram_delete.go +++ b/handler/circuit_diagram_delete.go @@ -145,7 +145,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) { } 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 { tx.Rollback() @@ -189,7 +189,7 @@ func CircuitDiagramDeleteHandler(c *gin.Context) { modelStruct := model.SelectModelByType(component.ComponentType) 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 { tx.Rollback() diff --git a/network/circuit_diagram_update_request.go b/network/circuit_diagram_update_request.go index c041888..934f3bf 100644 --- a/network/circuit_diagram_update_request.go +++ b/network/circuit_diagram_update_request.go @@ -57,6 +57,7 @@ type CircuitDiagramUpdateRequest struct { // ParseUUID define parse UUID by change type in topologic change struct func ParseUUID(info TopologicChangeInfo) (TopologicUUIDChangeInfos, error) { var UUIDChangeInfo TopologicUUIDChangeInfos + UUIDChangeInfo.ChangeType = info.ChangeType switch info.ChangeType { case constant.UUIDFromChangeType: diff --git a/orm/circuit_diagram_component.go b/orm/circuit_diagram_component.go index b626d00..e2062b1 100644 --- a/orm/circuit_diagram_component.go +++ b/orm/circuit_diagram_component.go @@ -12,9 +12,9 @@ type Component struct { ID int64 `gorm:"column:id;primaryKey"` GlobalUUID uuid.UUID `gorm:"column:global_uuid"` Tag string `gorm:"column:tag"` - GridID int64 `gorm:"column:grid"` - ZoneID int64 `gorm:"column:zone"` - StationID int64 `gorm:"column:station"` + GridID string `gorm:"column:grid"` + ZoneID string `gorm:"column:zone"` + StationID string `gorm:"column:station"` PageID int64 `gorm:"column:page_id"` ComponentType int `gorm:"column:type"` Name string `gorm:"column:name"` diff --git a/orm/demo.go b/orm/demo.go index f668a6c..8b0991b 100644 --- a/orm/demo.go +++ b/orm/demo.go @@ -4,17 +4,17 @@ package orm import "time" type Demo struct { - ID int64 `gorm:"column:id"` // 主键 ID - ComponentID int64 `gorm:"column:component_id"` // compoent表ID - Resistance float32 `gorm:"column:resistance"` // 电阻值 - AnchorV bool `gorm:"column:anchor_v"` // 是否锚定电压 - UVAlarm float32 `gorm:"column:uv_alarm"` // 欠压告警值 - OVAlarm float32 `gorm:"column:ov_alarm"` // 过压告警值 - AnchorI bool `gorm:"column:anchor_i"` // 是否锚定电流 - UIAlarm float32 `gorm:"column:ui_alarm"` // 低电流告警值 - OIAlarm float32 `gorm:"column:oi_alarm"` // 高电流告警值 - Op int `gorm:"column:op"` // 操作人 ID - Ts time.Time `gorm:"column:ts"` // 操作时间 + ID int64 `gorm:"column:id" json:"id"` // 主键 ID + ComponentID int64 `gorm:"column:component_id" json:"component_id"` // compoent表ID + Resistance float32 `gorm:"column:resistance" json:"resistance"` // 电阻值 + AnchorV bool `gorm:"column:anchor_v" json:"anchor_v"` // 是否锚定电压 + UVAlarm float32 `gorm:"column:uv_alarm" json:"uv_alarm"` // 欠压告警值 + OVAlarm float32 `gorm:"column:ov_alarm" json:"ov_alarm"` // 过压告警值 + AnchorI bool `gorm:"column:anchor_i" json:"anchor_i"` // 是否锚定电流 + UIAlarm float32 `gorm:"column:ui_alarm" json:"ui_alarm"` // 低电流告警值 + OIAlarm float32 `gorm:"column:oi_alarm" json:"oi_alarm"` // 高电流告警值 + Op int `gorm:"column:op" json:"op"` // 操作人 ID + Ts time.Time `gorm:"column:ts" json:"ts"` // 操作时间 } // TableName func respresent return table name of busbar section diff --git a/pool/concurrency_model_parse.go b/pool/concurrency_model_parse.go index 3b499c1..81e2893 100644 --- a/pool/concurrency_model_parse.go +++ b/pool/concurrency_model_parse.go @@ -59,7 +59,7 @@ var ParseFunc = func(parseConfig interface{}) { anchorParam := config.AnchorParamConfig{ AnchorParamBaseConfig: config.AnchorParamBaseConfig{ - StationID: strconv.FormatInt(modelParseConfig.ComponentInfo.StationID, 10), + StationID: modelParseConfig.ComponentInfo.StationID, ComponentID: strconv.FormatInt(modelParseConfig.ComponentInfo.ID, 10), UUID: uuid, AnchorName: anchorName,