package business import ( "context" "kra/internal/biz/business" "kra/internal/service/dto" "strings" "time" ) type LedgerUsecase interface { Registrations(context.Context, business.LedgerFilter, business.LedgerListOptions) (*business.RegistrationPage, error) Spends(context.Context, business.LedgerFilter, business.LedgerListOptions) (*business.SpendPage, error) ExportRegistrations(context.Context, business.LedgerFilter) ([]*business.Registration, error) ExportSpends(context.Context, business.LedgerFilter) ([]*business.Spend, error) Games(context.Context, []string, string, int) ([]*business.GameOption, error) } type LedgerService struct{ uc LedgerUsecase } func NewLedgerService(uc *business.LedgerUsecase) *LedgerService { return &LedgerService{uc: uc} } func ledgerRequest(req *dto.LedgerQuery, spend bool) (business.LedgerFilter, business.LedgerListOptions, error) { if req == nil { return business.LedgerFilter{}, business.LedgerListOptions{}, business.ErrLedgerInput } sources, err := normalizePlatforms(req.Sources) if err != nil { return business.LedgerFilter{}, business.LedgerListOptions{}, err } f := business.LedgerFilter{Sources: sources, GameNames: splitQueryValues(req.GameNames), UserID: req.UserID, PromoteAccount: strings.TrimSpace(req.PromoteAccount), BusierAccount: strings.TrimSpace(req.BusierAccount), PlayerName: strings.TrimSpace(req.PlayerName), OrderNumber: strings.TrimSpace(req.OrderNumber), PayStatus: req.PayStatus, PayWay: req.PayWay, SortField: strings.TrimSpace(req.SortField), Ascending: strings.EqualFold(strings.TrimSpace(req.SortOrder), "asc") || strings.EqualFold(strings.TrimSpace(req.SortOrder), "ascending")} begin, end := req.BeginRegisterTime, req.EndRegisterTime if spend { begin, end = req.BeginPayTime, req.EndPayTime } if (strings.TrimSpace(begin) == "") != (strings.TrimSpace(end) == "") { return f, business.LedgerListOptions{}, business.ErrLedgerInput } if strings.TrimSpace(begin) != "" && strings.TrimSpace(end) != "" { location := time.FixedZone("Asia/Shanghai", 8*3600) b, err := time.ParseInLocation("2006-01-02", strings.TrimSpace(begin), location) if err != nil { return f, business.LedgerListOptions{}, business.ErrLedgerInput } e, err := time.ParseInLocation("2006-01-02", strings.TrimSpace(end), location) if err != nil || e.Before(b) { return f, business.LedgerListOptions{}, business.ErrLedgerInput } f.StartDate = &b f.EndDate = &e } 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 f, business.LedgerListOptions{}, business.ErrLedgerInput } return f, business.LedgerListOptions{Offset: (page - 1) * size, Limit: size}, nil } func registrationResponse(r *business.Registration) *dto.RegistrationResponse { return &dto.RegistrationResponse{SourceDB: r.SourceDB, SourceDBLabel: business.PlatformName(r.SourceDB), ID: r.ID, UserID: r.ID, RegisterTime: r.RegisterTime, FirstGameID: r.FirstGameID, FirstGameName: r.FirstGameName, TotalPayAmount: r.TotalPayAmount, RegisterIP: r.RegisterIP, DeviceName: r.DeviceName, PromoteID: r.PromoteID, PromoteAccount: r.PromoteAccount, BusierName: r.DisplayBusiness()} } func spendResponse(r *business.Spend) *dto.SpendResponse { return &dto.SpendResponse{SourceDB: r.SourceDB, SourceDBLabel: business.PlatformName(r.SourceDB), ID: r.ID, UserID: r.UserID, OrderNumber: r.OrderNumber, GameID: r.GameID, GameName: r.GameName, ServerID: r.ServerID, ServerName: r.ServerName, PlayerName: r.PlayerName, PayAmount: r.PayAmount, PayTime: r.PayTime, PayWay: r.PayWay, PayStatus: r.PayStatus, IP: r.IP, PromoteID: r.PromoteID, PromoteAccount: r.PromoteAccount, BusierName: r.DisplayBusiness()} } func (s *LedgerService) Registrations(ctx context.Context, req *dto.LedgerQuery) (*dto.RegistrationPage, error) { f, o, err := ledgerRequest(req, false) if err != nil { return nil, err } page, err := s.uc.Registrations(ctx, f, o) if err != nil { return nil, err } out := &dto.RegistrationPage{Rows: []*dto.RegistrationResponse{}, Total: page.Total, Available: page.Available} for _, row := range page.Rows { out.Rows = append(out.Rows, registrationResponse(row)) } return out, nil } func (s *LedgerService) Spends(ctx context.Context, req *dto.LedgerQuery) (*dto.SpendPage, error) { f, o, err := ledgerRequest(req, true) if err != nil { return nil, err } page, err := s.uc.Spends(ctx, f, o) if err != nil { return nil, err } out := &dto.SpendPage{Rows: []*dto.SpendResponse{}, Total: page.Total, TotalPayAmount: page.TotalPayAmount, Available: page.Available} for _, row := range page.Rows { out.Rows = append(out.Rows, spendResponse(row)) } return out, nil } func (s *LedgerService) Games(ctx context.Context, req *dto.GameOptionQuery) ([]*dto.GameOptionResponse, error) { if req == nil { return nil, business.ErrLedgerInput } sources, err := normalizePlatforms(req.Sources) if err != nil { return nil, err } rows, err := s.uc.Games(ctx, sources, req.Keyword, req.Limit) if err != nil { return nil, err } result := make([]*dto.GameOptionResponse, 0, len(rows)) for _, row := range rows { result = append(result, &dto.GameOptionResponse{SourceDB: row.SourceDB, SourceDBLabel: business.PlatformName(row.SourceDB), GameID: row.ID, GameName: row.Name}) } return result, nil }