优化
This commit is contained in:
+153
-18
@@ -12,6 +12,123 @@ import (
|
||||
type VersionService struct{ uc *biz.VersionUsecase }
|
||||
|
||||
func NewVersionService(uc *biz.VersionUsecase) *VersionService { return &VersionService{uc: uc} }
|
||||
|
||||
type versionExportInfo struct {
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
Description string `json:"description"`
|
||||
ExportTime string `json:"exportTime"`
|
||||
}
|
||||
|
||||
type versionExportMenuParameter struct {
|
||||
ID uint `json:"ID"`
|
||||
CreatedAt time.Time `json:"CreatedAt"`
|
||||
UpdatedAt time.Time `json:"UpdatedAt"`
|
||||
SysBaseMenuID uint `json:"SysBaseMenuID"`
|
||||
Type string `json:"type"`
|
||||
Key string `json:"key"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
type versionExportMenuButton struct {
|
||||
ID uint `json:"ID"`
|
||||
CreatedAt time.Time `json:"CreatedAt"`
|
||||
UpdatedAt time.Time `json:"UpdatedAt"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"desc"`
|
||||
SysBaseMenuID uint `json:"sysBaseMenuID"`
|
||||
}
|
||||
|
||||
type versionExportMenu struct {
|
||||
ID uint `json:"ID"`
|
||||
CreatedAt time.Time `json:"CreatedAt"`
|
||||
UpdatedAt time.Time `json:"UpdatedAt"`
|
||||
ParentID uint `json:"parentId"`
|
||||
Path string `json:"path"`
|
||||
Name string `json:"name"`
|
||||
Hidden bool `json:"hidden"`
|
||||
Component string `json:"component"`
|
||||
Sort int `json:"sort"`
|
||||
Meta dto.VersionMenuMetaRequest `json:"meta"`
|
||||
Authorities []any `json:"authoritys"`
|
||||
Children []*versionExportMenu `json:"children"`
|
||||
Parameters []versionExportMenuParameter `json:"parameters"`
|
||||
MenuButtons []versionExportMenuButton `json:"menuBtn"`
|
||||
}
|
||||
|
||||
type versionExportAPI struct {
|
||||
ID uint `json:"ID"`
|
||||
CreatedAt time.Time `json:"CreatedAt"`
|
||||
UpdatedAt time.Time `json:"UpdatedAt"`
|
||||
Path string `json:"path"`
|
||||
Description string `json:"description"`
|
||||
APIGroup string `json:"apiGroup"`
|
||||
Method string `json:"method"`
|
||||
}
|
||||
|
||||
type versionExportDictionaryDetail struct {
|
||||
ID uint `json:"ID"`
|
||||
CreatedAt time.Time `json:"CreatedAt"`
|
||||
UpdatedAt time.Time `json:"UpdatedAt"`
|
||||
Label string `json:"label"`
|
||||
Value string `json:"value"`
|
||||
Extend string `json:"extend"`
|
||||
Status *bool `json:"status"`
|
||||
Sort int `json:"sort"`
|
||||
DictionaryID int `json:"sysDictionaryID"`
|
||||
ParentID *uint `json:"parentID"`
|
||||
Children []versionExportDictionaryDetail `json:"children"`
|
||||
Level int `json:"level"`
|
||||
Path string `json:"path"`
|
||||
Disabled bool `json:"disabled"`
|
||||
}
|
||||
|
||||
type versionExportDictionary struct {
|
||||
ID uint `json:"ID"`
|
||||
CreatedAt time.Time `json:"CreatedAt"`
|
||||
UpdatedAt time.Time `json:"UpdatedAt"`
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
Status *bool `json:"status"`
|
||||
Desc string `json:"desc"`
|
||||
ParentID *uint `json:"parentID"`
|
||||
Children []versionExportDictionary `json:"children"`
|
||||
Details []versionExportDictionaryDetail `json:"sysDictionaryDetails"`
|
||||
}
|
||||
|
||||
type versionExportDocument struct {
|
||||
Version versionExportInfo `json:"version"`
|
||||
Menus []*versionExportMenu `json:"menus"`
|
||||
APIs []versionExportAPI `json:"apis"`
|
||||
Dictionaries []versionExportDictionary `json:"dictionaries"`
|
||||
}
|
||||
|
||||
func exportMenu(value *biz.Menu) *versionExportMenu {
|
||||
menu := &versionExportMenu{
|
||||
Path: value.Path, Name: value.Name, Hidden: value.Hidden, Component: value.Component, Sort: value.Sort,
|
||||
Meta: dto.VersionMenuMetaRequest{ActiveName: value.ActiveName, KeepAlive: value.KeepAlive, DefaultMenu: value.DefaultMenu, Title: value.Title, Icon: value.Icon, CloseTab: value.CloseTab, TransitionType: value.TransitionType},
|
||||
}
|
||||
if len(value.Parameters) > 0 {
|
||||
menu.Parameters = make([]versionExportMenuParameter, 0, len(value.Parameters))
|
||||
for _, parameter := range value.Parameters {
|
||||
menu.Parameters = append(menu.Parameters, versionExportMenuParameter{Type: parameter.Type, Key: parameter.Key, Value: parameter.Value})
|
||||
}
|
||||
}
|
||||
if len(value.Buttons) > 0 {
|
||||
menu.MenuButtons = make([]versionExportMenuButton, 0, len(value.Buttons))
|
||||
for _, button := range value.Buttons {
|
||||
menu.MenuButtons = append(menu.MenuButtons, versionExportMenuButton{Name: button.Name, Description: button.Description})
|
||||
}
|
||||
}
|
||||
if len(value.Children) > 0 {
|
||||
menu.Children = make([]*versionExportMenu, 0, len(value.Children))
|
||||
for _, child := range value.Children {
|
||||
menu.Children = append(menu.Children, exportMenu(child))
|
||||
}
|
||||
}
|
||||
return menu
|
||||
}
|
||||
|
||||
func versionDTO(v *biz.Version) *dto.VersionResponse {
|
||||
return &dto.VersionResponse{ID: v.ID, CreatedAt: v.CreatedAt, UpdatedAt: v.UpdatedAt, DeletedAt: nil, VersionName: v.Name, VersionCode: v.Code, Description: v.Description, VersionData: v.Data}
|
||||
}
|
||||
@@ -38,7 +155,19 @@ func (s *VersionService) VersionData(ctx context.Context, id uint) (string, stri
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
return v.Data, v.Code, nil
|
||||
if v.Data != "" {
|
||||
return v.Data, v.Code, nil
|
||||
}
|
||||
payload := versionExportDocument{
|
||||
Version: versionExportInfo{Name: v.Name, Code: v.Code, Description: v.Description, ExportTime: v.CreatedAt.Format("2006-01-02 15:04:05")},
|
||||
Menus: []*versionExportMenu{},
|
||||
APIs: []versionExportAPI{},
|
||||
}
|
||||
raw, marshalErr := json.MarshalIndent(payload, "", " ")
|
||||
if marshalErr != nil {
|
||||
return "", "", marshalErr
|
||||
}
|
||||
return string(raw), v.Code, nil
|
||||
}
|
||||
func (s *VersionService) DeleteVersions(ctx context.Context, ids []uint) error {
|
||||
return s.uc.DeleteVersions(ctx, ids)
|
||||
@@ -53,24 +182,34 @@ func (s *VersionService) Export(ctx context.Context, name, code, description str
|
||||
bundle.Code = code
|
||||
bundle.Description = description
|
||||
bundle.ExportTime = time.Now().Format("2006-01-02 15:04:05")
|
||||
menus := make([]*dto.MenuResponse, 0, len(bundle.Menus))
|
||||
for _, v := range bundle.Menus {
|
||||
menus = append(menus, menuResponse(v))
|
||||
var menus []*versionExportMenu
|
||||
if bundle.Menus != nil {
|
||||
menus = make([]*versionExportMenu, 0, len(bundle.Menus))
|
||||
for _, value := range bundle.Menus {
|
||||
menus = append(menus, exportMenu(value))
|
||||
}
|
||||
}
|
||||
apis := make([]*dto.APIResponse, 0, len(bundle.APIs))
|
||||
for _, v := range bundle.APIs {
|
||||
apis = append(apis, apiResponse(v))
|
||||
apis := make([]versionExportAPI, 0, len(bundle.APIs))
|
||||
for _, value := range bundle.APIs {
|
||||
apis = append(apis, versionExportAPI{Path: value.Path, Description: value.Description, APIGroup: value.APIGroup, Method: value.Method})
|
||||
}
|
||||
dicts := make([]*dto.DictionaryResponse, 0, len(bundle.Dictionaries))
|
||||
for _, v := range bundle.Dictionaries {
|
||||
dicts = append(dicts, dictionaryDTO(v))
|
||||
dictionaries := make([]versionExportDictionary, 0, len(bundle.Dictionaries))
|
||||
for _, value := range bundle.Dictionaries {
|
||||
details := make([]versionExportDictionaryDetail, 0, len(value.Details))
|
||||
for _, detail := range value.Details {
|
||||
details = append(details, versionExportDictionaryDetail{Label: detail.Label, Value: detail.Value, Extend: detail.Extend, Status: detail.Status, Sort: detail.Sort})
|
||||
}
|
||||
dictionaries = append(dictionaries, versionExportDictionary{Name: value.Name, Type: value.Type, Status: value.Status, Desc: value.Desc, Details: details})
|
||||
}
|
||||
payload := map[string]any{"version": map[string]any{"name": name, "code": code, "description": description, "exportTime": bundle.ExportTime}, "menus": menus, "apis": apis, "dictionaries": dicts}
|
||||
payload := versionExportDocument{Version: versionExportInfo{Name: name, Code: code, Description: description, ExportTime: bundle.ExportTime}, Menus: menus, APIs: apis, Dictionaries: dictionaries}
|
||||
raw, err := json.MarshalIndent(payload, "", " ")
|
||||
if err != nil {
|
||||
return err
|
||||
return &biz.VersionStageError{Stage: biz.VersionStageJSON, Err: err}
|
||||
}
|
||||
return s.uc.CreateVersion(ctx, &biz.Version{Name: name, Code: code, Description: description, Data: string(raw)})
|
||||
if err = s.uc.CreateVersion(ctx, &biz.Version{Name: name, Code: code, Description: description, Data: string(raw)}); err != nil {
|
||||
return &biz.VersionStageError{Stage: biz.VersionStageSave, Err: err}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (s *VersionService) Import(ctx context.Context, bundle *biz.VersionBundle) error {
|
||||
return s.uc.ImportVersionBundle(ctx, bundle)
|
||||
@@ -102,11 +241,7 @@ func (s *VersionService) ImportRequest(ctx context.Context, req *dto.ImportVersi
|
||||
for _, dictionary := range req.Dictionaries {
|
||||
item := &biz.Dictionary{Name: dictionary.Name, Type: dictionary.Type, Status: dictionary.Status, Desc: dictionary.Description}
|
||||
for _, detail := range dictionary.Details {
|
||||
status := true
|
||||
if detail.Status != nil {
|
||||
status = *detail.Status
|
||||
}
|
||||
item.Details = append(item.Details, &biz.DictionaryDetail{Label: detail.Label, Value: detail.Value, Extend: detail.Extend, Status: status, Sort: detail.Sort, ParentID: detail.ParentID, Level: detail.Level, Path: detail.Path})
|
||||
item.Details = append(item.Details, &biz.DictionaryDetail{Label: detail.Label, Value: detail.Value, Extend: detail.Extend, Status: detail.Status, Sort: detail.Sort, ParentID: detail.ParentID, Level: detail.Level, Path: detail.Path})
|
||||
}
|
||||
bundle.Dictionaries = append(bundle.Dictionaries, item)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user