在实际业务中,经常需要将客户平均或随机分配给指定负责人,例如公海客户分配、员工离职后的客户交接等。本文总结了多种客户分配方式及其实现思路,便于灵活选用。

支持的分配场景
- 公海客户分配给指定负责人
- 离职员工客户分配
- 更换客户的现有负责人
下文将介绍三种常用的客户分配方式:
- 依次分配
- 平均随机分配
- 完全随机分配
1. 依次分配
适用场景:
需要均匀、顺序地将客户分配给各负责人。
实现思路:
- 计算每个负责人平均可分配的客户数,判断是否有剩余客户需额外分配。
- 通过循环和取模运算,依次将客户分配给负责人,实现均匀分配。
- 返回以负责人ID为键、客户ID数组为值的分配结果。
代码示例:
php
/**
* 平均分配客户.
* @param array $customer_ids 客户ID集合
* @param array $admin_ids 负责人ID集合
*/
function distribute_customers(array $customer_ids, array $admin_ids)
{
$customer_map = [];
foreach ($customer_ids as $customer_id) {
$customer_map[$customer_id] = null;
}
if (!$customer_ids || !$admin_ids) {
return $customer_map;
}
$admin_count = count($admin_ids);
// 遍历所有客户,通过取模运算均匀分配
foreach ($customer_ids as $index => $customerId) {
// 计算当前客户应该分配给第几个负责人
$managerIndex = $index % $admin_count;
$targetManagerId = $admin_ids[$managerIndex];
$customer_map[$customerId] = $targetManagerId;
}
return $customer_map;
}
示例输出:
php
$customer_ids = [10001,10002,10003,10004,10005,10006];
$admin_ids = [1,2,3,4,5];
dump(distribute_customers($customer_ids,$admin_ids));
// 输出
array(6) {
[10001] => int(1)
[10002] => int(2)
[10003] => int(3)
[10004] => int(4)
[10005] => int(5)
[10006] => int(1)
}
2. 随机分配 - 均衡分配
适用场景:
需要分配结果兼具随机性和均衡性。
实现思路:
- 对客户ID数组进行随机打乱,消除原有顺序影响。
- 使用依次分配(取模运算)逻辑,将打乱后的客户ID均匀分配给负责人。
- 结果既保证了分配的随机性,又保证了均衡。
代码示例:
php
$customer_ids = [10001,10002,10003,10004,10005,10006];
$admin_ids = [1,2,3,4,5];
shuffle($customer_ids);
dump(distribute_customers($customer_ids,$admin_ids));
// 多次执行后结果是不同的。
3. 随机分配 - 非均衡分配
适用场景:
只要求分配过程完全随机,不考虑负责人间的均衡。
实现思路:
- 遍历每一个客户ID,不依赖取模运算。
- 对每个客户,从负责人ID数组中随机选取一位负责人。
- 最终各负责人分配到的客户数量可能有明显差异。
代码示例:
php
/**
* 随机分配客户-非均衡分配.
*
* @param array $customer_ids 客户ID集合
* @param array $admin_ids 负责人ID集合
*/
function distribute_customers_v3(array $customer_ids, array $admin_ids)
{
$customer_map = [];
foreach ($customer_ids as $customer_id) {
$customer_map[$customer_id] = null;
}
if (!$customer_ids || !$admin_ids) {
return $customer_map;
}
$admin_count = count($admin_ids);
foreach ($customer_ids as $customerId) {
$randomManagerIndex = random_int(0, $admin_count - 1);
$targetManagerId = $admin_ids[$randomManagerIndex];
$customer_map[$customerId] = $targetManagerId;
}
return $customer_map;
}
提示: 在 PHP7+ 环境下,推荐使用 random_int() 替代 rand(),以获得更安全、均匀的随机结果。
综合分配函数示例
php
/**
* 客户分配.
*
* @param array $customer_ids 客户ID集合
* @param array $admin_ids 负责人ID集合
* @param int $type 类型 1=轮询 2=均衡随机 3=完全随机
*/
function distribute_customers(array $customer_ids, array $admin_ids, int $type = 1)
{
if (2 == $type) {
shuffle($customer_ids);
}
$customer_map = [];
foreach ($customer_ids as $customer_id) {
$customer_map[$customer_id] = null;
}
if (!$customer_ids || !$admin_ids) {
return $customer_map;
}
$admin_count = count($admin_ids);
// 遍历所有客户,通过取模运算均匀分配
foreach ($customer_ids as $index => $customerId) {
// 计算当前客户应该分配给第几个负责人
if (3 == $type) {
$managerIndex = random_int(0, $admin_count - 1);
} else {
$managerIndex = $index % $admin_count;
}
$targetManagerId = $admin_ids[$managerIndex];
$customer_map[$customerId] = $targetManagerId;
}
return $customer_map;
}
总结
- 可根据实际需求选择不同的客户分配方式(依次、随机均衡、完全随机)。
- 推荐在需要均衡分配时,采用依次分配或随机均衡分配方案。
- 对于强调随机性的场景,可选择完全随机分配。
如有更多分配需求或特殊场景,欢迎进一步交流!