// Code generated by sqlc. DO NOT EDIT. // versions: // sqlc v1.29.0 // source: company_stats.sql package dbgen import ( "context" "github.com/jackc/pgx/v5/pgtype" ) const GetCompanyStats = `-- name: GetCompanyStats :many SELECT DATE_TRUNC($1, interval_start)::timestamp AS interval_start, company_stats.company_id, company_stats.interval_start, company_stats.total_bets, company_stats.total_stake, company_stats.deducted_stake, company_stats.total_cash_out, company_stats.total_cash_backs, company_stats.number_of_unsettled, company_stats.total_unsettled_amount, company_stats.total_admins, company_stats.total_managers, company_stats.total_cashiers, company_stats.total_customers, company_stats.total_approvers, company_stats.total_branches, company_stats.updated_at FROM company_stats WHERE ( company_stats.company_id = $2 OR $2 IS NULL ) GROUP BY interval_start ORDER BY interval_start DESC ` type GetCompanyStatsParams struct { Interval pgtype.Text `json:"interval"` CompanyID pgtype.Int8 `json:"company_id"` } type GetCompanyStatsRow struct { IntervalStart pgtype.Timestamp `json:"interval_start"` CompanyID int64 `json:"company_id"` IntervalStart_2 pgtype.Timestamp `json:"interval_start_2"` TotalBets int64 `json:"total_bets"` TotalStake int64 `json:"total_stake"` DeductedStake int64 `json:"deducted_stake"` TotalCashOut int64 `json:"total_cash_out"` TotalCashBacks int64 `json:"total_cash_backs"` NumberOfUnsettled int64 `json:"number_of_unsettled"` TotalUnsettledAmount int64 `json:"total_unsettled_amount"` TotalAdmins int64 `json:"total_admins"` TotalManagers int64 `json:"total_managers"` TotalCashiers int64 `json:"total_cashiers"` TotalCustomers int64 `json:"total_customers"` TotalApprovers int64 `json:"total_approvers"` TotalBranches int64 `json:"total_branches"` UpdatedAt pgtype.Timestamp `json:"updated_at"` } func (q *Queries) GetCompanyStats(ctx context.Context, arg GetCompanyStatsParams) ([]GetCompanyStatsRow, error) { rows, err := q.db.Query(ctx, GetCompanyStats, arg.Interval, arg.CompanyID) if err != nil { return nil, err } defer rows.Close() var items []GetCompanyStatsRow for rows.Next() { var i GetCompanyStatsRow if err := rows.Scan( &i.IntervalStart, &i.CompanyID, &i.IntervalStart_2, &i.TotalBets, &i.TotalStake, &i.DeductedStake, &i.TotalCashOut, &i.TotalCashBacks, &i.NumberOfUnsettled, &i.TotalUnsettledAmount, &i.TotalAdmins, &i.TotalManagers, &i.TotalCashiers, &i.TotalCustomers, &i.TotalApprovers, &i.TotalBranches, &i.UpdatedAt, ); err != nil { return nil, err } items = append(items, i) } if err := rows.Err(); err != nil { return nil, err } return items, nil } const GetCompanyStatsByID = `-- name: GetCompanyStatsByID :many SELECT company_id, interval_start, total_bets, total_stake, deducted_stake, total_cash_out, total_cash_backs, number_of_unsettled, total_unsettled_amount, total_admins, total_managers, total_cashiers, total_customers, total_approvers, total_branches, updated_at FROM company_stats WHERE company_id = $1 ORDER BY interval_start DESC ` func (q *Queries) GetCompanyStatsByID(ctx context.Context, companyID int64) ([]CompanyStat, error) { rows, err := q.db.Query(ctx, GetCompanyStatsByID, companyID) if err != nil { return nil, err } defer rows.Close() var items []CompanyStat for rows.Next() { var i CompanyStat if err := rows.Scan( &i.CompanyID, &i.IntervalStart, &i.TotalBets, &i.TotalStake, &i.DeductedStake, &i.TotalCashOut, &i.TotalCashBacks, &i.NumberOfUnsettled, &i.TotalUnsettledAmount, &i.TotalAdmins, &i.TotalManagers, &i.TotalCashiers, &i.TotalCustomers, &i.TotalApprovers, &i.TotalBranches, &i.UpdatedAt, ); err != nil { return nil, err } items = append(items, i) } if err := rows.Err(); err != nil { return nil, err } return items, nil } const UpdateCompanyStats = `-- name: UpdateCompanyStats :exec WITH -- Aggregate bet data per company bet_stats AS ( SELECT company_id, COUNT(*) AS total_bets, COALESCE(SUM(amount), 0) AS total_stake, COALESCE( SUM(amount) * MAX(companies.deducted_percentage), 0 ) AS deducted_stake, COALESCE( SUM( CASE WHEN cashed_out THEN amount ELSE 0 END ), 0 ) AS total_cash_out, COALESCE( SUM( CASE WHEN status = 3 THEN amount ELSE 0 END ), 0 ) AS total_cash_backs, COUNT(*) FILTER ( WHERE status = 5 ) AS number_of_unsettled, COALESCE( SUM( CASE WHEN status = 5 THEN amount ELSE 0 END ), 0 ) AS total_unsettled_amount FROM shop_bet_detail LEFT JOIN companies ON companies.id = shop_bet_detail.company_id GROUP BY company_id ), user_stats AS ( SELECT company_id, COUNT(*) FILTER ( WHERE role = 'admin' ) AS total_admins, COUNT(*) FILTER ( WHERE role = 'branch_manager' ) AS total_managers, COUNT(*) FILTER ( WHERE role = 'cashier' ) AS total_cashiers, COUNT(*) FILTER ( WHERE role = 'customer' ) AS total_customers, COUNT(*) FILTER ( WHERE role = 'transaction_approver' ) AS total_approvers FROM users GROUP BY company_id ), branch_stats AS ( SELECT company_id, COUNT(*) AS total_branches FROM branches GROUP BY company_id ) -- Final combined aggregation INSERT INTO company_stats ( company_id, interval_start, total_bets, total_stake, deducted_stake, total_cash_out, total_cash_backs, number_of_unsettled, total_unsettled_amount, total_admins, total_managers, total_cashiers, total_customers, total_approvers, total_branches, updated_at ) SELECT c.id AS company_id, DATE_TRUNC('day', NOW() AT TIME ZONE 'UTC') AS interval_start, COALESCE(b.total_bets, 0) AS total_bets, COALESCE(b.total_stake, 0) AS total_stake, COALESCE(b.deducted_stake, 0) AS deducted_stake, COALESCE(b.total_cash_out, 0) AS total_cash_out, COALESCE(b.total_cash_backs, 0) AS total_cash_backs, COALESCE(b.number_of_unsettled, 0) AS number_of_unsettled, COALESCE(b.total_unsettled_amount, 0) AS total_unsettled_amount, COALESCE(u.total_admins, 0) AS total_admins, COALESCE(u.total_managers, 0) AS total_managers, COALESCE(u.total_cashiers, 0) AS total_cashiers, COALESCE(u.total_customers, 0) AS total_customers, COALESCE(u.total_approvers, 0) AS total_approvers, COALESCE(br.total_branches, 0) AS total_branches, NOW() AS updated_at FROM companies c LEFT JOIN bet_stats b ON b.company_id = c.id LEFT JOIN user_stats u ON u.company_id = c.id LEFT JOIN branch_stats br ON br.company_id = c.id ON CONFLICT (company_id, interval_start) DO UPDATE SET total_bets = EXCLUDED.total_bets, total_stake = EXCLUDED.total_stake, deducted_stake = EXCLUDED.deducted_stake, total_cash_out = EXCLUDED.total_cash_out, total_cash_backs = EXCLUDED.total_cash_backs, number_of_unsettled = EXCLUDED.number_of_unsettled, total_unsettled_amount = EXCLUDED.total_unsettled_amount, total_admins = EXCLUDED.total_admins, total_managers = EXCLUDED.total_managers, total_cashiers = EXCLUDED.total_cashiers, total_customers = EXCLUDED.total_customers, total_approvers = EXCLUDED.total_approvers, total_branches = EXCLUDED.total_branches, updated_at = EXCLUDED.updated_at ` // Aggregate user counts per company // Aggregate branch counts per company func (q *Queries) UpdateCompanyStats(ctx context.Context) error { _, err := q.db.Exec(ctx, UpdateCompanyStats) return err }