使用Node.js中的Round Robin方法,如何将一个潜在客户分配给下一个专业人员?

huangapple go评论60阅读模式
英文:

Using Round Robin Method in Node Js, How Can I Assigned a Lead to the Next Professional

问题

我了解你的需求。你可以使用以下步骤在Node.js中分配潜在客户给专业人士:

  1. 首先,当潜在客户提交潜在客户生成表单时,你需要获取所选服务的列表。

  2. 然后,使用这些服务查找可用的专业人员。你提到了多态关系,可以使用它来选择提供所需服务的专业人员。

  3. 接下来,创建一个队列或轮循机制,将潜在客户分配给下一个可用的专业人员。你可以使用循环或递归来实现此目标。

  4. 将潜在客户的信息插入到leads表中,并将专业人员的ID分配给professional_id列。

  5. 确保每次分配后更新专业人员的可用状态,以便下次轮循时不会再次分配给相同的专业人员。

需要注意的是,你需要处理并发情况,以确保多个潜在客户同时提交表单时,能够正确地分配给不同的专业人员。

这是一个高级任务,需要深入的编程知识和数据库操作技巧。你可能需要使用Node.js的数据库库(如MySQL或MongoDB)来执行数据库操作,以及使用循环或递归来管理分配逻辑。如果需要更详细的代码示例或特定的问题解答,请提出。

英文:

I have a user-professionals table and a leads table. Based on their specialties, I would like to assign leads to the next available professional. Below is the list of tables available for this.
<pre>
professionals
--id
--firstname
--lastname
</pre>

<pre>
services
--id
--name
--description
</pre>

<pre>
professional_services
--service_id
--prefessional_id
</pre>

<pre>
leads
--id
--professional_id
--firstname
--lastname
--email
--phone
</pre>

If a lead comes to the page and submits the lead generation form, selecting one or more of our services, and I have 3 professionals offering those services, I want to assign each lead to the next available professional. So if I get 10 leads that day, they should be assigned respectively as follow:
<pre>
In the leads table, the assignment should be

id professional_id
1 1
----- -----------------
2 2
---- -----------------
3 3
---- -----------------
4 1
----- -----------------
5 2
---- -----------------
6 3
---- -----------------
7 1
----- -----------------
8 2
---- -----------------
9 3
---- -----------------
10 1
---- -----------------
</pre>

I know that the polymorphic relationship method can be used to SELECT all professionals who provide the service or services the lead selected when submitting the form. How do I assign the lead to the next and the next and the next professional in Node js and do the circle over and over?

答案1

得分: 1

以下是翻译好的部分:

要在每次添加新潜在客户时逐个分配专业人员,您可以执行以下几个操作:

首先,找出上一个由最高id标识的最后一个潜在客户分配的professional_id

SELECT professional_id FROM leads
WHERE professional_id IS NOT NULL
ORDER BY id DESC LIMIT 1

然后,通过使用上述查询找到的最后一个professional_id,找到下一个professional_id

SELECT professional_id FROM professionals
WHERE professional_id > ?
ORDER BY professional_id LIMIT 1

或者

SELECT MIN(professional_id) FROM professionals
WHERE professional_id > ?

如果上述查询没有结果,即最大的专业人员ID已分配给最后分配的潜在客户,或者尚未分配专业人员给任何潜在客户(即这是第一个潜在客户),则需要分配给第一个潜在客户,因此找到具有最小ID的专业人员

SELECT MIN(professional_id) FROM professionals

无论检索到哪个professional_id,都将其分配给下一个潜在客户(假设您已经知道了上一个潜在客户的ID)

UPDATE leads SET professional_id = ?
WHERE id = ?

如果您不知道上一个潜在客户的ID,那么首先找到它

SELECT MIN(id) FROM leads WHERE professional_id IS NULL

然后将该记录更新为最小的潜在客户ID和没有专业人员ID。

英文:

To assign professionals one by one whenever new leads are added, you may perform several operations like this:

First, find out the last professional_id assigned to the last lead identified by the highest id:

SELECT professional_id FROM leads
WHERE professional_id IS NOT NULL
ORDER BY id DESC LIMIT 1

Then, find the next professional_id by using the last professional_id found by the above query:

SELECT professional_id FROM professionals
WHERE professional_id &gt; ?
ORDER BY professional_id LIMIT 1

OR

SELECT MIN(professional_id) FROM professionals
WHERE professional_id &gt; ?

If the above query does not give any result, i.e. the MAX professional ID has been allocated to the last allocated lead, OR, no professionals have been assigned to any leads yet (i.e. this is the first lead), then the first lead will need to be assigned, so find the professional with the MIN ID

SELECT MIN(professional_id) FROM professionals

Whatever professional_id is retrieved, assign that to the next lead (assuming you already know the last lead ID)

UPDATE leads SET professional_id = ?
WHERE id = ?

If you don't know the last Lead ID, then find it first

SELECT MIN(id) FROM leads WHERE professional_id IS NULL

And then update that record with min lead id and no professional id.

huangapple
  • 本文由 发表于 2023年5月17日 14:37:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定