package business import ( "context" "kra/internal/biz/business" "kra/internal/service/dto" "strings" "time" "unicode/utf8" ) type BindingUsecase interface { List(context.Context, business.BindingFilter, ...business.ListOption) ([]*business.Binding, int64, error) ForUser(context.Context, uint) ([]*business.Binding, error) Bind(context.Context, string, string, string) error Unbind(context.Context, business.PlatformKey) error } type BindingService struct{ uc BindingUsecase } func NewBindingService(uc *business.BindingUsecase) *BindingService { return &BindingService{uc: uc} } func localTime(t *time.Time) *string { if t == nil || t.IsZero() { return nil } s := t.Format("2006-01-02 15:04:05") return &s } func bindingResponse(b *business.Binding) *dto.BusinessBindingResponse { return &dto.BusinessBindingResponse{UserID: b.UserID, SourceDB: b.SourceDB, PlatformName: business.PlatformName(b.SourceDB), PlatformBusierID: b.BusierID, Account: b.Account, UserName: b.UserName, PromoteCount: b.PromoteCount, Status: b.Status, RejectReason: b.RejectReason, CreateTime: localTime(&b.CreatedAt), ReviewTime: localTime(b.ReviewTime), ReviewAccount: b.ReviewAccount} } func normalizePlatforms(values []string) ([]string, error) { result := []string{} seen := map[string]bool{} for _, raw := range values { for _, value := range strings.Split(raw, ",") { if strings.TrimSpace(value) == "" { continue } value, err := business.NormalizePlatform(value) if err != nil { return nil, err } if !seen[value] { seen[value] = true result = append(result, value) } } } return result, nil } func (s *BindingService) List(ctx context.Context, req *dto.BusinessBindingQuery) (*dto.BusinessBindingPage, error) { if req == nil { return nil, business.ErrAccountInput } sources, err := normalizePlatforms(req.SourceDB) if err != nil { return nil, err } if req.Status != "" && req.Status != "0" && req.Status != "2" && req.Status != "3" { return nil, business.ErrAccountInput } page, size := req.Page, req.PageSize if page < 1 { page = 1 } if size < 1 { size = 10 } if size > 500 { size = 500 } if page > int(^uint(0)>>1)/size { return nil, business.ErrAccountInput } rows, total, err := s.uc.List(ctx, business.BindingFilter{SourceDBs: sources, Account: req.Account, Status: req.Status}, business.ListOffset((page-1)*size), business.ListLimit(size)) if err != nil { return nil, err } out := &dto.BusinessBindingPage{Rows: make([]*dto.BusinessBindingResponse, 0, len(rows)), Total: total} for _, b := range rows { out.Rows = append(out.Rows, bindingResponse(b)) } return out, nil } func (s *BindingService) ForUser(ctx context.Context, id uint) ([]*dto.BusinessBindingResponse, error) { if id == 0 { return nil, business.ErrAccountInput } rows, err := s.uc.ForUser(ctx, id) if err != nil { return nil, err } out := make([]*dto.BusinessBindingResponse, 0, len(rows)) for _, b := range rows { out = append(out, bindingResponse(b)) } return out, nil } func (s *BindingService) Bind(ctx context.Context, req *dto.BusinessBindingRequest) error { if req == nil || strings.TrimSpace(req.Account) == "" || utf8.RuneCountInString(req.Account) > 30 || strings.TrimSpace(req.Password) == "" || utf8.RuneCountInString(req.Password) > 100 { return business.ErrAccountInput } source, err := business.NormalizePlatform(req.SourceDB) if err != nil { return err } return s.uc.Bind(ctx, source, req.Account, req.Password) } func (s *BindingService) Unbind(ctx context.Context, req *dto.BusinessBindingRequest) error { if req == nil || req.PlatformBusierID == 0 { return business.ErrAccountInput } source, err := business.NormalizePlatform(req.SourceDB) if err != nil { return err } return s.uc.Unbind(ctx, business.PlatformKey{SourceDB: source, BusierID: uint64(req.PlatformBusierID)}) }