igengx
This commit is contained in:
@@ -71,26 +71,22 @@ final class AssistantServiceAlert
|
||||
{
|
||||
$actor = AssistantServiceAccess::actor($adminId);
|
||||
$accounts = self::archiveAccounts();
|
||||
$staff = [];
|
||||
foreach (AssistantServiceAccess::assistantRows($adminId, $actor) as $row) {
|
||||
$count = 0;
|
||||
foreach ($accounts as $account) {
|
||||
if ($account['name'] !== '' && $account['name'] === $row['name']) { $count += $account['conversations']; }
|
||||
}
|
||||
$wechat = trim((string) Db::name('admin')->where('id', $row['id'])->value('work_wechat_userid'));
|
||||
foreach ($accounts as $account) {
|
||||
if ($wechat !== '' && $account['external_id'] === $wechat) { $count += $account['conversations']; }
|
||||
}
|
||||
$staff[] = $row + ['conversations' => $count];
|
||||
}
|
||||
$owners = $accounts ? WecomArchiveScope::accountOwners() : [];
|
||||
$staff = AssistantServiceAccess::assistantRows($adminId, $actor);
|
||||
$visible = array_column($staff, 'id');
|
||||
$counts = [];
|
||||
$unmapped = [];
|
||||
if (AssistantServiceAccess::visibleStaffIds($adminId, $actor) === null) {
|
||||
$names = array_column($staff, 'name');
|
||||
$wechatIds = array_map('strval', Db::name('admin')->whereIn('id', array_column($staff, 'id') ?: [0])->column('work_wechat_userid'));
|
||||
foreach ($accounts as $account) {
|
||||
if (!in_array($account['name'], $names, true) && !in_array($account['external_id'], $wechatIds, true)) { $unmapped[] = $account; }
|
||||
$unrestricted = AssistantServiceAccess::visibleStaffIds($adminId, $actor) === null;
|
||||
foreach ($accounts as $account) {
|
||||
$owner = $owners[$account['external_id']] ?? 0;
|
||||
if ($owner && in_array($owner, $visible, true)) {
|
||||
$counts[$owner] = ($counts[$owner] ?? 0) + $account['conversations'];
|
||||
} elseif ($unrestricted) {
|
||||
$unmapped[] = $account;
|
||||
}
|
||||
}
|
||||
foreach ($staff as &$row) { $row['conversations'] = $counts[$row['id']] ?? 0; }
|
||||
unset($row);
|
||||
return ['ready' => self::archiveReady(), 'staff' => $staff, 'accounts' => $unmapped];
|
||||
}
|
||||
|
||||
@@ -117,6 +113,7 @@ final class AssistantServiceAlert
|
||||
try {
|
||||
$actor = AssistantServiceAccess::actor($adminId);
|
||||
$allowed = self::allowedAccounts($p, $adminId, $actor);
|
||||
if (!$allowed) { throw new DomainException('会话不存在或不在当前可见范围'); }
|
||||
$thread = self::archiveMessages((string) $p['conversation_id']);
|
||||
} catch (\PDOException $e) {
|
||||
throw new DomainException('企微会话暂时读不出来,请稍后重试');
|
||||
@@ -147,11 +144,11 @@ final class AssistantServiceAlert
|
||||
if ($row['id'] === $staffId) { $visible = true; break; }
|
||||
}
|
||||
if (!$visible) { throw new DomainException('医助不存在、已停用或不在当前员工范围'); }
|
||||
$name = trim((string) Db::name('admin')->where('id', $staffId)->value('name'));
|
||||
$wechat = trim((string) Db::name('admin')->where('id', $staffId)->value('work_wechat_userid'));
|
||||
$accounts = self::archiveAccounts();
|
||||
$owners = $accounts ? WecomArchiveScope::accountOwners() : [];
|
||||
$allowed = [];
|
||||
foreach (self::archiveAccounts() as $row) {
|
||||
if (($name !== '' && $row['name'] === $name) || ($wechat !== '' && $row['external_id'] === $wechat)) { $allowed[] = $row['external_id']; }
|
||||
foreach ($accounts as $row) {
|
||||
if (($owners[$row['external_id']] ?? 0) === $staffId) { $allowed[] = $row['external_id']; }
|
||||
}
|
||||
return array_values(array_unique($allowed));
|
||||
}
|
||||
@@ -212,7 +209,7 @@ final class AssistantServiceAlert
|
||||
$actor = AssistantServiceAccess::actor($adminId, 'reviewQuality');
|
||||
if (!self::alertReady()) { throw new DomainException('预警表尚未就绪,请先执行模块迁移'); }
|
||||
$row = Db::name(self::TABLE)->where('id', $p['id'])->lock(true)->find();
|
||||
if (!$row || !self::visible($row, $adminId, $actor)) { throw new DomainException('预警不存在或已不在当前范围'); }
|
||||
if (!$row || !self::scoped($adminId, $actor)->where('a.id', (int) $row['id'])->find()) { throw new DomainException('预警不存在或已不在当前范围'); }
|
||||
$now = time();
|
||||
Db::name(self::TABLE)->where('id', (int) $row['id'])->update(['status' => $p['status'], 'reviewer_id' => $adminId,
|
||||
'review_note' => $p['note'], 'reviewed_at' => $now, 'updated_at' => $now]);
|
||||
@@ -243,6 +240,14 @@ final class AssistantServiceAlert
|
||||
$state['source_version'] = 3;
|
||||
$state['next_scan_at'] = 0;
|
||||
}
|
||||
$tenant = WecomArchiveScope::tenant();
|
||||
if (($state['wecom_tenant'] ?? null) !== $tenant) {
|
||||
$state['wecom_tenant'] = $tenant;
|
||||
$state['wecom_cursor'] = '';
|
||||
$state['wording_cursor'] = '';
|
||||
$state['wording_next_at'] = 0;
|
||||
$state['next_scan_at'] = 0;
|
||||
}
|
||||
$wording = $this->analyzeWording($state, $now);
|
||||
$counts['created'] += $wording['created'];
|
||||
if ((int) ($state['next_scan_at'] ?? 0) > $now && ($state['state'] ?? '') !== 'error') {
|
||||
@@ -329,6 +334,7 @@ final class AssistantServiceAlert
|
||||
/** @param array<string,mixed> $state @param array<int,string> $speakers @param array{scanned:int,created:int} $counts */
|
||||
private function scanWecom(array &$state, array $speakers, int $now, int $limit, array &$counts): void
|
||||
{
|
||||
if (WecomArchiveScope::tenant() === '') { return; }
|
||||
$cursor = (string) ($state['wecom_cursor'] ?? '');
|
||||
for ($round = 0; $round < 100; $round++) {
|
||||
$page = $this->wecomPage($cursor, $limit);
|
||||
@@ -407,7 +413,7 @@ final class AssistantServiceAlert
|
||||
if ($staffId <= 0 && $staffName === '') { return; }
|
||||
if ($patientId <= 0 && $patientName === '') { return; }
|
||||
foreach (self::hits($text) as $hit) {
|
||||
$identity = ['channel' => $channel, 'source_id' => $sourceId, 'source_key' => $sourceKey, 'rule_code' => $hit['code']];
|
||||
$identity = ['channel' => $channel, 'archive_tenant_id' => $channel === 'wecom' ? WecomArchiveScope::tenant() : '', 'source_id' => $sourceId, 'source_key' => $sourceKey, 'rule_code' => $hit['code']];
|
||||
if (Db::name(self::TABLE)->where($identity)->find()) { continue; }
|
||||
Db::name(self::TABLE)->insert($identity + ['staff_admin_id' => max(0, $staffId), 'patient_id' => max(0, $patientId),
|
||||
'diagnosis_id' => $diagnosisId, 'external_staff' => $staffName, 'external_patient' => $patientName,
|
||||
@@ -494,15 +500,27 @@ final class AssistantServiceAlert
|
||||
private static function scoped(int $adminId, array $actor)
|
||||
{
|
||||
$sql = AssistantServiceAccess::patientQuery($adminId, $actor)->distinct(true)->field('d.patient_id')->buildSql();
|
||||
$query = Db::name(self::TABLE)->alias('a');
|
||||
if (AssistantServiceAccess::visibleStaffIds($adminId, $actor) !== null) {
|
||||
return $query->whereRaw('a.patient_id IN ' . $sql);
|
||||
}
|
||||
return $query->where(static function ($nested) use ($sql): void {
|
||||
$nested->whereRaw('a.patient_id IN ' . $sql)->whereOr(static function ($open): void {
|
||||
$open->where('a.patient_id', 0)->where('a.channel', 'wecom');
|
||||
$tenant = WecomArchiveScope::tenant();
|
||||
$visible = AssistantServiceAccess::visibleStaffIds($adminId, $actor);
|
||||
$query = Db::name(self::TABLE)->alias('a')->where(static function ($scope) use ($tenant, $visible): void {
|
||||
$scope->where('a.channel', '<>', 'wecom');
|
||||
if ($tenant === '') { return; }
|
||||
$scope->whereOr(static function ($wecom) use ($tenant, $visible): void {
|
||||
$wecom->where('a.channel', 'wecom')->where('a.archive_tenant_id', $tenant);
|
||||
if ($visible !== null) {
|
||||
if (!$visible) { $wecom->whereRaw('1=0'); }
|
||||
else { $wecom->whereIn('a.staff_admin_id', $visible); }
|
||||
}
|
||||
});
|
||||
});
|
||||
return $query->where(static function ($patients) use ($sql, $visible): void {
|
||||
$patients->whereRaw('a.patient_id IN ' . $sql);
|
||||
if ($visible === null) {
|
||||
$patients->whereOr(static function ($open): void {
|
||||
$open->where('a.patient_id', 0)->where('a.channel', 'wecom');
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static function count($query, array $where): int
|
||||
@@ -580,14 +598,6 @@ final class AssistantServiceAlert
|
||||
return $names;
|
||||
}
|
||||
|
||||
private static function visible(array $row, int $adminId, array $actor): bool
|
||||
{
|
||||
if ((int) $row['patient_id'] === 0 && (string) $row['channel'] === 'wecom') {
|
||||
return AssistantServiceAccess::visibleStaffIds($adminId, $actor) === null;
|
||||
}
|
||||
return (bool) AssistantServiceAccess::patientQuery($adminId, $actor)->where('d.patient_id', (int) $row['patient_id'])->find();
|
||||
}
|
||||
|
||||
private static function label(string $code): string
|
||||
{
|
||||
foreach (self::RULES as $rule) { if ($rule['code'] === $code) { return $rule['label']; } }
|
||||
@@ -596,7 +606,7 @@ final class AssistantServiceAlert
|
||||
|
||||
private static function alertReady(): bool
|
||||
{
|
||||
return self::readable(self::TABLE, 'id,staff_admin_id,patient_id,diagnosis_id,channel,source_id,source_key,external_staff,external_patient,rule_code,rule_version,severity,snippet,status,reviewer_id,review_note,reviewed_at,occurred_at,created_at,updated_at');
|
||||
return self::readable(self::TABLE, 'id,staff_admin_id,patient_id,diagnosis_id,channel,source_id,source_key,archive_tenant_id,external_staff,external_patient,rule_code,rule_version,severity,snippet,status,reviewer_id,review_note,reviewed_at,occurred_at,created_at,updated_at');
|
||||
}
|
||||
|
||||
private static function imReady(): bool
|
||||
@@ -628,6 +638,9 @@ final class AssistantServiceAlert
|
||||
if (WecomArchiveSqlite::configured()) { return ['status' => 'available', 'note' => '本地企微归档只记员工发出的文字;对不上患者时仍保留会话名']; }
|
||||
if (WecomArchiveHttp::configured()) { return ['status' => 'available', 'note' => '线上企微归档只记员工发出的文字;对不上患者时仍保留会话名']; }
|
||||
$base = rtrim((string) config('assistant_service.wecom_archive_base_url', ''), '/');
|
||||
if (($base !== '' || (string) config('assistant_service.wecom_archive_sqlite', '') !== '') && WecomArchiveScope::tenant() === '') {
|
||||
return ['status' => 'not_connected', 'note' => '请配置本业务实例对应的归档租户'];
|
||||
}
|
||||
if ($base !== '') { return ['status' => 'not_connected', 'note' => '企微聊天归档地址已填写,还缺连接令牌']; }
|
||||
return ['status' => 'not_connected', 'note' => '企微聊天归档尚未接入'];
|
||||
}
|
||||
@@ -739,7 +752,7 @@ final class AssistantServiceAlert
|
||||
if ($staffId <= 0 && $staffName === '') { return 0; }
|
||||
if ($patientId <= 0 && $patientName === '') { return 0; }
|
||||
if ($staffId > 0 && !isset(self::speakers()[$staffId])) { return 0; }
|
||||
$identity = ['channel' => 'wecom', 'source_id' => 0, 'source_key' => $sourceKey, 'rule_code' => 'inappropriate_wording'];
|
||||
$identity = ['channel' => 'wecom', 'archive_tenant_id' => WecomArchiveScope::tenant(), 'source_id' => 0, 'source_key' => $sourceKey, 'rule_code' => 'inappropriate_wording'];
|
||||
if (Db::name(self::TABLE)->where($identity)->find()) { return 0; }
|
||||
$occurred = (int) ($item['sent_at_epoch'] ?? 0);
|
||||
if ($occurred <= 0) { $occurred = self::epoch((string) ($item['sent_at'] ?? '')); }
|
||||
@@ -769,6 +782,7 @@ final class AssistantServiceAlert
|
||||
try {
|
||||
if (!flock($handle, LOCK_EX)) { throw new RuntimeException('ALERT_STORAGE'); }
|
||||
$json = json_encode(['cursor' => (int) ($state['cursor'] ?? 0), 'call_cursor' => (int) ($state['call_cursor'] ?? 0),
|
||||
'wecom_tenant' => (string) ($state['wecom_tenant'] ?? ''),
|
||||
'wecom_cursor' => substr((string) ($state['wecom_cursor'] ?? ''), 0, 200),
|
||||
'wording_cursor' => substr((string) ($state['wording_cursor'] ?? ''), 0, 200),
|
||||
'wording_next_at' => (int) ($state['wording_next_at'] ?? 0),
|
||||
|
||||
Reference in New Issue
Block a user