| 
									
										
										
										
											2025-06-30 19:21:56 +08:00
										 |  |  | package interfaces | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2025-07-02 16:17:59 +08:00
										 |  |  | 	"errors" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-13 16:36:20 +08:00
										 |  |  | 	"tyapi-server/internal/application/user/dto/commands" | 
					
						
							| 
									
										
										
										
											2025-07-02 16:17:59 +08:00
										 |  |  | 	"tyapi-server/internal/domains/user/entities" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // 常见错误定义 | 
					
						
							|  |  |  | var ( | 
					
						
							|  |  |  | 	ErrCacheMiss = errors.New("cache miss") | 
					
						
							| 
									
										
										
										
											2025-06-30 19:21:56 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Service 通用服务接口 | 
					
						
							|  |  |  | type Service interface { | 
					
						
							|  |  |  | 	// 服务名称 | 
					
						
							|  |  |  | 	Name() string | 
					
						
							|  |  |  | 	// 服务初始化 | 
					
						
							|  |  |  | 	Initialize(ctx context.Context) error | 
					
						
							|  |  |  | 	// 服务健康检查 | 
					
						
							|  |  |  | 	HealthCheck(ctx context.Context) error | 
					
						
							|  |  |  | 	// 服务关闭 | 
					
						
							|  |  |  | 	Shutdown(ctx context.Context) error | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-02 16:17:59 +08:00
										 |  |  | // UserService 用户服务接口 | 
					
						
							|  |  |  | type UserService interface { | 
					
						
							|  |  |  | 	Service | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// 用户注册 | 
					
						
							| 
									
										
										
										
											2025-07-13 16:36:20 +08:00
										 |  |  | 	Register(ctx context.Context, req *commands.RegisterUserCommand) (*entities.User, error) | 
					
						
							| 
									
										
										
										
											2025-07-02 16:17:59 +08:00
										 |  |  | 	// 密码登录 | 
					
						
							| 
									
										
										
										
											2025-07-13 16:36:20 +08:00
										 |  |  | 	LoginWithPassword(ctx context.Context, req *commands.LoginWithPasswordCommand) (*entities.User, error) | 
					
						
							| 
									
										
										
										
											2025-07-02 16:17:59 +08:00
										 |  |  | 	// 短信验证码登录 | 
					
						
							| 
									
										
										
										
											2025-07-13 16:36:20 +08:00
										 |  |  | 	LoginWithSMS(ctx context.Context, req *commands.LoginWithSMSCommand) (*entities.User, error) | 
					
						
							| 
									
										
										
										
											2025-07-02 16:17:59 +08:00
										 |  |  | 	// 修改密码 | 
					
						
							| 
									
										
										
										
											2025-07-13 16:36:20 +08:00
										 |  |  | 	ChangePassword(ctx context.Context, userID string, req *commands.ChangePasswordCommand) error | 
					
						
							| 
									
										
										
										
											2025-07-02 16:17:59 +08:00
										 |  |  | 	// 根据ID获取用户 | 
					
						
							|  |  |  | 	GetByID(ctx context.Context, id string) (*entities.User, error) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-30 19:21:56 +08:00
										 |  |  | // 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{} | 
					
						
							|  |  |  | } |