65 lines
2.1 KiB
PHP
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);
|
|
}
|
|
}
|
|
}
|