refs #5858 perf: update db/changes
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Javier Segarra 2023-12-19 07:54:49 +01:00
parent 5b6112720f
commit a31b689ae2
2 changed files with 99 additions and 51 deletions

View File

@ -1,60 +1,107 @@
DROP PROCEDURE IF EXISTS vn.zoneIncluded_checkCollisions;
DELIMITER $$
$$
CREATE DEFINER=`root`@`localhost` PROCEDURE `vn`.`zoneIncluded_checkCollisions`()
BEGIN
DECLARE zic_id, zic_zone, zic_geoFk, zic_action, zic_zoneFk, zic_userFk INT;
DECLARE zic2_geoFk, zic2_zoneFk INT;
DECLARE z_name VARCHAR(255);
DECLARE g_name VARCHAR(255);
DECLARE vDone boolean;
/**
* Calcula si para un mismo codigo postal y dia
* hay mas de una zona configurada y manda correo
*
*/
DECLARE vGeoFk INT DEFAULT NULL;
DECLARE vZoneFk INT;
DECLARE vIsDone INT DEFAULT FALSE;
DECLARE vTableCollisions TEXT;
DECLARE json_data JSON;
DECLARE cur1 CURSOR FOR SELECT zoneFk from tmp.zoneOption;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vIsDone = TRUE;
DECLARE cur CURSOR FOR
SELECT zic.id, z.name , zg.name ,zic.zoneFK, zic.geoFk ,zic.`action` , zic.userFK
FROM vn.zoneIncludedCheck zic, vn.`zone` z, vn.zoneGeo zg
WHERE zic.zoneFK =z.id AND zic.geoFK = zg.id and zic.action ='insert';
DROP TEMPORARY TABLE IF EXISTS tmp.zone;
CREATE TEMPORARY TABLE tmp.zone
SELECT z.id
FROM zone z
JOIN agencyMode am ON am.id = z.agencyModeFk
JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk
WHERE dm.code IN ('AGENCY','DELIVERY');
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone = TRUE;
SET vDone := FALSE;
OPEN cur;
LOOP1: LOOP
SET vDone := FALSE;
FETCH cur INTO zic_id, z_name, g_name, zic_zoneFk, zic_geoFk, zic_action, zic_userFk ;
IF vDone THEN
select concat('NO RECORDS LOOP 1' ) AS '** DEBUG:';
CLOSE cur;
LEAVE LOOP1;
END IF;
-- SELECT concat('** ', zic_id, '** ',z_name, '** ',g_name,'**',zic_zoneFk,'**',zic_geoFk,'** ', zic_action,'** ', zic_userFk ) AS '** DEBUG:';
DELETE FROM vn.zoneIncludedCheck WHERE id =zic_id;
CALL zone_getOptionsForShipment(util.VN_CURDATE(),FALSE);
BLOCK2: BEGIN
DECLARE vDone2 boolean;
DECLARE cur2 CURSOR FOR
SELECT zi.zoneFk,zi.geoFk
FROM vn.zoneIncluded zi, vn.zone z
where z.id = zi.zoneFk and zi.zoneFk=zic_zoneFk and zi.geoFk=zic_geoFk and zi.isIncluded = 1;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET vDone2 = TRUE;
SET vDone2 := FALSE;
OPEN cur2;
LOOP2: LOOP
FETCH cur2 INTO zic2_zoneFk, zic2_geoFk ;
IF vDone2 THEN
select concat('NO RECORDS LOOP 2' ) AS '** DEBUG:';
CLOSE cur2;
LEAVE LOOP2;
END IF;
SELECT concat('COLLISION DETECTED', zic2_zoneFk, zic2_geoFk ) AS '** DEBUG:';
DROP TEMPORARY TABLE IF EXISTS tmp.zoneNodes;
CREATE TEMPORARY TABLE tmp.zoneNodes (
geoFk INT,
name VARCHAR(100),
parentFk INT,
sons INT,
isChecked BOOL DEFAULT 0,
zoneFk INT,
PRIMARY KEY zoneFkk (zoneFk, geoFk),
INDEX(geoFk))
ENGINE = MyISAM;
SELECT util.notification_send('zone-included',
JSON_OBJECT('zoneSelected', z_name, 'geoSelected', g_name),
account.myUser_getId()
);
END LOOP LOOP2;
END BLOCK2;
END LOOP LOOP1;
END
$$
OPEN cur1;
cur1Loop: LOOP
SET vIsDone = FALSE;
FETCH cur1 INTO vZoneFk;
IF vIsDone THEN
LEAVE cur1Loop;
END IF;
CALL zone_getLeaves(vZoneFk, NULL, NULL, TRUE);
myLoop: LOOP
SET vGeoFk = NULL;
SELECT geoFk INTO vGeoFk
FROM tmp.zoneNodes zn
WHERE NOT isChecked
LIMIT 1;
IF vGeoFk IS NULL THEN
LEAVE myLoop;
END IF;
CALL zone_getLeaves(vZoneFk, vGeoFk, NULL, TRUE);
UPDATE tmp.zoneNodes
SET isChecked = TRUE
WHERE geoFk = vGeoFk;
END LOOP;
END LOOP;
CLOSE cur1;
DELETE FROM tmp.zoneNodes
WHERE sons > 0;
DROP TEMPORARY TABLE IF EXISTS geoCollision;
CREATE TEMPORARY TABLE geoCollision
SELECT z.agencyModeFk, zn.geoFk, zw.warehouseFk
FROM tmp.zoneNodes zn
JOIN zone z ON z.id = zn.zoneFk
JOIN zoneWarehouse zw ON z.id = zw.zoneFk
GROUP BY z.agencyModeFk, zn.geoFk, zw.warehouseFk
HAVING count(*) > 1;
SELECT JSON_ARRAYAGG(
JSON_OBJECT(
'zoneFk', zoneFk,
'zn', JSON_OBJECT('name', zn.name),
'z', JSON_OBJECT('name', z.name,'price', z.price),
'w', JSON_OBJECT('name', w.name)
)
) FROM tmp.zoneNodes zn
JOIN zone z ON z.id = zn.zoneFk
JOIN geoCollision gc ON gc.agencyModeFk = z.agencyModeFk AND zn.geoFk = gc.geoFk
JOIN warehouse w ON w.id = gc.warehouseFk
INTO json_data;
SELECT util.notification_send(
'zone-included',
JSON_OBJECT('zoneCollisions',json_data),
account.myUser_getId()
);
DROP TEMPORARY TABLE
geoCollision,
tmp.zone,
tmp.zoneNodes;
END ;;
DELIMITER ;

View File

@ -1,5 +1,6 @@
INSERT INTO util.notification (name,description)
VALUES ('zone-included','An email to notify zoneCollisions');
INSERT INTO util.notification (id, name,description)
VALUES (
(SELECT MAX(id)+1 from util.notification n) , 'zone-included','An email to notify zoneCollisions');
INSERT INTO util.notificationSubscription (notificationFk,userFk)
SELECT id, account.myUser_getId() FROM util.notification WHERE name= "zone-included";