Yimaru-BackEnd/db/query/virtual_games.sql

387 lines
8.2 KiB
SQL

-- name: CreateVirtualGameProvider :one
INSERT INTO virtual_game_providers (
provider_id,
provider_name,
logo_dark,
logo_light,
enabled
)
VALUES ($1, $2, $3, $4, $5)
RETURNING id,
provider_id,
provider_name,
logo_dark,
logo_light,
enabled,
created_at,
updated_at;
-- name: DeleteVirtualGameProvider :exec
DELETE FROM virtual_game_providers
WHERE provider_id = $1;
-- name: DeleteAllVirtualGameProviders :exec
DELETE FROM virtual_game_providers;
-- name: GetVirtualGameProviderByID :one
SELECT id,
provider_id,
provider_name,
logo_dark,
logo_light,
enabled,
created_at,
updated_at
FROM virtual_game_providers
WHERE provider_id = $1;
-- name: ListVirtualGameProviders :many
SELECT id,
provider_id,
provider_name,
logo_dark,
logo_light,
enabled,
created_at,
updated_at
FROM virtual_game_providers
ORDER BY created_at DESC
LIMIT $1 OFFSET $2;
-- name: CountVirtualGameProviders :one
SELECT COUNT(*) AS total
FROM virtual_game_providers;
-- name: UpdateVirtualGameProviderEnabled :one
UPDATE virtual_game_providers
SET enabled = $2,
updated_at = CURRENT_TIMESTAMP
WHERE provider_id = $1
RETURNING id,
provider_id,
provider_name,
logo_dark,
logo_light,
enabled,
created_at,
updated_at;
-- name: CreateVirtualGameSession :one
INSERT INTO virtual_game_sessions (
user_id,
game_id,
session_token
)
VALUES ($1, $2, $3)
RETURNING
id,
user_id,
game_id,
session_token,
created_at,
updated_at;
-- name: GetVirtualGameSessionByUserID :one
SELECT
id,
user_id,
game_id,
session_token,
created_at,
updated_at
FROM virtual_game_sessions
WHERE user_id = $1;
-- name: GetVirtualGameSessionByToken :one
SELECT id,
user_id,
game_id,
session_token,
created_at,
updated_at
FROM virtual_game_sessions
WHERE session_token = $1;
-- name: CreateVirtualGameTransaction :one
INSERT INTO virtual_game_transactions (
-- session_id,
user_id,
company_id,
provider,
wallet_id,
transaction_type,
amount,
currency,
external_transaction_id,
status
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id,
-- session_id,
user_id,
company_id,
provider,
wallet_id,
transaction_type,
amount,
currency,
external_transaction_id,
status,
created_at,
updated_at;
-- name: CreateVirtualGameHistory :one
INSERT INTO virtual_game_histories (
-- session_id,
user_id,
company_id,
provider,
wallet_id,
game_id,
transaction_type,
amount,
currency,
external_transaction_id,
reference_transaction_id,
status
)
VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11
-- $12
)
RETURNING id,
-- session_id,
user_id,
company_id,
provider,
wallet_id,
game_id,
transaction_type,
amount,
currency,
external_transaction_id,
reference_transaction_id,
status,
created_at,
updated_at;
-- name: GetVirtualGameTransactionByExternalID :one
SELECT id,
-- session_id,
user_id,
wallet_id,
transaction_type,
amount,
currency,
external_transaction_id,
status,
created_at,
updated_at
FROM virtual_game_transactions
WHERE external_transaction_id = $1;
-- name: UpdateVirtualGameTransactionStatus :exec
UPDATE virtual_game_transactions
SET status = $2,
updated_at = CURRENT_TIMESTAMP
WHERE id = $1;
-- name: GetVirtualGameSummaryInRange :many
SELECT c.name AS company_name,
vg.name AS game_name,
COUNT(vgt.id) AS number_of_bets,
COALESCE(SUM(vgt.amount), 0) AS total_transaction_sum
FROM virtual_game_transactions vgt
-- JOIN virtual_game_sessions vgs ON vgt.session_id = vgs.id
JOIN virtual_games vg ON vgs.game_id = vg.id
JOIN companies c ON vgt.company_id = c.id
WHERE vgt.transaction_type = 'BET'
AND vgt.created_at BETWEEN $1 AND $2
GROUP BY c.name,
vg.name;
-- name: AddFavoriteGame :exec
INSERT INTO virtual_game_favourites (user_id, game_id, provider_id, created_at)
VALUES ($1, $2, $3, NOW())
ON CONFLICT (game_id, user_id) DO NOTHING;
-- name: RemoveFavoriteGame :exec
DELETE FROM virtual_game_favourites
WHERE user_id = $1 AND game_id = $2 AND provider_id = $3;
-- name: GetUserFavoriteGamesPaginated :many
SELECT
vg.*
FROM virtual_games vg
JOIN virtual_game_favourites vf
ON vf.game_id = vg.id
WHERE
vf.user_id = $1
AND ($2::varchar IS NULL OR vf.provider_id = $2)
ORDER BY vf.created_at DESC
LIMIT $3 OFFSET $4;
-- name: CreateVirtualGame :one
INSERT INTO virtual_games (
game_id,
provider_id,
name,
category,
device_type,
volatility,
rtp,
has_demo,
has_free_bets,
bets,
thumbnail,
status
)
VALUES (
$1,
$2,
$3,
$4,
$5,
$6,
$7,
$8,
$9,
$10,
$11,
$12
)
RETURNING id,
game_id,
provider_id,
name,
category,
device_type,
volatility,
rtp,
has_demo,
has_free_bets,
bets,
thumbnail,
status,
created_at,
updated_at;
-- name: GetAllVirtualGames :many
SELECT vg.id,
vg.game_id,
vg.provider_id,
vp.provider_name,
vg.name,
vg.category,
vg.device_type,
vg.volatility,
vg.rtp,
vg.has_demo,
vg.has_free_bets,
vg.bets,
vg.thumbnail,
vg.status,
vg.created_at,
vg.updated_at
FROM virtual_games vg
JOIN virtual_game_providers vp ON vg.provider_id = vp.provider_id
WHERE (
vg.category = sqlc.narg('category')
OR sqlc.narg('category') IS NULL
)
AND (
name ILIKE '%' || sqlc.narg('name') || '%'
OR sqlc.narg('name') IS NULL
)
AND (
vg.provider_id = sqlc.narg('provider_id')
OR sqlc.narg('provider_id') IS NULL
)
ORDER BY vg.created_at DESC
LIMIT sqlc.narg('limit') OFFSET sqlc.narg('offset');
-- name: DeleteAllVirtualGames :exec
DELETE FROM virtual_games;
-- name: CreateVirtualGameProviderReport :one
INSERT INTO virtual_game_provider_reports (
provider_id,
report_date,
total_games_played,
total_bets,
total_payouts,
total_players,
report_type,
created_at,
updated_at
) VALUES (
$1, $2, $3, $4, $5, $6, COALESCE($7, 'daily'), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
)
ON CONFLICT (provider_id, report_date, report_type) DO UPDATE
SET
total_games_played = EXCLUDED.total_games_played,
total_bets = EXCLUDED.total_bets,
total_payouts = EXCLUDED.total_payouts,
total_players = EXCLUDED.total_players,
updated_at = CURRENT_TIMESTAMP
RETURNING *;
-- name: CreateVirtualGameReport :one
INSERT INTO virtual_game_reports (
game_id,
provider_id,
report_date,
total_rounds,
total_bets,
total_payouts,
total_players,
report_type,
created_at,
updated_at
) VALUES (
$1, $2, $3, $4, $5, $6, $7, COALESCE($8, 'daily'), CURRENT_TIMESTAMP, CURRENT_TIMESTAMP
)
ON CONFLICT (game_id, report_date, report_type) DO UPDATE
SET
total_rounds = EXCLUDED.total_rounds,
total_bets = EXCLUDED.total_bets,
total_payouts = EXCLUDED.total_payouts,
total_players = EXCLUDED.total_players,
updated_at = CURRENT_TIMESTAMP
RETURNING *;
-- name: GetVirtualGameProviderReportByProviderAndDate :one
SELECT *
FROM virtual_game_provider_reports
WHERE provider_id = $1
AND report_date = $2
AND report_type = $3;
-- name: UpdateVirtualGameProviderReportByDate :exec
UPDATE virtual_game_provider_reports
SET
total_games_played = total_games_played + $4,
total_bets = total_bets + $5,
total_payouts = total_payouts + $6,
total_players = total_players + $7,
updated_at = CURRENT_TIMESTAMP
WHERE
provider_id = $1
AND report_date = $2
AND report_type = $3;
-- name: ListVirtualGameProviderReportsByGamesPlayedAsc :many
SELECT *
FROM virtual_game_provider_reports
ORDER BY total_games_played ASC;
-- name: ListVirtualGameProviderReportsByGamesPlayedDesc :many
SELECT *
FROM virtual_game_provider_reports
ORDER BY total_games_played DESC;