php CRM客户分配方案整理

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


支持的分配场景

  • 公海客户分配给指定负责人
  • 离职员工客户分配
  • 更换客户的现有负责人
    下文将介绍三种常用的客户分配方式:
  1. 依次分配
  2. 平均随机分配
  3. 完全随机分配

1. 依次分配

适用场景:
需要均匀、顺序地将客户分配给各负责人。
实现思路:

  1. 计算每个负责人平均可分配的客户数,判断是否有剩余客户需额外分配。
  2. 通过循环和取模运算,依次将客户分配给负责人,实现均匀分配。
  3. 返回以负责人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. 随机分配 - 均衡分配

适用场景:
需要分配结果兼具随机性和均衡性。
实现思路:

  1. 对客户ID数组进行随机打乱,消除原有顺序影响。
  2. 使用依次分配(取模运算)逻辑,将打乱后的客户ID均匀分配给负责人。
  3. 结果既保证了分配的随机性,又保证了均衡。
    代码示例:
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. 随机分配 - 非均衡分配

适用场景:
只要求分配过程完全随机,不考虑负责人间的均衡。
实现思路:

  1. 遍历每一个客户ID,不依赖取模运算。
  2. 对每个客户,从负责人ID数组中随机选取一位负责人。
  3. 最终各负责人分配到的客户数量可能有明显差异。
    代码示例:
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;
}

总结

  • 可根据实际需求选择不同的客户分配方式(依次、随机均衡、完全随机)。
  • 推荐在需要均衡分配时,采用依次分配或随机均衡分配方案。
  • 对于强调随机性的场景,可选择完全随机分配。
    如有更多分配需求或特殊场景,欢迎进一步交流!
相关推荐
JaguarJack1 天前
为什么 PHP 闭包要加 static?
后端·php·服务端
ServBay2 天前
垃圾堆里编码?真的不要怪 PHP 不行
后端·php
用户962377954482 天前
CTF 伪协议
php
BingoGo5 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php
JaguarJack5 天前
当你的 PHP 应用的 API 没有限流时会发生什么?
后端·php·服务端
BingoGo6 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack6 天前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack7 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo7 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
JaguarJack8 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel