package business import ( "context" "github.com/go-kratos/kratos/v3/errors" "kra/internal/biz/system" ) var ( ErrUnauthenticated = errors.Unauthorized("SW_UNAUTHENTICATED", "请先登录") ErrPreferenceRequired = errors.BadRequest("SW_PREFERENCE_REQUIRED", "偏好参数不能为空") ErrStorage = errors.InternalServer("SW_STORAGE_ERROR", "商务数据访问失败") ) // UserPreference 使用新用户 ID,保留旧商务两个全局开关的含义。 type UserPreference struct { IncludeSubPromote bool IDCardDedup bool } type UserPreferenceRepo interface { Find(context.Context, uint) (*UserPreference, error) Save(context.Context, uint, UserPreference) error } type UserPreferenceUsecase struct{ repo UserPreferenceRepo } func NewUserPreferenceUsecase(repo UserPreferenceRepo) *UserPreferenceUsecase { return &UserPreferenceUsecase{repo: repo} } func (uc *UserPreferenceUsecase) Current(ctx context.Context) (*UserPreference, error) { actor, ok := system.ActorFromContext(ctx) if !ok || actor.UserID == 0 { return nil, ErrUnauthenticated } return uc.ForUser(ctx, actor.UserID) } func (uc *UserPreferenceUsecase) ForUser(ctx context.Context, id uint) (*UserPreference, error) { if id == 0 { return nil, ErrUnauthenticated } pref, err := uc.repo.Find(ctx, id) if err != nil { return nil, err } if pref == nil { return &UserPreference{IncludeSubPromote: true, IDCardDedup: true}, nil } return pref, nil } func (uc *UserPreferenceUsecase) UpdateCurrent(ctx context.Context, pref UserPreference) error { actor, ok := system.ActorFromContext(ctx) if !ok || actor.UserID == 0 { return ErrUnauthenticated } return uc.repo.Save(ctx, actor.UserID, pref) }