feat: 更新多个模块以支持新的数据结构和日志格式
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"be.ems/src/framework/i18n"
|
||||
"be.ems/src/framework/utils/ctx"
|
||||
"be.ems/src/framework/vo/result"
|
||||
"be.ems/src/framework/reqctx"
|
||||
"be.ems/src/framework/resp"
|
||||
"be.ems/src/modules/chart/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
)
|
||||
|
||||
// 实例化控制层 ChartGraphController 结构体
|
||||
@@ -36,7 +38,7 @@ type ChartGraphController struct {
|
||||
// @Router /chart/graph/groups [get]
|
||||
func (s *ChartGraphController) GroupNames(c *gin.Context) {
|
||||
data := s.chartGraphService.SelectGroup()
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
}
|
||||
|
||||
// 获取关系图数据
|
||||
@@ -47,25 +49,25 @@ func (s *ChartGraphController) GroupNames(c *gin.Context) {
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param group query string true "Group"
|
||||
// @Param type query string true "Type" Enums(node, edge, combo)
|
||||
// @Param type query string false "Type" Enums(node, edge, combo)
|
||||
// @Success 200 {object} object "Response Results"
|
||||
// @Security TokenAuth
|
||||
// @Summary Getting Relationship Map Data
|
||||
// @Description Getting Relationship Map Data
|
||||
// @Router /chart/graph [get]
|
||||
func (s *ChartGraphController) Load(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var querys struct {
|
||||
Group string `form:"group" binding:"required"`
|
||||
Type string `form:"type" binding:"omitempty,oneof=node edge combo"`
|
||||
}
|
||||
if err := c.ShouldBindQuery(&querys); err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
data := s.chartGraphService.LoadData(querys.Group, querys.Type)
|
||||
c.JSON(200, result.OkData(data))
|
||||
c.JSON(200, resp.OkData(data))
|
||||
}
|
||||
|
||||
// 保存关系图数据
|
||||
@@ -80,9 +82,8 @@ func (s *ChartGraphController) Load(c *gin.Context) {
|
||||
// @Security TokenAuth
|
||||
// @Summary Saving Relationship Diagram Data
|
||||
// @Description Saving Relationship Diagram Data
|
||||
// @Router /chart/graph/ [post]
|
||||
// @Router /chart/graph [post]
|
||||
func (s *ChartGraphController) Save(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body struct {
|
||||
Group string `json:"group" binding:"required"`
|
||||
Data struct {
|
||||
@@ -91,9 +92,9 @@ func (s *ChartGraphController) Save(c *gin.Context) {
|
||||
Combos []map[string]any `json:"combos" binding:"required"`
|
||||
} `json:"data" binding:"required"`
|
||||
}
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
if err := c.ShouldBindBodyWithJSON(&body); err != nil {
|
||||
errMsgs := fmt.Sprintf("bind err: %s", resp.FormatBindError(err))
|
||||
c.JSON(422, resp.CodeMsg(40422, errMsgs))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -104,10 +105,10 @@ func (s *ChartGraphController) Save(c *gin.Context) {
|
||||
}
|
||||
saveNum := s.chartGraphService.SaveData(body.Group, data)
|
||||
if saveNum > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
// 删除关系图数据
|
||||
@@ -124,17 +125,17 @@ func (s *ChartGraphController) Save(c *gin.Context) {
|
||||
// @Description Deleting Relationship Diagram Data
|
||||
// @Router /chart/graph/{group} [delete]
|
||||
func (s *ChartGraphController) Delete(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
language := reqctx.AcceptLanguage(c)
|
||||
group := c.Param("group")
|
||||
if group == "" {
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
c.JSON(400, resp.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
deleteNum := s.chartGraphService.DeleteGroup(group)
|
||||
if deleteNum > 0 {
|
||||
c.JSON(200, result.Ok(nil))
|
||||
c.JSON(200, resp.Ok(nil))
|
||||
return
|
||||
}
|
||||
c.JSON(200, result.Err(nil))
|
||||
c.JSON(200, resp.Err(nil))
|
||||
}
|
||||
|
||||
@@ -2,28 +2,28 @@ package model
|
||||
|
||||
// ChartGraph 图表-G6关系图数据对象 chart_graph
|
||||
type ChartGraph struct {
|
||||
RowID int64 `json:"rowId,omitempty" gorm:"column:row_id;primaryKey;autoIncrement"` // 记录ID
|
||||
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配置
|
||||
RowId int64 `json:"rowId" gorm:"column:row_id;primaryKey;autoIncrement"` // 记录ID
|
||||
RowType string `json:"rowType" gorm:"column:row_type"` // 记录类型
|
||||
RowGroup string `json:"rowGroup" gorm:"column:row_group"` // 记录组名
|
||||
ID string `json:"id" gorm:"column:id"` // 元素ID
|
||||
Type string `json:"type" gorm:"column:type"` // node/combo 类型
|
||||
Depth int64 `json:"depth" gorm:"column:depth"` // node/combo 深度
|
||||
X float64 `json:"x" gorm:"column:x"` // node/combo 横向坐标
|
||||
Y float64 `json:"y" gorm:"column:y"` // node/combo 纵向坐标
|
||||
Size string `json:"size" gorm:"column:size"` // node/combo 大小-JSON数组
|
||||
Icon string `json:"icon" gorm:"column:icon"` // node-部分类型支持图标JSON配置
|
||||
Img string `json:"img" gorm:"column:img"` // node-img 图片
|
||||
ClipCfg string `json:"clipCfg" gorm:"column:clip_cfg"` // node-img 图片裁剪JSON配置
|
||||
Direction string `json:"direction" gorm:"column:direction"` // node-triangle 三角形的方向
|
||||
Source string `json:"source" gorm:"column:source"` // edge-边起始
|
||||
Target string `json:"target" gorm:"column:target"` // edge-边目标
|
||||
ComboId string `json:"comboId" gorm:"column:combo_id"` // combo-分组
|
||||
Padding string `json:"padding" gorm:"column:padding"` // combo-JSON分组内边距
|
||||
ParentId string `json:"parentId" gorm:"column:parent_id"` // combo-父级分组
|
||||
Children string `json:"children" gorm:"column:children"` // combo-JSON分组内含元素
|
||||
Style string `json:"style" gorm:"column:style"` // 元素样式-JONS配置
|
||||
Label string `json:"label" gorm:"column:label"` // 标签文本
|
||||
LabelCfg string `json:"labelCfg" gorm:"column:label_cfg"` // 标签文本-JSON配置
|
||||
}
|
||||
|
||||
// TableName 表名称
|
||||
|
||||
@@ -1,183 +1,83 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"be.ems/src/framework/datasource"
|
||||
"be.ems/src/framework/database/db"
|
||||
"be.ems/src/framework/logger"
|
||||
"be.ems/src/framework/utils/parse"
|
||||
"be.ems/src/framework/utils/repo"
|
||||
"be.ems/src/modules/chart/model"
|
||||
)
|
||||
|
||||
// 实例化数据层 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`,
|
||||
|
||||
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",
|
||||
},
|
||||
}
|
||||
var NewChartGraph = &ChartGraph{}
|
||||
|
||||
// ChartGraph G6关系图数据表 数据层处理
|
||||
type ChartGraph struct {
|
||||
// 查询视图对象SQL
|
||||
selectSql string
|
||||
// 结果字段与实体映射
|
||||
resultMap map[string]string
|
||||
}
|
||||
type ChartGraph struct{}
|
||||
|
||||
// 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 {
|
||||
// SelectByPage 分页查询集合
|
||||
func (r ChartGraph) SelectByPage(query map[string]string) ([]model.ChartGraph, int64) {
|
||||
tx := db.DB("").Model(&model.ChartGraph{})
|
||||
// 查询条件拼接
|
||||
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), " "))
|
||||
tx = tx.Where("row_type = ?", v)
|
||||
}
|
||||
if v, ok := query["rowGroup"]; ok && v != "" {
|
||||
conditions = append(conditions, "row_group = ?")
|
||||
params = append(params, strings.Trim(v.(string), " "))
|
||||
tx = tx.Where("row_group = ?", v)
|
||||
}
|
||||
|
||||
// 构建查询条件语句
|
||||
whereSql := ""
|
||||
if len(conditions) > 0 {
|
||||
whereSql += " where " + strings.Join(conditions, " and ")
|
||||
// 查询结果
|
||||
var total int64 = 0
|
||||
rows := []model.ChartGraph{}
|
||||
|
||||
// 查询数量为0直接返回
|
||||
if err := tx.Count(&total).Error; err != nil || total <= 0 {
|
||||
return rows, total
|
||||
}
|
||||
|
||||
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)
|
||||
// 查询数据分页
|
||||
pageNum, pageSize := db.PageNumSize(query["pageNum"], query["pageSize"])
|
||||
tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
|
||||
err := tx.Find(&rows).Error
|
||||
if err != nil {
|
||||
logger.Errorf("total err => %v", err)
|
||||
return result
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows, total
|
||||
}
|
||||
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
|
||||
return rows, total
|
||||
}
|
||||
|
||||
// SelectList 根据实体查询
|
||||
func (r *ChartGraph) SelectList(graph model.ChartGraph) []model.ChartGraph {
|
||||
// Select 查询集合
|
||||
func (r ChartGraph) Select(param model.ChartGraph) []model.ChartGraph {
|
||||
tx := db.DB("").Model(&model.ChartGraph{})
|
||||
// 查询条件拼接
|
||||
var conditions []string
|
||||
var params []any
|
||||
if graph.RowType != "" {
|
||||
conditions = append(conditions, "row_type = ?")
|
||||
params = append(params, graph.RowType)
|
||||
if param.RowType != "" {
|
||||
tx = tx.Where("row_type = ?", param.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 ")
|
||||
if param.RowGroup != "" {
|
||||
tx = tx.Where("row_group = ?", param.RowGroup)
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
querySql := r.selectSql + whereSql + " order by depth asc "
|
||||
results, err := datasource.RawDB("", querySql, params)
|
||||
if err != nil {
|
||||
logger.Errorf("query err => %v", err)
|
||||
rows := []model.ChartGraph{}
|
||||
if err := tx.Order("depth asc").Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
return rows
|
||||
}
|
||||
|
||||
// 转换实体
|
||||
return r.convertResultRows(results)
|
||||
return rows
|
||||
}
|
||||
|
||||
// SelectGroup 查询组名
|
||||
func (r *ChartGraph) SelectGroup() []string {
|
||||
func (r ChartGraph) SelectGroup() []string {
|
||||
tx := db.DB("").Model(&model.ChartGraph{})
|
||||
// 查询数据
|
||||
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)
|
||||
if err := tx.Select("row_group").Group("row_group").Find(&rows).Error; err != nil {
|
||||
logger.Errorf("query find err => %v", err.Error())
|
||||
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)
|
||||
func (r ChartGraph) Inserts(graphs []model.ChartGraph) int64 {
|
||||
tx := db.DB("").CreateInBatches(graphs, 2000)
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("CreateInBatches err => %v", err)
|
||||
}
|
||||
@@ -185,8 +85,8 @@ func (r *ChartGraph) Inserts(graphs []model.ChartGraph) int64 {
|
||||
}
|
||||
|
||||
// Delete 删除组数据
|
||||
func (r *ChartGraph) DeleteGroup(rowGroup string) int64 {
|
||||
tx := datasource.DefaultDB().Where("row_group = ?", rowGroup).Delete(&model.ChartGraph{})
|
||||
func (r ChartGraph) DeleteGroup(rowGroup string) int64 {
|
||||
tx := db.DB("").Where("row_group = ?", rowGroup).Delete(&model.ChartGraph{})
|
||||
if err := tx.Error; err != nil {
|
||||
logger.Errorf("Delete err => %v", err)
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ func (s *ChartGraph) LoadData(rowGroup, rowType string) map[string]any {
|
||||
if rowType != "" {
|
||||
graph.RowType = rowType
|
||||
}
|
||||
data := s.graphRepository.SelectList(graph)
|
||||
data := s.graphRepository.Select(graph)
|
||||
|
||||
// 数据项
|
||||
nodes := []map[string]any{}
|
||||
|
||||
Reference in New Issue
Block a user