Files
cunkebao_v3/Server/application/superadmin/controller/traffic/PostPoolMigrateController.php
Manus AI 5517457929 sync: 以本地为准同步全量变更至 GitHub
含四端需求文档、前端/后端/超管/触客宝迭代及部署脚本更新;未拉取远程。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-30 04:24:56 +08:00

65 lines
2.1 KiB
PHP

<?php
namespace app\superadmin\controller\traffic;
use app\superadmin\controller\BaseController;
use app\superadmin\service\ProjectContextService;
use library\ResponseHelper;
use think\Db;
/**
* 客户池 · 批量迁移客资到指定项目
*/
class PostPoolMigrateController extends BaseController
{
public function index()
{
try {
$poolCompanyIds = $this->request->post('poolCompanyIds/a', []);
$targetProjectId = (int) $this->request->post('targetProjectId/d', 0);
if ($targetProjectId <= 0) {
throw new \Exception('请选择目标项目', 400);
}
$ids = array_values(array_unique(array_filter(array_map('intval', $poolCompanyIds ?: []))));
if (empty($ids)) {
throw new \Exception('请选择要迁移的客户', 400);
}
$targetCompanyId = ProjectContextService::resolveCompanyId($targetProjectId);
Db::startTrans();
$existing = Db::name('traffic_pool_company')
->whereIn('id', $ids)
->where('isDel', 0)
->column('companyId', 'id');
if (count($existing) !== count($ids)) {
throw new \Exception('部分客户不存在或已删除', 400);
}
$sameTarget = array_filter($existing, function ($cid) use ($targetCompanyId) {
return (int) $cid === $targetCompanyId;
});
if (count($sameTarget) === count($ids)) {
throw new \Exception('所选客户已在目标项目中', 400);
}
$migrated = Db::name('traffic_pool_company')
->whereIn('id', $ids)
->where('isDel', 0)
->update(['companyId' => $targetCompanyId, 'updateTime' => time()]);
Db::commit();
return ResponseHelper::success(['migrated' => (int) $migrated]);
} catch (\Exception $e) {
Db::rollback();
$code = (int) $e->getCode();
return ResponseHelper::error($e->getMessage(), $code > 0 ? $code : 500);
}
}
}