package business import ( "context" "golang.org/x/sync/errgroup" "kra/internal/biz/business" ) type dashboardRepo struct{ data Provider } func NewDashboardRepo(data Provider) business.DashboardRepo { return &dashboardRepo{data: data} } func (r *dashboardRepo) Overview(ctx context.Context, scope business.AnalyticsScope, sources []string, w business.DashboardWindow) (*business.DashboardData, error) { db := r.data.DB().WithContext(ctx) out := &business.DashboardData{Current: business.DashboardMetrics{Pay: "0.00"}, Previous: business.DashboardMetrics{Pay: "0.00"}, Available: true} empty, available, err := workbenchAvailable(db, scope, sources, "") if err != nil { return nil, err } out.Available = available if empty || !available { return out, nil } begin, end, previous := w.Begin.Format("2006-01-02"), w.End.Format("2006-01-02"), w.PreviousBegin.Format("2006-01-02") f := business.AnalyticsFilter{Sources: sources, Begin: previous, End: end} mode := 0 if scope.IDCardDedup { mode = 1 } var counts []struct { Current bool Metric int Count int64 } var amounts struct{ CurrentPay, PreviousPay string } type activeRow struct { Current bool SourceDB string `gorm:"column:source_db"` PromoteID uint64 } var registered, paid []activeRow var channels struct{ CurrentNew, PreviousNew, CurrentEffective, PreviousEffective int64 } group, gctx := errgroup.WithContext(ctx) group.SetLimit(2) qdb := db.WithContext(gctx) group.Go(func() error { return storageError(analyticsQuery(qdb, scope, f, "tab_sw_report_day_key", mode).Where("r.metric IN (1,2)").Select("(r.stat_date>=?) AS current,r.metric,COUNT(DISTINCT "+portraitIdentity(qdb, scope.IDCardDedup)+") AS count", begin).Group("current,r.metric").Scan(&counts).Error) }) group.Go(func() error { return storageError(analyticsQuery(qdb, scope, f, "tab_sw_report_day", 0).Select("CAST(COALESCE(SUM(CASE WHEN r.stat_date>=? THEN r.total_pay ELSE 0 END),0) AS CHAR) AS current_pay,CAST(COALESCE(SUM(CASE WHEN r.stat_date=?) AS current,r.source_db,r.promote_id", begin).Scan(®istered).Error) }) group.Go(func() error { return storageError(analyticsQuery(qdb, scope, f, "tab_sw_report_day", 0).Select("(r.stat_date>=?) AS current,r.source_db,r.promote_id", begin).Group("current,r.source_db,r.promote_id").Having("SUM(r.total_pay)>0").Scan(&paid).Error) }) group.Go(func() error { q := analyticsBasePromotes(qdb, scope, f, "p") q = joinEffectiveness(q) return storageError(q.Select("COALESCE(SUM(CASE WHEN p.create_time>=? AND p.create_time=? AND p.create_time=? AND e.qualified_at=? AND e.qualified_at