重构优惠券表结构与功能

This commit is contained in:
2026-04-09 14:58:20 +08:00
parent 3040b10eed
commit 7d7b979b66
16 changed files with 1012 additions and 199 deletions

View File

@@ -989,7 +989,7 @@ create table bill (
trade_id int,
resource_id int,
refund_id int,
coupon_id int,
coupon_user_id int,
bill_no text not null,
info text,
type int not null,
@@ -1004,7 +1004,7 @@ create index idx_bill_user_id on bill (user_id) where deleted_at is null;
create index idx_bill_trade_id on bill (trade_id) where deleted_at is null;
create index idx_bill_resource_id on bill (resource_id) where deleted_at is null;
create index idx_bill_refund_id on bill (refund_id) where deleted_at is null;
create index idx_bill_coupon_id on bill (coupon_id) where deleted_at is null;
create index idx_bill_coupon_id on bill (coupon_user_id) where deleted_at is null;
create index idx_bill_created_at on bill (created_at) where deleted_at is null;
-- bill表字段注释
@@ -1014,6 +1014,7 @@ comment on column bill.user_id is '用户ID';
comment on column bill.trade_id is '订单ID';
comment on column bill.resource_id is '套餐ID';
comment on column bill.refund_id is '退款ID';
comment on column bill.coupon_user_id is '优惠券发放ID';
comment on column bill.bill_no is '易读账单号';
comment on column bill.info is '产品可读信息';
comment on column bill.type is '账单类型1-消费2-退款3-充值';
@@ -1056,36 +1057,59 @@ comment on column balance_activity.created_at is '创建时间';
-- coupon 优惠券
drop table if exists coupon cascade;
create table coupon (
id int generated by default as identity primary key,
user_id int,
code text not null,
remark text,
amount decimal(12, 2) not null default 0,
min_amount decimal(12, 2) not null default 0,
status int not null default 0,
expire_at timestamptz,
created_at timestamptz default current_timestamp,
updated_at timestamptz default current_timestamp,
deleted_at timestamptz
id int generated by default as identity primary key,
name text not null,
amount decimal(12, 2) not null default 0,
min_amount decimal(12, 2) not null default 0,
count int not null default 0,
status int not null default 1,
expire_type int not null default 0,
expire_at timestamptz,
expire_in int,
created_at timestamptz default current_timestamp,
updated_at timestamptz default current_timestamp,
deleted_at timestamptz
);
create index idx_coupon_user_id on coupon (user_id) where deleted_at is null;
create index idx_coupon_code on coupon (code) where deleted_at is null;
create index idx_coupon_created_at on coupon (created_at) where deleted_at is null;
-- coupon表字段注释
comment on table coupon is '优惠券表';
comment on column coupon.id is '优惠券ID';
comment on column coupon.user_id is '用户ID';
comment on column coupon.code is '优惠券代码';
comment on column coupon.remark is '优惠券备注';
comment on column coupon.name is '优惠券名称';
comment on column coupon.amount is '优惠券金额';
comment on column coupon.min_amount is '最低消费金额';
comment on column coupon.status is '优惠券状态0-未使用1-已使用2-已过期';
comment on column coupon.expire_at is '过期时间';
comment on column coupon.count is '优惠券数量';
comment on column coupon.status is '优惠券状态0-禁用1-正常';
comment on column coupon.expire_type is '过期类型0-不过期1-固定日期2-相对日期(从发放时间算起)';
comment on column coupon.expire_at is '过期时间,固定日期必填';
comment on column coupon.expire_in is '过期时长(天),相对日期必填';
comment on column coupon.created_at is '创建时间';
comment on column coupon.updated_at is '更新时间';
comment on column coupon.deleted_at is '删除时间';
-- coupon_user 优惠券发放
drop table if exists coupon_user cascade;
create table coupon_user (
id int generated by default as identity primary key,
coupon_id int not null,
user_id int not null,
status int not null default 0,
expire_at timestamptz,
used_at timestamptz,
created_at timestamptz default current_timestamp
);
create index idx_coupon_user_coupon_id on coupon_user (coupon_id);
create index idx_coupon_user_user_id on coupon_user (user_id);
-- coupon_user表字段注释
comment on table coupon_user is '优惠券发放表';
comment on column coupon_user.id is '记录ID';
comment on column coupon_user.coupon_id is '优惠券ID';
comment on column coupon_user.user_id is '用户ID';
comment on column coupon_user.status is '使用状态0-未使用1-已使用';
comment on column coupon_user.expire_at is '过期时间';
comment on column coupon_user.used_at is '使用时间';
comment on column coupon_user.created_at is '创建时间';
-- endregion
-- ====================
@@ -1190,11 +1214,13 @@ alter table bill
alter table bill
add constraint fk_bill_refund_id foreign key (refund_id) references refund (id) on delete set null;
alter table bill
add constraint fk_bill_coupon_id foreign key (coupon_id) references coupon (id) on delete set null;
add constraint fk_bill_coupon_id foreign key (coupon_user_id) references coupon_user (id) on delete set null;
-- coupon表外键
alter table coupon
add constraint fk_coupon_user_id foreign key (user_id) references "user" (id) on delete cascade;
-- coupon_user表外键
alter table coupon_user
add constraint fk_coupon_user_user_id foreign key (user_id) references "user" (id) on delete cascade;
alter table coupon_user
add constraint fk_coupon_user_coupon_id foreign key (coupon_id) references coupon (id) on delete cascade;
-- product_sku表外键
alter table product_sku