package service import ( "errors" "opencatd-open/team/dao" "opencatd-open/team/model" "time" "gorm.io/gorm" ) var _ ApiKeyService = (*ApiKeyServiceImpl)(nil) type ApiKeyService interface { Create(apiKey *model.ApiKey) error GetByID(id int64) (*model.ApiKey, error) GetByName(name string) (*model.ApiKey, error) GetByApiKey(apiKeyValue string) (*model.ApiKey, error) Update(apiKey *model.ApiKey) error Delete(id int64) error List(offset, limit int, status *int) ([]model.ApiKey, error) ListWithFilters(offset, limit int, filters map[string]interface{}) ([]model.ApiKey, int64, error) Enable(id int64) error Disable(id int64) error BatchEnable(ids []int64) error BatchDisable(ids []int64) error BatchDelete(ids []int64) error Count() (int64, error) } type ApiKeyServiceImpl struct { apiKeyRepo dao.ApiKeyRepository db *gorm.DB } func NewApiKeyService(apiKeyDao dao.ApiKeyRepository, db *gorm.DB) ApiKeyService { return &ApiKeyServiceImpl{apiKeyRepo: apiKeyDao, db: db} } func (s *ApiKeyServiceImpl) Create(apiKey *model.ApiKey) error { if apiKey == nil { return errors.New("apiKey不能为空") } if apiKey.Name == "" { return errors.New("apiKey名称不能为空") } if apiKey.ApiKey == "" { return errors.New("apiKey值不能为空") } apiKey.CreatedAt = time.Now().Unix() apiKey.UpdatedAt = time.Now().Unix() return s.apiKeyRepo.Create(apiKey) } func (s *ApiKeyServiceImpl) GetByID(id int64) (*model.ApiKey, error) { if id <= 0 { return nil, errors.New("id 必须大于 0") } return s.apiKeyRepo.GetByID(id) } func (s *ApiKeyServiceImpl) GetByName(name string) (*model.ApiKey, error) { if name == "" { return nil, errors.New("name 不能为空") } return s.apiKeyRepo.GetByName(name) } func (s *ApiKeyServiceImpl) GetByApiKey(apiKeyValue string) (*model.ApiKey, error) { if apiKeyValue == "" { return nil, errors.New("apiKeyValue 不能为空") } return s.apiKeyRepo.GetByApiKey(apiKeyValue) } func (s *ApiKeyServiceImpl) Update(apiKey *model.ApiKey) error { if apiKey == nil { return errors.New("apiKey不能为空") } if apiKey.ID <= 0 { return errors.New("apiKey ID 必须大于 0") } return s.apiKeyRepo.Update(apiKey) } func (s *ApiKeyServiceImpl) Delete(id int64) error { if id <= 0 { return errors.New("id 必须大于 0") } return s.apiKeyRepo.Delete(id) } func (s *ApiKeyServiceImpl) List(offset, limit int, status *int) ([]model.ApiKey, error) { if offset < 0 { offset = 0 } if limit <= 0 { limit = 10 // 设置默认值 } return s.apiKeyRepo.List(offset, limit, status) } func (s *ApiKeyServiceImpl) ListWithFilters(offset, limit int, filters map[string]interface{}) ([]model.ApiKey, int64, error) { if offset < 0 { offset = 0 } if limit <= 0 { limit = 10 // 设置默认值 } return s.apiKeyRepo.ListWithFilters(offset, limit, filters) } func (s *ApiKeyServiceImpl) Enable(id int64) error { if id <= 0 { return errors.New("id 必须大于 0") } return s.apiKeyRepo.Enable(id) } func (s *ApiKeyServiceImpl) Disable(id int64) error { if id <= 0 { return errors.New("id 必须大于 0") } return s.apiKeyRepo.Disable(id) } func (s *ApiKeyServiceImpl) BatchEnable(ids []int64) error { if len(ids) == 0 { return errors.New("ids 不能为空") } return s.apiKeyRepo.BatchEnable(ids) } func (s *ApiKeyServiceImpl) BatchDisable(ids []int64) error { if len(ids) == 0 { return errors.New("ids 不能为空") } return s.apiKeyRepo.BatchDisable(ids) } func (s *ApiKeyServiceImpl) BatchDelete(ids []int64) error { if len(ids) == 0 { return errors.New("ids 不能为空") } return s.apiKeyRepo.BatchDelete(ids) } func (s *ApiKeyServiceImpl) Count() (int64, error) { return s.apiKeyRepo.Count() }