package interfaces import ( "context" "errors" "tyapi-server/internal/application/user/dto/commands" "tyapi-server/internal/domains/user/entities" ) // 常见错误定义 var ( ErrCacheMiss = errors.New("cache miss") ) // Service 通用服务接口 type Service interface { // 服务名称 Name() string // 服务初始化 Initialize(ctx context.Context) error // 服务健康检查 HealthCheck(ctx context.Context) error // 服务关闭 Shutdown(ctx context.Context) error } // UserService 用户服务接口 type UserService interface { Service // 用户注册 Register(ctx context.Context, req *commands.RegisterUserCommand) (*entities.User, error) // 密码登录 LoginWithPassword(ctx context.Context, req *commands.LoginWithPasswordCommand) (*entities.User, error) // 短信验证码登录 LoginWithSMS(ctx context.Context, req *commands.LoginWithSMSCommand) (*entities.User, error) // 修改密码 ChangePassword(ctx context.Context, userID string, req *commands.ChangePasswordCommand) error // 根据ID获取用户 GetByID(ctx context.Context, id string) (*entities.User, error) } // DomainService 领域服务接口,支持泛型 type DomainService[T Entity] interface { Service // 基础业务操作 Create(ctx context.Context, dto interface{}) (*T, error) GetByID(ctx context.Context, id string) (*T, error) Update(ctx context.Context, id string, dto interface{}) (*T, error) Delete(ctx context.Context, id string) error // 列表和查询 List(ctx context.Context, options ListOptions) ([]*T, error) Search(ctx context.Context, query string, options ListOptions) ([]*T, error) Count(ctx context.Context, options CountOptions) (int64, error) // 业务规则验证 Validate(ctx context.Context, entity *T) error ValidateCreate(ctx context.Context, dto interface{}) error ValidateUpdate(ctx context.Context, id string, dto interface{}) error } // EventService 事件服务接口 type EventService interface { Service // 事件发布 Publish(ctx context.Context, event Event) error PublishBatch(ctx context.Context, events []Event) error // 事件订阅 Subscribe(eventType string, handler EventHandler) error Unsubscribe(eventType string, handler EventHandler) error // 异步处理 PublishAsync(ctx context.Context, event Event) error } // CacheService 缓存服务接口 type CacheService interface { Service // 基础缓存操作 Get(ctx context.Context, key string, dest interface{}) error Set(ctx context.Context, key string, value interface{}, ttl ...interface{}) error Delete(ctx context.Context, keys ...string) error Exists(ctx context.Context, key string) (bool, error) // 批量操作 GetMultiple(ctx context.Context, keys []string) (map[string]interface{}, error) SetMultiple(ctx context.Context, data map[string]interface{}, ttl ...interface{}) error // 模式操作 DeletePattern(ctx context.Context, pattern string) error Keys(ctx context.Context, pattern string) ([]string, error) // 缓存统计 Stats(ctx context.Context) (CacheStats, error) } // CacheStats 缓存统计信息 type CacheStats struct { Hits int64 `json:"hits"` Misses int64 `json:"misses"` Keys int64 `json:"keys"` Memory int64 `json:"memory"` Connections int64 `json:"connections"` } // TransactionService 事务服务接口 type TransactionService interface { Service // 事务操作 Begin(ctx context.Context) (Transaction, error) RunInTransaction(ctx context.Context, fn func(Transaction) error) error } // Transaction 事务接口 type Transaction interface { Commit() error Rollback() error GetDB() interface{} }