This repository was archived by the owner on Jun 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDB_PROC.sql
More file actions
246 lines (214 loc) · 7.2 KB
/
DB_PROC.sql
File metadata and controls
246 lines (214 loc) · 7.2 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
####################################################
# -1 -> order not taken
# -2 -> delivery not available
# -3 -> item unavailable
# -4 -> secret key mismatch
# 0 (or) actual_delivery_price
DROP PROCEDURE IF EXISTS get_delivery_price;
DROP PROCEDURE IF EXISTS calculate_price;
DROP PROCEDURE IF EXISTS verify_pricing;
DROP PROCEDURE IF EXISTS validate_order_status;
DROP PROCEDURE IF EXISTS order_status_update;
DROP PROCEDURE IF EXISTS shop_rating_update;
####################################################
DELIMITER $$
CREATE PROCEDURE get_delivery_price(
IN s_id INT,
IN order_type char,
OUT d_price INT,
OUT m_id VARCHAR(32)
)
BEGIN
DECLARE actual_delivery_price DOUBLE;
DECLARE actual_is_delivery_available INT;
DECLARE actual_is_order_taken INT;
DECLARE actual_m_id varchar(32) DEFAULT NULL;
SELECT delivery_price, is_delivery_available, is_order_taken,merchant_id
into actual_delivery_price, actual_is_delivery_available, actual_is_order_taken,actual_m_id
from configurations
where shop_id = s_id;
IF actual_is_order_taken = 0 THEN
set d_price = -1;
ELSE
IF order_type = 'D' THEN
IF actual_is_delivery_available = 1 THEN
set d_price = actual_delivery_price;
set m_id = actual_m_id;
ELSE
set d_price = -2;
END IF;
ELSE
set d_price = 0;
set m_id = actual_m_id;
END IF;
END IF;
END$$
DELIMITER ;
####################################################
DELIMITER $$
CREATE PROCEDURE calculate_price(
IN item_list json,
OUT total_price INT
)
BEGIN
DECLARE item_length BIGINT UNSIGNED DEFAULT JSON_LENGTH(item_list);
DECLARE item_index BIGINT UNSIGNED DEFAULT 0;
DECLARE item_id INT DEFAULT 0;
DECLARE item_quantity INT DEFAULT 0;
DECLARE item_price INT;
set total_price = 0;
item_loop:
WHILE item_index < item_length DO
set item_quantity = JSON_EXTRACT(item_list, CONCAT('$[', item_index, '].quantity'));
set item_id = JSON_EXTRACT(item_list, CONCAT('$[', item_index, '].itemId'));
set item_price = null;
select price
into item_price
from item
where item.id = item_id and
item.is_available = 1 and
item.is_delete = 0;
IF(item_price is null) THEN
set total_price = -3;
LEAVE item_loop;
end if;
set total_price = total_price + (item_price * item_quantity);
SET item_index = item_index + 1;
END WHILE item_loop;
END$$;
DELIMITER ;
####################################################
DELIMITER $$
CREATE PROCEDURE verify_pricing(
IN item_list json,
IN s_id INT,
IN order_type char,
OUT total_price INT,
OUT m_id varchar(32)
)
BEGIN
CALL get_delivery_price(s_id, order_type, @delivery_price, @merchant_id);
if(@delivery_price < 0) THEN
SET total_price = @delivery_price;
ELSE
CALL calculate_price(item_list, total_price);
if(total_price > 0) THEN
SET total_price = total_price + @delivery_price;
SET m_id = @merchant_id;
END IF;
END IF;
END$$;
DELIMITER ;
####################################################
DELIMITER $$
CREATE PROCEDURE validate_order_status(
IN o_id INT,
IN new_status ENUM ('PENDING', 'TXN_FAILURE', 'PLACED',
'CANCELLED_BY_USER', 'ACCEPTED', 'CANCELLED_BY_SELLER',
'READY', 'OUT_FOR_DELIVERY', 'COMPLETED',
'DELIVERED', 'REFUND_INITIATED', 'REFUND_COMPLETED'),
IN new_secret_key VARCHAR(10),
OUT result INT
)
BEGIN
DECLARE actual_status ENUM ('PENDING', 'TXN_FAILURE', 'PLACED',
'CANCELLED_BY_USER', 'ACCEPTED', 'CANCELLED_BY_SELLER',
'READY', 'OUT_FOR_DELIVERY', 'COMPLETED',
'DELIVERED', 'REFUND_INITIATED', 'REFUND_COMPLETED') DEFAULT NULL;
DECLARE actual_delivery_location VARCHAR(128) DEFAULT NULL;
DECLARE actual_secret_key VARCHAR(10) DEFAULT NULL;
SET result = 0;
SELECT status, delivery_location, secret_key
INTO actual_status, actual_delivery_location, actual_secret_key
from orders
where id = o_id;
case actual_status
when 'PENDING' THEN
set result = (new_status = 'TXN_FAILURE') OR (new_status = 'PLACED') OR (new_status = 'REFUND_INITIATED');
when 'PLACED' THEN
set result = (new_status = 'CANCELLED_BY_SELLER') OR (new_status = 'CANCELLED_BY_USER') OR (new_status = 'ACCEPTED');
when 'ACCEPTED' THEN
IF actual_delivery_location IS NULL THEN
set result = (new_status = 'READY') OR (new_status = 'CANCELLED_BY_SELLER');
ELSE
set result = (new_status = 'OUT_FOR_DELIVERY') OR (new_status = 'CANCELLED_BY_SELLER');
END IF;
when 'READY' THEN
IF new_status = 'COMPLETED' THEN
IF actual_secret_key = new_secret_key THEN
set result = 1;
ELSE
set result = -4;
END IF;
END IF;
when 'OUT_FOR_DELIVERY' THEN
IF new_status = 'DELIVERED' THEN
IF actual_secret_key = new_secret_key THEN
set result = 1;
ELSE
set result = -4;
END IF;
END IF;
ELSE
IF actual_status = 'CANCELLED_BY_USER' OR actual_status = 'CANCELLED_BY_SELLER' OR actual_status = 'REFUND_INITIATED' THEN
set result = (new_status = 'REFUND_COMPLETED');
ELSEIF actual_status IS NULL THEN
set result = (new_status = 'TXN_FAILURE') OR (new_status = 'PENDING') OR (new_status = 'PLACED');
END IF;
END CASE;
END$$
DELIMITER ;
####################################################
DELIMITER $$
CREATE PROCEDURE order_status_update(
IN o_id INT,
IN new_status ENUM ('PENDING', 'TXN_FAILURE', 'PLACED',
'CANCELLED_BY_USER', 'ACCEPTED', 'CANCELLED_BY_SELLER',
'READY', 'OUT_FOR_DELIVERY', 'COMPLETED',
'DELIVERED', 'REFUND_INITIATED', 'REFUND_COMPLETED'),
IN new_secret_key VARCHAR(10),
OUT result INT
)
BEGIN
DECLARE gen_secret_key VARCHAR(10) DEFAULT NULL;
CALL validate_order_status(o_id, new_status, new_secret_key, result);
IF result = 1 THEN
IF new_status = 'READY' OR new_status = 'OUT_FOR_DELIVERY' THEN
SET new_secret_key = LPAD(FLOOR(RAND() * 999999.99), 6, '0');
UPDATE orders
set secret_key = new_secret_key, status = new_status
where id = o_id;
else
UPDATE orders
set status = new_status
where id = o_id;
END IF;
END IF;
END$$
DELIMITER ;
####################################################
DELIMITER $$
CREATE PROCEDURE shop_rating_update(
IN s_id INT
)
BEGIN
DECLARE actual_rating DOUBLE(2, 1) DEFAULT NULL;
DECLARE actual_user_count INT DEFAULT NULL;
SELECT COUNT(rating), AVG(rating)
INTO actual_user_count, actual_rating
FROM orders
WHERE shop_id = s_id AND
rating IS NOT NULL;
UPDATE rating
SET rating = actual_rating,
user_count = actual_user_count
WHERE shop_id = s_id;
END$$
DELIMITER ;
####################################################
# CALL verify_pricing('[{"itemId":1,"quantity":1},{"itemId":2,"quantity":2}]', 1, 'P', @total_price, @m_id);
# select @total_price, @m_id;
# CALL order_status_update(1, 'DELIVERED', '966318', @result);
# SELECT @result;
# CALL shop_rating_update(1);
####################################################