From 3492c802b953ed9e2709c17c02bbe6bda8878c80 Mon Sep 17 00:00:00 2001 From: Trowbridge Date: Wed, 4 Mar 2020 17:13:17 -0500 Subject: [PATCH] add route to get suggested swap --- index.js | 40 ++++++++++++++++++++++++ route_sql/swap_fit.sql | 71 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 route_sql/swap_fit.sql diff --git a/index.js b/index.js index 65128c8..f068a1f 100644 --- a/index.js +++ b/index.js @@ -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) { var sql = ""; diff --git a/route_sql/swap_fit.sql b/route_sql/swap_fit.sql new file mode 100644 index 0000000..da9a861 --- /dev/null +++ b/route_sql/swap_fit.sql @@ -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 \ No newline at end of file