add route to get suggested swap

This commit is contained in:
Trowbridge 2020-03-04 17:13:17 -05:00
parent 6bee5571dc
commit 3492c802b9
2 changed files with 111 additions and 0 deletions

View File

@ -134,6 +134,46 @@ server.get('/scenario_package', bodyParser.json(), function(req, res) {
}; };
}) })
server.get('/swap_fit', bodyParser.json(), function(req, res) {
var sql = "";
var w = "";
var c = 1;
var d = 1;
var args = [];
var path = './route_sql/swap_fit.sql';
fs.readFile(path, 'utf8', function(err, data) {
if (!err) {
callback(data);
} else {
console.log("fatal error pulling sql file")
callback(err);
}
});
var callback = function(arg) {
sql = arg;
//parse request body into a where clause
({ c, w, d } = build_where(req, c, w, d, args));
//if there was no body sent, return with nothing
if (c == 1) {
res.send("no body was sent");
return;
}
console.log(new Date().toISOString() + "-------------------------get swap fit:------------------------------")
console.log(req.body);
//parse the where clause into the main sql statement
sql = sql.replace(new RegExp("where_clause", 'g'), w);
sql = sql.replace(new RegExp("replace_new_mold", 'g'), req.body.new_mold);
//execute the sql and send the result
console.log(sql);
Postgres.FirstRow(sql, [], res)
};
})
server.get('/list_changes', bodyParser.json(), function(req, res) { server.get('/list_changes', bodyParser.json(), function(req, res) {
var sql = ""; var sql = "";

71
route_sql/swap_fit.sql Normal file
View File

@ -0,0 +1,71 @@
WITH
target AS (select replace_new_mold new_mold)
,basemix AS (
SELECT
part,
stlc,
colc,
colgrp,
coltier,
sizc,
SUM(value_usd) value_usd
FROM
rlarp.osm_pool o
LEFT OUTER JOIN rlarp.itemmv i ON
i.item = o.part
WHERE
-----------------scenario----------------------------
where_clause
-----------------additional params-------------------
AND calc_status||flag <> 'CLOSEDREMAINDER' --exclude short ships when building order adjustments
AND order_date <= ship_date
GROUP BY
part,
stlc,
colc,
colgrp,
coltier,
sizc
)
--SELECT * FROM basemix
,tag AS (
SELECT
b.part
,b.value_usd
--,b.stlc
--,b.sizc
--,b.colgrp
--,b.coltier
--,b.colc
,i.item
,CASE WHEN b.sizc = i.sizc THEN '1' ELSE '0' END||
CASE WHEN i.aplnt = 'I' THEN '0' ELSE '1' END||
CASE WHEN b.colgrp = i.colgrp THEN '1' ELSE '0' END||
CASE WHEN b.coltier = i.coltier THEN '1' ELSE '0' END||
CASE WHEN b.colc = i.colc THEN '1' ELSE '0' END||
CASE WHEN substring(b.part,9,12) = substring(i.item,9,12) THEN '1' ELSE 0 END fit
FROM
basemix b
LEFT OUTER JOIN rlarp.itemmv i ON
i.stlc = (SELECT new_mold FROM target)
)
--SELECT * FROM tag
,rsort AS (
SELECT
part,
value_usd,
item swap,
fit,
ROW_NUMBER() OVER (PARTITION BY tag.part ORDER BY fit DESC) ranked
FROM
tag
ORDER BY
part
,fit desc
)
SELECT
json_agg(row_to_json(rsort)) x
FROM
rsort
WHERE
ranked = 1