package business import ( "context" "kra/internal/biz/business" "kra/internal/service/dto" "strings" "time" ) func activationPoolFilter(req *dto.ActivationPoolQuery) (business.ActivationPoolFilter, []business.ListOption, error) { f := business.ActivationPoolFilter{} if req == nil { return f, nil, business.ErrActivationInput } for _, source := range splitQueryValues(req.Sources) { value, err := business.NormalizePlatform(source) if err != nil { return f, nil, err } f.Sources = append(f.Sources, value) } f.Name = strings.TrimSpace(req.Name) f.Target = strings.TrimSpace(req.Target) f.Status = req.Status f.ChannelType = req.ChannelType if f.ChannelType != "" && f.ChannelType != "公会" && f.ChannelType != "个人" { return f, nil, business.ErrActivationInput } if f.Status != "" && f.Status != "正常" && f.Status != "锁定" && f.Status != "解除合作" { return f, nil, business.ErrActivationInput } loc := time.FixedZone("Asia/Shanghai", 8*3600) if req.Begin != "" { v, err := time.ParseInLocation("2006-01-02", req.Begin, loc) if err != nil { return f, nil, business.ErrActivationInput } f.Begin = &v } if req.End != "" { v, err := time.ParseInLocation("2006-01-02", req.End, loc) if err != nil { return f, nil, business.ErrActivationInput } f.End = &v } if f.Begin != nil && f.End != nil && f.Begin.After(*f.End) { return f, nil, business.ErrActivationInput } page, size := req.Page, req.PageSize if page < 1 { page = 1 } if size < 1 { size = 10 } if size > 100 { size = 100 } if page > int(^uint(0)>>1)/size { return f, nil, business.ErrActivationInput } return f, []business.ListOption{business.ListOffset((page - 1) * size), business.ListLimit(size)}, nil } func (s *ActivationService) Pool(ctx context.Context, req *dto.ActivationPoolQuery) (*dto.ActivationPoolPage, error) { f, opts, err := activationPoolFilter(req) if err != nil { return nil, err } page, err := s.pool.Pool(ctx, f, opts...) if err != nil { return nil, err } out := &dto.ActivationPoolPage{Rows: []*dto.ActivationPoolRow{}, Total: page.Total, Available: page.Available} for _, r := range page.Rows { out.Rows = append(out.Rows, &dto.ActivationPoolRow{ID: r.ID, SourceDB: r.SourceDB, PlatformTitle: business.PlatformName(r.SourceDB), Name: r.Name, ChannelType: r.ChannelType, Status: r.Status, NoLoginDays: r.NoLoginDays, Rejected: r.Rejected, RejectReason: r.RejectReason, RejectedAt: localTime(r.RejectedAt)}) } return out, nil } func (s *ActivationService) Options(ctx context.Context) ([]*dto.ActivationBindingOption, error) { rows, err := s.pool.Options(ctx) if err != nil { return nil, err } out := []*dto.ActivationBindingOption{} for _, r := range rows { out = append(out, &dto.ActivationBindingOption{ID: r.ID, SourceDB: r.SourceDB, Account: r.Account, Level: r.Level}) } return out, nil } func (s *ActivationService) Apply(ctx context.Context, req *dto.ActivationApplyRequest) error { if req == nil || req.PoolID == 0 || req.BusierID == 0 { return business.ErrActivationInput } return s.pool.Apply(ctx, req.PoolID, req.BusierID, req.SourceDB) } func (s *ActivationService) Churn(ctx context.Context, req *dto.ActivationPoolQuery) (*dto.ActivationChurnPage, error) { f, opts, err := activationPoolFilter(req) if err != nil { return nil, err } page, err := s.pool.Churn(ctx, f, opts...) if err != nil { return nil, err } out := &dto.ActivationChurnPage{Rows: []*dto.ActivationChurnRow{}, Total: page.Total, Available: page.Available} for _, r := range page.Rows { out.Rows = append(out.Rows, &dto.ActivationChurnRow{ID: r.ID, SourceDB: r.SourceDB, PlatformTitle: business.PlatformName(r.SourceDB), Name: r.Name, From: r.From, To: r.To, Reason: r.Reason, Operator: r.Operator, At: *localTime(&r.At), ToPool: r.ToPool}) } return out, nil }