-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLCouponFunction.sql
98 lines (90 loc) · 1.89 KB
/
SQLCouponFunction.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
-- create new category --
-- create_category(name, id_type)
-- return void
-- CREATE OR REPLACE function (
-- )
-- returns void
-- language plpgsql
-- as $$
-- --Declare
-- --err integer;
-- begin
-- end;$$;
-- create new coupon --
-- create_coupon(id_coupone, months, value, amount)
-- return void
CREATE OR REPLACE function create_coupon(
p_id_coupon text,
p_month int,
p_value int,
p_amount int
)
returns void
language plpgsql
as $$
-- Declare
-- mon_txt text;
begin
insert into coupon(id_coupon, expiration_date, value, quantity)
values(p_id_coupon, CURRENT_TIMESTAMP + interval '1 month' * p_month, p_value, p_amount);
end;$$;
CREATE OR REPLACE function get_public_coupon()
returns table(
id_coupon text,
value int
)
language plpgsql
as $$
-- Declare
-- mon_txt text;
begin
return query
select c.id_coupon, c.value
from coupon c
where c.quantity > 0 and CURRENT_TIMESTAMP < c.expiration_date
order by c.value desc;
end;$$;
-- select coupon --
-- select_coupon(id_coupon)
-- return -1 || 1
CREATE OR REPLACE function select_coupon(
p_id_coupon text
)
returns table(
err int,
value int
)
language plpgsql
as $$
-- Declare
-- err integer;
begin
if exists(
select *
from coupon
where id_coupon = p_id_coupon and CURRENT_TIMESTAMP < expiration_date and quantity > 0
) then
return query
select 1, c.value
from coupon c
where id_coupon = p_id_coupon;
else
return query
select -1, 0;
end if;
end;$$;
CREATE OR REPLACE function create_public_coupon(
p_id_coupon text,
p_month int,
p_value int,
p_amount int
)
returns void
language plpgsql
as $$
-- Declare
-- mon_txt text;
begin
insert into coupon(id_coupon, expiration_date, value, quantity)
values(p_id_coupon, CURRENT_TIMESTAMP + interval '1 month' * p_month, p_value, p_amount);
end;$$;