全局配置服务功能提交

This commit is contained in:
wong
2026-01-15 14:24:25 +08:00
parent cec64b889b
commit 430b8eee91
11 changed files with 443 additions and 13 deletions

View File

@@ -49,7 +49,12 @@ class GetAddResultedV1Controller extends BaseController
$deviceIds = $this->getAllDevicesIdWithInCompany($companyId) ?: [0];
// 从 s2_device 导入数据。
$this->getNewDeviceFromS2_device($deviceIds, $companyId);
$newDeviceIds = $this->getNewDeviceFromS2_device($deviceIds, $companyId);
// 如果有新设备,自动加入到全局配置中
if (!empty($newDeviceIds)) {
$this->addDevicesToGlobalConfigs($newDeviceIds, $companyId);
}
}
/**
@@ -57,12 +62,23 @@ class GetAddResultedV1Controller extends BaseController
*
* @param array $ids
* @param int $companyId
* @return void
* @return array 返回新添加的设备ID数组
*/
protected function getNewDeviceFromS2_device(array $ids, int $companyId): void
protected function getNewDeviceFromS2_device(array $ids, int $companyId): array
{
$ids = implode(',', $ids);
// 先查询要插入的新设备ID
$newDeviceIds = Db::query("SELECT d.id
FROM s2_device d
JOIN s2_company_account a ON d.currentAccountId = a.id
WHERE isDeleted = 0 AND deletedAndStop = 0 AND d.id NOT IN ({$ids}) AND a.departmentId = {$companyId}");
$newDeviceIds = array_column($newDeviceIds, 'id');
if (empty($newDeviceIds)) {
return [];
}
$sql = "INSERT INTO ck_device(`id`, `imei`, `model`, phone, operatingSystem, memo, alive, brand, rooted, xPosed, softwareVersion, extra, createTime, updateTime, deleteTime, companyId)
SELECT
d.id, d.imei, d.model, d.phone, d.operatingSystem, d.memo, d.alive, d.brand, d.rooted, d.xPosed, d.softwareVersion, d.extra, d.createTime, d.lastUpdateTime, d.deleteTime, a.departmentId AS companyId
@@ -86,6 +102,8 @@ class GetAddResultedV1Controller extends BaseController
companyId = VALUES(companyId)";
Db::query($sql);
return $newDeviceIds;
}
/**
@@ -162,4 +180,144 @@ class GetAddResultedV1Controller extends BaseController
]
);
}
/**
* 将新设备自动加入到全局配置中planType=0的计划和工作台
*
* @param array $newDeviceIds 新添加的设备ID数组
* @param int $companyId 公司ID
* @return void
*/
protected function addDevicesToGlobalConfigs(array $newDeviceIds, int $companyId): void
{
try {
// 1. 更新全局计划(场景获客)的设备组
$this->addDevicesToGlobalPlans($newDeviceIds, $companyId);
// 2. 更新全局工作台的设备组
$this->addDevicesToGlobalWorkbenches($newDeviceIds, $companyId);
} catch (\Exception $e) {
// 记录错误但不影响设备添加流程
\think\facade\Log::error('自动添加设备到全局配置失败:' . $e->getMessage(), [
'newDeviceIds' => $newDeviceIds,
'companyId' => $companyId
]);
}
}
/**
* 将新设备加入到全局计划planType=0的设备组
*
* @param array $newDeviceIds 新添加的设备ID数组
* @param int $companyId 公司ID
* @return void
*/
protected function addDevicesToGlobalPlans(array $newDeviceIds, int $companyId): void
{
// 查询所有全局计划planType=0
$plans = Db::name('customer_acquisition_task')
->where('companyId', $companyId)
->where('planType', 0) // 全局计划
->where('deleteTime', 0)
->field('id,reqConf')
->select();
foreach ($plans as $plan) {
$reqConf = json_decode($plan['reqConf'], true) ?: [];
$deviceGroups = isset($reqConf['device']) ? $reqConf['device'] : [];
if (!is_array($deviceGroups)) {
$deviceGroups = [];
}
// 合并新设备ID去重
$deviceGroups = array_unique(array_merge($deviceGroups, $newDeviceIds));
$reqConf['device'] = array_values($deviceGroups); // 重新索引数组
// 更新数据库
Db::name('customer_acquisition_task')
->where('id', $plan['id'])
->update([
'reqConf' => json_encode($reqConf, JSON_UNESCAPED_UNICODE),
'updateTime' => time()
]);
}
}
/**
* 将新设备加入到全局工作台planType=0的设备组
*
* @param array $newDeviceIds 新添加的设备ID数组
* @param int $companyId 公司ID
* @return void
*/
protected function addDevicesToGlobalWorkbenches(array $newDeviceIds, int $companyId): void
{
// 查询所有全局工作台planType=0
$workbenches = Db::name('workbench')
->where('companyId', $companyId)
->where('planType', 0) // 全局工作台
->where('isDel', 0)
->field('id,type')
->select();
foreach ($workbenches as $workbench) {
// 根据工作台类型更新对应的配置表
$this->updateWorkbenchDevices($workbench['id'], $workbench['type'], $newDeviceIds);
}
}
/**
* 更新工作台的设备组
*
* @param int $workbenchId 工作台ID
* @param int $type 工作台类型
* @param array $newDeviceIds 新设备ID数组
* @return void
*/
protected function updateWorkbenchDevices(int $workbenchId, int $type, array $newDeviceIds): void
{
$configTableMap = [
1 => 'workbench_auto_like', // 自动点赞
2 => 'workbench_moments_sync', // 朋友圈同步
3 => 'workbench_group_push', // 群消息推送
4 => 'workbench_group_create', // 自动建群
5 => 'workbench_traffic_config', // 流量分发
6 => 'workbench_import_contact', // 通讯录导入
7 => 'workbench_group_welcome', // 入群欢迎语
];
$tableName = $configTableMap[$type] ?? null;
if (empty($tableName)) {
return;
}
// 查询配置
$config = Db::name($tableName)
->where('workbenchId', $workbenchId)
->field('id,devices')
->find();
if (empty($config)) {
return;
}
// 解析设备组
$deviceGroups = json_decode($config['devices'], true) ?: [];
if (!is_array($deviceGroups)) {
$deviceGroups = [];
}
// 合并新设备ID去重
$deviceGroups = array_unique(array_merge($deviceGroups, $newDeviceIds));
$deviceGroups = array_values($deviceGroups); // 重新索引数组
// 更新数据库
Db::name($tableName)
->where('id', $config['id'])
->update([
'devices' => json_encode($deviceGroups, JSON_UNESCAPED_UNICODE),
'updateTime' => time()
]);
}
}