From 92170c34d506a8d9e2b236eafacd225783c11080 Mon Sep 17 00:00:00 2001 From: Joan Sanchez Date: Tue, 12 Mar 2019 15:04:09 +0100 Subject: [PATCH 1/2] worker department #1133 --- db/install/changes/0-ACL.sql | 4 +- db/install/changes/12-department.sql | 21 +++++ db/install/changes/13-nodeAdd.sql | 83 ++++++++++++++++++ db/install/changes/14-nodeRecalc.sql | 28 ++++++ db/install/changes/15-zoneGeo.sql | 4 + front/core/components/treeview/child.html | 51 ++++++++--- front/core/components/treeview/child.js | 23 ++++- front/core/components/treeview/index.html | 10 ++- front/core/components/treeview/index.js | 24 +++-- front/core/components/treeview/style.scss | 6 +- front/core/directives/acl.js | 10 ++- front/core/locale/es.yml | 4 +- loopback/common/models/vn-model.js | 63 ++++++-------- loopback/locale/en.json | 3 +- loopback/locale/es.json | 3 +- .../agency/back/methods/zone-geo/getLeaves.js | 20 +++-- modules/agency/front/location/index.html | 2 +- .../back/methods/department/getLeaves.js | 87 +++++++++++++++++++ .../worker/back/methods/department/nodeAdd.js | 43 +++++++++ .../back/methods/department/nodeDelete.js | 29 +++++++ modules/worker/back/models/department.js | 19 ++++ modules/worker/front/department/index.html | 45 ++++++++++ modules/worker/front/department/index.js | 78 +++++++++++++++++ modules/worker/front/department/locale/es.yml | 3 + modules/worker/front/index.js | 1 + modules/worker/front/index/index.html | 22 +++-- modules/worker/front/index/index.js | 13 ++- modules/worker/front/index/style.scss | 11 +++ modules/worker/front/locale/es.yml | 1 + modules/worker/front/routes.json | 9 +- 30 files changed, 639 insertions(+), 81 deletions(-) create mode 100644 db/install/changes/12-department.sql create mode 100644 db/install/changes/13-nodeAdd.sql create mode 100644 db/install/changes/14-nodeRecalc.sql create mode 100644 db/install/changes/15-zoneGeo.sql create mode 100644 modules/worker/back/methods/department/getLeaves.js create mode 100644 modules/worker/back/methods/department/nodeAdd.js create mode 100644 modules/worker/back/methods/department/nodeDelete.js create mode 100644 modules/worker/back/models/department.js create mode 100644 modules/worker/front/department/index.html create mode 100644 modules/worker/front/department/index.js create mode 100644 modules/worker/front/department/locale/es.yml create mode 100644 modules/worker/front/index/style.scss diff --git a/db/install/changes/0-ACL.sql b/db/install/changes/0-ACL.sql index 099e3c5a9..a2f3991f1 100644 --- a/db/install/changes/0-ACL.sql +++ b/db/install/changes/0-ACL.sql @@ -1,2 +1,4 @@ INSERT INTO `salix`.`ACL` (`id`,`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES (149, 'Sip', '*', 'WRITE', 'ALLOW', 'ROLE', 'hr'); -INSERT INTO `salix`.`ACL` (`id`,`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES (150, 'Sip', '*', 'READ', 'ALLOW', 'ROLE', 'employee'); \ No newline at end of file +INSERT INTO `salix`.`ACL` (`id`,`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES (150, 'Sip', '*', 'READ', 'ALLOW', 'ROLE', 'employee'); +INSERT INTO `salix`.`ACL` (`id`,`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES (151, 'Department','*','READ','ALLOW','ROLE','employee'); +INSERT INTO `salix`.`ACL` (`id`,`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) VALUES (152, 'Department','*','WRITE','ALLOW','ROLE','hr'); diff --git a/db/install/changes/12-department.sql b/db/install/changes/12-department.sql new file mode 100644 index 000000000..38c68a45c --- /dev/null +++ b/db/install/changes/12-department.sql @@ -0,0 +1,21 @@ +USE `vn2008`; + +ALTER TABLE vn2008.department ADD `depth` int DEFAULT 0 NOT NULL; +ALTER TABLE vn2008.department ADD sons int DEFAULT 0 NOT NULL; + +USE `vn`; + +CREATE +OR REPLACE +VIEW `vn`.`department` AS select + `b`.`department_id` AS `id`, + `b`.`name` AS `name`, + `b`.`father_id` AS `fatherFk`, + `b`.`production` AS `isProduction`, + `b`.`lft` AS `lft`, + `b`.`rgt` AS `rgt`, + `b`.`isSelected` AS `isSelected`, + `b`.`depth` AS `depth`, + `b`.`sons` AS `sons` +from + `vn2008`.`department` `b`; \ No newline at end of file diff --git a/db/install/changes/13-nodeAdd.sql b/db/install/changes/13-nodeAdd.sql new file mode 100644 index 000000000..b3c537548 --- /dev/null +++ b/db/install/changes/13-nodeAdd.sql @@ -0,0 +1,83 @@ +DROP PROCEDURE IF EXISTS nst.NodeAdd; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`%` PROCEDURE `nst`.`nodeAdd`(IN `vScheme` VARCHAR(45), IN `vTable` VARCHAR(45), IN `vParentFk` INT, IN `vChild` VARCHAR(100)) +BEGIN + DECLARE vSql TEXT; + DECLARE vTableClone VARCHAR(45); + + SET vTableClone = CONCAT(vTable, 'Clone'); + + CALL util.exec(CONCAT('DROP TEMPORARY TABLE IF EXISTS tmp.', vTableClone)); + CALL util.exec(CONCAT( + 'CREATE TEMPORARY TABLE tmp.', vTableClone, + ' ENGINE = MEMORY' + ' SELECT * FROM ', vScheme, '.', vTable + )); + + -- Check parent childs + SET vSql = sql_printf(' + SELECT COUNT(c.id) INTO @childs + FROM %t.%t p + LEFT JOIN %t.%t c ON c.lft BETWEEN p.lft AND p.rgt AND c.id != %v + WHERE p.id = %v', + vScheme, vTable, 'tmp', vTableClone, vParentFk, vParentFk); + SET @qrySql := vSql; + + PREPARE stmt FROM @qrySql; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; + + -- Select left from last child + IF @childs = 0 THEN + SET vSql = sql_printf('SELECT lft INTO @vLeft FROM %t.%t WHERE id = %v', vScheme, vTable, vParentFk); + SET @qrySql := vSql; + ELSE + SET vSql = sql_printf(' + SELECT c.rgt INTO @vLeft + FROM %t.%t p + JOIN %t.%t c ON c.lft BETWEEN p.lft AND p.rgt + WHERE p.id = %v + ORDER BY c.lft + DESC LIMIT 1', + vScheme, vTable, 'tmp', vTableClone, vParentFk); + SET @qrySql := vSql; + END IF; + + PREPARE stmt FROM @qrySql; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; + + -- Update right + SET vSql = sql_printf('UPDATE %t.%t SET rgt = rgt + 2 WHERE rgt > %v ORDER BY rgt DESC', vScheme, vTable, @vLeft); + SET @qrySql := vSql; + + PREPARE stmt FROM @qrySql; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; + + SET vSql = sql_printf('UPDATE %t.%t SET lft = lft + 2 WHERE lft > %v ORDER BY lft DESC', vScheme, vTable, @vLeft); + SET @qrySql := vSql; + + PREPARE stmt FROM @qrySql; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; + + -- Escape character + SET vChild = REPLACE(vChild, "'", "\\'"); + + -- Add child + SET vSql = sql_printf('INSERT INTO %t.%t (name, lft, rgt) VALUES (%v, %v, %v)', vScheme, vTable, vChild, @vLeft + 1, @vLeft + 2); + SET @qrySql := vSql; + + PREPARE stmt FROM @qrySql; + EXECUTE stmt; + DEALLOCATE PREPARE stmt; + + SELECT id, name, lft, rgt, depth, sons FROM vn.department + WHERE id = LAST_INSERT_ID(); + + CALL util.exec(CONCAT('DROP TEMPORARY TABLE tmp.', vTableClone)); +END$$ +DELIMITER ; diff --git a/db/install/changes/14-nodeRecalc.sql b/db/install/changes/14-nodeRecalc.sql new file mode 100644 index 000000000..a8f4d932c --- /dev/null +++ b/db/install/changes/14-nodeRecalc.sql @@ -0,0 +1,28 @@ +DROP PROCEDURE IF EXISTS nst.nodeRecalc; + +DELIMITER $$ +$$ +CREATE DEFINER=`root`@`%` PROCEDURE `nst`.`nodeRecalc`(IN `vScheme` VARCHAR(45), IN `vTable` VARCHAR(45)) +BEGIN + CALL util.exec (sql_printf ( + 'UPDATE %t.%t d + JOIN (SELECT + node.id, + COUNT(parent.id) - 1 as depth, + cast((node.rgt - node.lft - 1) / 2 as DECIMAL) as sons + FROM + %t.%t AS node, + %t.%t AS parent + WHERE node.lft BETWEEN parent.lft AND parent.rgt + GROUP BY node.id + ORDER BY node.lft) n ON n.id = d.id + SET d.`depth` = n.depth, d.sons = n.sons', + vScheme, + vTable, + vScheme, + vTable, + vScheme, + vTable + )); +END$$ +DELIMITER ; diff --git a/db/install/changes/15-zoneGeo.sql b/db/install/changes/15-zoneGeo.sql new file mode 100644 index 000000000..1037fe089 --- /dev/null +++ b/db/install/changes/15-zoneGeo.sql @@ -0,0 +1,4 @@ +USE `vn`; + +CREATE UNIQUE INDEX zoneGeo_lft_IDX USING BTREE ON vn.zoneGeo (lft); +CREATE UNIQUE INDEX zoneGeo_rgt_IDX USING BTREE ON vn.zoneGeo (rgt); \ No newline at end of file diff --git a/front/core/components/treeview/child.html b/front/core/components/treeview/child.html index 8589790ec..a079d8f40 100644 --- a/front/core/components/treeview/child.html +++ b/front/core/components/treeview/child.html @@ -1,25 +1,54 @@ -