fix sync
This commit is contained in:
@@ -51,11 +51,11 @@ class TaskServer extends Server
|
|||||||
// todo 临时测试,回头封装到类里调用,每个任务一个类
|
// todo 临时测试,回头封装到类里调用,每个任务一个类
|
||||||
Timer::add(5, function () use ($current_worker_id, $process_count_for_status_0) {
|
Timer::add(5, function () use ($current_worker_id, $process_count_for_status_0) {
|
||||||
if ($current_worker_id == self::PROCESS_COUNT - 1) {
|
if ($current_worker_id == self::PROCESS_COUNT - 1) {
|
||||||
// TODO 专门检查添加后的情况,是否通过
|
// TODO 专门检查添加后的情况,是否通过 ; 使用独立的进程和定时器处理,周期改为1min
|
||||||
|
|
||||||
$tasks = Db::name('task_customer')
|
$tasks = Db::name('task_customer')
|
||||||
->where('status', 1)
|
->where('status', 1)
|
||||||
->limit(100)
|
->limit(50)
|
||||||
->select();
|
->select();
|
||||||
if ($tasks) {
|
if ($tasks) {
|
||||||
// TODO 检查是否添加成功,是否需要再次发送,然后,更新状态为2或3 ...
|
// TODO 检查是否添加成功,是否需要再次发送,然后,更新状态为2或3 ...
|
||||||
@@ -68,16 +68,55 @@ class TaskServer extends Server
|
|||||||
$tasks = Db::name('task_customer')
|
$tasks = Db::name('task_customer')
|
||||||
->where('status', 0)
|
->where('status', 0)
|
||||||
->whereRaw("id % $process_count_for_status_0 = {$current_worker_id}")
|
->whereRaw("id % $process_count_for_status_0 = {$current_worker_id}")
|
||||||
->limit(100)
|
->limit(50)
|
||||||
->select();
|
->select();
|
||||||
if ($tasks) {
|
if ($tasks) {
|
||||||
// ... 更新状态为1,然后处理,再更新为2或3 ...
|
// ... 更新状态为1,然后处理,~~再更新为2或3~~ ...
|
||||||
|
foreach ($tasks as $task) {
|
||||||
|
|
||||||
|
// 查找 ck_customer_acquisition_task 表,是否存在该任务;然后拿到任务配置详情;童谣的任务id需要缓存信息不要重复查询
|
||||||
|
$task_id = $task['task_id'];
|
||||||
|
// 先读取缓存
|
||||||
|
$task_info = cache('task_info_' . $task_id);
|
||||||
|
if (!$task_info) {
|
||||||
|
$task_info = Db::name('customer_acquisition_task')
|
||||||
|
->where('id', $task_id)
|
||||||
|
->find();
|
||||||
|
|
||||||
|
if ($task_info) {
|
||||||
|
cache('task_info_' . $task['task_id'], $task_info);
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($task_info['status']) || empty($task_info['reqConf'])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 先更新状态为1
|
||||||
|
Db::name('task_customer')
|
||||||
|
->where('id', $task['id'])
|
||||||
|
->update(['status' => 1]);
|
||||||
|
|
||||||
|
|
||||||
|
// todo 基于 $task['phone'] 和 $task_info['reqConf'] 进行处理
|
||||||
|
//通过conpany_id拿到设备/微信,判断这些微信的状态(是否在线,是否能加人)
|
||||||
|
|
||||||
|
|
||||||
|
// ~~// 更新状态为1 || 4~~
|
||||||
|
// // 更新状态为2
|
||||||
|
// Db::name('task_customer')
|
||||||
|
// ->where('id', $task['id'])
|
||||||
|
// ->update(['status' => 1]);
|
||||||
|
|
||||||
|
// 之后可能会更新为~~失败~~或者不处理 -- 失败一定是另一个进程/定时器在检查的
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -141,6 +141,70 @@ class Adapter implements WeChatServiceInterface
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// checkIfIsWeChatFriend
|
||||||
|
public function checkIfIsWeChatFriendByPhone(string $wxId, string $phone): bool
|
||||||
|
{
|
||||||
|
if (empty($wxId) || empty($phone)) {
|
||||||
|
// Avoid queries with empty essential parameters.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// The SQL hint provided is:
|
||||||
|
// SELECT ownerWechatId, phone, passTime, createTime
|
||||||
|
// FROM `s2_wechat_friend`
|
||||||
|
// WHERE ownerWechatId = '您的微信ID' -- Corresponds to $wxId
|
||||||
|
// AND phone LIKE 'phone%' -- Corresponds to $phone . '%'
|
||||||
|
// ORDER BY createTime DESC;
|
||||||
|
|
||||||
|
// $friendRecord = Db::table('s2_wechat_friend')
|
||||||
|
// ->where('ownerWechatId', $wxId)
|
||||||
|
// ->where('phone', 'like', $phone . '%') // Match phone numbers starting with $phone
|
||||||
|
// ->order('createTime', 'desc') // Order by creation time as hinted
|
||||||
|
// ->find(); // Fetches the first matching record or null
|
||||||
|
$friendRecord = Db::table('s2_wechat_friend')
|
||||||
|
->where('ownerWechatId', $wxId)
|
||||||
|
->where('phone', 'like', $phone . '%') // Match phone numbers starting with $phone
|
||||||
|
->order('createTime', 'desc') // Order by creation time as hinted
|
||||||
|
// ->column('id');
|
||||||
|
->value('id');
|
||||||
|
|
||||||
|
// If a record is found, $friendRecord will not be empty.
|
||||||
|
return !empty($friendRecord);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
// Log the exception for diagnostics.
|
||||||
|
Log::error("Error in checkIfIsWeChatFriendByPhone (wxId: {$wxId}, phone: {$phone}): " . $e->getMessage());
|
||||||
|
// Return false in case of an error, indicating not a friend or unable to determine.
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getWeChatFriendPassTimeByPhone
|
||||||
|
public function getWeChatFriendPassTimeByPhone(string $wxId, string $phone): int
|
||||||
|
{
|
||||||
|
if (empty($wxId) || empty($phone)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// $passTime = Db::table('s2_wechat_friend')
|
||||||
|
// ->where('ownerWechatId', $wxId)
|
||||||
|
// ->where('phone', 'like', $phone . '%') // Match phone numbers starting with $phone
|
||||||
|
// ->order('createTime', 'desc') // Order by creation time as hinted
|
||||||
|
// ->value('passTime');
|
||||||
|
$record = Db::table('s2_wechat_friend')
|
||||||
|
->where('ownerWechatId', $wxId)
|
||||||
|
->where('phone', 'like', $phone . '%') // Match phone numbers starting with $phone
|
||||||
|
->field('id,createTime,passTime') // Order by creation time as hinted
|
||||||
|
->find();
|
||||||
|
|
||||||
|
return $record['passTime'] ?? $record['createTime'] ?? 0;
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
Log::error("Error in getWeChatFriendPassTimeByPhone (wxId: {$wxId}, phone: {$phone}): " . $e->getMessage());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/* todo 以上方法待实现,基于/参考 application/api/controller/WebSocketController.php 去实现 */
|
/* todo 以上方法待实现,基于/参考 application/api/controller/WebSocketController.php 去实现 */
|
||||||
|
|
||||||
@@ -274,7 +338,7 @@ class Adapter implements WeChatServiceInterface
|
|||||||
|
|
||||||
foreach ($cursor as $item) {
|
foreach ($cursor as $item) {
|
||||||
|
|
||||||
if (empty($item['deviceId']) || empty($item['wechatId'])) {
|
if (empty($item['deviceId']) || empty($item['wechatId']) || empty($item['companyId'])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -282,6 +346,7 @@ class Adapter implements WeChatServiceInterface
|
|||||||
$exists = Db::table('ck_device_wechat_login')
|
$exists = Db::table('ck_device_wechat_login')
|
||||||
->where('deviceId', $item['deviceId'])
|
->where('deviceId', $item['deviceId'])
|
||||||
->where('wechatId', $item['wechatId'])
|
->where('wechatId', $item['wechatId'])
|
||||||
|
->where('companyId', $item['companyId'])
|
||||||
// ->where('createTime', $item['createTime'])
|
// ->where('createTime', $item['createTime'])
|
||||||
->find();
|
->find();
|
||||||
|
|
||||||
@@ -289,6 +354,7 @@ class Adapter implements WeChatServiceInterface
|
|||||||
Db::table('ck_device_wechat_login')
|
Db::table('ck_device_wechat_login')
|
||||||
->where('deviceId', $item['deviceId'])
|
->where('deviceId', $item['deviceId'])
|
||||||
->where('wechatId', $item['wechatId'])
|
->where('wechatId', $item['wechatId'])
|
||||||
|
->where('companyId', $item['companyId'])
|
||||||
->update(['alive' => $item['alive'], 'updateTime' => $item['updateTime']]);
|
->update(['alive' => $item['alive'], 'updateTime' => $item['updateTime']]);
|
||||||
} else {
|
} else {
|
||||||
$item['createTime'] = $item['updateTime'];
|
$item['createTime'] = $item['updateTime'];
|
||||||
@@ -602,7 +668,7 @@ class Adapter implements WeChatServiceInterface
|
|||||||
`updateTime` = VALUES(`updateTime`),
|
`updateTime` = VALUES(`updateTime`),
|
||||||
`deleteTime` = VALUES(`deleteTime`),
|
`deleteTime` = VALUES(`deleteTime`),
|
||||||
`companyId` = VALUES(`companyId`)";
|
`companyId` = VALUES(`companyId`)";
|
||||||
|
|
||||||
$affected = Db::execute($sql);
|
$affected = Db::execute($sql);
|
||||||
return $affected;
|
return $affected;
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user