consumer模块完成95%,在和商家端对接聊天购物闭环

This commit is contained in:
2026-02-06 17:10:31 +08:00
parent 06b7369494
commit e2f1dfb097
1454 changed files with 5425 additions and 210555 deletions

View File

@@ -0,0 +1,50 @@
-- 1. 用户钱包表 (余额)
create table if not exists ml_user_wallets (
user_id uuid not null primary key,
balance decimal(10,2) default 0.00 not null,
frozen_balance decimal(10,2) default 0.00 not null,
currency varchar(10) default 'CNY',
status int default 1, -- 1: 正常, 0: 冻结
pay_password varchar(100), -- 支付密码 (加密)
created_at timestamptz default now(),
updated_at timestamptz default now()
);
-- 2. 钱包交易记录表
create table if not exists ml_wallet_transactions (
id uuid default gen_random_uuid() primary key,
user_id uuid not null,
amount decimal(10,2) not null, -- 正数表示收入,负数表示支出
balance_after decimal(10,2) not null, -- 变动后余额
type varchar(50) not null, -- 交易类型: recharge(充值), payment(支付), refund(退款), withdrawal(提现)
description text,
order_id varchar(100), -- 关联订单ID (可选)
created_at timestamptz default now()
);
-- 3. 用户积分表
create table if not exists ml_user_points (
user_id uuid not null primary key,
points int default 0 not null, -- 当前可用积分
total_earned int default 0, -- 历史累计获得积分
updated_at timestamptz default now()
);
-- 4. 积分变动记录表
create table if not exists ml_point_records (
id uuid default gen_random_uuid() primary key,
user_id uuid not null,
points int not null, -- 变动积分 (正/负)
type varchar(50) not null, -- 类型: signin(签到), shopping(购物), redeem(兑换), expire(过期)
description text,
created_at timestamptz default now()
);
-- 添加说明
comment on table ml_user_wallets is '用户钱包余额表';
comment on table ml_wallet_transactions is '钱包余额变动明细';
comment on table ml_user_points is '用户积分汇总表';
comment on table ml_point_records is '积分变动明细表';
-- 插入一些测试数据 (可选,仅用于开发环境)
-- insert into ml_user_points (user_id, points, total_earned) values ('YOUR_USER_ID', 1000, 1000);

View File

@@ -0,0 +1,86 @@
-- 钱包相关存储过程
-- 1. 充值函数
create or replace function recharge_wallet(
p_user_id uuid,
p_amount decimal
)
returns json
language plpgsql
security definer
as $$
declare
v_new_balance decimal;
v_wallet_exists boolean;
begin
if p_amount <= 0 then
return json_build_object('success', false, 'message', '充值金额必须大于0');
end if;
-- 检查并锁定用户钱包行
select exists(select 1 from ml_user_wallets where user_id = p_user_id for update) into v_wallet_exists;
if not v_wallet_exists then
-- 如果钱包不存在,创建它
insert into ml_user_wallets (user_id, balance) values (p_user_id, 0);
end if;
-- 更新余额
update ml_user_wallets
set balance = balance + p_amount, updated_at = now()
where user_id = p_user_id
returning balance into v_new_balance;
-- 插入交易记录
insert into ml_wallet_transactions (user_id, amount, balance_after, type, description)
values (p_user_id, p_amount, v_new_balance, 'recharge', '账户在线充值');
return json_build_object('success', true, 'message', '充值成功', 'new_balance', v_new_balance);
exception when others then
return json_build_object('success', false, 'message', SQLERRM);
end;
$$;
-- 2. 提现函数
create or replace function withdraw_wallet(
p_user_id uuid,
p_amount decimal
)
returns json
language plpgsql
security definer
as $$
declare
v_current_balance decimal;
v_new_balance decimal;
begin
if p_amount <= 0 then
return json_build_object('success', false, 'message', '提现金额必须大于0');
end if;
-- 检查并锁定用户钱包
select balance into v_current_balance from ml_user_wallets where user_id = p_user_id for update;
if not found then
return json_build_object('success', false, 'message', '钱包不存在');
end if;
if v_current_balance < p_amount then
return json_build_object('success', false, 'message', '余额不足');
end if;
-- 扣除余额
update ml_user_wallets
set balance = balance - p_amount, updated_at = now()
where user_id = p_user_id
returning balance into v_new_balance;
-- 插入交易记录
insert into ml_wallet_transactions (user_id, amount, balance_after, type, description)
values (p_user_id, -p_amount, v_new_balance, 'withdraw', '余额提现');
return json_build_object('success', true, 'message', '提现申请提交成功', 'new_balance', v_new_balance);
exception when others then
return json_build_object('success', false, 'message', SQLERRM);
end;
$$;