Created
October 10, 2013 17:46
-
-
Save islador/6922536 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def self.build(user_id) | |
#Trigger: Order API | |
#Attributes touched: type_id, station_id, char_id, entity, vol_entered, vol_remaining | |
#Basic Concept: Iterate thorugh all MOs tied to the user. Compare each MO's type_id to the users existing | |
#MS type_ids. If a match is found, check that the station_id, char_id, and entity match as well. If all match | |
#set that MO's market_item_summary_id to the MS's id. If any of them do not match, create a new MS with those values. | |
#Assemble a hash for use in building new MIS | |
temphash = {"type_id" => nil, "station_id" => nil, "char_id" => nil, "entity" => nil, "bid" => nil} | |
#Assemble the user and apilist arrays. | |
user = User.where('id = ?', user_id) | |
apilist = Api.where('user_id = ?', user_id) | |
#Iterate through each API. | |
apilist.each do |api| | |
#Set the API's entity value. | |
temphash["entity"] = api.entity | |
#Query for orders that are not in a MIS but belong to the current API and User. | |
returned_orders = MarketOrder.where('user_id = :userid AND api_id = :apiid AND market_item_summary_id IS NULL', {userid: user_id, apiid: api.id}) | |
#Iterate through each returned order. | |
returned_orders.each do |ro| | |
#Query for any MIS that match the current order. | |
mis = MarketItemSummaries.where('user_id = :userid AND entity = :entity AND type_id = :mo_type_id AND station_id = :mo_station_id AND char_id = :mo_char_id AND bid = :mo_bid', {userid: user_id, entity: api.entity, mo_type_id: ro.type_id, mo_station_id: ro.station_id, mo_char_id: ro.char_id, mo_bid: ro.bid}) | |
#Check if an empty array is returned or if it contains a model. | |
if mis.empty? | |
#if it does not contain a model query for the order. | |
#this is done because I am unsure how the index will behave if I use the ro instead of a new variable. | |
order = MarketOrder.where('id = ?', ro.id) | |
#assemble a hash | |
temphash["type_id"] = order[0].type_id | |
temphash["station_id"] = order[0].station_id | |
temphash["char_id"] = order[0].char_id | |
temphash["bid"] = order[0].bid | |
#and build a new MIS. | |
new_mis = user[0].MarketItemSummaries.build(temphash) | |
new_mis.save | |
else | |
#If it contains a model, associate the MO with the matching MIS. | |
order = MarketOrder.where('id = ?', ro.id) | |
order[0].market_item_summary_id = mis.id | |
order[0].save | |
end | |
end | |
end | |
#fingerprinting would be much faster. | |
#fingerprint = MarketOrder.find_by_id(201).api_id.to_s + MarketOrder.find_by_id(201).station_id.to_s + MarketOrder.find_by_id(201).char_id.to_s + Api.find_by_id(MarketOrder.find_by_id(201).api_id).entity.to_s | |
#Batch queries could be better, as with proper where linking to determine what is what. | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment