zoneClosure
gitea/salix/master This commit looks good Details

This commit is contained in:
Javi Gallego 2020-01-22 10:41:09 +01:00
parent b2e0a41fe6
commit 5bd31eb082
9 changed files with 144 additions and 1 deletions

View File

@ -0,0 +1,5 @@
CREATE TABLE `vn`.`zoneClosure` (
`zoneFk` INT NOT NULL,
`dated` DATE NOT NULL,
`hour` TIME NOT NULL,
PRIMARY KEY (`zoneFk`, `dated`));

View File

@ -0,0 +1,50 @@
DROP procedure IF EXISTS vn.`zoneClosure_recalc`;
DELIMITER $$
CREATE DEFINER=`root`@`%` PROCEDURE vn.`zoneClosure_recalc`()
proc: BEGIN
/**
* Recalculates the delivery time (hour) for every zone in days + scope in future
*/
DECLARE vScope INT;
DECLARE vCounter INT DEFAULT 0;
DECLARE vShipped DATE DEFAULT CURDATE();
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
DO RELEASE_LOCK('vn.zoneClosure_recalc');
RESIGNAL;
END;
IF NOT GET_LOCK('vn.zoneClosure_recalc', 0) THEN
LEAVE proc;
END IF;
SELECT scope INTO vScope
FROM zoneConfig;
DROP TEMPORARY TABLE IF EXISTS tmp.zone;
CREATE TEMPORARY TABLE tmp.zone
(INDEX (id))
ENGINE = MEMORY
SELECT id FROM zone;
TRUNCATE TABLE zoneClosure;
REPEAT
CALL zone_getOptionsForShipment(vShipped);
INSERT INTO zoneClosure(zoneFk, dated, `hour`)
SELECT zoneFk, vShipped, `hour` FROM tmp.zoneOption;
SET vCounter = vCounter + 1;
SET vShipped = TIMESTAMPADD(DAY, 1, vShipped);
UNTIL vCounter > vScope
END REPEAT;
DROP TEMPORARY TABLE tmp.zone;
DO RELEASE_LOCK('vn.zoneClosure_recalc');
END$$
DELIMITER ;

View File

@ -0,0 +1,11 @@
CREATE TABLE `vn`.`zoneConfig` (
`id` INT UNSIGNED NOT NULL,
`scope` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`));
ALTER TABLE `vn`.`zoneConfig`
CHANGE COLUMN `id` `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT ;
INSERT INTO `vn`.`zoneConfig` (`scope`) VALUES ('1');
INSERT INTO `bs`.`nightTask` (`order`, `schema`, `procedure`) VALUES ('100', 'vn', 'zoneClosure_recalc');

View File

@ -11,7 +11,7 @@
"Zone": { "Zone": {
"dataSource": "vn" "dataSource": "vn"
}, },
"ZoneGeo": { "ZoneClosure": {
"dataSource": "vn" "dataSource": "vn"
}, },
"ZoneEvent": { "ZoneEvent": {
@ -20,6 +20,9 @@
"ZoneExclusion": { "ZoneExclusion": {
"dataSource": "vn" "dataSource": "vn"
}, },
"ZoneGeo": {
"dataSource": "vn"
},
"ZoneIncluded": { "ZoneIncluded": {
"dataSource": "vn" "dataSource": "vn"
}, },

View File

@ -0,0 +1,13 @@
module.exports = Self => {
Self.doRecalc = async function() {
try {
await Self.rawSql(`
CREATE EVENT zoneClosure_doRecalc
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 15 SECOND
DO CALL zoneClosure_recalc;
`);
} catch (err) {
if (err.code != 'ER_EVENT_ALREADY_EXISTS') throw err;
}
};
};

View File

@ -0,0 +1,30 @@
{
"name": "ZoneClosure",
"base": "VnModel",
"options": {
"mysql": {
"table": "zoneClosure"
}
},
"properties": {
"zoneFk": {
"id": true,
"type": "Number"
},
"dated": {
"type": "Date",
"required": true
},
"hour": {
"type": "date",
"required": true
}
},
"relations": {
"zone": {
"type": "belongsTo",
"model": "Zone",
"foreignKey": "zoneFk"
}
}
}

View File

@ -1,3 +1,5 @@
const app = require('vn-loopback/server/server');
module.exports = Self => { module.exports = Self => {
Self.validate('range', function(err) { Self.validate('range', function(err) {
if (this.type == 'range' if (this.type == 'range'
@ -32,4 +34,12 @@ module.exports = Self => {
}, { }, {
message: `You should mark at least one week day` message: `You should mark at least one week day`
}); });
Self.observe('after save', async function() {
await app.models.ZoneClosure.doRecalc();
});
Self.observe('after delete', async function() {
await app.models.ZoneClosure.doRecalc();
});
}; };

View File

@ -0,0 +1,11 @@
const app = require('vn-loopback/server/server');
module.exports = Self => {
Self.observe('after save', async function() {
await app.models.ZoneClosure.doRecalc();
});
Self.observe('after delete', async function() {
await app.models.ZoneClosure.doRecalc();
});
};

View File

@ -1,3 +1,5 @@
const app = require('vn-loopback/server/server');
module.exports = Self => { module.exports = Self => {
require('../methods/zone/clone')(Self); require('../methods/zone/clone')(Self);
require('../methods/zone/getLeaves')(Self); require('../methods/zone/getLeaves')(Self);
@ -7,4 +9,12 @@ module.exports = Self => {
Self.validatesPresenceOf('agencyModeFk', { Self.validatesPresenceOf('agencyModeFk', {
message: `Agency cannot be blank` message: `Agency cannot be blank`
}); });
Self.observe('after save', async function() {
await app.models.ZoneClosure.doRecalc();
});
Self.observe('after delete', async function() {
await app.models.ZoneClosure.doRecalc();
});
}; };