-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_procedure_trigg_DoAn.sql
406 lines (323 loc) · 12.4 KB
/
store_procedure_trigg_DoAn.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
-- store procedure
-- Tao ma san pham
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_maSanPham`$$
CREATE PROCEDURE sp_maSanPham (out maSP int(10))
BEGIN
DECLARE masanpham int(10) ;
DECLARE tam int(10) ;
Declare v_Found Integer default 1;
DECLARE DanhSachMaSanPham CURSOR FOR SELECT id FROM db_admin_final.products order by id asc ;
DECLARE CONTINUE HANDLER FOR NOT FOUND Set v_Found = 0;
Set tam = 1 ;
OPEN DanhSachMaSanPham ;
-- lay tung dong du lieu trong cursor
My_Loop : Loop
FETCH DanhSachMaSanPham INTO masanpham ;
if v_Found = 0 then
Leave My_Loop;
End if;
IF( masanpham > tam ) then
LEAVE My_Loop ;
-- khong lien tuc
ELSE
-- lien tuc
set tam = tam + 1 ;
END IF;
End Loop My_Loop;
set maSP = tam ;
CLOSE DanhSachMaSanPham;
END; $$
DELIMITER ;
CALL sp_maSanPham(@result2);
SELECT @result2;
--------------------------------------------
-- Sp_ThemSanPham : Thêm san pham
DELIMITER $$
DROP PROCEDURE IF EXISTS `Sp_ThemSanPham`$$
CREATE PROCEDURE Sp_ThemSanPham ( code_product varchar(191) , name varchar(191) , slug varchar(191) ,details varchar(191),
price double , price_in double , price_promotion double,description text ,brand_id int(10),
category_id int(10),featured tinyint(1) , new tinyint(1),hot_price int(10),
image varchar(191) ,quanity int(10) ,status tinyint(1)
)
BEGIN
CALL sp_maSanPham(@result);
SELECT @result;
INSERT INTO `products`(`id`, `code_product`, `name`, `slug`, `details`, `price`, `price_in`, `price_promotion`,
`description`, `brand_id`, `category_id`, `featured`, `new`, `hot_price`, `image`, `quanity`, `status`, `created_at`, `updated_at`)
VALUES ( (SELECT @result) , code_product , name,slug,details,price,price_in,price_promotion,
description, brand_id, category_id ,featured,
new,hot_price,image,quanity , status , null , null);
END; $$
DELIMITER ;
CALL Sp_ThemSanPham ( 'nh0007', 'Khay son lì Mira Hydro Shine nh0007 ', 'nh0007-khay-sson-li-mira-22064', 'null khong co',
96750, 129000, 0,
'Không có !!!',
225,
1,
1,
0,
1,
'products/June2018/Ba0jgp3eeSwQ7wuxWlA2.png',
15 , 1 );
-----------------------------
-- Update Slide
DELIMITER $$
DROP PROCEDURE IF EXISTS `Sp_SuaSlide`$$
CREATE PROCEDURE Sp_SuaSlide ( id int(10) ,image varchar(191) , link varchar(191) ,title varchar(191) , status tinyint(1) ,
category int(10)
)
BEGIN
DECLARE count int default 0 ;
DECLARE thongbao nvarchar(191) ;
set count = (select count(*) from db_admin_final.slides where slides.id = id) ;
IF ( count > 0 ) then
-- cap nhat slide
UPDATE slides SET image = image, link =link, title=title,status=status,category_id= category WHERE slides.id = id limit 1;
select thongbao = 'Update thanh cong';
ELSE
select thongbao = 'Mã slide không ton tai hoac hinh da trung , vui long kiem tra lai';
ROLLBACK ;
END IF ;
END; $$
DELIMITER ;
CALL Sp_SuaSlide ( 9 , 'slides/June2018/PUuHVvgrt0u6sBpn2XCx.jpg' , 'Chuwa cso', null , 1 , 1 );
----------------------------------------------------
-- Update so luong ma giam gia
DELIMITER $$
DROP PROCEDURE IF EXISTS `Sp_CapNhatSoLuongMaGiamGia`$$
CREATE PROCEDURE Sp_CapNhatSoLuongMaGiamGia ( code_nhap varchar(191) , out thongbao nvarchar(191)
)
BEGIN
DECLARE qty int ;
DECLARE count int ;
set count = ( SELECT count(*) FROM db_admin_final.coupons where coupons.code = code_nhap AND status = 1 );
IF (count > 0 ) then
set qty = ( SELECT quanity FROM db_admin_final.coupons where coupons.code = code_nhap limit 1);
set qty = qty - 1 ;
UPDATE coupons SET quanity = qty WHERE code = code_nhap limit 1 ;
set thongbao = 'true' ;
ELSE
set thongbao= 'Ap dung ma khong thanh cong';
ROLLBACK ;
END IF ;
END; $$
DELIMITER ;
CALL Sp_CapNhatSoLuongMaGiamGia('nhh002',@thongbao) ;
select @thongbao;
--------------------------------
-- cap nhat so luong san pham
DELIMITER $$
DROP PROCEDURE IF EXISTS `Sp_CapNhatSoLuongSanPham`$$
CREATE PROCEDURE Sp_CapNhatSoLuongSanPham ( product_id int(10), qty int , out thongbao nvarchar(191)
)
BEGIN
DECLARE quanitys int ;
DECLARE count int ;
-- DECLARE thongbao nvarchar(191) ;
set count = ( SELECT count(*) FROM db_admin_final.products where products.id = product_id AND status = 1 );
IF (count > 0 ) then
set quanitys = ( SELECT quanity FROM db_admin_final.products where products.id = product_id );
set quanitys = quanitys - qty ;
UPDATE products SET quanity = quanitys WHERE products.id = product_id limit 1 ;
set thongbao= 'Cap nhat thanh cong!';
ELSE
set thongbao = 'cap nhat that bai';
ROLLBACK ;
END IF ;
END; $$
DELIMITER ;
CALL Sp_CapNhatSoLuongSanPham(2, 2 , @thongbao) ;
select @thongbao ;
---------------------------------
-- Tao ma order
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_MaHoaDon`$$
CREATE PROCEDURE sp_MaHoaDon (out maHD int(10))
BEGIN
DECLARE maHDtam int(10) ;
DECLARE tam int(10) ;
Declare v_Found Integer default 1;
DECLARE DanhSachMaHoaDon CURSOR FOR SELECT id FROM db_admin_final.orders order by id asc ;
DECLARE CONTINUE HANDLER FOR NOT FOUND Set v_Found = 0;
Set tam = 1 ;
OPEN DanhSachMaHoaDon ;
-- lay tung dong du lieu trong cursor
My_Loop : Loop
FETCH DanhSachMaHoaDon INTO maHDtam ;
-- select maHDtam;
if v_Found = 0 then
Leave My_Loop;
End if;
IF( maHDtam > tam ) then
LEAVE My_Loop ;
-- khong lien tuc
ELSE
-- lien tuc
set tam = tam + 1 ;
END IF;
End Loop My_Loop;
set maHD = tam ;
CLOSE DanhSachMaHoaDon;
END; $$
DELIMITER ;
CALL sp_MaHoaDon(@resulst2);
SELECT @resulst2;
-------------------------------
-- them hoa don
DELIMITER $$
DROP PROCEDURE IF EXISTS `Sp_ThemHoaDon`$$
CREATE PROCEDURE Sp_ThemHoaDon ( user_id int(10) ,billing_name_on_card varchar(191) ,billing_discount int(11),billing_discount_code varchar(191)
,billing_total int(11), payment_gateway varchar(191) , shipped varchar(20),error_order varchar(191)
)
BEGIN
DECLARE diemcong float ;
DECLARE chuoi1 nvarchar(191) ;
DECLARE id_order int(10) ;
DECLARE loaikhachhang int(10) ;
DECLARE diemtichluy float ;
-- goi tao ma hoa don
CALL sp_MaHoaDon(@id_order);
set id_order = (SELECT @id_order );
CALL Sp_CapNhatSoLuongMaGiamGia(billing_discount_code,@chuoi1);
set chuoi1 = (select @chuoi1 );
select chuoi1 ;
IF ( @chuoi1='true' )
then
-- khach hang thuoc loai nao
set loaikhachhang = ( SELECT member FROM db_admin_final.users , db_admin_final.customer
where users.id = customer.user_id AND users.id= user_id) ;
Case loaikhachhang
When 1 then
set billing_total = billing_total * 1 ;
When 2 then
set billing_total = billing_total * 0.95 ;
Else
set billing_total = billing_total * 0.9 ;
End case;
-- cong diem tich luy cho khach hang
set diemtichluy = (SELECT customer.point FROM db_admin_final.users , db_admin_final.customer
where users.id = customer.user_id AND users.id= user_id);
set diemtichluy = diemtichluy + billing_total*0.1 ;
update customer set customer.point = diemtichluy where customer.user_id = user_id limit 1 ;
INSERT INTO orders(id, user_id, billing_name_on_card, billing_discount, billing_discount_code, billing_tax,
billing_total, payment_gateway, shipped, error)
VALUES (id_order,user_id,billing_name_on_card,billing_discount,
billing_discount_code,10,billing_total,payment_gateway,shipped,error_order) ;
ELSE
select 'Tao hoa don that bai' ;
ROLLBACK ;
END IF ;
END; $$
DELIMITER ;
CALL Sp_ThemHoaDon ( 22 , 'Nguyen hoang hiep' ,50000,'nh003' ,165000, 'ATM' , 'Đang chờ' , 'null' );
-----------------------------------
-- --------Trigger---------
DROP TRIGGER IF EXISTS `Tg_CapNhat_TinhTrang_SanPham`
-- Update bang san pham
DELIMITER $$
CREATE TRIGGER Tg_CapNhat_TinhTrang_SanPham AFTER UPDATE
ON products
FOR EACH ROW
BEGIN
DECLARE idd int(10) ;
SET idd = new.id; -- products
IF( (select products.quanity from products where products.id = idd )=0 and (select products.status from products where products.id = idd) = 1 ) then
update products set products.status = 0 where products.id = idd limit 1;
END IF ;
END ;$$
DELIMITER ;
------------------------------------------
DELIMITER $$
CREATE TRIGGER Tg_CapNhat_LoaiKhachHang AFTER INSERT
ON orders
FOR EACH ROW
BEGIN
declare id int(10) ;
set id = new.user_id ;
if( (select customer.member from customer where user_id = id) = 1 and (select customer.point from customer where user_id = id) >= 10000
and (select customer.point from customer where user_id = id) < 30000)
then
update customer set member = 2 where user_id = id ;
end if;
if( (select customer.member from customer where user_id = id) = 1 and (select customer.point from customer where user_id = id) >= 30000)
then
update customer set member = 3 where user_id = id ;
end if;
if( (select customer.member from customer where user_id = id) = 2 and (select customer.point from customer where user_id = id) >= 30000)
then
update customer set member = 3 where user_id = id ;
end if;
END ;$$
DELIMITER ;
CALL Sp_ThemHoaDon (23 , 'Nguyen hoang hiep' ,50000,'nh003' ,165000, 'ATM' , 'Đang chờ' , 'null' );
-------------------------------------------
DELIMITER $$
CREATE TRIGGER Tg_CapNhat_TrangThai_Coupons AFTER INSERT
ON orders
FOR EACH ROW
BEGIN
declare code int(10) ;
set code = new.code ;
if( (select coupons.quanity from coupons where coupons.code = code) = 0)
then
update coupons set status = 0 where coupons.code = code ;
end if;
END ;$$
DELIMITER ;
-- ------------------------------------------
SET GLOBAL TRANSACTION ISOLATION LEVEL READ UNCOMMITTED ;
START TRANSACTION;
CALL Sp_ThemSanPham ( 'nh0007', 'Khay son lì Mira Hydro Shine nh0007 ', 'nh0007-khay-sson-li-mira-22064', 'null khong co',
96750, 129000, 0,
'Không có !!!',
225,
1,
1,
0,
1,
'products/June2018/Ba0jgp3eeSwQ7wuxWlA2.png',
15 , 1 );
DO sleep(10);
ROLLBACK;
START TRANSACTION;
select * from products WHERE 1 ;
COMMIT ;
--------------------------------
SET GLOBAL TRANSACTION ISOLATION LEVEL REPEATABLE READ ;
START TRANSACTION ;
SELECT SUM(customer.point) as sum from customer WHERE customer.member = 1 group by customer.member ; -- customer.user_id = 22
DO sleep(8);
SELECT sum(customer.point) as sum from customer WHERE customer.member = 1 group by customer.member ; -- customer.user_id = 22 and
COMMIT ;
-------------------------------------
SELECT orders.id , orders.billing_name_on_card , orders.billing_total from orders WHERE orders.created_at = CURDATE() ;
select @toangdanhthu := ( SELECT SUM(orders.billing_total) from orders WHERE orders.created_at = CURDATE() ) ;
-------------------------------
SET GLOBAL TRANSACTION ISOLATION LEVEL SERIALIZABLE ;
START TRANSACTION ;
SELECT orders.id , orders.billing_name_on_card , orders.billing_total from orders WHERE orders.created_at = CURDATE() ;
DO sleep(8) ;
select @toangdanhthu := (SELECT SUM(orders.billing_total) from orders WHERE orders.created_at = CURDATE() ) ;
COMMIT ;
----------------------------------------------------
DELIMITER $$
DROP PROCEDURE IF EXISTS `Sp_XoaSanPham`$$
CREATE PROCEDURE Sp_XoaSanPham ( product_code nvarchar(191) )
BEGIN
DECLARE sl int ;
-- start transaction ;
-- SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE ;
SET SESSION TRANSACTION ISOLATION LEVEL Repeatable Read ;
START TRANSACTION;
SET sl = ( SELECT products.quanity FROM db_admin_final.products where products.code_product = product_code);
do sleep(5);
IF (sl = 0 ) then
DELETE FROM products WHERE products.code_product = product_code ;
ELSE
ROLLBACK ;
END IF ;
commit;
END; $$
DELIMITER ;
CALL Sp_XoaSanPham ('C380') ;