package system import ( "context" "errors" "strconv" "time" ) var ( ErrVersionNotFound = errors.New("版本不存在") ) // SysVersion 版本管理实体 type SysVersion struct { ID int64 VersionName string VersionCode string Description string VersionData string CreatedAt time.Time UpdatedAt time.Time } // VersionSearchReq 版本搜索请求 type VersionSearchReq struct { Page int PageSize int VersionName *string VersionCode *string CreatedAtRange []time.Time } // VersionRepo 版本仓储接口 type VersionRepo interface { Create(ctx context.Context, version *SysVersion) error Delete(ctx context.Context, id string) error DeleteByIds(ctx context.Context, ids []string) error GetById(ctx context.Context, id string) (*SysVersion, error) GetList(ctx context.Context, req *VersionSearchReq) ([]*SysVersion, int64, error) } // VersionUsecase 版本用例 type VersionUsecase struct { repo VersionRepo menuRepo MenuRepo apiRepo ApiRepo dictRepo DictionaryRepo } // NewVersionUsecase 创建版本用例 func NewVersionUsecase(repo VersionRepo, menuRepo MenuRepo, apiRepo ApiRepo, dictRepo DictionaryRepo) *VersionUsecase { return &VersionUsecase{ repo: repo, menuRepo: menuRepo, apiRepo: apiRepo, dictRepo: dictRepo, } } // CreateSysVersion 创建版本 func (uc *VersionUsecase) CreateSysVersion(ctx context.Context, version *SysVersion) error { return uc.repo.Create(ctx, version) } // DeleteSysVersion 删除版本 func (uc *VersionUsecase) DeleteSysVersion(ctx context.Context, id string) error { return uc.repo.Delete(ctx, id) } // DeleteSysVersionByIds 批量删除版本 func (uc *VersionUsecase) DeleteSysVersionByIds(ctx context.Context, ids []string) error { return uc.repo.DeleteByIds(ctx, ids) } // GetSysVersion 根据ID获取版本 func (uc *VersionUsecase) GetSysVersion(ctx context.Context, id string) (*SysVersion, error) { return uc.repo.GetById(ctx, id) } // GetSysVersionInfoList 分页获取版本列表 func (uc *VersionUsecase) GetSysVersionInfoList(ctx context.Context, req *VersionSearchReq) ([]*SysVersion, int64, error) { return uc.repo.GetList(ctx, req) } // GetMenusByIds 根据ID列表获取菜单数据 func (uc *VersionUsecase) GetMenusByIds(ctx context.Context, ids []uint) ([]*Menu, error) { // 转换为string类型 strIds := make([]string, len(ids)) for i, id := range ids { strIds[i] = strconv.FormatUint(uint64(id), 10) } baseMenus, err := uc.menuRepo.FindBaseMenusByIds(ctx, strIds) if err != nil { return nil, err } // 转换为Menu类型 menus := make([]*Menu, len(baseMenus)) for i, bm := range baseMenus { menus[i] = &Menu{ BaseMenu: *bm, MenuId: bm.ID, } } return menus, nil } // GetApisByIds 根据ID列表获取API数据 func (uc *VersionUsecase) GetApisByIds(ctx context.Context, ids []uint) ([]*Api, error) { return uc.apiRepo.FindByIds(ctx, ids) } // GetDictionariesByIds 根据ID列表获取字典数据(简化实现) func (uc *VersionUsecase) GetDictionariesByIds(ctx context.Context, ids []uint) ([]*Dictionary, error) { var result []*Dictionary for _, id := range ids { dict, err := uc.dictRepo.FindByID(ctx, id) if err != nil { continue } result = append(result, dict) } return result, nil }