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;
}

总结

  • 可根据实际需求选择不同的客户分配方式(依次、随机均衡、完全随机)。
  • 推荐在需要均衡分配时,采用依次分配或随机均衡分配方案。
  • 对于强调随机性的场景,可选择完全随机分配。
    如有更多分配需求或特殊场景,欢迎进一步交流!
相关推荐
两个人的幸福2 天前
Windows 桌面应用自研 PHP 队列(下):完整代码与六大工程化优化
php
BingoGo5 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
JaguarJack5 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
用户3074596982075 天前
PHP 扩展——从入门到理解
php
鹏仔先生6 天前
拷贝漫画APP下载页PHP程序,后台带免费AI写作
php
云水一下6 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php
xingpanvip6 天前
星盘接口开发文档:本命盘接口指南
android·开发语言·css·php·lua
酉鬼女又兒6 天前
零基础入门计算机网络运输层:端到端通信核心作用、端口号分类规则、复用分用工作机制及UDP与TCP协议全方位对比详解
网络·网络协议·tcp/ip·计算机网络·考研·udp·php
dog2506 天前
不要再继续优化 TCP
网络协议·tcp/ip·php
Channing Lewis6 天前
PHP 解析 Excel 的那些坑:一次“行号错位”引发的数据丢失
开发语言·php·excel