thread-master/apps/backend/internal/logic/crm/get_crm_stats_logic.go

57 lines
1.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package crm
import (
"context"
crmUC "apps/backend/internal/module/crm/usecase"
"apps/backend/internal/svc"
"apps/backend/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCrmStatsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetCrmStatsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCrmStatsLogic {
return &GetCrmStatsLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
}
func (l *GetCrmStatsLogic) GetCrmStats(req *types.CrmStatsReq) (*types.CrmStatsData, error) {
uid, err := ownerUID(l.ctx)
if err != nil {
return nil, err
}
stats, err := l.svcCtx.Crm.Stats(l.ctx, uid, req.From, req.To)
if err != nil {
return nil, err
}
terms := make([]types.TermConversionStat, 0, len(stats.Terms))
for _, s := range stats.Terms {
row := types.TermConversionStat{
Term: s.Key, Accepted: s.Accepted, Replied: s.Replied, Won: s.Won,
InsufficientSample: s.Accepted < crmUC.StatsMinSample,
}
// 樣本不足只給絕對數不給會被過度解讀的比率spec §9.5
if !row.InsufficientSample {
row.ConversionRate = float64(s.Won) / float64(s.Accepted)
}
terms = append(terms, row)
}
sources := make([]types.SourceConversionStat, 0, len(stats.Sources))
for _, s := range stats.Sources {
sources = append(sources, types.SourceConversionStat{
Source: s.Key, Won: s.Won, InsufficientSample: s.Won < crmUC.StatsMinSample,
})
}
return &types.CrmStatsData{
Terms: terms,
Variants: []types.VariantConversionStat{},
Sources: sources,
UnavailableDimensions: stats.UnavailableDimensions,
}, nil
}