feat: 图表功能接口优化,补充SMSC网元

This commit is contained in:
TsMask
2024-10-09 11:00:12 +08:00
parent 1a9cb04e98
commit 8616620b02
8 changed files with 617 additions and 657 deletions

View File

@@ -13,10 +13,8 @@ import (
func Setup(router *gin.Engine) {
logger.Infof("开始加载 ====> chart 模块路由")
chartGroup := router.Group("/chart")
// 关系图
chartGraphGroup := chartGroup.Group("/graph")
// G6关系图
chartGraphGroup := router.Group("/chart/graph")
{
chartGraphGroup.GET("",
middleware.PreAuthorize(nil),

View File

@@ -4,14 +4,14 @@ import (
"be.ems/src/framework/i18n"
"be.ems/src/framework/utils/ctx"
"be.ems/src/framework/vo/result"
chartService "be.ems/src/modules/chart/service"
"be.ems/src/modules/chart/service"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
)
// 实例化控制层 ChartGraphController 结构体
var NewChartGraph = &ChartGraphController{
chartGraphService: chartService.NewChartGraphImpl,
chartGraphService: service.NewChartGraph,
}
// G6关系图
@@ -19,7 +19,7 @@ var NewChartGraph = &ChartGraphController{
// PATH /graph
type ChartGraphController struct {
// G6关系图数据表服务
chartGraphService chartService.IChartGraph
chartGraphService *service.ChartGraph
}
// 获取关系图组名

View File

@@ -1,31 +1,32 @@
package model
// ChartGraph G6关系图数据对象 chart_graph
// ChartGraph 图表-G6关系图数据对象 chart_graph
type ChartGraph struct {
RowID int64 `json:"rowId,omitempty" gorm:"column:row_id;primaryKey;autoIncrement"` // 记录ID
RowType string `json:"rowType,omitempty" gorm:"column:row_type"` // 记录类型(node/edge/combo)
RowGroup string `json:"rowGroup,omitempty" gorm:"column:row_group"` // 记录组名
ID string `json:"id,omitempty" gorm:"column:id"` // 元素ID
Type string `json:"type,omitempty" gorm:"column:type"` // node/combo 类型
Depth int `json:"depth,omitempty" gorm:"column:depth"` // node/combo 深度
X float64 `json:"x,omitempty" gorm:"column:x"` // node/combo 横向坐标
Y float64 `json:"y,omitempty" gorm:"column:y"` // node/combo 纵向坐标
Size string `json:"size,omitempty" gorm:"column:size"` // node/combo 大小-JSON数组
Icon string `json:"icon,omitempty" gorm:"column:icon"` // node-部分类型支持图标JSON配置
Img string `json:"img,omitempty" gorm:"column:img"` // node-img 图片
ClipCfg string `json:"clipCfg,omitempty" gorm:"column:clip_cfg"` // node-img 图片裁剪JSON配置
Direction string `json:"direction,omitempty" gorm:"column:direction"` // node-triangle 三角形的方向(up/down/left/right)
Source string `json:"source,omitempty" gorm:"column:source"` // edge-边起始
Target string `json:"target,omitempty" gorm:"column:target"` // edge-边目标
ComboID string `json:"combo_id,omitempty" gorm:"column:combo_id"` // combo-分组
Padding string `json:"padding,omitempty" gorm:"column:padding"` // combo-JSON分组内边距
ParentID string `json:"parentId,omitempty" gorm:"column:parent_id"` // combo-父级分组
Children string `json:"children,omitempty" gorm:"column:children"` // combo-JSON分组内含元素
Style string `json:"style,omitempty" gorm:"column:style"` // 元素样式-JONS配置
Label string `json:"label,omitempty" gorm:"column:label"` // 标签文本
LabelCfg string `json:"labelCfg,omitempty" gorm:"column:label_cfg"` // 标签文本-JSON配置
RowType string `json:"rowType" gorm:"row_type"` // 记录类型
RowGroup string `json:"rowGroup" gorm:"row_group"` // 记录组名
ID string `json:"id" gorm:"id"` // 元素ID
Type string `json:"type" gorm:"type"` // node/combo 类型
Depth int64 `json:"depth" gorm:"depth"` // node/combo 深度
X float64 `json:"x" gorm:"x"` // node/combo 横向坐标
Y float64 `json:"y" gorm:"y"` // node/combo 纵向坐标
Size string `json:"size" gorm:"size"` // node/combo 大小-JSON数组
Icon string `json:"icon" gorm:"icon"` // node-部分类型支持图标JSON配置
Img string `json:"img" gorm:"img"` // node-img 图片
ClipCfg string `json:"clipCfg" gorm:"clip_cfg"` // node-img 图片裁剪JSON配置
Direction string `json:"direction" gorm:"direction"` // node-triangle 三角形的方向
Source string `json:"source" gorm:"source"` // edge-边起始
Target string `json:"target" gorm:"target"` // edge-边目标
ComboId string `json:"comboId" gorm:"combo_id"` // combo-分组
Padding string `json:"padding" gorm:"padding"` // combo-JSON分组内边距
ParentId string `json:"parentId" gorm:"parent_id"` // combo-父级分组
Children string `json:"children" gorm:"children"` // combo-JSON分组内含元素
Style string `json:"style" gorm:"style"` // 元素样式-JONS配置
Label string `json:"label" gorm:"label"` // 标签文本
LabelCfg string `json:"labelCfg" gorm:"label_cfg"` // 标签文本-JSON配置
}
func (ChartGraph) TableName() string {
// TableName 表名称
func (*ChartGraph) TableName() string {
return "chart_graph"
}

View File

@@ -1,21 +1,194 @@
package repository
import "be.ems/src/modules/chart/model"
import (
"strings"
// G6关系图数据 数据层接口
type IChartGraph interface {
// SelectPage 根据条件分页查询字典类型
SelectPage(query map[string]any) map[string]any
"be.ems/src/framework/datasource"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/parse"
"be.ems/src/framework/utils/repo"
"be.ems/src/modules/chart/model"
)
// SelectList 根据实体查询
SelectList(graph model.ChartGraph) []model.ChartGraph
// 实例化数据层 ChartGraph 结构体
var NewChartGraph = &ChartGraph{
selectSql: `select
row_id, row_type, row_group,
id, type, depth, x, y, size, icon, img,
clip_cfg, direction,
source, target, combo_id,
padding, parent_id, children,
style, label, label_cfg
from chart_graph`,
// SelectGroup 查询组名
SelectGroup() []string
// Insert 批量添加
Inserts(graphs []model.ChartGraph) int64
// Delete 删除组数据
DeleteGroup(rowGroup string) int64
resultMap: map[string]string{
"row_id": "RowID",
"row_type": "RowType",
"row_group": "RowGroup",
"id": "ID",
"type": "Type",
"depth": "Depth",
"x": "X",
"y": "Y",
"size": "Size",
"icon": "Icon",
"img": "Img",
"clip_cfg": "ClipCfg",
"direction": "Direction",
"source": "Source",
"target": "Target",
"combo_id": "ComboID",
"padding": "Padding",
"parent_id": "ParentID",
"children": "Children",
"style": "Style",
"label": "Label",
"label_cfg": "LabelCfg",
},
}
// ChartGraph G6关系图数据表 数据层处理
type ChartGraph struct {
// 查询视图对象SQL
selectSql string
// 结果字段与实体映射
resultMap map[string]string
}
// convertResultRows 将结果记录转实体结果组
func (r *ChartGraph) convertResultRows(rows []map[string]any) []model.ChartGraph {
arr := make([]model.ChartGraph, 0)
for _, row := range rows {
item := model.ChartGraph{}
for key, value := range row {
if keyMapper, ok := r.resultMap[key]; ok {
repo.SetFieldValue(&item, keyMapper, value)
}
}
arr = append(arr, item)
}
return arr
}
// SelectPage 根据条件分页查询字典类型
func (r *ChartGraph) SelectPage(query map[string]any) map[string]any {
// 查询条件拼接
var conditions []string
var params []any
if v, ok := query["rowType"]; ok && v != "" {
conditions = append(conditions, "row_type = ?")
params = append(params, strings.Trim(v.(string), " "))
}
if v, ok := query["rowGroup"]; ok && v != "" {
conditions = append(conditions, "row_group = ?")
params = append(params, strings.Trim(v.(string), " "))
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
result := map[string]any{
"total": 0,
"rows": []model.ChartGraph{},
}
// 查询数量 长度为0直接返回
totalSql := "select count(1) as 'total' from chart_graph"
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
if err != nil {
logger.Errorf("total err => %v", err)
return result
}
total := parse.Number(totalRows[0]["total"])
if total == 0 {
return result
} else {
result["total"] = total
}
// 分页
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
pageSql := " limit ?,? "
params = append(params, pageNum*pageSize)
params = append(params, pageSize)
// 查询数据
querySql := r.selectSql + whereSql + pageSql
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
return result
}
// 转换实体
result["rows"] = r.convertResultRows(results)
return result
}
// SelectList 根据实体查询
func (r *ChartGraph) SelectList(graph model.ChartGraph) []model.ChartGraph {
// 查询条件拼接
var conditions []string
var params []any
if graph.RowType != "" {
conditions = append(conditions, "row_type = ?")
params = append(params, graph.RowType)
}
if graph.RowGroup != "" {
conditions = append(conditions, "row_group = ?")
params = append(params, graph.RowGroup)
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
// 查询数据
querySql := r.selectSql + whereSql + " order by depth asc "
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
}
// 转换实体
return r.convertResultRows(results)
}
// SelectGroup 查询组名
func (r *ChartGraph) SelectGroup() []string {
rows := []string{}
// 查询数量 长度为0直接返回
querySql := "select row_group as 'str' from chart_graph GROUP BY row_group"
strRows, err := datasource.RawDB("", querySql, nil)
if err != nil {
logger.Errorf("Query err => %v", err)
return rows
}
for _, v := range strRows {
rows = append(rows, v["str"].(string))
}
return rows
}
// Insert 批量添加
func (r *ChartGraph) Inserts(graphs []model.ChartGraph) int64 {
tx := datasource.DefaultDB().CreateInBatches(graphs, 2000)
if err := tx.Error; err != nil {
logger.Errorf("CreateInBatches err => %v", err)
}
return tx.RowsAffected
}
// Delete 删除组数据
func (r *ChartGraph) DeleteGroup(rowGroup string) int64 {
tx := datasource.DefaultDB().Where("row_group = ?", rowGroup).Delete(&model.ChartGraph{})
if err := tx.Error; err != nil {
logger.Errorf("Delete err => %v", err)
}
return tx.RowsAffected
}

View File

@@ -1,194 +0,0 @@
package repository
import (
"strings"
"be.ems/src/framework/datasource"
"be.ems/src/framework/logger"
"be.ems/src/framework/utils/parse"
"be.ems/src/framework/utils/repo"
"be.ems/src/modules/chart/model"
)
// 实例化数据层 NewChartGraphImpl 结构体
var NewChartGraphImpl = &ChartGraphImpl{
selectSql: `select
row_id, row_type, row_group,
id, type, depth, x, y, size, icon, img,
clip_cfg, direction,
source, target, combo_id,
padding, parent_id, children,
style, label, label_cfg
from chart_graph`,
resultMap: map[string]string{
"row_id": "RowID",
"row_type": "RowType",
"row_group": "RowGroup",
"id": "ID",
"type": "Type",
"depth": "Depth",
"x": "X",
"y": "Y",
"size": "Size",
"icon": "Icon",
"img": "Img",
"clip_cfg": "ClipCfg",
"direction": "Direction",
"source": "Source",
"target": "Target",
"combo_id": "ComboID",
"padding": "Padding",
"parent_id": "ParentID",
"children": "Children",
"style": "Style",
"label": "Label",
"label_cfg": "LabelCfg",
},
}
// ChartGraphImpl G6关系图数据表 数据层处理
type ChartGraphImpl struct {
// 查询视图对象SQL
selectSql string
// 结果字段与实体映射
resultMap map[string]string
}
// convertResultRows 将结果记录转实体结果组
func (r *ChartGraphImpl) convertResultRows(rows []map[string]any) []model.ChartGraph {
arr := make([]model.ChartGraph, 0)
for _, row := range rows {
item := model.ChartGraph{}
for key, value := range row {
if keyMapper, ok := r.resultMap[key]; ok {
repo.SetFieldValue(&item, keyMapper, value)
}
}
arr = append(arr, item)
}
return arr
}
// SelectPage 根据条件分页查询字典类型
func (r *ChartGraphImpl) SelectPage(query map[string]any) map[string]any {
// 查询条件拼接
var conditions []string
var params []any
if v, ok := query["rowType"]; ok && v != "" {
conditions = append(conditions, "row_type = ?")
params = append(params, strings.Trim(v.(string), " "))
}
if v, ok := query["rowGroup"]; ok && v != "" {
conditions = append(conditions, "row_group = ?")
params = append(params, strings.Trim(v.(string), " "))
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
result := map[string]any{
"total": 0,
"rows": []model.ChartGraph{},
}
// 查询数量 长度为0直接返回
totalSql := "select count(1) as 'total' from chart_graph"
totalRows, err := datasource.RawDB("", totalSql+whereSql, params)
if err != nil {
logger.Errorf("total err => %v", err)
return result
}
total := parse.Number(totalRows[0]["total"])
if total == 0 {
return result
} else {
result["total"] = total
}
// 分页
pageNum, pageSize := repo.PageNumSize(query["pageNum"], query["pageSize"])
pageSql := " limit ?,? "
params = append(params, pageNum*pageSize)
params = append(params, pageSize)
// 查询数据
querySql := r.selectSql + whereSql + pageSql
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
return result
}
// 转换实体
result["rows"] = r.convertResultRows(results)
return result
}
// SelectList 根据实体查询
func (r *ChartGraphImpl) SelectList(graph model.ChartGraph) []model.ChartGraph {
// 查询条件拼接
var conditions []string
var params []any
if graph.RowType != "" {
conditions = append(conditions, "row_type = ?")
params = append(params, graph.RowType)
}
if graph.RowGroup != "" {
conditions = append(conditions, "row_group = ?")
params = append(params, graph.RowGroup)
}
// 构建查询条件语句
whereSql := ""
if len(conditions) > 0 {
whereSql += " where " + strings.Join(conditions, " and ")
}
// 查询数据
querySql := r.selectSql + whereSql + " order by depth asc "
results, err := datasource.RawDB("", querySql, params)
if err != nil {
logger.Errorf("query err => %v", err)
}
// 转换实体
return r.convertResultRows(results)
}
// SelectGroup 查询组名
func (r *ChartGraphImpl) SelectGroup() []string {
rows := []string{}
// 查询数量 长度为0直接返回
querySql := "select row_group as 'str' from chart_graph GROUP BY row_group"
strRows, err := datasource.RawDB("", querySql, nil)
if err != nil {
logger.Errorf("Query err => %v", err)
return rows
}
for _, v := range strRows {
rows = append(rows, v["str"].(string))
}
return rows
}
// Insert 批量添加
func (r *ChartGraphImpl) Inserts(graphs []model.ChartGraph) int64 {
tx := datasource.DefaultDB().CreateInBatches(graphs, 2000)
if err := tx.Error; err != nil {
logger.Errorf("CreateInBatches err => %v", err)
}
return tx.RowsAffected
}
// Delete 删除组数据
func (r *ChartGraphImpl) DeleteGroup(rowGroup string) int64 {
tx := datasource.DefaultDB().Where("row_group = ?", rowGroup).Delete(&model.ChartGraph{})
if err := tx.Error; err != nil {
logger.Errorf("Delete err => %v", err)
}
return tx.RowsAffected
}

View File

@@ -1,16 +1,359 @@
package service
// G6关系图数据 服务层接口
type IChartGraph interface {
// SelectGroup 查询组名
SelectGroup() []string
import (
"encoding/json"
"strings"
// LoadData 查询所组图数据
LoadData(rowGroup, rowType string) map[string]any
"be.ems/src/framework/utils/parse"
"be.ems/src/modules/chart/model"
"be.ems/src/modules/chart/repository"
)
// SaveData 添加组图数据
SaveData(rowGroup string, data map[string]any) int64
// DeleteGroup 删除所组图数据
DeleteGroup(rowGroup string) int64
// 实例化服务层 ChartGraph 结构体
var NewChartGraph = &ChartGraph{
graphRepository: repository.NewChartGraph,
}
// ChartGraph G6关系图数据表 服务层处理
type ChartGraph struct {
// G6关系图数据服务
graphRepository *repository.ChartGraph
}
// SelectGroup 查询组名
func (s *ChartGraph) SelectGroup() []string {
return s.graphRepository.SelectGroup()
}
// LoadData 查询所组图数据
func (s *ChartGraph) LoadData(rowGroup, rowType string) map[string]any {
// 查询数据
graph := model.ChartGraph{
RowGroup: rowGroup,
}
if rowType != "" {
graph.RowType = rowType
}
data := s.graphRepository.SelectList(graph)
// 数据项
nodes := []map[string]any{}
edges := []map[string]any{}
combos := []map[string]any{}
for _, v := range data {
if v.RowType == "node" {
nodes = append(nodes, s.loadNode(v))
}
if v.RowType == "edge" {
edges = append(edges, s.loadEdge(v))
}
if v.RowType == "combo" {
combos = append(combos, s.loadCombo(v))
}
}
return map[string]any{
"nodes": nodes,
"edges": edges,
"combos": combos,
}
}
// loadNode 图数据Node
func (s *ChartGraph) loadNode(v model.ChartGraph) map[string]any {
node := map[string]any{
"id": v.ID,
"comboId": v.ComboId,
"x": v.X,
"y": v.Y,
"type": v.Type,
"depth": v.Depth,
}
// 元素样式
style := map[string]any{}
if len(v.Style) > 7 {
json.Unmarshal([]byte(v.Style), &style)
}
node["style"] = style
// 元素大小
if strings.Contains(v.Size, "[") {
sizeArr := []int64{}
json.Unmarshal([]byte(v.Size), &sizeArr)
node["size"] = sizeArr
} else {
node["size"] = parse.Number(v.Size)
}
// 标签文本
node["label"] = v.Label
labelCfg := map[string]any{}
if len(v.LabelCfg) > 7 {
json.Unmarshal([]byte(v.LabelCfg), &labelCfg)
}
node["labelCfg"] = labelCfg
// 三角形属性
if v.Type == "triangle" {
node["direction"] = v.Direction
}
// 图片属性
if strings.Index(v.Type, "image") == 0 {
node["img"] = v.Img
clipCfg := map[string]any{}
if len(v.ClipCfg) > 7 {
json.Unmarshal([]byte(v.ClipCfg), &clipCfg)
}
node["clipCfg"] = clipCfg
}
// 图标属性
if v.Icon != "" {
icon := map[string]any{}
if len(v.Icon) > 7 {
json.Unmarshal([]byte(v.Icon), &icon)
}
node["icon"] = icon
}
return node
}
// loadEdge 图数据Edge
func (s *ChartGraph) loadEdge(v model.ChartGraph) map[string]any {
edge := map[string]any{
"id": v.ID,
"source": v.Source,
"target": v.Target,
"type": v.Type,
}
// 元素样式
style := map[string]any{}
if len(v.Style) > 7 {
json.Unmarshal([]byte(v.Style), &style)
}
edge["style"] = style
// 标签文本
edge["label"] = v.Label
labelCfg := map[string]any{}
if len(v.LabelCfg) > 7 {
json.Unmarshal([]byte(v.LabelCfg), &labelCfg)
}
edge["labelCfg"] = labelCfg
return edge
}
// loadCombo 图数据Combo
func (s *ChartGraph) loadCombo(v model.ChartGraph) map[string]any {
combo := map[string]any{
"id": v.ID,
"x": v.X,
"y": v.Y,
"type": v.Type,
"depth": v.Depth,
}
// 元素样式
style := map[string]any{}
if len(v.Style) > 7 {
json.Unmarshal([]byte(v.Style), &style)
}
combo["style"] = style
// 元素大小
if strings.Contains(v.Size, "[") {
sizeArr := []int64{}
json.Unmarshal([]byte(v.Size), &sizeArr)
combo["size"] = sizeArr
} else {
combo["size"] = parse.Number(v.Size)
}
// 元素内边距
if strings.Contains(v.Padding, "[") {
paddingArr := []int64{}
json.Unmarshal([]byte(v.Padding), &paddingArr)
combo["padding"] = paddingArr
} else {
combo["padding"] = parse.Number(v.Padding)
}
// 标签文本
combo["label"] = v.Label
labelCfg := map[string]any{}
if len(v.LabelCfg) > 7 {
json.Unmarshal([]byte(v.LabelCfg), &labelCfg)
}
combo["labelCfg"] = labelCfg
// 分组内元素
if v.Children != "" {
children := []map[string]any{}
if len(v.Children) > 7 {
json.Unmarshal([]byte(v.Children), &children)
}
combo["children"] = children
}
return combo
}
// SaveData 添加组图数据
func (s *ChartGraph) SaveData(rowGroup string, data map[string]any) int64 {
graphs := []model.ChartGraph{}
nodes := data["nodes"].([]map[string]any)
graphNodes := s.saveNode(rowGroup, nodes)
graphs = append(graphs, graphNodes...)
edges := data["edges"].([]map[string]any)
graphEdges := s.saveEdge(rowGroup, edges)
graphs = append(graphs, graphEdges...)
combos := data["combos"].([]map[string]any)
graphCombos := s.saveCombo(rowGroup, combos)
graphs = append(graphs, graphCombos...)
// 删除组数据后插入
if len(graphs) > 0 {
s.graphRepository.DeleteGroup(rowGroup)
}
return s.graphRepository.Inserts(graphs)
}
// saveNode 图数据Node
func (s *ChartGraph) saveNode(rowGroup string, nodes []map[string]any) []model.ChartGraph {
var graphs []model.ChartGraph
for _, v := range nodes {
node := model.ChartGraph{
RowType: "node",
RowGroup: rowGroup,
ID: v["id"].(string),
X: v["x"].(float64),
Y: v["y"].(float64),
Type: v["type"].(string),
}
if comboId, ok := v["comboId"]; ok && comboId != nil {
node.ComboId = comboId.(string)
}
if depth, ok := v["depth"]; ok && depth != nil {
node.Depth = int64(depth.(float64))
}
if styleByte, err := json.Marshal(v["style"]); err == nil {
node.Style = string(styleByte)
}
// 元素大小
if sizeByte, err := json.Marshal(v["size"]); err == nil {
node.Size = string(sizeByte)
}
// 标签文本
if label, ok := v["label"]; ok && label != nil {
node.Label = label.(string)
}
if labelCfgByte, err := json.Marshal(v["labelCfg"]); err == nil {
node.LabelCfg = string(labelCfgByte)
}
// 三角形属性
if direction, ok := v["direction"]; ok && direction != nil && node.Type == "triangle" {
node.Direction = direction.(string)
}
// 图片属性
if img, ok := v["img"]; ok && img != nil {
node.Img = img.(string)
if clipCfgByte, err := json.Marshal(v["clipCfg"]); err == nil {
node.ClipCfg = string(clipCfgByte)
}
}
// 图标属性
if icon, ok := v["icon"]; ok && icon != nil {
if iconByte, err := json.Marshal(icon); err == nil {
node.Icon = string(iconByte)
}
}
graphs = append(graphs, node)
}
return graphs
}
// saveEdge 图数据Edge
func (s *ChartGraph) saveEdge(rowGroup string, edges []map[string]any) []model.ChartGraph {
var graphs []model.ChartGraph
for _, v := range edges {
edge := model.ChartGraph{
RowType: "edge",
RowGroup: rowGroup,
ID: v["id"].(string),
Source: v["source"].(string),
Target: v["target"].(string),
Type: v["type"].(string),
}
if styleByte, err := json.Marshal(v["style"]); err == nil {
edge.Style = string(styleByte)
}
// 标签文本
if label, ok := v["label"]; ok && label != nil {
edge.Label = label.(string)
}
if labelCfgByte, err := json.Marshal(v["labelCfg"]); err == nil {
edge.LabelCfg = string(labelCfgByte)
}
graphs = append(graphs, edge)
}
return graphs
}
// saveCombo 图数据Combo
func (s *ChartGraph) saveCombo(rowGroup string, combos []map[string]any) []model.ChartGraph {
var graphs []model.ChartGraph
for _, v := range combos {
combo := model.ChartGraph{
RowType: "combo",
RowGroup: rowGroup,
ID: v["id"].(string),
X: v["x"].(float64),
Y: v["y"].(float64),
Type: v["type"].(string),
}
if depth, ok := v["depth"]; ok && depth != nil {
combo.Depth = int64(depth.(float64))
}
if styleByte, err := json.Marshal(v["style"]); err == nil {
combo.Style = string(styleByte)
}
if paddingByte, err := json.Marshal(v["padding"]); err == nil {
combo.Padding = string(paddingByte)
}
if childrenByte, err := json.Marshal(v["children"]); err == nil {
combo.Children = string(childrenByte)
}
// 元素大小
if sizeByte, err := json.Marshal(v["size"]); err == nil {
combo.Size = string(sizeByte)
}
// 标签文本
if label, ok := v["label"]; ok && label != nil {
combo.Label = label.(string)
}
if labelCfgByte, err := json.Marshal(v["labelCfg"]); err == nil {
combo.LabelCfg = string(labelCfgByte)
}
graphs = append(graphs, combo)
}
return graphs
}
// Delete 删除所组图数据
func (s *ChartGraph) DeleteGroup(rowGroup string) int64 {
return s.graphRepository.DeleteGroup(rowGroup)
}

View File

@@ -1,359 +0,0 @@
package service
import (
"strings"
"be.ems/src/framework/utils/parse"
"be.ems/src/modules/chart/model"
chartRepository "be.ems/src/modules/chart/repository"
"github.com/goccy/go-json"
)
// 实例化服务层 ChartGraphImpl 结构体
var NewChartGraphImpl = &ChartGraphImpl{
graphRepository: chartRepository.NewChartGraphImpl,
}
// ChartGraphImpl G6关系图数据表 服务层处理
type ChartGraphImpl struct {
// G6关系图数据服务
graphRepository chartRepository.IChartGraph
}
// SelectGroup 查询组名
func (s *ChartGraphImpl) SelectGroup() []string {
return s.graphRepository.SelectGroup()
}
// LoadData 查询所组图数据
func (s *ChartGraphImpl) LoadData(rowGroup, rowType string) map[string]any {
// 查询数据
graph := model.ChartGraph{
RowGroup: rowGroup,
}
if rowType != "" {
graph.RowType = rowType
}
data := s.graphRepository.SelectList(graph)
// 数据项
nodes := []map[string]any{}
edges := []map[string]any{}
combos := []map[string]any{}
for _, v := range data {
if v.RowType == "node" {
nodes = append(nodes, s.loadNode(v))
}
if v.RowType == "edge" {
edges = append(edges, s.loadEdge(v))
}
if v.RowType == "combo" {
combos = append(combos, s.loadCombo(v))
}
}
return map[string]any{
"nodes": nodes,
"edges": edges,
"combos": combos,
}
}
// loadNode 图数据Node
func (s *ChartGraphImpl) loadNode(v model.ChartGraph) map[string]any {
node := map[string]any{
"id": v.ID,
"comboId": v.ComboID,
"x": v.X,
"y": v.Y,
"type": v.Type,
"depth": v.Depth,
}
// 元素样式
style := map[string]any{}
if len(v.Style) > 7 {
json.Unmarshal([]byte(v.Style), &style)
}
node["style"] = style
// 元素大小
if strings.Contains(v.Size, "[") {
sizeArr := []int64{}
json.Unmarshal([]byte(v.Size), &sizeArr)
node["size"] = sizeArr
} else {
node["size"] = parse.Number(v.Size)
}
// 标签文本
node["label"] = v.Label
labelCfg := map[string]any{}
if len(v.LabelCfg) > 7 {
json.Unmarshal([]byte(v.LabelCfg), &labelCfg)
}
node["labelCfg"] = labelCfg
// 三角形属性
if v.Type == "triangle" {
node["direction"] = v.Direction
}
// 图片属性
if strings.Index(v.Type, "image") == 0 {
node["img"] = v.Img
clipCfg := map[string]any{}
if len(v.ClipCfg) > 7 {
json.Unmarshal([]byte(v.ClipCfg), &clipCfg)
}
node["clipCfg"] = clipCfg
}
// 图标属性
if v.Icon != "" {
icon := map[string]any{}
if len(v.Icon) > 7 {
json.Unmarshal([]byte(v.Icon), &icon)
}
node["icon"] = icon
}
return node
}
// loadEdge 图数据Edge
func (s *ChartGraphImpl) loadEdge(v model.ChartGraph) map[string]any {
edge := map[string]any{
"id": v.ID,
"source": v.Source,
"target": v.Target,
"type": v.Type,
}
// 元素样式
style := map[string]any{}
if len(v.Style) > 7 {
json.Unmarshal([]byte(v.Style), &style)
}
edge["style"] = style
// 标签文本
edge["label"] = v.Label
labelCfg := map[string]any{}
if len(v.LabelCfg) > 7 {
json.Unmarshal([]byte(v.LabelCfg), &labelCfg)
}
edge["labelCfg"] = labelCfg
return edge
}
// loadCombo 图数据Combo
func (s *ChartGraphImpl) loadCombo(v model.ChartGraph) map[string]any {
combo := map[string]any{
"id": v.ID,
"x": v.X,
"y": v.Y,
"type": v.Type,
"depth": v.Depth,
}
// 元素样式
style := map[string]any{}
if len(v.Style) > 7 {
json.Unmarshal([]byte(v.Style), &style)
}
combo["style"] = style
// 元素大小
if strings.Contains(v.Size, "[") {
sizeArr := []int64{}
json.Unmarshal([]byte(v.Size), &sizeArr)
combo["size"] = sizeArr
} else {
combo["size"] = parse.Number(v.Size)
}
// 元素内边距
if strings.Contains(v.Padding, "[") {
paddingArr := []int64{}
json.Unmarshal([]byte(v.Padding), &paddingArr)
combo["padding"] = paddingArr
} else {
combo["padding"] = parse.Number(v.Padding)
}
// 标签文本
combo["label"] = v.Label
labelCfg := map[string]any{}
if len(v.LabelCfg) > 7 {
json.Unmarshal([]byte(v.LabelCfg), &labelCfg)
}
combo["labelCfg"] = labelCfg
// 分组内元素
if v.Children != "" {
children := []map[string]any{}
if len(v.Children) > 7 {
json.Unmarshal([]byte(v.Children), &children)
}
combo["children"] = children
}
return combo
}
// SaveData 添加组图数据
func (s *ChartGraphImpl) SaveData(rowGroup string, data map[string]any) int64 {
graphs := []model.ChartGraph{}
nodes := data["nodes"].([]map[string]any)
graphNodes := s.saveNode(rowGroup, nodes)
graphs = append(graphs, graphNodes...)
edges := data["edges"].([]map[string]any)
graphEdges := s.saveEdge(rowGroup, edges)
graphs = append(graphs, graphEdges...)
combos := data["combos"].([]map[string]any)
graphCombos := s.saveCombo(rowGroup, combos)
graphs = append(graphs, graphCombos...)
// 删除组数据后插入
if len(graphs) > 0 {
s.graphRepository.DeleteGroup(rowGroup)
}
return s.graphRepository.Inserts(graphs)
}
// saveNode 图数据Node
func (s *ChartGraphImpl) saveNode(rowGroup string, nodes []map[string]any) []model.ChartGraph {
var graphs []model.ChartGraph
for _, v := range nodes {
node := model.ChartGraph{
RowType: "node",
RowGroup: rowGroup,
ID: v["id"].(string),
X: v["x"].(float64),
Y: v["y"].(float64),
Type: v["type"].(string),
}
if comboId, ok := v["comboId"]; ok && comboId != nil {
node.ComboID = comboId.(string)
}
if depth, ok := v["depth"]; ok && depth != nil {
node.Depth = int(depth.(float64))
}
if styleByte, err := json.Marshal(v["style"]); err == nil {
node.Style = string(styleByte)
}
// 元素大小
if sizeByte, err := json.Marshal(v["size"]); err == nil {
node.Size = string(sizeByte)
}
// 标签文本
if label, ok := v["label"]; ok && label != nil {
node.Label = label.(string)
}
if labelCfgByte, err := json.Marshal(v["labelCfg"]); err == nil {
node.LabelCfg = string(labelCfgByte)
}
// 三角形属性
if direction, ok := v["direction"]; ok && direction != nil && node.Type == "triangle" {
node.Direction = direction.(string)
}
// 图片属性
if img, ok := v["img"]; ok && img != nil {
node.Img = img.(string)
if clipCfgByte, err := json.Marshal(v["clipCfg"]); err == nil {
node.ClipCfg = string(clipCfgByte)
}
}
// 图标属性
if icon, ok := v["icon"]; ok && icon != nil {
if iconByte, err := json.Marshal(icon); err == nil {
node.Icon = string(iconByte)
}
}
graphs = append(graphs, node)
}
return graphs
}
// saveEdge 图数据Edge
func (s *ChartGraphImpl) saveEdge(rowGroup string, edges []map[string]any) []model.ChartGraph {
var graphs []model.ChartGraph
for _, v := range edges {
edge := model.ChartGraph{
RowType: "edge",
RowGroup: rowGroup,
ID: v["id"].(string),
Source: v["source"].(string),
Target: v["target"].(string),
Type: v["type"].(string),
}
if styleByte, err := json.Marshal(v["style"]); err == nil {
edge.Style = string(styleByte)
}
// 标签文本
if label, ok := v["label"]; ok && label != nil {
edge.Label = label.(string)
}
if labelCfgByte, err := json.Marshal(v["labelCfg"]); err == nil {
edge.LabelCfg = string(labelCfgByte)
}
graphs = append(graphs, edge)
}
return graphs
}
// saveCombo 图数据Combo
func (s *ChartGraphImpl) saveCombo(rowGroup string, combos []map[string]any) []model.ChartGraph {
var graphs []model.ChartGraph
for _, v := range combos {
combo := model.ChartGraph{
RowType: "combo",
RowGroup: rowGroup,
ID: v["id"].(string),
X: v["x"].(float64),
Y: v["y"].(float64),
Type: v["type"].(string),
}
if depth, ok := v["depth"]; ok && depth != nil {
combo.Depth = int(depth.(float64))
}
if styleByte, err := json.Marshal(v["style"]); err == nil {
combo.Style = string(styleByte)
}
if paddingByte, err := json.Marshal(v["padding"]); err == nil {
combo.Padding = string(paddingByte)
}
if childrenByte, err := json.Marshal(v["children"]); err == nil {
combo.Children = string(childrenByte)
}
// 元素大小
if sizeByte, err := json.Marshal(v["size"]); err == nil {
combo.Size = string(sizeByte)
}
// 标签文本
if label, ok := v["label"]; ok && label != nil {
combo.Label = label.(string)
}
if labelCfgByte, err := json.Marshal(v["labelCfg"]); err == nil {
combo.LabelCfg = string(labelCfgByte)
}
graphs = append(graphs, combo)
}
return graphs
}
// Delete 删除所组图数据
func (s *ChartGraphImpl) DeleteGroup(rowGroup string) int64 {
return s.graphRepository.DeleteGroup(rowGroup)
}