Files
ckb-SuperAdmin/Server/application/cunkebao/controller/TrafficPoolV2Controller.php
2026-02-02 11:00:26 +08:00

570 lines
18 KiB
PHP

<?php
namespace app\cunkebao\controller;
use app\cunkebao\service\TrafficPoolService;
use app\cunkebao\service\TrafficPoolGroupService;
use app\common\model\TrafficPoolGroup;
use app\common\model\TrafficPoolCompany;
use app\common\model\TrafficPoolTag;
use app\common\model\TrafficPoolTagCategory;
use app\common\model\TrafficPoolTagDefine;
use app\common\model\TrafficPoolAllotRecord;
use app\common\service\ClassTableService;
use library\ResponseHelper;
/**
* 流量池控制器 V2
* 基于新架构的流量池 API 接口
*/
class TrafficPoolV2Controller extends BaseController
{
/**
* @var TrafficPoolService
*/
protected $poolService;
/**
* @var TrafficPoolGroupService
*/
protected $groupService;
public function __construct(ClassTableService $classTable)
{
parent::__construct($classTable);
$this->poolService = new TrafficPoolService();
$this->groupService = new TrafficPoolGroupService();
}
// ==================== 分组相关接口 ====================
/**
* 获取流量池分组列表
* @return \think\response\Json
*/
public function getGroups()
{
$companyId = $this->getUserInfo('companyId');
try {
$groups = $this->groupService->getGroupList($companyId, true);
return ResponseHelper::success($groups);
} catch (\Exception $e) {
return ResponseHelper::error('获取分组列表失败:' . $e->getMessage());
}
}
/**
* 获取分组详情
* @return \think\response\Json
*/
public function getGroupDetail()
{
$groupId = $this->request->param('groupId', 0, 'intval');
$companyId = $this->getUserInfo('companyId');
if (empty($groupId)) {
return ResponseHelper::error('分组ID不能为空');
}
try {
$detail = $this->groupService->getGroupDetail($groupId, $companyId);
if (!$detail) {
return ResponseHelper::error('分组不存在');
}
return ResponseHelper::success($detail);
} catch (\Exception $e) {
return ResponseHelper::error('获取分组详情失败:' . $e->getMessage());
}
}
/**
* 创建分组
* @return \think\response\Json
*/
public function createGroup()
{
$companyId = $this->getUserInfo('companyId');
$userId = $this->getUserInfo('id');
$data = $this->request->param();
if (empty($data['groupName'])) {
return ResponseHelper::error('分组名称不能为空');
}
try {
$group = $this->groupService->createGroup($companyId, $data, $userId);
return ResponseHelper::success(['id' => $group->id], '创建成功');
} catch (\Exception $e) {
return ResponseHelper::error('创建分组失败:' . $e->getMessage());
}
}
/**
* 更新分组
* @return \think\response\Json
*/
public function updateGroup()
{
$groupId = $this->request->param('groupId', 0, 'intval');
$companyId = $this->getUserInfo('companyId');
if (empty($groupId)) {
return ResponseHelper::error('分组ID不能为空');
}
$data = $this->request->param();
try {
$this->groupService->updateGroup($groupId, $companyId, $data);
return ResponseHelper::success(null, '更新成功');
} catch (\Exception $e) {
return ResponseHelper::error('更新分组失败:' . $e->getMessage());
}
}
/**
* 删除分组
* @return \think\response\Json
*/
public function deleteGroup()
{
$groupId = $this->request->param('groupId', 0, 'intval');
$companyId = $this->getUserInfo('companyId');
if (empty($groupId)) {
return ResponseHelper::error('分组ID不能为空');
}
try {
$this->groupService->deleteGroup($groupId, $companyId);
return ResponseHelper::success(null, '删除成功');
} catch (\Exception $e) {
return ResponseHelper::error('删除分组失败:' . $e->getMessage());
}
}
// ==================== 流量池成员相关接口 ====================
/**
* 获取分组成员列表(用户列表)
* @return \think\response\Json
*/
public function getGroupMembers()
{
$groupId = $this->request->param('groupId', 0, 'intval');
$companyId = $this->getUserInfo('companyId');
$page = $this->request->param('page', 1, 'intval');
$pageSize = $this->request->param('pageSize', 10, 'intval');
$keyword = $this->request->param('keyword', '');
if (empty($groupId)) {
return ResponseHelper::error('分组ID不能为空');
}
$filters = [
'keyword' => $keyword
];
try {
$result = $this->groupService->getGroupMembers($groupId, $companyId, $page, $pageSize, $filters);
return ResponseHelper::success($result);
} catch (\Exception $e) {
return ResponseHelper::error('获取成员列表失败:' . $e->getMessage());
}
}
/**
* 获取流量池列表(全量,带筛选)
* @return \think\response\Json
*/
public function getPoolList()
{
$companyId = $this->getUserInfo('companyId');
$page = $this->request->param('page', 1, 'intval');
$pageSize = $this->request->param('pageSize', 10, 'intval');
$filters = [
'keyword' => $this->request->param('keyword', ''),
'friendStatus' => $this->request->param('friendStatus'),
'level' => $this->request->param('level'),
'lifecycle' => $this->request->param('lifecycle'),
'allocateStatus' => $this->request->param('allocateStatus'),
'ownerWechatId' => $this->request->param('ownerWechatId', ''),
'rfmMMin' => $this->request->param('rfmMMin'),
'rfmMMax' => $this->request->param('rfmMMax'),
];
// 移除空值
$filters = array_filter($filters, function($v) {
return $v !== null && $v !== '';
});
try {
$result = $this->poolService->getPoolList($companyId, $page, $pageSize, $filters);
return ResponseHelper::success($result);
} catch (\Exception $e) {
return ResponseHelper::error('获取流量池列表失败:' . $e->getMessage());
}
}
/**
* 获取流量详情
* @return \think\response\Json
*/
public function getPoolDetail()
{
$poolCompanyId = $this->request->param('id', 0, 'intval');
$companyId = $this->getUserInfo('companyId');
if (empty($poolCompanyId)) {
return ResponseHelper::error('流量ID不能为空');
}
try {
$detail = $this->poolService->getPoolDetail($poolCompanyId, $companyId);
if (!$detail) {
return ResponseHelper::error('流量不存在');
}
return ResponseHelper::success($detail);
} catch (\Exception $e) {
return ResponseHelper::error('获取流量详情失败:' . $e->getMessage());
}
}
/**
* 更新流量信息
* @return \think\response\Json
*/
public function updatePool()
{
$poolCompanyId = $this->request->param('id', 0, 'intval');
$companyId = $this->getUserInfo('companyId');
if (empty($poolCompanyId)) {
return ResponseHelper::error('流量ID不能为空');
}
$data = $this->request->param();
try {
$this->poolService->updatePool($poolCompanyId, $companyId, $data);
return ResponseHelper::success(null, '更新成功');
} catch (\Exception $e) {
return ResponseHelper::error('更新失败:' . $e->getMessage());
}
}
/**
* 添加成员到分组(手动分组)
* @return \think\response\Json
*/
public function addMembersToGroup()
{
$groupId = $this->request->param('groupId', 0, 'intval');
$poolCompanyIds = $this->request->param('poolCompanyIds/a', []);
$companyId = $this->getUserInfo('companyId');
$userId = $this->getUserInfo('id');
if (empty($groupId)) {
return ResponseHelper::error('分组ID不能为空');
}
if (empty($poolCompanyIds)) {
return ResponseHelper::error('请选择要添加的成员');
}
try {
$count = $this->groupService->addMembers($groupId, $poolCompanyIds, $companyId, $userId);
return ResponseHelper::success(['count' => $count], "成功添加 {$count} 个成员");
} catch (\Exception $e) {
return ResponseHelper::error('添加失败:' . $e->getMessage());
}
}
/**
* 从分组移除成员
* @return \think\response\Json
*/
public function removeMembersFromGroup()
{
$groupId = $this->request->param('groupId', 0, 'intval');
$poolCompanyIds = $this->request->param('poolCompanyIds/a', []);
$companyId = $this->getUserInfo('companyId');
if (empty($groupId)) {
return ResponseHelper::error('分组ID不能为空');
}
if (empty($poolCompanyIds)) {
return ResponseHelper::error('请选择要移除的成员');
}
try {
$count = $this->groupService->removeMembers($groupId, $poolCompanyIds, $companyId);
return ResponseHelper::success(['count' => $count], "成功移除 {$count} 个成员");
} catch (\Exception $e) {
return ResponseHelper::error('移除失败:' . $e->getMessage());
}
}
// ==================== 标签相关接口 ====================
/**
* 获取标签类目列表
* @return \think\response\Json
*/
public function getTagCategories()
{
$companyId = $this->getUserInfo('companyId');
$tagType = $this->request->param('tagType');
try {
$categories = TrafficPoolTagCategory::getCategoryTree($companyId, $tagType);
return ResponseHelper::success($categories);
} catch (\Exception $e) {
return ResponseHelper::error('获取标签类目失败:' . $e->getMessage());
}
}
/**
* 获取标签定义列表
* @return \think\response\Json
*/
public function getTagDefines()
{
$companyId = $this->getUserInfo('companyId');
$tagType = $this->request->param('tagType');
$categoryId = $this->request->param('categoryId');
try {
$defines = TrafficPoolTagDefine::getTagDefinesByCompany($companyId, $tagType, $categoryId);
return ResponseHelper::success($defines);
} catch (\Exception $e) {
return ResponseHelper::error('获取标签定义失败:' . $e->getMessage());
}
}
/**
* 为流量添加标签
* @return \think\response\Json
*/
public function addTag()
{
$poolCompanyId = $this->request->param('poolCompanyId', 0, 'intval');
$tagDefineId = $this->request->param('tagDefineId', 0, 'intval');
$tagValue = $this->request->param('tagValue', '');
$companyId = $this->getUserInfo('companyId');
$userId = $this->getUserInfo('id');
if (empty($poolCompanyId) || empty($tagDefineId)) {
return ResponseHelper::error('参数不完整');
}
// 验证流量归属
$poolCompany = TrafficPoolCompany::where('id', $poolCompanyId)
->where('companyId', $companyId)
->where('isDel', 0)
->find();
if (!$poolCompany) {
return ResponseHelper::error('流量不存在');
}
try {
$tag = TrafficPoolTag::addTag(
$poolCompanyId,
$poolCompany->identifier,
$companyId,
$tagDefineId,
TrafficPoolTag::SOURCE_MANUAL,
$userId,
$tagValue
);
if ($tag) {
return ResponseHelper::success(['id' => $tag->id], '添加成功');
} else {
return ResponseHelper::error('添加失败');
}
} catch (\Exception $e) {
return ResponseHelper::error('添加标签失败:' . $e->getMessage());
}
}
/**
* 移除流量标签
* @return \think\response\Json
*/
public function removeTag()
{
$poolCompanyId = $this->request->param('poolCompanyId', 0, 'intval');
$tagDefineId = $this->request->param('tagDefineId', 0, 'intval');
$companyId = $this->getUserInfo('companyId');
if (empty($poolCompanyId) || empty($tagDefineId)) {
return ResponseHelper::error('参数不完整');
}
// 验证流量归属
$poolCompany = TrafficPoolCompany::where('id', $poolCompanyId)
->where('companyId', $companyId)
->where('isDel', 0)
->find();
if (!$poolCompany) {
return ResponseHelper::error('流量不存在');
}
try {
$result = TrafficPoolTag::removeTag($poolCompanyId, $tagDefineId);
if ($result) {
return ResponseHelper::success(null, '移除成功');
} else {
return ResponseHelper::error('标签不存在');
}
} catch (\Exception $e) {
return ResponseHelper::error('移除标签失败:' . $e->getMessage());
}
}
/**
* 获取流量的标签
* @return \think\response\Json
*/
public function getPoolTags()
{
$poolCompanyId = $this->request->param('poolCompanyId', 0, 'intval');
$tagType = $this->request->param('tagType');
$companyId = $this->getUserInfo('companyId');
if (empty($poolCompanyId)) {
return ResponseHelper::error('流量ID不能为空');
}
// 验证流量归属
$poolCompany = TrafficPoolCompany::where('id', $poolCompanyId)
->where('companyId', $companyId)
->where('isDel', 0)
->find();
if (!$poolCompany) {
return ResponseHelper::error('流量不存在');
}
try {
$tags = TrafficPoolTag::getTagsByPoolCompany($poolCompanyId, $tagType);
return ResponseHelper::success($tags);
} catch (\Exception $e) {
return ResponseHelper::error('获取标签失败:' . $e->getMessage());
}
}
// ==================== 分配相关接口 ====================
/**
* 分配流量给客服
* @return \think\response\Json
*/
public function allocatePool()
{
$poolCompanyId = $this->request->param('poolCompanyId', 0, 'intval');
$toWechatId = $this->request->param('toWechatId', '');
$toAccountId = $this->request->param('toAccountId', 0, 'intval');
$toUserId = $this->request->param('toUserId', 0, 'intval');
$expireDays = $this->request->param('expireDays', 30, 'intval');
$companyId = $this->getUserInfo('companyId');
$operatorId = $this->getUserInfo('id');
if (empty($poolCompanyId) || empty($toWechatId)) {
return ResponseHelper::error('参数不完整');
}
// 验证流量归属
$poolCompany = TrafficPoolCompany::where('id', $poolCompanyId)
->where('companyId', $companyId)
->where('isDel', 0)
->find();
if (!$poolCompany) {
return ResponseHelper::error('流量不存在');
}
try {
$fromInfo = [
'fromWechatId' => $poolCompany->ownerWechatId,
'fromAccountId' => $poolCompany->ownerAccountId,
'fromUserId' => $poolCompany->ownerUserId,
];
$record = TrafficPoolAllotRecord::createAllotRecord(
$poolCompanyId,
$poolCompany->identifier,
$companyId,
$toWechatId,
$toAccountId ?: null,
$toUserId ?: null,
$expireDays,
$operatorId,
$fromInfo
);
return ResponseHelper::success(['id' => $record->id], '分配成功');
} catch (\Exception $e) {
return ResponseHelper::error('分配失败:' . $e->getMessage());
}
}
/**
* 回收流量分配
* @return \think\response\Json
*/
public function recyclePool()
{
$poolCompanyId = $this->request->param('poolCompanyId', 0, 'intval');
$companyId = $this->getUserInfo('companyId');
$operatorId = $this->getUserInfo('id');
if (empty($poolCompanyId)) {
return ResponseHelper::error('流量ID不能为空');
}
// 验证流量归属
$poolCompany = TrafficPoolCompany::where('id', $poolCompanyId)
->where('companyId', $companyId)
->where('isDel', 0)
->find();
if (!$poolCompany) {
return ResponseHelper::error('流量不存在');
}
try {
TrafficPoolAllotRecord::recycleAllot($poolCompanyId, $operatorId);
return ResponseHelper::success(null, '回收成功');
} catch (\Exception $e) {
return ResponseHelper::error('回收失败:' . $e->getMessage());
}
}
// ==================== 统计相关接口 ====================
/**
* 获取流量池统计数据
* @return \think\response\Json
*/
public function getStatistics()
{
$companyId = $this->getUserInfo('companyId');
try {
$statistics = $this->poolService->getStatistics($companyId);
return ResponseHelper::success($statistics);
} catch (\Exception $e) {
return ResponseHelper::error('获取统计数据失败:' . $e->getMessage());
}
}
}