Skip to content

Commit 11aa8c5

Browse files
author
piexlMax(奇淼
committed
feat(version): 添加字典数据支持到版本管理功能
1 parent d8e6f2f commit 11aa8c5

File tree

6 files changed

+243
-71
lines changed

6 files changed

+243
-71
lines changed

server/api/v1/system/sys_version.go

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,17 @@ func (sysVersionApi *SysVersionApi) ExportVersion(c *gin.Context) {
267267
}
268268
}
269269

270+
// 获取选中的字典数据
271+
var dictData []system.SysDictionary
272+
if len(req.DictIds) > 0 {
273+
dictData, err = sysVersionService.GetDictionariesByIds(ctx, req.DictIds)
274+
if err != nil {
275+
global.GVA_LOG.Error("获取字典数据失败!", zap.Error(err))
276+
response.FailWithMessage("获取字典数据失败:"+err.Error(), c)
277+
return
278+
}
279+
}
280+
270281
// 处理菜单数据,构建递归的children结构
271282
processedMenus := buildMenuTree(menuData)
272283

@@ -282,6 +293,34 @@ func (sysVersionApi *SysVersionApi) ExportVersion(c *gin.Context) {
282293
processedApis = append(processedApis, cleanApi)
283294
}
284295

296+
// 处理字典数据,清除ID和时间戳字段,包含字典详情
297+
processedDicts := make([]system.SysDictionary, 0, len(dictData))
298+
for _, dict := range dictData {
299+
cleanDict := system.SysDictionary{
300+
Name: dict.Name,
301+
Type: dict.Type,
302+
Status: dict.Status,
303+
Desc: dict.Desc,
304+
}
305+
306+
// 处理字典详情数据,清除ID和时间戳字段
307+
cleanDetails := make([]system.SysDictionaryDetail, 0, len(dict.SysDictionaryDetails))
308+
for _, detail := range dict.SysDictionaryDetails {
309+
cleanDetail := system.SysDictionaryDetail{
310+
Label: detail.Label,
311+
Value: detail.Value,
312+
Extend: detail.Extend,
313+
Status: detail.Status,
314+
Sort: detail.Sort,
315+
// 不复制 ID, CreatedAt, UpdatedAt, SysDictionaryID
316+
}
317+
cleanDetails = append(cleanDetails, cleanDetail)
318+
}
319+
cleanDict.SysDictionaryDetails = cleanDetails
320+
321+
processedDicts = append(processedDicts, cleanDict)
322+
}
323+
285324
// 构建导出数据
286325
exportData := systemRes.ExportVersionResponse{
287326
Version: systemReq.VersionInfo{
@@ -290,8 +329,9 @@ func (sysVersionApi *SysVersionApi) ExportVersion(c *gin.Context) {
290329
Description: req.Description,
291330
ExportTime: time.Now().Format("2006-01-02 15:04:05"),
292331
},
293-
Menus: processedMenus,
294-
Apis: processedApis,
332+
Menus: processedMenus,
333+
Apis: processedApis,
334+
Dictionaries: processedDicts,
295335
}
296336

297337
// 转换为JSON
@@ -418,6 +458,15 @@ func (sysVersionApi *SysVersionApi) ImportVersion(c *gin.Context) {
418458
}
419459
}
420460

461+
// 导入字典数据
462+
if len(importData.ExportDictionary) > 0 {
463+
if err := sysVersionService.ImportDictionaries(importData.ExportDictionary); err != nil {
464+
global.GVA_LOG.Error("导入字典失败!", zap.Error(err))
465+
response.FailWithMessage("导入字典失败: "+err.Error(), c)
466+
return
467+
}
468+
}
469+
421470
// 创建导入记录
422471
jsonData, _ := json.Marshal(importData)
423472
version := system.SysVersion{

server/model/system/request/sys_version.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ type ExportVersionRequest struct {
2020
Description string `json:"description"` // 版本描述
2121
MenuIds []uint `json:"menuIds"` // 选中的菜单ID列表
2222
ApiIds []uint `json:"apiIds"` // 选中的API ID列表
23+
DictIds []uint `json:"dictIds"` // 选中的字典ID列表
2324
}
2425

2526
// ImportVersionRequest 导入版本请求结构体
2627
type ImportVersionRequest struct {
27-
VersionInfo VersionInfo `json:"version" binding:"required"` // 版本信息
28-
ExportMenu []system.SysBaseMenu `json:"menus"` // 菜单数据,直接复用SysBaseMenu
29-
ExportApi []system.SysApi `json:"apis"` // API数据,直接复用SysApi
28+
VersionInfo VersionInfo `json:"version" binding:"required"` // 版本信息
29+
ExportMenu []system.SysBaseMenu `json:"menus"` // 菜单数据,直接复用SysBaseMenu
30+
ExportApi []system.SysApi `json:"apis"` // API数据,直接复用SysApi
31+
ExportDictionary []system.SysDictionary `json:"dictionaries"` // 字典数据,直接复用SysDictionary
3032
}
3133

3234
// VersionInfo 版本信息结构体

server/model/system/response/sys_version.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77

88
// ExportVersionResponse 导出版本响应结构体
99
type ExportVersionResponse struct {
10-
Version request.VersionInfo `json:"version"` // 版本信息
11-
Menus []system.SysBaseMenu `json:"menus"` // 菜单数据,直接复用SysBaseMenu
12-
Apis []system.SysApi `json:"apis"` // API数据,直接复用SysApi
13-
}
10+
Version request.VersionInfo `json:"version"` // 版本信息
11+
Menus []system.SysBaseMenu `json:"menus"` // 菜单数据,直接复用SysBaseMenu
12+
Apis []system.SysApi `json:"apis"` // API数据,直接复用SysApi
13+
Dictionaries []system.SysDictionary `json:"dictionaries"` // 字典数据,直接复用SysDictionary
14+
}

server/service/system/sys_version.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ func (sysVersionService *SysVersionService) GetApisByIds(ctx context.Context, id
8686
return
8787
}
8888

89+
// GetDictionariesByIds 根据ID列表获取字典数据
90+
func (sysVersionService *SysVersionService) GetDictionariesByIds(ctx context.Context, ids []uint) (dictionaries []system.SysDictionary, err error) {
91+
err = global.GVA_DB.Where("id in ?", ids).Preload("SysDictionaryDetails").Find(&dictionaries).Error
92+
return
93+
}
94+
8995
// ImportMenus 导入菜单数据
9096
func (sysVersionService *SysVersionService) ImportMenus(ctx context.Context, menus []system.SysBaseMenu) error {
9197
return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
@@ -194,3 +200,31 @@ func (sysVersionService *SysVersionService) ImportApis(apis []system.SysApi) err
194200
return nil
195201
})
196202
}
203+
204+
// ImportDictionaries 导入字典数据
205+
func (sysVersionService *SysVersionService) ImportDictionaries(dictionaries []system.SysDictionary) error {
206+
return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
207+
for _, dict := range dictionaries {
208+
// 检查字典是否已存在
209+
var existingDict system.SysDictionary
210+
if err := tx.Where("type = ?", dict.Type).First(&existingDict).Error; err == nil {
211+
// 字典已存在,跳过
212+
continue
213+
}
214+
215+
// 创建新字典
216+
newDict := system.SysDictionary{
217+
Name: dict.Name,
218+
Type: dict.Type,
219+
Status: dict.Status,
220+
Desc: dict.Desc,
221+
SysDictionaryDetails: dict.SysDictionaryDetails,
222+
}
223+
224+
if err := tx.Create(&newDict).Error; err != nil {
225+
return err
226+
}
227+
}
228+
return nil
229+
})
230+
}

server/source/system/dictionary_detail.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,23 @@ func (i *initDictDetail) InitializeData(ctx context.Context) (context.Context, e
6666
}
6767

6868
dicts[2].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
69-
{Label: "date", Status: &True},
69+
{Label: "date", Value: "0", Status: &True, Extend: "mysql", Sort: 0},
7070
{Label: "time", Value: "1", Status: &True, Extend: "mysql", Sort: 1},
7171
{Label: "year", Value: "2", Status: &True, Extend: "mysql", Sort: 2},
7272
{Label: "datetime", Value: "3", Status: &True, Extend: "mysql", Sort: 3},
7373
{Label: "timestamp", Value: "5", Status: &True, Extend: "mysql", Sort: 5},
7474
{Label: "timestamptz", Value: "6", Status: &True, Extend: "pgsql", Sort: 5},
7575
}
7676
dicts[3].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
77-
{Label: "float", Status: &True},
77+
{Label: "float", Value: "0", Status: &True, Extend: "mysql", Sort: 0},
7878
{Label: "double", Value: "1", Status: &True, Extend: "mysql", Sort: 1},
7979
{Label: "decimal", Value: "2", Status: &True, Extend: "mysql", Sort: 2},
8080
{Label: "numeric", Value: "3", Status: &True, Extend: "pgsql", Sort: 3},
8181
{Label: "smallserial", Value: "4", Status: &True, Extend: "pgsql", Sort: 4},
8282
}
8383

8484
dicts[4].SysDictionaryDetails = []sysModel.SysDictionaryDetail{
85-
{Label: "char", Status: &True},
85+
{Label: "char", Value: "0", Status: &True, Extend: "mysql", Sort: 0},
8686
{Label: "varchar", Value: "1", Status: &True, Extend: "mysql", Sort: 1},
8787
{Label: "tinyblob", Value: "2", Status: &True, Extend: "mysql", Sort: 2},
8888
{Label: "tinytext", Value: "3", Status: &True, Extend: "mysql", Sort: 3},

0 commit comments

Comments
 (0)