Created
April 5, 2017 14:32
-
-
Save ineersa/87ab03966e8a5bef5fa8631f873e44f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--local rooms = cjson.decode(redis.call('GET', 'searchRoom')) | |
--local rates = cjson.decode(redis.call('GET', 'searchRates')) | |
--local currentRedisData = cjson.decode(redis.call('GET', 'currentRedisData')) | |
--local currentRoomsData = cjson.decode(redis.call('GET', 'currentRoomsData')) | |
--local searchParams = cjson.decode(redis.call('GET', 'searchParams')) | |
local searchParams = {days = {'20160701', '20160702', '20160703', '20160704', '20160705', '20160706', '20160708', '20160709'}} | |
local util = {} | |
local refFields = { | |
fields = { | |
quantity = 'a', | |
sold = 'b', | |
total_sold = 'c', | |
allotment = 'd', | |
adults_1 = 'e', | |
adults_2 = 'f', | |
adults_3 = 'g', | |
adults_4 = 'h', | |
extra_adult = 'i', | |
child = 'j', | |
id_guarantee_policy = 'k', | |
id_cancel_policy = 'm', | |
id_markup_policy = 'n', | |
id_price = 'l', | |
rules = 'o', | |
discount = 'p', | |
force_confirmation = 'r', | |
ifallotused = 's' | |
}, | |
policy = { | |
close = 'a', | |
cta = 'b', | |
ctd_prev = 'c', | |
min_los = 'd', | |
max_los = 'e', | |
min_advance_book = 'f', | |
max_advance_book = 'g', | |
min_occupancy = 'h', | |
max_occupancy = 'i' | |
}} | |
local grouped = {} | |
local keysAlotments = {} | |
local listAllotmentKeys = {} | |
function util.getVal(row, pos, defvalue) | |
if not row[pos] then | |
return defvalue | |
end | |
return row[pos] | |
end | |
function util.unpack(t, i) | |
i = i or 1 | |
if t[i] then | |
return t[i], util.unpack(t, i + 1) | |
end | |
end | |
local rooms = {}; | |
local rates = {}; | |
local currentRedisData = {}; | |
for i = 1, 2400 do | |
rooms[i] = i; | |
if not currentRedisData[i] then | |
currentRedisData[i] = {} | |
end | |
for j = 1, 10 do | |
rates[j] = j; | |
if not currentRedisData[i][j] then | |
currentRedisData[i][j] = {} | |
end | |
for p = 1, #searchParams.days do | |
currentRedisData[i][j][searchParams.days[p]] = cjson.encode({l = 3300000, o = {{a = 0}, {d = 1}}}); | |
end | |
end | |
end | |
for roomId, roomData in pairs(rooms) do | |
for rateId, rateData in pairs(rates) do | |
if nil ~= currentRedisData[roomId][rateId] then | |
for day, row in pairs(currentRedisData[roomId][rateId]) do | |
row = cjson.decode(row) | |
for fieldName, pos in pairs(refFields.fields) do | |
local val = util.getVal(row, pos, nil) | |
if "rules" == fieldName then | |
local temp = '' | |
if type(val) == "table" then | |
temp = {} | |
for ruleName, posRule in pairs(refFields.policy) do | |
if nil ~= val[posRule] then | |
table.insert(temp, {rule = ruleName, quantity = val[posRule]}) | |
end | |
end | |
val = temp | |
end | |
end | |
if not grouped[roomId] then | |
grouped[roomId] = {} | |
end | |
if not grouped[roomId][rateId] then | |
grouped[roomId][rateId] = {} | |
end | |
if not grouped[roomId][rateId][day] then | |
grouped[roomId][rateId][day] = {} | |
end | |
grouped[roomId][rateId][day][fieldName] = val | |
end | |
grouped[roomId][rateData][day]['day'] = day | |
grouped[roomId][rateData][day]['id_room'] = roomId | |
grouped[roomId][rateData][day]['id_rate'] = rateId | |
--grouped[roomId][rateData][day]['id_hotel'] = currentRoomsData[roomId].id_hotel | |
end | |
end | |
end | |
for i = 1, #searchParams.days do | |
local tb = {}; | |
for p in string.gmatch(searchParams.days[i], "([^-]+)") do | |
table.insert(tb, p); | |
end | |
local key = table.concat({table.concat(tb, ""), roomId, 0}, ":") | |
keysAlotments[key] = {id_room = roomId, day = searchParams.days[i]} | |
table.insert(listAllotmentKeys, key) | |
end | |
end | |
local temp = redis.call('HMGET', 'inventory', util.unpack(listAllotmentKeys)) | |
local allotmentRules = {} | |
for pos = 1, #temp do | |
if nil ~= temp[pos] and type(temp[pos]) == "string" then | |
local row = cjson.decode(temp[pos]) | |
local val = util.getVal(row, refFields.fields.rules, nil) | |
if nil ~= val then | |
temp = {} | |
for ruleName, posRule in pairs(refFields.policy) do | |
if nil ~= val[posRule] then | |
table.insert(temp, {rule = ruleName, quantity = val[posRule]}) | |
end | |
end | |
local key = listAllotmentKeys[pos] | |
local roomId = keysAlotments[key].id_room | |
local day = keysAlotments[key].day | |
if not allotmentRules[roomId] then | |
allotmentRules[roomId] = {} | |
end | |
if not allotmentRules[roomId][day] then | |
allotmentRules[roomId][day] = {} | |
end | |
allotmentRules[roomId][day] = temp | |
end | |
end | |
end | |
for roomId, roomData in pairs(grouped) do | |
for rateId, daysData in pairs(grouped[roomId]) do | |
for day, value in pairs(grouped[roomId][rateId]) do | |
if nil ~= allotmentRules[roomId] and nil ~= allotmentRules[roomId][day] then | |
grouped[roomId][rateId][day]['allotmentRules'] = allotmentRules[roomId][day] | |
end | |
end | |
end | |
end | |
return cjson.encode(grouped); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment