-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023-03-17 18_16_36.sql
189 lines (142 loc) · 4 KB
/
2023-03-17 18_16_36.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
/**
* Generado por Database Modeler Pro
* https://play.google.com/store/apps/details?id=adrian.adbm.pro
*
* Creado: 17 mar 2023
*/
/*
DROP TABLE IF EXISTS `ticket`;
DROP TABLE IF EXISTS `modulo_atencion`;
*/
create database tickets;
use tickets;
CREATE TABLE `modulo_atencion` (
`id_modulo` INT (10) NOT NULL AUTO_INCREMENT,
`tipo_modulo` VARCHAR (30) NOT NULL,
PRIMARY KEY (`id_modulo`)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8;
INSERT INTO `modulo_atencion` (`tipo_modulo`) VALUES
('Caja'),
('Caja'),
('Ejecutivo'),
('Caja'),
('Ejecutivo'),
('Caja'),
('Ejecutivo'),
('Ejecutivo'),
('Caja'),
('Ejecutivo');
drop table ticket;
CREATE TABLE `ticket` (
`id_ticket` INT (10) NOT NULL AUTO_INCREMENT,
`id_modulo` INT (10),
`descripcion` VARCHAR (100) NOT NULL,
`timestamp` DATETIME NOT NULL,
`status` TINYINT (1) NOT NULL,
`es_cliente` TINYINT (1) NOT NULL,
PRIMARY KEY (`id_ticket`)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8;
select * from ticket where status = 0;
ALTER TABLE `ticket`
ADD FOREIGN KEY modulo_atencion_ticket_fk (`id_modulo`) REFERENCES `modulo_atencion`(`id_modulo`);
drop procedure insertar_ticket;
DELIMITER //
CREATE PROCEDURE `insertar_ticket` (
IN p_id_modulo INT(10),
IN p_descripcion VARCHAR(100),
IN p_status TINYINT(1),
IN p_cliente TINYINT(1)
)
BEGIN
INSERT INTO ticket (`id_modulo`, `descripcion`, `timestamp`, `status`, `es_cliente`)
VALUES (p_id_modulo, CONCAT(p_descripcion,'#', LAST_INSERT_ID()), NOW(), p_status, p_cliente);
END
//
DELIMITER ;
describe ticket;
DELIMITER //
CREATE PROCEDURE `actualizar_ticket` (
IN p_id_ticket INT(10),
IN p_id_modulo INT(10),
IN p_status TINYINT(1)
)
BEGIN
UPDATE ticket
SET id_modulo = p_id_modulo, status = p_status
WHERE id_ticket = p_id_ticket;
END //
DELIMITER ;
select * from ticket;
drop procedure actualizar_ticket;
use ticket;
DELIMITER //
CREATE PROCEDURE actualizar_tickets(
IN p_id_modulo INT(10)
)
BEGIN
DECLARE contador INT DEFAULT 0;
DECLARE cliente INT DEFAULT 0;
DECLARE id INT DEFAULT 0;
DECLARE registro CURSOR FOR SELECT id_ticket, es_cliente FROM ticket WHERE status = 0 ORDER BY id_ticket ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET contador = -1;
OPEN registro;
read_loop: LOOP
FETCH registro INTO id, cliente;
IF contador = -1 THEN
LEAVE read_loop;
END IF;
IF cliente = 1 THEN
IF contador % 2 = 0 THEN
UPDATE ticket SET status = 1, id_modulo = p_id_modulo WHERE id_ticket = id;
END IF;
SET contador = contador + 1;
ELSE
UPDATE ticket SET status = 1, id_modulo = p_id_modulo WHERE id_ticket = id;
END IF;
END LOOP;
IF contador = 0 THEN
UPDATE ticket SET status = 1 WHERE status = 0;
ELSEIF contador > 0 AND contador % 2 = 1 THEN
UPDATE ticket SET status = 1 , id_modulo = p_id_modulo WHERE es_cliente = 1 AND status = 0 ORDER BY id_ticket ASC LIMIT 1;
END IF;
CLOSE registro;
END //
DELIMITER ;
CALL actualizar_tickets();
use tickets;
select * from ticket;
DELETE FROM ticket WHERE id_ticket > 0;
select * from modulo_atencion;
DELIMITER //
CREATE PROCEDURE `actualizar_ticket` (IN id_modulo_param INT)
BEGIN
UPDATE ticket SET id_modulo = id_modulo + id_modulo_param, status = 1
WHERE status = 0 AND es_cliente = 1
ORDER BY id_ticket ASC
LIMIT 1;
IF ROW_COUNT() = 0 THEN
UPDATE ticket SET id_modulo = id_modulo + id_modulo_param, status = 1
WHERE status = 0
ORDER BY id_ticket ASC
LIMIT 1;
END IF;
END;
//
DELIMITER ;
DELIMITER //
CREATE PROCEDURE actualizar_ticket(IN modulo INT)
BEGIN
DECLARE ticket_id INT;
DECLARE ticket_cliente TINYINT;
SET ticket_id = NULL;
SET ticket_cliente = NULL;
SELECT id_ticket, es_cliente INTO ticket_id, ticket_cliente FROM ticket WHERE status = 0 ORDER BY es_cliente DESC, RAND() < 0.35 DESC, id_ticket ASC LIMIT 1;
IF ticket_id IS NOT NULL THEN
UPDATE ticket SET id_modulo = modulo, status= 1 WHERE id_ticket = ticket_id;
END IF;
END //
DELIMITER ;