125 lines
5.1 KiB
Go
125 lines
5.1 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"kra/internal/biz"
|
|
"kra/internal/service/dto"
|
|
)
|
|
|
|
type VersionService struct{ uc *biz.VersionUsecase }
|
|
|
|
func NewVersionService(uc *biz.VersionUsecase) *VersionService { return &VersionService{uc: uc} }
|
|
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}
|
|
}
|
|
func (s *VersionService) Versions(ctx context.Context, page, size int, name, code string, createdAtRange []*time.Time) ([]*dto.VersionResponse, int64, error) {
|
|
items, total, err := s.uc.ListVersions(ctx, page, size, name, code, createdAtRange)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
out := make([]*dto.VersionResponse, 0, len(items))
|
|
for _, v := range items {
|
|
out = append(out, versionDTO(v))
|
|
}
|
|
return out, total, nil
|
|
}
|
|
func (s *VersionService) Version(ctx context.Context, id uint) (*dto.VersionResponse, error) {
|
|
v, err := s.uc.FindVersion(ctx, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return versionDTO(v), nil
|
|
}
|
|
func (s *VersionService) VersionData(ctx context.Context, id uint) (string, string, error) {
|
|
v, err := s.uc.FindVersion(ctx, id)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
return v.Data, v.Code, nil
|
|
}
|
|
func (s *VersionService) DeleteVersions(ctx context.Context, ids []uint) error {
|
|
return s.uc.DeleteVersions(ctx, ids)
|
|
}
|
|
|
|
func (s *VersionService) Export(ctx context.Context, name, code, description string, menuIDs, apiIDs, dictIDs []uint) error {
|
|
bundle, err := s.uc.BuildVersionBundle(ctx, menuIDs, apiIDs, dictIDs)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
bundle.Name = name
|
|
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))
|
|
}
|
|
apis := make([]*dto.APIResponse, 0, len(bundle.APIs))
|
|
for _, v := range bundle.APIs {
|
|
apis = append(apis, apiResponse(v))
|
|
}
|
|
dicts := make([]*dto.DictionaryResponse, 0, len(bundle.Dictionaries))
|
|
for _, v := range bundle.Dictionaries {
|
|
dicts = append(dicts, dictionaryDTO(v))
|
|
}
|
|
payload := map[string]any{"version": map[string]any{"name": name, "code": code, "description": description, "exportTime": bundle.ExportTime}, "menus": menus, "apis": apis, "dictionaries": dicts}
|
|
raw, err := json.MarshalIndent(payload, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return s.uc.CreateVersion(ctx, &biz.Version{Name: name, Code: code, Description: description, Data: string(raw)})
|
|
}
|
|
func (s *VersionService) Import(ctx context.Context, bundle *biz.VersionBundle) error {
|
|
return s.uc.ImportVersionBundle(ctx, bundle)
|
|
}
|
|
func versionMenuDomain(value dto.VersionMenuRequest) *biz.Menu {
|
|
menu := &biz.Menu{Path: value.Path, Name: value.Name, Hidden: value.Hidden, Component: value.Component, Sort: value.Sort, ActiveName: value.Meta.ActiveName, KeepAlive: value.Meta.KeepAlive, DefaultMenu: value.Meta.DefaultMenu, Title: value.Meta.Title, Icon: value.Meta.Icon, CloseTab: value.Meta.CloseTab, TransitionType: value.Meta.TransitionType}
|
|
for _, button := range value.MenuButtons {
|
|
menu.Buttons = append(menu.Buttons, &biz.MenuButton{Name: button.Name, Description: button.Description})
|
|
}
|
|
for _, parameter := range value.Parameters {
|
|
menu.Parameters = append(menu.Parameters, &biz.MenuParameter{Type: parameter.Type, Key: parameter.Key, Value: parameter.Value})
|
|
}
|
|
for _, child := range value.Children {
|
|
menu.Children = append(menu.Children, versionMenuDomain(child))
|
|
}
|
|
return menu
|
|
}
|
|
func (s *VersionService) ImportRequest(ctx context.Context, req *dto.ImportVersionRequest) error {
|
|
if req.Version.Name == "" || req.Version.Code == "" {
|
|
return biz.ErrInvalidVersion
|
|
}
|
|
bundle := &biz.VersionBundle{Name: req.Version.Name, Code: req.Version.Code, Description: req.Version.Description, ExportTime: req.Version.ExportTime}
|
|
for _, menu := range req.Menus {
|
|
bundle.Menus = append(bundle.Menus, versionMenuDomain(menu))
|
|
}
|
|
for _, api := range req.APIs {
|
|
bundle.APIs = append(bundle.APIs, &biz.API{Path: api.Path, Description: api.Description, APIGroup: api.APIGroup, Method: api.Method})
|
|
}
|
|
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})
|
|
}
|
|
bundle.Dictionaries = append(bundle.Dictionaries, item)
|
|
}
|
|
if err := s.Import(ctx, bundle); err != nil {
|
|
return err
|
|
}
|
|
raw, _ := json.Marshal(req)
|
|
_ = s.uc.CreateVersion(ctx, &biz.Version{
|
|
Name: req.Version.Name,
|
|
Code: req.Version.Code + "_imported_" + time.Now().Format("20060102150405"),
|
|
Description: "导入版本: " + req.Version.Description,
|
|
Data: string(raw),
|
|
})
|
|
return nil
|
|
}
|