Files
kra-new/internal/service/business/business_portrait.go
T

79 lines
3.5 KiB
Go

package business
import (
"context"
"kra/internal/biz/business"
"kra/internal/service/dto"
)
type BusinessPortraitUsecase interface {
ForUser(context.Context, uint, []string, string) (*business.BusinessPortrait, error)
ForBinding(context.Context, business.PlatformKey, string) (*business.BusinessPortrait, error)
}
type BusinessPortraitService struct{ uc BusinessPortraitUsecase }
func NewBusinessPortraitService(uc *business.BusinessPortraitUsecase) *BusinessPortraitService {
return &BusinessPortraitService{uc: uc}
}
func portraitMetricResponse(m *business.PortraitMetric) *dto.PortraitMetric {
if m == nil {
return nil
}
var label *string
if m.CompareLabel != "" {
value := m.CompareLabel
label = &value
}
return &dto.PortraitMetric{Value: m.Value, CompareValue: m.CompareValue, ChangeRate: m.ChangeRate, ChangeDiff: m.ChangeDiff, CompareLabel: label}
}
func portraitResponse(p *business.BusinessPortrait) *dto.BusinessPortraitResponse {
b := p.Basic
out := &dto.BusinessPortraitResponse{Basic: &dto.PortraitBasic{UserID: b.UserID, UserName: b.UserName, RealName: b.RealName, Level: b.Level, LevelLabel: business.BusinessLevelLabel(b.Level), ParentUserID: b.ParentUserID, ParentUserName: b.ParentUserName, ParentRealName: b.ParentRealName, CreateTime: localTime(b.CreatedAt), Status: b.Status, StatusLabel: b.StatusLabel, PromoteCount: b.PromoteCount, MonthNewPromote: b.MonthNewPromote}, Period: p.Period, RegisterTrend: []*dto.PortraitTrend{}, PayTrend: []*dto.PortraitTrend{}, RegisterTop5: []*dto.PortraitRank{}, PayTop5: []*dto.PortraitRank{}, Available: p.Available, UnavailableReason: p.UnavailableReason}
if m := p.Metrics; m != nil {
out.Metrics = &dto.PortraitMetrics{Register: portraitMetricResponse(m.Register), PayAmount: portraitMetricResponse(m.PayAmount), PayRate: portraitMetricResponse(m.PayRate), ARPU: portraitMetricResponse(m.ARPU)}
}
for _, point := range p.RegisterTrend {
out.RegisterTrend = append(out.RegisterTrend, &dto.PortraitTrend{Label: point.Label, Value: point.Value})
}
for _, point := range p.PayTrend {
out.PayTrend = append(out.PayTrend, &dto.PortraitTrend{Label: point.Label, Value: point.Value})
}
ranks := func(rows []*business.PortraitRank) []*dto.PortraitRank {
result := make([]*dto.PortraitRank, 0, len(rows))
for _, row := range rows {
result = append(result, &dto.PortraitRank{Rank: row.Rank, SourceDB: row.SourceDB, SourceDBLabel: business.PlatformName(row.SourceDB), PromoteID: row.ID, PromoteName: row.PromoteName, Value: row.Value})
}
return result
}
out.RegisterTop5 = ranks(p.RegisterTop5)
out.PayTop5 = ranks(p.PayTop5)
return out
}
func (s *BusinessPortraitService) ForUser(ctx context.Context, id uint, req *dto.BusinessPortraitQuery) (*dto.BusinessPortraitResponse, error) {
if id == 0 || req == nil {
return nil, business.ErrAccountInput
}
sources, err := normalizePlatforms(req.Sources)
if err != nil {
return nil, err
}
if len(sources) == 0 {
return nil, business.ErrPortraitPlatforms
}
portrait, err := s.uc.ForUser(ctx, id, sources, req.Period)
if err != nil {
return nil, err
}
return portraitResponse(portrait), nil
}
func (s *BusinessPortraitService) ForBinding(ctx context.Context, req *dto.BindingPortraitQuery) (*dto.BusinessPortraitResponse, error) {
if req == nil || req.PlatformBusierID == 0 {
return nil, business.ErrAccountInput
}
portrait, err := s.uc.ForBinding(ctx, business.PlatformKey{SourceDB: req.SourceDB, BusierID: req.PlatformBusierID}, req.Period)
if err != nil {
return nil, err
}
return portraitResponse(portrait), nil
}