[英]I was trying to implement round robin assignment of leads to the members-users
我試圖對成員用戶實施循環分配線索。 但是,當我的 function 被調用時,用戶周期又開始了,所有的線索都分配給了第一個用戶。 如何將下一個潛在客戶分配給先前分配的用戶旁邊的用戶。 例如; 如果將lead1 分配給U2 並聲明,則下一個lead2 分配應從U3 繼續。
我使用的代碼: 粗略:
def round_robin(selected_lead)
lead = selected_lead
users = %i(U1 U2 U3).cycle
while lead.status !=“Claimed” do
assigned_user=users.next
# assigned_user <= Assign lead to
# Check to wait 10 min for claimed response...
end
puts “Lead #{lead} has been assigned to #{assigned_user}
end
我是用 ruby 語言做的。 任何建議,將不勝感激。
您需要堅持下一個用戶將被分配領導的概念。 我建議next
用戶表的 boolean 列中執行此操作。 只有一個用戶將此屬性設置為 true,所有其他用戶都將其設置為 nil 或 false。
因此,在用戶 model 中, User.next
是下一個將收到潛在客戶分配的用戶
# in user.rb
has_many :leads
def self.next
find_by(next: true) || first
end
User.next_next
是User.next
之后的用戶,其中 order 只是 id 的值。 (不能只在 id 上加 1,b/c 可能會缺少 id,由於已刪除的用戶)
# in user.rb
def self.next_next
# get the list of all User ids, and find the index of the one that has next=true
next_index = ids.index(next.id)
# find the index in the list of ids of the User after next, modulo User.count.
# So when you get to the last user, "round-robin" back to the first
next_next_index = next_index.succ % count
# return the User after next
find(ids[next_next_index])
end
分配潛在客戶時,將其分配給User.next
,該用戶的next
屬性的值設置為 false,並且User.next
之后的用戶的值將next
屬性的值設置為 true
# in lead.rb
belongs_to :user
def assign_to_next_user
User.assign_lead(self)
end
# in user.rb
def self.assign_lead(lead)
next.leads << self
next.update(next: false)
next_next.update(next: true)
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.