chore: initial commit of HouseholdBudgetBook (backend, frontend, desktop)
This commit is contained in:
commit
4a5db3f1f9
167 files changed
+21732
No files matched your search
@@ -0,0 +1,88 @@
|
||||
package service
|
||||
|
||||
// accountBalancesCTE 供净值汇总等查询复用的 account_balances CTE 片段(不含外层 WITH)。
|
||||
func accountBalancesCTE() string {
|
||||
return `transaction_effects AS (
|
||||
SELECT account_id, SUM(CASE
|
||||
WHEN type = 'income' THEN amount
|
||||
WHEN type IN ('expense', 'transfer') THEN -amount
|
||||
ELSE 0
|
||||
END) AS delta
|
||||
FROM transactions
|
||||
WHERE account_id IS NOT NULL
|
||||
GROUP BY account_id
|
||||
|
||||
UNION ALL
|
||||
|
||||
SELECT to_account_id AS account_id, SUM(amount) AS delta
|
||||
FROM transactions
|
||||
WHERE type = 'transfer' AND to_account_id IS NOT NULL
|
||||
GROUP BY to_account_id
|
||||
),
|
||||
account_deltas AS (
|
||||
SELECT account_id, SUM(delta) AS delta
|
||||
FROM transaction_effects
|
||||
GROUP BY account_id
|
||||
),
|
||||
account_balances AS (
|
||||
SELECT a.id, a.type, a.balance + COALESCE(d.delta, 0) AS current_balance
|
||||
FROM accounts a
|
||||
LEFT JOIN account_deltas d ON d.account_id = a.id
|
||||
)`
|
||||
}
|
||||
|
||||
func accountSelectSQL() string {
|
||||
return `WITH ` + accountBalancesCTE() + `
|
||||
SELECT a.id, a.name, a.type, a.balance,
|
||||
ab.current_balance,
|
||||
COALESCE(a.bank_name,''), COALESCE(a.card_number,''), COALESCE(a.branch_name,''), COALESCE(a.note,''),
|
||||
a.is_active, a.created_at, a.updated_at
|
||||
FROM accounts a
|
||||
JOIN account_balances ab ON ab.id = a.id`
|
||||
}
|
||||
|
||||
func assetSelectSQL() string {
|
||||
return `WITH ` + accountBalancesCTE() + `
|
||||
SELECT a.id, a.name, a.type, a.value, a.account_id, COALESCE(ac.name,''),
|
||||
COALESCE(ab.current_balance, 0) AS bound_balance,
|
||||
a.note, a.created_at, a.updated_at
|
||||
FROM assets a
|
||||
LEFT JOIN accounts ac ON a.account_id = ac.id
|
||||
LEFT JOIN account_balances ab ON ab.id = ac.id`
|
||||
}
|
||||
|
||||
func netWorthSQL() string {
|
||||
return `WITH ` + accountBalancesCTE() + `,
|
||||
totals AS (
|
||||
SELECT
|
||||
COALESCE((
|
||||
SELECT SUM(CASE
|
||||
WHEN ass.account_id IS NOT NULL THEN COALESCE(ab.current_balance, 0)
|
||||
ELSE ass.value
|
||||
END)
|
||||
FROM assets ass
|
||||
LEFT JOIN account_balances ab ON ab.id = ass.account_id
|
||||
), 0) AS total_assets,
|
||||
COALESCE((SELECT SUM(principal) FROM liabilities), 0) AS total_debt,
|
||||
COALESCE((
|
||||
SELECT SUM(CASE
|
||||
WHEN type != 'credit_card' AND current_balance > 0 THEN current_balance
|
||||
ELSE 0
|
||||
END)
|
||||
FROM account_balances
|
||||
WHERE id IN (SELECT id FROM accounts WHERE is_active = 1)
|
||||
AND id NOT IN (SELECT account_id FROM assets WHERE account_id IS NOT NULL)
|
||||
), 0) AS liquid_assets,
|
||||
COALESCE((
|
||||
SELECT SUM(CASE
|
||||
WHEN type = 'credit_card' AND current_balance < 0 THEN -current_balance
|
||||
ELSE 0
|
||||
END)
|
||||
FROM account_balances
|
||||
WHERE id IN (SELECT id FROM accounts WHERE is_active = 1)
|
||||
AND id NOT IN (SELECT account_id FROM assets WHERE account_id IS NOT NULL)
|
||||
), 0) AS liquid_debt
|
||||
)
|
||||
SELECT total_assets, total_debt, liquid_assets, liquid_debt
|
||||
FROM totals`
|
||||
}
|
||||
Reference in new issue
Block a user