-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.sql
2401 lines (2059 loc) · 123 KB
/
db.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- phpMyAdmin SQL Dump
-- version 4.9.7deb1
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Sep 12, 2022 at 03:52 AM
-- Server version: 10.3.24-MariaDB-2
-- PHP Version: 7.4.11
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `alshab_staging2`
--
DELIMITER $$
--
-- Procedures
--
CREATE DEFINER=`root`@`localhost` PROCEDURE `ArticleCreate` (IN `Iuser_id` INT, IN `Icategory_id` INT, IN `Iviews_count_rate` INT, IN `Ititle` VARCHAR(250), IN `Iimg` VARCHAR(250), IN `Icontent` TEXT, IN `Istatus` VARCHAR(250)) BEGIN
INSERT INTO
articles (
user_id,
category_id,
views_count_rate,
title,
img,
content,
status
)
VALUES
(
Iuser_id,
Icategory_id,
Iviews_count_rate,
Ititle,
Iimg,
Icontent,
Istatus
);
IF Istatus = 'active' THEN
UPDATE articles SET published_at = NOW() WHERE id = LAST_INSERT_ID();
END IF;
SELECT LAST_INSERT_ID() id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ArticleDelete` (IN `id` INT) BEGIN
UPDATE
articles a
SET
deleted_at = now()
WHERE
a.id = id;
SELECT id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ArticleList` (IN `page` SMALLINT(3), IN `u` INT, IN `cat` INT) BEGIN DECLARE userCond VARCHAR(100) DEFAULT '';
DECLARE catCond VARCHAR(100) DEFAULT '';
IF u != 0 THEN
SET
userCond = CONCAT(' AND user_id = ', u);
END IF;
IF u != 0 THEN
SET
catCond = CONCAT(' AND category_id = ', cat);
END IF;
SET
@query = CONCAT(
'SELECT
a.* , u.name u_name , c.name cat_name FROM articles = a
JOIN categories c ON c.id = a.category_id JOIN users u ON u.id = a.user_id WHERE 1 = 1',
userCond,
catCond,
" LIMIT 16 OFFSET ",
16 * (page - 1)
);
PREPARE stmt
FROM
@query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT
@query;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ArticleListByCategoryUserSearch` (IN `ICategory` INT, IN `IUserName` VARCHAR(200), IN `dateFrom` VARCHAR(200), IN `dateTo` VARCHAR(200), IN `search` VARCHAR(200)) BEGIN
SELECT
a.id,
c.name categoryName,
a.views_count_rate,
u.name_ar userName,
u.img userImg,
a.title,
a.img,
a.views,
a.Published_at
FROM articles a
JOIN users u ON u.id = a.user_id
JOIN categories c ON c.id = a.category_id
WHERE a.status = "active" AND a.deleted_at IS NULL
AND CASE WHEN ICategory = 0 THEN 1 = 1 ELSE a.category_id = ICategory END
AND CASE WHEN search = '' THEN 1 = 1 ELSE a.title LIKE CONCAT('%' , search , '%') END
AND CASE WHEN IUserName = '' THEN 1 = 1 ELSE u.name_ar LIKE CONCAT('%' , IUserName , '%') END
AND CASE WHEN dateFrom = '' THEN 1 = 1 ELSE a.Published_at >= dateFrom END
AND CASE WHEN dateTo = '' THEN 1 = 1 ELSE a.Published_at <= dateTo END;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ArticlePending` (`Istatus` VARCHAR(100), `Iname_ar` VARCHAR(200), `Ititle` VARCHAR(200), `Iphone` VARCHAR(200), `Iemail` VARCHAR(200), `dateFrom` VARCHAR(200), `dateTo` VARCHAR(200)) BEGIN
SELECT a.id, u.name_ar , u.phone , u.email , a.title ,a.status, a.created_at
FROM articles a JOIN users u ON a.user_id = u.id
WHERE published_at IS NULL
AND a.deleted_at IS NULL
AND (CASE WHEN Istatus = "" THEN '1' ELSE a.status = Istatus END)
AND (CASE WHEN Iname_ar = "" THEN '1' ELSE u.name_ar LIKE CONCAT('%' , Iname_ar , '%') END)
AND (CASE WHEN Ititle = "" THEN '1' ELSE a.title LIKE CONCAT('%' , Ititle , '%') END)
AND (CASE WHEN Iphone = "" THEN '1' ELSE u.phone LIKE CONCAT('%' , Iphone , '%') END)
AND (CASE WHEN Iemail = "" THEN '1' ELSE u.email LIKE CONCAT('%' , Iemail , '%') END)
AND (CASE WHEN dateFrom = "" THEN '1' ELSE a.created_at >= dateFrom END)
AND (CASE WHEN dateTo = "" THEN '1' ELSE a.created_at <= dateTo END);
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ArticlePendingAction` (`Iid` INT, `Istatus` VARCHAR(100)) BEGIN
UPDATE articles SET
status = CASE WHEN Istatus = "approved" THEN 'active' ELSE Istatus END ,
published_at = CASE WHEN Istatus = "approved" THEN NOW() ELSE NULL END
WHERE id = Iid;
SELECT Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ArticleRead` (IN `Iid` INT) BEGIN
UPDATE articles set views = views + views_count_rate WHERE id = Iid;
SELECT
a.id,
a.user_id,
a.category_id,
a.views_count_rate,
u.name userName,
u.img userImg,
c.name categoryName,
a.title,
a.img,
a.views,
a.status,
a.content,
a.created_at,
IFNULL(a.published_at , '')
FROM articles a
JOIN users u ON u.id = a.user_id
JOIN categories c ON c.id = a.category_id
WHERE a.id = Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ArticleUpdate` (IN `Iid` INT, IN `Iuser_id` INT, IN `Icategory_id` INT, IN `Iviews_count_rate` INT, IN `Ititle` VARCHAR(250), IN `Iimg` VARCHAR(250), IN `Icontent` TEXT, IN `Istatus` VARCHAR(250)) BEGIN
UPDATE
articles
SET
user_id = Iuser_id,
category_id = Icategory_id,
views_count_rate = Iviews_count_rate,
title = Ititle,
img = Iimg,
content = Icontent,
status = Istatus WHERE id = Iid;
SELECT Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `CategoryCreate` (IN `Iname` VARCHAR(300), IN `Iicon` VARCHAR(300), IN `Itype` VARCHAR(10)) BEGIN
INSERT INTO categories (name , icon , type) VALUES (Iname , Iicon , Itype);
SELECT LAST_INSERT_ID() id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `CategoryFind` (IN `Iid` INT) BEGIN
SELECT
id,name,icon,type
FROM categories
WHERE id = Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `CategoryListByType` (IN `Itype` VARCHAR(50)) BEGIN
SELECT
id,name,icon,type
FROM categories
WHERE type = CASE WHEN Itype = "" THEN type ELSE Itype END ;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `CategoryUpdate` (IN `Iid` INT, IN `Iname` VARCHAR(300), IN `Iicon` VARCHAR(300), IN `Itype` VARCHAR(10)) BEGIN
UPDATE categories SET
name = Iname ,
icon = Iicon ,
type = Itype WHERE id = Iid ;
SELECT Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `CityCreate` (IN `Iname` TEXT) BEGIN
INSERT INTO cities (name) VALUES (Iname);
SELECT LAST_INSERT_ID() id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `CityFind` (IN `Iid` INT) BEGIN
SELECT id,name from cities WHERE id = Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `CityListAll` () BEGIN
SELECT id,name from cities;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `CityUpdate` (IN `Iid` INT, IN `Iname` TEXT) BEGIN
UPDATE cities SET name = Iname WHERE id = Iid;
SELECT Iid id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ConsultuntById` (IN `Iid` INT) BEGIN
SELECT
id,
name,
title,
skills,
img,
is_team,
breif
FROM consultunts WHERE id = Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ConsultuntsCreate` (IN `Iname` VARCHAR(250), IN `Ititle` VARCHAR(250), IN `Iskills` VARCHAR(250), IN `Iimg` TEXT, IN `Iis_team` BOOLEAN, IN `Ibreif` TEXT) BEGIN
INSERT INTO consultunts (
name,
title,
skills,
img,
is_team,
breif
)
VALUES (
Iname,
Ititle,
Iskills,
Iimg,
Iis_team,
Ibreif
);
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ConsultuntsListAll` (IN `Iis_team` BOOLEAN, IN `Iname` VARCHAR(200), IN `Ititle` VARCHAR(200), IN `Iskills` VARCHAR(200)) BEGIN
SELECT
id,
name,
title,
skills,
img,
is_team,
breif
FROM consultunts WHERE
is_team = Iis_team
AND CASE WHEN Iname = '' THEN 1 = 1 ELSE name LIKE CONCAT('%' , Iname , '%') END
AND CASE WHEN Ititle = '' THEN 1 = 1 ELSE title LIKE CONCAT('%' , Ititle , '%') END
AND CASE WHEN Iskills = '' THEN 1 = 1 ELSE skills LIKE CONCAT('%' , Iskills , '%') END
AND deleted_at IS NULL;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ConsultuntsUpdate` (IN `Iid` INT, IN `Iname` VARCHAR(250), IN `Ititle` VARCHAR(250), IN `Iskills` VARCHAR(250), IN `Iimg` TEXT, IN `Iis_team` BOOLEAN, IN `Ibreif` TEXT) BEGIN
UPDATE consultunts SET
name = Iname ,
title = Ititle ,
skills = Iskills ,
img = Iimg ,
is_team = Iis_team ,
breif = Ibreif
WHERE id = Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ContactRequestsList` (`Istatus` VARCHAR(100), `Iname_ar` VARCHAR(200), `Iemail` VARCHAR(200), `Iphone` VARCHAR(200), `dateFrom` VARCHAR(200), `dateTo` VARCHAR(200)) BEGIN
SELECT id, IFNULL(user_id , 0 ) user_id ,
name , email , phone , IFNULL(subject , '') ,
msg , status,
created_at
FROM contact_requests
WHERE (CASE WHEN Istatus = '' THEN '1' ELSE status = Istatus END)
AND (CASE WHEN Iname_ar = '' THEN 1 = 1 ELSE name LIKE CONCAT('%' , Iname_ar , '%') END)
AND (CASE WHEN Iemail = '' THEN 1 = 1 ELSE email LIKE CONCAT('%' , Iemail , '%') END)
AND (CASE WHEN Iphone = '' THEN 1 = 1 ELSE phone LIKE CONCAT('%' , Iphone , '%') END)
AND (CASE WHEN dateFrom = '' THEN '1' ELSE created_at >= dateFrom END)
AND (CASE WHEN dateTo = '' THEN '1' ELSE created_at <= dateTo END);
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `DeleteRecord` (IN `Itable` VARCHAR(30), IN `Iid` INT) BEGIN
SET @query = CONCAT('UPDATE ' , Itable , ' SET deleted_at = "', NOW() , '" WHERE id = ' , Iid);
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT 1 deleted;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `EventCreate` (IN `Ititle` VARCHAR(250), IN `Iimg` VARCHAR(250), IN `Ivideo` TEXT, IN `Ibreif` TEXT, IN `Idate` DATE, IN `Iprice` FLOAT, IN `Ifeatured` BOOLEAN, IN `Icategory_id` INT) BEGIN
INSERT INTO events (
title,
img,
video,
breif,
date,
price,
featured,
category_id
) VALUES (
Ititle,
Iimg,
Ivideo,
Ibreif,
Idate,
Iprice,
Ifeatured,
Icategory_id
);
SELECT LAST_INSERT_ID( ) id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `EventEdit` (IN `Iid` INT, IN `Ititle` VARCHAR(250), IN `Iimg` VARCHAR(250), IN `Ivideo` TEXT, IN `Ibreif` TEXT, IN `Idate` VARCHAR(250), IN `Iprice` FLOAT, IN `Ifeatured` BOOLEAN, IN `Icategory_id` INT) BEGIN
UPDATE events SET
title = Ititle,
img = CASE WHEN Iimg = "" THEN img ELSE Iimg END,
video = Ivideo,
breif = Ibreif,
`date` = Idate,
price = Iprice,
featured = Ifeatured,
category_id = Icategory_id
WHERE id = Iid;
SELECT Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `EventRead` (IN `Iid` INT) BEGIN
SELECT e.id ,e.title,e.img ,IFNULL(e.breif,"") breif ,day(e.date) d,month(e.date) m,year(e.date) y,e.date, e.price ,e.featured ,e.created_at , c.Id cat_id, c.name cat_name , e.video FROM events e JOIN categories c on e.category_id = c.id WHERE e.id = Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `EventsList` (IN `Ifeatured` BOOLEAN, IN `Ititle` VARCHAR(100), IN `Istatus` VARCHAR(100), IN `Icategory` INT, IN `dateFrom` VARCHAR(100), IN `dateTo` VARCHAR(100), IN `priceFrom` FLOAT, IN `priceTo` FLOAT) BEGIN
SELECT e.id ,e.title,e.img ,IFNULL(e.breif,"") ,day(e.date) d,
month(e.date) m,year(e.date) y, e.price ,e.featured ,e.created_at ,
c.name cat_name , c.id cat_id
FROM events e
JOIN categories c
on e.category_id = c.id
WHERE e.deleted_at IS NULL
AND CASE WHEN Ititle = '' THEN 1 = 1 ELSE title LIKE CONCAT('%' , Ititle , '%') END
AND CASE WHEN Ifeatured IS NULL THEN 1 = 1 ELSE featured = Ifeatured END
AND CASE WHEN Icategory = 0 THEN 1 = 1 ELSE category_id = Icategory END
AND CASE WHEN dateFrom = '' THEN 1 = 1 ELSE e.date >= dateFrom END
AND CASE WHEN dateTo = '' THEN 1 = 1 ELSE e.date <= dateTo END
AND CASE WHEN priceFrom = 0 THEN 1 = 1 ELSE e.price >= priceFrom END
AND CASE WHEN priceTo = 0 THEN 1 = 1 ELSE e.price <= priceTo END
ORDER BY e.date DESC;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `EventsListByCategorySearch` (IN `ICategory` INT, IN `search` VARCHAR(200)) BEGIN
DECLARE categoryCond VARCHAR(50) DEFAULT '';
DECLARE searchCond VARCHAR(50) DEFAULT '';
IF ICategory != 0 THEN
SET categoryCond = CONCAT('AND category_id = ' , ICategory);
END IF;
IF search != '' THEN
SET searchCond = CONCAT('AND title LIKE "%' , search , '%"');
END IF;
SET @query = CONCAT(
'SELECT
e.id ,e.title,e.img ,IFNULL(e.breif,"") ,day(e.date) d,month(e.date) m,year(e.date) y, e.price ,e.featured ,e.created_at , c.name cat_name , c.id cat_id FROM events e JOIN categories c on e.category_id = c.id WHERE 1 = 1 ',
categoryCond,
searchCond);
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `FeaturesEditAdd` (IN `Iid` INT, IN `Iname` VARCHAR(200), IN `Ibreif` TEXT, IN `Ilevel` INT) BEGIN
IF Iid != 0 THEN
UPDATE features SET name = Iname , level = Ilevel, breif = Ibreif WHERE id = Iid;
ELSE
INSERT INTO features (
name,
breif,
level
) VALUES (
Iname,
Ibreif,
Ilevel
);
SET Iid = LAST_INSERT_ID();
END IF;
SELECT Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `FeaturesFindById` (IN `Iid` INT) BEGIN
SELECT * from features WHERE id = Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `FeaturesListAll` () BEGIN
SELECT * from features;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `FeaturesListByRole` (IN `role` INT, IN `Iname` VARCHAR(250)) BEGIN
SELECT * from features WHERE
(CASE WHEN role IS NULL THEN '1' ELSE level <= (role -1) END)
AND CASE WHEN Iname = '' THEN 1 = 1 ELSE name LIKE CONCAT('%' , Iname , '%') END;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `FindDashboardCounts` () BEGIN
SET @projects = (SELECT COUNT(*) FROM projects WHERE deleted_at IS NULL);
SET @events = (SELECT COUNT(*) FROM events WHERE deleted_at IS NULL);
SET @users = (SELECT COUNT(*) FROM users WHERE deleted_at IS NULL);
SET @pendingUsers = (SELECT COUNT(*) FROM users WHERE active = 0 AND deleted_at IS NULL);
SET @ryadeen = (SELECT COUNT(*) FROM users WHERE active = 1 AND deleted_at IS NULL AND role_id = 3);
SET @tamooheen = (SELECT COUNT(*) FROM users WHERE active = 1 AND deleted_at IS NULL AND role_id = 2);
SET @mobadreen = (SELECT COUNT(*) FROM users WHERE active = 1 AND deleted_at IS NULL AND role_id = 1);
SELECT
@projects projects,
@events events,
@users users,
@pendingUsers pendingUsers,
@ryadeen ryadeen,
@tamooheen tamooheen,
@mobadreen mobadreen;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `Merge` () BEGIN
CALL MergeRoles();
CALL MergeRich();
CALL MergeCities();
CALL MergeCats();
CALL MergeServices();
CALL MergeConsultunts();
CALL MergeVideos();
CALL MergeEvents();
CALL MergeUsers();
CALL MergeArticles();
CALL MergeProjects();
CALL MergeNotifications();
CALL MergeMsgs();
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeArticles` () BEGIN
INSERT INTO alshab_staging2.articles(
user_id,
category_id,
title,
img,
status,
content,
views,
created_at,
published_at,
deleted_at
)
SELECT
(SELECT id FROM alshab_staging2.users WHERE phone = u.phone),
(SELECT id FROM alshab_staging2.categories WHERE name = c.name LIMIT 1),
ua.title,
ua.img,
ua.status,
ua.content,
ua.views,
ua.created_at,
ua.published_at,
ua.deleted_at
FROM alshab_staging.articles ua
JOIN alshab_staging.users u
ON ua.user_id = u.id
JOIN alshab_staging.categories c
ON ua.category_id = c.id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeCats` () BEGIN
INSERT INTO alshab_staging2.categories(name , icon , type) SELECT name , icon , type FROM alshab_staging.categories;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeCities` () BEGIN
INSERT INTO alshab_staging2.cities(name) SELECT name FROM alshab_staging.cities;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeConsultunts` () BEGIN
INSERT INTO alshab_staging2.consultunts(name , title , skills , img , is_team , breif) SELECT name , title , skills , img , is_team , breif FROM alshab_staging.consultunts;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeEvents` () BEGIN
INSERT INTO alshab_staging2.events(title , img , video , breif , `date` , price , featured , category_id) SELECT title , img , video , breif , `date` , price , featured , (SELECT id FROM alshab_staging2.categories WHERE name = c.name LIMIT 1) FROM alshab_staging.events e JOIN alshab_staging.categories c ON e.category_id = c.id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeMsgs` () BEGIN
INSERT INTO alshab_staging2.msgs(
from_id,
to_id,
created_at,
breif,
seen
)
SELECT
(SELECT id FROM alshab_staging2.users WHERE phone = fr.phone),
(SELECT id FROM alshab_staging2.users WHERE phone = t.phone),
now(),
m.breif,
m.seen
FROM alshab_staging.msgs m
JOIN alshab_staging.users fr ON m.from_id = fr.id
JOIN alshab_staging.users t ON m.to_id = t.id ;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeNotifications` () BEGIN
INSERT INTO alshab_staging2.notifications(
title,
breif,
link
)
SELECT
title,
breif,
link
FROM alshab_staging.notifications;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeProjects` () BEGIN
INSERT INTO alshab_staging2.projects(
user_id,
category_id,
city_id,
title,
logo,
img,
fund,
status,
breif,
imgs,
location,
phone,
file,
email,
featured,
website,
instagram,
twitter,
active,
created_at,
deleted_at
)
SELECT
(SELECT id FROM alshab_staging2.users WHERE phone = u.phone),
(SELECT id FROM alshab_staging2.categories WHERE name = ca.name LIMIT 1),
(SELECT id FROM alshab_staging2.cities WHERE name = ci.name LIMIT 1),
up.title,
up.logo,
up.img,
up.fund,
up.status,
up.breif,
up.imgs,
up.location,
up.phone,
up.file,
up.email,
up.featured,
up.website,
up.instagram,
up.twitter,
up.active,
up.created_at,
up.deleted_at
FROM alshab_staging.projects up
JOIN alshab_staging.users u
ON up.user_id = u.id
JOIN alshab_staging.categories ca
ON up.category_id = ca.id
JOIN alshab_staging.cities ci
ON up.category_id = ci.id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeRich` () BEGIN
INSERT INTO alshab_staging2.rich_text(`key` , `value` , title , image , `group` , icon) SELECT `key` , `value` , title , image , `group` , icon FROM alshab_staging.rich_text;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeRoles` () BEGIN
INSERT INTO alshab_staging2.features(name , breif , level ) SELECT name , breif , level FROM alshab_staging.features;
INSERT INTO alshab_staging2.roles(name , image , breif , price , color) SELECT name , image , breif , price , color FROM alshab_staging.roles;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeServices` () BEGIN
INSERT INTO alshab_staging2.services(name , icon) SELECT name , icon FROM alshab_staging.services;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeUsers` () BEGIN
INSERT INTO alshab_staging2.users(
name ,
name_ar ,
email ,
img ,
password ,
serial ,
points ,
role_id ,
city_id ,
phone ,
breif ,
featured ,
active ,
admin ,
created_at ,
deleted_at
)
SELECT
u.name ,
u.name_ar ,
u.email ,
u.img ,
u.password ,
u.serial ,
u.points ,
(SELECT id FROM roles WHERE name = r.name LIMIT 1) ,
(SELECT id FROM cities WHERE name = c.name LIMIT 1) ,
u.phone ,
u.breif ,
u.featured ,
u.active ,
u.admin ,
u.created_at ,
u.deleted_at
FROM alshab_staging.users u
JOIN alshab_staging.roles r ON u.role_id = r.id
LEFT JOIN alshab_staging.cities c ON u.city_id = c.id;
INSERT INTO alshab_staging2.user_subs(
user_id,
role_id ,
price,
method,
points,
created_at,
start_at,
end_at )
SELECT
(SELECT id FROM alshab_staging2.users WHERE phone = u.phone),
(SELECT id FROM roles WHERE name = r.name LIMIT 1) ,
us.price,
us.method,
us.points,
us.start_at,
us.start_at,
us.end_at
FROM alshab_staging.user_subs us
JOIN alshab_staging.users u
ON us.user_id = u.id
JOIN alshab_staging.roles r ON us.role_id = r.id;
INSERT INTO alshab_staging2.user_events(
user_id,
event_id,
price,
method,
points
)
SELECT
(SELECT id FROM alshab_staging2.users WHERE phone = u.phone),
(SELECT id FROM events WHERE title = e.title LIMIT 1) ,
ue.price,
ue.method,
ue.points
FROM alshab_staging.user_events ue
JOIN alshab_staging.users u
ON ue.user_id = u.id
JOIN alshab_staging.events e
ON ue.event_id = e.id;
INSERT INTO alshab_staging2.user_services(
user_id,
breif,
service_id,
seen_at,
created_at
)
SELECT
(SELECT id FROM alshab_staging2.users WHERE phone = u.phone),
us.breif,
(SELECT id FROM alshab_staging2.services WHERE name = s.name LIMIT 1),
us.seen_at,
us.created_at
FROM alshab_staging.user_services us
JOIN alshab_staging.users u
ON us.user_id = u.id
JOIN alshab_staging.services s
ON us.service_id = s.id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MergeVideos` () BEGIN
INSERT INTO alshab_staging2.videos(name , url , image , Breif , category_id) SELECT v.name , v.url , v.image , v.Breif , (SELECT id FROM alshab_staging2.categories WHERE name = c.name LIMIT 1) FROM alshab_staging.videos v JOIN alshab_staging.categories c ON v.category_id = c.id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MsgsCreate` (`Ifrom_id` INT, `Ito_id` INT, `Ibreif` TEXT) BEGIN
INSERT INTO msgs (from_id , to_id , breif) VALUES (Ifrom_id , Ito_id , Ibreif);
SELECT LAST_INSERT_ID() id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MsgsList` (IN `Iuser_id` INT) BEGIN
SELECT id , name_ar , img FROM users WHERE id != Iuser_id;
SELECT DISTINCT anotherUser.id ,anotherUser.name , anotherUser.img
FROM users currentUser
JOIN msgs m
ON m.from_id = currentUser.id
JOIN users anotherUser
ON m.to_id = anotherUser.id
WHERE currentUser.id = Iuser_id
UNION
SELECT DISTINCT anotherUser.id ,anotherUser.name , anotherUser.img
FROM users currentUser
JOIN msgs m
ON m.to_id = currentUser.id
JOIN users anotherUser
ON m.from_id = anotherUser.id
WHERE currentUser.id = Iuser_id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `MsgsListByUser` (`id1` INT, `id2` INT) BEGIN
SELECT m.id ,IF(m.from_id = id1 , TRUE , FALSE) mine, u.name_ar name ,m.breif , m.created_at , IFNULL(m.seen , '') seen
FROM msgs m
JOIN users u ON m.from_id = u.id
WHERE (m.from_id = id1 AND m.to_id = id2) OR (m.from_id = id2 AND m.to_id = id1)
ORDER BY m.created_at ASC, id ASC;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `NotificationCreate` (IN `Ititle` VARCHAR(250), IN `Ibreif` TEXT, IN `Iurl` VARCHAR(250)) BEGIN
DECLARE x INT;
DECLARE max INT;
DECLARE u_id INT;
DECLARE n_id INT;
INSERT INTO notifications (
title,
breif,
link
) VALUES (
Ititle,
Ibreif,
Iurl
);
SET n_id = (SELECT LAST_INSERT_ID());
SET x = 0;
SET max = (SELECT (COUNT(*)) FROM users WHERE users.admin = 1);
loop_label: LOOP
IF x > max THEN
LEAVE loop_label;
END IF;
SET u_id = (SELECT id FROM users WHERE admin = 1 LIMIT 1);
INSERT INTO user_notifications (
user_id,
notification_id
) VALUES (
u_id,
n_id
);
SET x = x + 1;
END LOOP;
SELECT n_id ;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `NotificationsByUserId` (`IId` INT) BEGIN
SELECT Title , Breif , Link FROM notifications n
JOIN user_notifications un
ON n.id = un.notification_id
WHERE un.user_id = IId;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ProjectDelete` (IN `Iid` INT) BEGIN
UPDATE projects SET deleted_at = now() WHERE id = Iid;
SELECT Iid id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ProjectPending` (`Istatus` VARCHAR(100), `Iname_ar` VARCHAR(200), `Ititle` VARCHAR(200), `Iphone` VARCHAR(200), `Iemail` VARCHAR(200), `dateFrom` VARCHAR(200), `dateTo` VARCHAR(200)) BEGIN
SELECT p.id, u.id user_id ,u.name_ar , u.email , p.title , p.phone , p.status ,p.created_at
FROM projects p JOIN users u ON p.user_id = u.id WHERE
p.active = 0 AND p.deleted_at IS NULL
AND (CASE WHEN Istatus = "" THEN '1' ELSE p.status = Istatus END)
AND (CASE WHEN Iname_ar = "" THEN '1' ELSE u.name_ar LIKE CONCAT('%' , Iname_ar , '%') END)
AND (CASE WHEN Ititle = "" THEN '1' ELSE p.title LIKE CONCAT('%' , Ititle , '%') END)
AND (CASE WHEN Iphone = "" THEN '1' ELSE u.phone LIKE CONCAT('%' , Iphone , '%') END)
AND (CASE WHEN Iemail = "" THEN '1' ELSE u.email LIKE CONCAT('%' , Iemail , '%') END)
AND (CASE WHEN dateFrom = "" THEN '1' ELSE p.created_at >= dateFrom END)
AND (CASE WHEN dateTo = "" THEN '1' ELSE p.created_at <= dateTo END);
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ProjectPendingAction` (`Iid` INT, `action` VARCHAR(100)) BEGIN
UPDATE projects SET
active = CASE WHEN action = "approved" THEN 1 ELSE 0 END,
status = action
WHERE id = Iid;
SELECT Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ProjectRead` (IN `Iid` INT) BEGIN
SELECT
u.name userName,
ca.name categoryName,
ca.id categoryId,
ci.name city,
ci.id cityId,
p.title,
p.img,
p.logo,
p.fund,
p.status,
p.breif,
IFNULL(p.imgs , '') imgs,
p.location,
p.phone,
IFNULL(p.file , '') file,
p.email,
p.featured,
IFNULL(p.website , '') website,
IFNULL(p.instagram , '') instagram,
IFNULL(p.twitter , '') twitter
FROM projects p
JOIN users u ON u.id = p.user_id
JOIN cities ci ON ci.id = p.city_id
JOIN categories ca ON ca.id = p.category_id
WHERE p.id = Iid;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ProjectsCreate` (IN `Iuser_id` INT, IN `Icategory_id` INT, IN `Icity_id` INT, IN `Ititle` VARCHAR(250), IN `Istatus` VARCHAR(100), IN `Iimg` VARCHAR(250), IN `Iimgs` VARCHAR(250), IN `Ilogo` VARCHAR(250), IN `Ifund` FLOAT, IN `Ibreif` TEXT, IN `Ilocation` TEXT, IN `Iphone` VARCHAR(250), IN `Ifile` VARCHAR(250), IN `Iemail` VARCHAR(250), IN `Iwebsite` VARCHAR(250), IN `Iinstagram` VARCHAR(250), IN `Itwitter` VARCHAR(250), IN `Ifeatured` VARCHAR(250), IN `Iactive` VARCHAR(250)) BEGIN
INSERT INTO
projects (
user_id,
category_id,
city_id,
title,
status,
img,
imgs,
logo,
fund,
breif,
location,
phone,
file,
email,
website,
instagram,
twitter,
featured,
active
)
VALUES
(
Iuser_id,
Icategory_id,
Icity_id,
Ititle,
Istatus,
Iimg,
Iimgs,
Ilogo,
Ifund,
Ibreif,
Ilocation,
Iphone,
Ifile,
Iemail,
Iwebsite,
Iinstagram,
Itwitter,
Ifeatured,
Iactive
);
SELECT LAST_INSERT_ID() id;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ProjectsListByCategoryUserSearch` (IN `ICategory` INT, IN `ICity` INT, IN `Iuser` INT, IN `search` VARCHAR(200), IN `userName` VARCHAR(200), IN `Istatus` VARCHAR(200)) BEGIN
SELECT
p.id,
p.title,
u.name user_name,
c.name category_name,
ci.name city_name,
p.status,
p.logo,
p.img
FROM projects p
JOIN users u ON p.user_id = u.id
JOIN categories c ON p.category_id = c.id
JOIN cities ci ON p.city_id = ci.id
WHERE p.active = 1
AND p.user_id = CASE WHEN Iuser = 0 THEN p.user_id ELSE Iuser END
AND p.category_id = CASE WHEN ICategory = 0 THEN p.category_id ELSE ICategory END
AND p.city_id = CASE WHEN ICity = 0 THEN p.city_id ELSE ICity END
AND CASE WHEN search = '' THEN 1 = 1 ELSE p.title LIKE CONCAT('%' , search , '%') END
AND CASE WHEN userName = '' THEN 1 = 1 ELSE u.name LIKE CONCAT('%' , userName , '%') END
AND CASE WHEN Istatus = '' THEN 1 = 1 ELSE p.status = Istatus END;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ProjectsListFeatured` () BEGIN
SELECT
p.id,
p.title,
p.logo,
p.img
FROM projects p
WHERE featured = 1 ORDER BY RAND() LIMIT 4;
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `ProjectUpdate` (IN `Iid` INT, IN `Icategory_id` INT, IN `Icity_id` INT, IN `Ititle` VARCHAR(250), IN `Istatus` VARCHAR(100), IN `Iimg` VARCHAR(250), IN `Iimgs` VARCHAR(250), IN `Ilogo` VARCHAR(250), IN `Ifund` FLOAT, IN `Ibreif` TEXT, IN `Ilocation` TEXT, IN `Iphone` VARCHAR(250), IN `Ifile` VARCHAR(250), IN `Iemail` VARCHAR(250), IN `Iwebsite` VARCHAR(250), IN `Iinstagram` VARCHAR(250), IN `Itwitter` VARCHAR(250), IN `Ifeatured` VARCHAR(250), IN `Iactive` VARCHAR(250)) BEGIN
UPDATE
projects
SET
category_id = Icategory_id,
city_id = Icity_id,
title = Ititle,
status = Istatus,
img = CASE WHEN Iimg = "" THEN img ELSE Iimg END ,
imgs = CASE WHEN Iimgs = "" THEN imgs ELSE Iimgs END ,
logo = CASE WHEN Ilogo = "" THEN logo ELSE Ilogo END ,
fund = Ifund,
breif = Ibreif,
location = Ilocation,
phone = Iphone,
file = CASE WHEN Ifile = "" THEN file ELSE Ifile END ,
email = Iemail,
website = Iwebsite,
instagram = Iinstagram,
twitter = Itwitter,
featured = Ifeatured,
active = Iactive
WHERE id = Iid;
SELECT LAST_INSERT_ID();
END$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `Register` (IN `IName` VARCHAR(250), IN `IName_ar` VARCHAR(250), IN `IEmail` VARCHAR(250), IN `IPassword` VARCHAR(250), IN `IPhone` VARCHAR(250), IN `IRole` INT, IN `IAdmin` BOOLEAN) BEGIN
SELECT MAX(`serial`) FROM users INTO @maxSerial;