新版流量池服务端提交
This commit is contained in:
@@ -97,12 +97,156 @@ class TrafficPoolV2Controller extends BaseController
|
||||
|
||||
try {
|
||||
$group = $this->groupService->createGroup($companyId, $data, $userId);
|
||||
return ResponseHelper::success(['id' => $group->id], '创建成功');
|
||||
return ResponseHelper::success([
|
||||
'id' => $group->id,
|
||||
'groupName' => $group->groupName
|
||||
], '创建成功');
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error('创建分组失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据筛选条件预览用户列表
|
||||
* GET /v1/traffic/pool/v2/preview-users
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function previewUsers()
|
||||
{
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$ruleConfig = $this->request->param('ruleConfig');
|
||||
$page = $this->request->param('page', 1, 'intval');
|
||||
$pageSize = $this->request->param('pageSize', 20, 'intval');
|
||||
$keyword = $this->request->param('keyword', '');
|
||||
|
||||
if (empty($ruleConfig)) {
|
||||
return ResponseHelper::error('筛选条件不能为空');
|
||||
}
|
||||
|
||||
// 如果ruleConfig是JSON字符串,解析它
|
||||
if (is_string($ruleConfig)) {
|
||||
$ruleConfig = json_decode($ruleConfig, true);
|
||||
}
|
||||
|
||||
// 从ruleConfig中提取keyword(如果前端放在里面的话)
|
||||
if (empty($keyword) && isset($ruleConfig['keyword'])) {
|
||||
$keyword = $ruleConfig['keyword'];
|
||||
unset($ruleConfig['keyword']);
|
||||
}
|
||||
|
||||
try {
|
||||
$result = $this->groupService->previewGroupMembers($companyId, $ruleConfig, $page, $pageSize, $keyword);
|
||||
return ResponseHelper::success($result);
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error('获取用户列表失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取筛选条件可选项(字段元数据)
|
||||
* GET /v1/traffic/pool/v2/filter-fields
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getFilterFields()
|
||||
{
|
||||
try {
|
||||
$fields = [
|
||||
[
|
||||
'field' => 'lifecycle',
|
||||
'label' => '客户周期',
|
||||
'type' => 'select',
|
||||
'options' => [
|
||||
['label' => '新流量', 'value' => 1],
|
||||
['label' => '成长期', 'value' => 2],
|
||||
['label' => '成熟期', 'value' => 3],
|
||||
['label' => '衰退期', 'value' => 4],
|
||||
['label' => '流失期', 'value' => 5],
|
||||
]
|
||||
],
|
||||
[
|
||||
'field' => 'intentionLevel',
|
||||
'label' => '意向等级',
|
||||
'type' => 'select',
|
||||
'options' => [
|
||||
['label' => '未知', 'value' => 0],
|
||||
['label' => '低意向', 'value' => 1],
|
||||
['label' => '中意向', 'value' => 2],
|
||||
['label' => '高意向', 'value' => 3],
|
||||
]
|
||||
],
|
||||
[
|
||||
'field' => 'level',
|
||||
'label' => '客户等级',
|
||||
'type' => 'select',
|
||||
'options' => [
|
||||
['label' => '普通', 'value' => 0],
|
||||
['label' => '白银', 'value' => 1],
|
||||
['label' => '黄金', 'value' => 2],
|
||||
['label' => '钻石', 'value' => 3],
|
||||
]
|
||||
],
|
||||
[
|
||||
'field' => 'gender',
|
||||
'label' => '性别',
|
||||
'type' => 'select',
|
||||
'options' => [
|
||||
['label' => '未知', 'value' => 0],
|
||||
['label' => '男', 'value' => 1],
|
||||
['label' => '女', 'value' => 2],
|
||||
]
|
||||
],
|
||||
[
|
||||
'field' => 'friendStatus',
|
||||
'label' => '好友状态',
|
||||
'type' => 'select',
|
||||
'options' => [
|
||||
['label' => '未添加', 'value' => 0],
|
||||
['label' => '已申请', 'value' => 1],
|
||||
['label' => '已通过', 'value' => 2],
|
||||
['label' => '已拒绝', 'value' => 3],
|
||||
['label' => '已删除', 'value' => 4],
|
||||
]
|
||||
],
|
||||
[
|
||||
'field' => 'province',
|
||||
'label' => '地区',
|
||||
'type' => 'province',
|
||||
],
|
||||
[
|
||||
'field' => 'totalOrderAmount',
|
||||
'label' => '总消费金额',
|
||||
'type' => 'number',
|
||||
],
|
||||
[
|
||||
'field' => 'totalOrderCount',
|
||||
'label' => '订单数量',
|
||||
'type' => 'number',
|
||||
],
|
||||
[
|
||||
'field' => 'totalMsgCount',
|
||||
'label' => '消息数量',
|
||||
'type' => 'number',
|
||||
],
|
||||
[
|
||||
'field' => 'rfmF',
|
||||
'label' => 'RFM-F值',
|
||||
'type' => 'number',
|
||||
],
|
||||
[
|
||||
'field' => 'rfmM',
|
||||
'label' => 'RFM-M值',
|
||||
'type' => 'number',
|
||||
],
|
||||
];
|
||||
|
||||
return ResponseHelper::success($fields);
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error('获取字段列表失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新分组
|
||||
* @return \think\response\Json
|
||||
@@ -658,5 +802,96 @@ class TrafficPoolV2Controller extends BaseController
|
||||
return ResponseHelper::error('获取行为轨迹失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算并更新RFM评分
|
||||
* POST /v1/traffic/pool/v2/calculate-rfm
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function calculateRfm()
|
||||
{
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$identifier = $this->request->post('identifier', null); // 可选,指定用户标识
|
||||
|
||||
try {
|
||||
// 实例化RFM控制器(传递ClassTableService)
|
||||
$rfmController = new RFMController($this->classTable);
|
||||
|
||||
// 获取配置参数(可从请求参数中获取,或使用默认值)
|
||||
$config = [
|
||||
'cycle_days' => $this->request->post('cycle_days', 180),
|
||||
'weight_R' => $this->request->post('weight_R', 0.4),
|
||||
'weight_F' => $this->request->post('weight_F', 0.3),
|
||||
'weight_M' => $this->request->post('weight_M', 0.3),
|
||||
'score_scale' => $this->request->post('score_scale', 5),
|
||||
];
|
||||
|
||||
// 调用RFM计算方法
|
||||
// 注意:这里不传ownerWechatId,因为V2系统是按companyId区分的
|
||||
$result = $rfmController->calculateRfmFromTrafficOrder($identifier, null, $config);
|
||||
|
||||
if ($result['code'] == 200) {
|
||||
return ResponseHelper::success($result['data'], 'RFM计算完成');
|
||||
} else {
|
||||
return ResponseHelper::error($result['msg']);
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error('RFM计算失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量更新指定分组的RFM评分
|
||||
* POST /v1/traffic/pool/v2/group/{groupId}/calculate-rfm
|
||||
*
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function calculateGroupRfm()
|
||||
{
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$groupId = $this->request->param('groupId');
|
||||
|
||||
if (empty($groupId)) {
|
||||
return ResponseHelper::error('分组ID不能为空');
|
||||
}
|
||||
|
||||
try {
|
||||
// 获取分组成员
|
||||
$members = $this->groupService->getGroupMembers($groupId, $companyId, 1, 9999, []);
|
||||
|
||||
if (empty($members['list'])) {
|
||||
return ResponseHelper::error('分组无成员');
|
||||
}
|
||||
|
||||
// 实例化RFM控制器(传递ClassTableService)
|
||||
$rfmController = new RFMController($this->classTable);
|
||||
|
||||
$successCount = 0;
|
||||
$failCount = 0;
|
||||
|
||||
// 为每个成员计算RFM
|
||||
foreach ($members['list'] as $member) {
|
||||
$identifier = $member['identifier'];
|
||||
$result = $rfmController->calculateRfmFromTrafficOrder($identifier, null, []);
|
||||
|
||||
if ($result['code'] == 200) {
|
||||
$successCount++;
|
||||
} else {
|
||||
$failCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return ResponseHelper::success([
|
||||
'total' => count($members['list']),
|
||||
'success' => $successCount,
|
||||
'fail' => $failCount
|
||||
], 'RFM批量计算完成');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error('RFM批量计算失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user