Files
medical-mall/docs/sql/10_schema/finance/ml_user_bill_v1.sql

34 lines
1.6 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- =====================================================================================
-- Schema: 用户资金流水表
-- 位置docs/sql/10_schema/finance/
-- 对象类型Schema (DDL)
-- 版本v1
-- 说明:记录用户余额、积分、佣金的所有增减流水(原子日志)
-- =====================================================================================
CREATE TABLE IF NOT EXISTS public.ml_user_bill (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
uid UUID NOT NULL REFERENCES public.ak_users(id),
link_id TEXT NULL, -- 关联业务ID订单号、提现ID、充值ID等
pm SMALLINT NOT NULL DEFAULT 1, -- 0:支出, 1:收入
title TEXT NOT NULL, -- 流水标题(如:商品购买、充值、提现)
category TEXT NOT NULL, -- 业务大类balance-余额, integral-积分, brokerage-佣金)
type TEXT NOT NULL, -- 业务子类型recharge, extract, pay, refund, system_add, system_sub
number DECIMAL(12,2) NOT NULL DEFAULT 0, -- 变动金额
balance DECIMAL(12,2) NOT NULL DEFAULT 0, -- 变动后的余额快照
mark TEXT NULL, -- 备注
status SMALLINT NOT NULL DEFAULT 1, -- 状态1:有效, 0:无效/冲正)
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- 常用查询索引
CREATE INDEX IF NOT EXISTS ml_user_bill_uid_idx ON public.ml_user_bill (uid);
CREATE INDEX IF NOT EXISTS ml_user_bill_category_type_idx ON public.ml_user_bill (category, type);
CREATE INDEX IF NOT EXISTS ml_user_bill_created_at_idx ON public.ml_user_bill (created_at DESC);