156 lines
3.9 KiB
Go
156 lines
3.9 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.29.0
|
|
// source: report.sql
|
|
|
|
package dbgen
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const GetBranchWiseReport = `-- name: GetBranchWiseReport :many
|
|
SELECT
|
|
b.branch_id,
|
|
br.name AS branch_name,
|
|
br.company_id,
|
|
COUNT(*) AS total_bets,
|
|
COALESCE(SUM(b.amount), 0) AS total_cash_made,
|
|
COALESCE(
|
|
SUM(
|
|
CASE
|
|
WHEN sb.cashed_out THEN b.amount -- use cashed_out from shop_bets
|
|
ELSE 0
|
|
END
|
|
), 0
|
|
) AS total_cash_out,
|
|
COALESCE(
|
|
SUM(
|
|
CASE
|
|
WHEN b.status = 5 THEN b.amount
|
|
ELSE 0
|
|
END
|
|
), 0
|
|
) AS total_cash_backs
|
|
FROM shop_bet_detail b
|
|
JOIN branches br ON b.branch_id = br.id
|
|
JOIN shop_bets sb ON sb.id = b.id -- join to get cashed_out
|
|
WHERE b.created_at BETWEEN $1 AND $2
|
|
GROUP BY b.branch_id, br.name, br.company_id
|
|
`
|
|
|
|
type GetBranchWiseReportParams struct {
|
|
From pgtype.Timestamp `json:"from"`
|
|
To pgtype.Timestamp `json:"to"`
|
|
}
|
|
|
|
type GetBranchWiseReportRow struct {
|
|
BranchID int64 `json:"branch_id"`
|
|
BranchName string `json:"branch_name"`
|
|
CompanyID int64 `json:"company_id"`
|
|
TotalBets int64 `json:"total_bets"`
|
|
TotalCashMade interface{} `json:"total_cash_made"`
|
|
TotalCashOut interface{} `json:"total_cash_out"`
|
|
TotalCashBacks interface{} `json:"total_cash_backs"`
|
|
}
|
|
|
|
func (q *Queries) GetBranchWiseReport(ctx context.Context, arg GetBranchWiseReportParams) ([]GetBranchWiseReportRow, error) {
|
|
rows, err := q.db.Query(ctx, GetBranchWiseReport, arg.From, arg.To)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetBranchWiseReportRow
|
|
for rows.Next() {
|
|
var i GetBranchWiseReportRow
|
|
if err := rows.Scan(
|
|
&i.BranchID,
|
|
&i.BranchName,
|
|
&i.CompanyID,
|
|
&i.TotalBets,
|
|
&i.TotalCashMade,
|
|
&i.TotalCashOut,
|
|
&i.TotalCashBacks,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const GetCompanyWiseReport = `-- name: GetCompanyWiseReport :many
|
|
SELECT
|
|
b.company_id,
|
|
c.name AS company_name,
|
|
COUNT(*) AS total_bets,
|
|
COALESCE(SUM(b.amount), 0) AS total_cash_made,
|
|
COALESCE(
|
|
SUM(
|
|
CASE
|
|
WHEN sb.cashed_out THEN b.amount -- use actual cashed_out flag from shop_bets
|
|
ELSE 0
|
|
END
|
|
), 0
|
|
) AS total_cash_out,
|
|
COALESCE(
|
|
SUM(
|
|
CASE
|
|
WHEN b.status = 5 THEN b.amount
|
|
ELSE 0
|
|
END
|
|
), 0
|
|
) AS total_cash_backs
|
|
FROM shop_bet_detail b
|
|
JOIN companies c ON b.company_id = c.id
|
|
JOIN shop_bets sb ON sb.id = b.id -- join to get cashed_out
|
|
WHERE b.created_at BETWEEN $1 AND $2
|
|
GROUP BY b.company_id, c.name
|
|
`
|
|
|
|
type GetCompanyWiseReportParams struct {
|
|
From pgtype.Timestamp `json:"from"`
|
|
To pgtype.Timestamp `json:"to"`
|
|
}
|
|
|
|
type GetCompanyWiseReportRow struct {
|
|
CompanyID int64 `json:"company_id"`
|
|
CompanyName string `json:"company_name"`
|
|
TotalBets int64 `json:"total_bets"`
|
|
TotalCashMade interface{} `json:"total_cash_made"`
|
|
TotalCashOut interface{} `json:"total_cash_out"`
|
|
TotalCashBacks interface{} `json:"total_cash_backs"`
|
|
}
|
|
|
|
func (q *Queries) GetCompanyWiseReport(ctx context.Context, arg GetCompanyWiseReportParams) ([]GetCompanyWiseReportRow, error) {
|
|
rows, err := q.db.Query(ctx, GetCompanyWiseReport, arg.From, arg.To)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []GetCompanyWiseReportRow
|
|
for rows.Next() {
|
|
var i GetCompanyWiseReportRow
|
|
if err := rows.Scan(
|
|
&i.CompanyID,
|
|
&i.CompanyName,
|
|
&i.TotalBets,
|
|
&i.TotalCashMade,
|
|
&i.TotalCashOut,
|
|
&i.TotalCashBacks,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|