2025-08-05 15:20:07 +08:00
// Package database define database operation functions
package database
import (
"context"
"errors"
2025-08-08 15:27:51 +08:00
"fmt"
2025-08-05 15:20:07 +08:00
"strings"
2025-08-08 15:27:51 +08:00
"modelRT/diagram"
2025-08-05 15:20:07 +08:00
"modelRT/model"
"gorm.io/gorm"
)
2025-08-08 15:27:51 +08:00
// ParseAttrToken define return the attribute model interface based on the input attribute token. doc addr http://server.baseware.net:6875/books/product-design-docs/page/d6baf
2025-08-05 15:20:07 +08:00
func ParseAttrToken ( ctx context . Context , tx * gorm . DB , attrToken string ) ( model . AttrModelInterface , error ) {
2025-08-08 15:27:51 +08:00
rs := diagram . NewRedisString ( ctx , attrToken , "" , 10 , true )
2025-08-05 15:20:07 +08:00
attrSlice := strings . Split ( attrToken , "." )
attrLen := len ( attrSlice )
if attrLen == 4 {
short := & model . ShortAttrInfo {
AttrGroupName : attrSlice [ 2 ] ,
AttrKey : attrSlice [ 3 ] ,
}
err := FillingShortAttrModel ( ctx , tx , attrSlice , short )
if err != nil {
return nil , err
}
2025-08-08 15:27:51 +08:00
attrValue , err := rs . Get ( attrToken )
if err != nil {
return nil , err
}
short . AttrValue = attrValue
2025-08-05 15:20:07 +08:00
return short , nil
} else if attrLen == 7 {
long := & model . LongAttrInfo {
AttrGroupName : attrSlice [ 5 ] ,
AttrKey : attrSlice [ 6 ] ,
}
err := FillingLongAttrModel ( ctx , tx , attrSlice , long )
if err != nil {
return nil , err
}
2025-08-08 15:27:51 +08:00
attrValue , err := rs . Get ( attrToken )
if err != nil {
return nil , err
}
long . AttrValue = attrValue
2025-08-05 15:20:07 +08:00
return long , nil
}
return nil , errors . New ( "invalid attribute token format" )
}
// FillingShortAttrModel define filling short attribute model info
func FillingShortAttrModel ( ctx context . Context , tx * gorm . DB , attrItems [ ] string , attrModel * model . ShortAttrInfo ) error {
component , err := QueryComponentByNsPath ( ctx , tx , attrItems [ 0 ] )
if err != nil {
return err
}
attrModel . ComponentInfo = & component
return nil
}
// FillingLongAttrModel define filling long attribute model info
func FillingLongAttrModel ( ctx context . Context , tx * gorm . DB , attrItems [ ] string , attrModel * model . LongAttrInfo ) error {
2025-09-01 16:15:30 +08:00
grid , err := QueryGridByTagName ( ctx , tx , attrItems [ 0 ] )
2025-08-05 15:20:07 +08:00
if err != nil {
return err
}
attrModel . GridInfo = & grid
2025-09-01 16:15:30 +08:00
zone , err := QueryZoneByTagName ( ctx , tx , attrItems [ 1 ] )
2025-08-05 15:20:07 +08:00
if err != nil {
return err
}
attrModel . ZoneInfo = & zone
2025-09-01 16:15:30 +08:00
station , err := QueryStationByTagName ( ctx , tx , attrItems [ 2 ] )
2025-08-05 15:20:07 +08:00
if err != nil {
return err
}
attrModel . StationInfo = & station
component , err := QueryComponentByNsPath ( ctx , tx , attrItems [ 3 ] )
if err != nil {
return err
}
attrModel . ComponentInfo = & component
return nil
}
2025-08-08 15:27:51 +08:00
// QueryAttrValueFromRedis define query attribute value from redis by attrKey
func QueryAttrValueFromRedis ( attrKey string ) string {
fmt . Println ( attrKey )
return ""
}