package service import ( "context" "encoding/json" "time" "kra/internal/biz" ) type VersionService struct{ uc *biz.VersionUsecase } func NewVersionService(uc *biz.VersionUsecase) *VersionService { return &VersionService{uc: uc} } func versionDTO(v *biz.Version) map[string]any { return map[string]any{"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) ([]map[string]any, int64, error) { items, total, err := s.uc.Repo().ListVersions(ctx, page, size, name, code) if err != nil { return nil, 0, err } out := make([]map[string]any, 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) (map[string]any, error) { v, err := s.uc.Repo().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.Repo().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.Repo().DeleteVersions(ctx, ids) } func (s *VersionService) Export(ctx context.Context, name, code, description string, menuIDs, apiIDs, dictIDs []uint) error { bundle, err := s.uc.Repo().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([]map[string]any, 0, len(bundle.Menus)) for _, v := range bundle.Menus { menus = append(menus, convertMenu(v)) } apis := make([]map[string]any, 0, len(bundle.APIs)) for _, v := range bundle.APIs { apis = append(apis, apiDTO(v)) } dicts := make([]map[string]any, 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.Repo().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.Repo().ImportVersionBundle(ctx, bundle) }