-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.sql
56 lines (52 loc) · 2.24 KB
/
data.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
-- Chay tao user truoc
DO $$
DECLARE
first_names VARCHAR[] := ARRAY['Nguyễn', 'Trần', 'Lê', 'Phạm', 'Hoàng', 'Huỳnh', 'Phan', 'Võ', 'Đặng', 'Bùi'];
last_names VARCHAR[] := ARRAY['Hoài', 'Hoàng', 'Đức', 'Anh', 'Thảo', 'Hiếu', 'Hào', 'Nam', 'Cường', 'Lan'];
BEGIN
FOR i IN 10..60 LOOP
DECLARE
username_suffix VARCHAR := i;
full_name VARCHAR := first_names[i % 10 + 1] || ' ' || last_names[i % 10 + 1];
BEGIN
INSERT INTO "user"("username", "password", "name", "email", "password_changed_at", "public_id", "url_image", "role", "active", "money")
VALUES (
LOWER(TRANSLATE(LOWER(full_name), 'áàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđ ', 'aaaaaaaaaaaaaaaaaeeeeeeeeeeeiiiiiooooooooooooooooouuuuuuuuuuuyyyyyd'))|| username_suffix,
'password',
full_name,
NOW(),
i,
'',
'user',
true,
200000
);
END;
END LOOP;
END $$;
--Do random nen co the bi trung id, chay khoi order vai lan neu khong thanh cong
DO $$
DECLARE
user_id INTEGER;
item_id INTEGER;
order_id INTEGER;
BEGIN
FOR user_id IN 2..52 LOOP
FOR i IN 1..2 + FLOOR(RANDOM() * 4) LOOP
-- Insert order
INSERT INTO "order"("id_user", "order_date", "status", "payment", "id_cashier", "total_price")
VALUES (user_id, CURRENT_DATE - (FLOOR(RANDOM() * 30) || ' days')::INTERVAL, 'Completed', true, user_id, 0) RETURNING "id_order" INTO order_id;
-- Insert order details
FOR j IN 1..1 + FLOOR(RANDOM() * 3) LOOP
SELECT "id_item" FROM "item" ORDER BY RANDOM() LIMIT 1 INTO item_id;
INSERT INTO "orderdetail"("id_order", "id_item", "quantity")
VALUES (order_id, item_id, 1 + FLOOR(RANDOM() * 2));
END LOOP;
-- Update total price in the order
UPDATE "order"
SET "total_price" = (SELECT SUM(("item"."price" - ("item"."price" * "item"."discount" / 100)) * "orderdetail"."quantity") FROM "item" JOIN "orderdetail" ON "item"."id_item" = "orderdetail"."id_item" WHERE "id_order" = order_id)
WHERE "id_order" = order_id;
END LOOP;
END LOOP;
END $$;