Merge branch 'feature/allocation' into develop_bm

# Conflicts:
#	app/crm/controller/CustomerController.php
#	app/crm/service/CustomerService.php
#	app/crmAdmin/service/CustomerService.php
This commit is contained in:
2025-08-18 16:59:13 +08:00
3 changed files with 85 additions and 0 deletions
@@ -207,4 +207,28 @@ class CustomerController extends BaseController
}
}
/**
* 离职员工客户分配
* @param Request $request
* @return \support\Response
*/
public function allocation(Request $request)
{
try {
$param = $request->all();
if (empty($param['corp_id'])) {
return $this->error('corp_id'.trans('not_null'));
}
if (empty($param['user_id'])) {
return $this->error('user_id'.trans('not_null'));
}
$result = (new CustomerService())->allocation($param);
return $this->success($result,'成功分配'.$result['userCount'].'位离职用户的'.$result['customerCount'].'位客户');
} catch (\Throwable $th) {
return $this->error($th->getMessage());
}
}
}
+60
View File
@@ -21,6 +21,8 @@ use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use support\Db;
use support\exception\BusinessException;
use support\Log;
class CustomerService
{
@@ -462,4 +464,62 @@ class CustomerService
return $follow;
}
/**
* 离职员工客户分配
* @param $params
* @return string[]
*/
public function allocation($params)
{
$corpId = $params['corp_id'];
$userId = explode(',', $params['user_id']);
log::info('开始分配离职员工客户,corp_id:'.$corpId.' 被分配员工'. implode(',', $userId));
//查询离职客户
$userResign = User::where('status', 2)
->where('corp_id', $corpId)
->get(['id','name']);
$resignUerIds = [];
$userNames = [];
foreach ($userResign as $row) {
$resignUerIds[] = (int)$row->id;
$userNames[] = $row->name;
}
if (empty($userResign)) {
log::info('未匹配到离职用户');
throw new BusinessException('未匹配到离职用户', 500);
}
log::info('匹配到离职用户数:' . count($resignUerIds) .' '. implode(',',$userNames));
$customerCount = Customer::whereIn('user_id', $resignUerIds)
->orderBy('id', 'asc')
->count();
if ($customerCount === 0) {
log::info('未找到离职员工相关的客户记录。');
throw new BusinessException('未找到离职员工相关的客户记录', 500);
}
// 初始化 UID 轮询器
$uidRotator = new \app\common\utils\RedisUidRotator('allocation_customer_user');
$uidRotator->setUids($userId, false);
Customer::whereIn('user_id',$resignUerIds)->chunk(500, function ($customers) use ($uidRotator){
foreach ($customers as $customer) {
// 获取下一个分配的UID
$newUserId = $uidRotator->next();
$customer->user_id = $newUserId;
$customer->save();
}
});
return [
'userCount' => count($resignUerIds),
'customerCount' => $customerCount,
];
}
}