更新
:i# Please enter the commit message for your changes. Lines starting
This commit is contained in:
@@ -11,6 +11,18 @@ use think\db\Query;
|
||||
|
||||
class MediaChannelService
|
||||
{
|
||||
private const ACTIVE_ROWS_CACHE_TTL_SECONDS = 1.0;
|
||||
private const SCAN_DUPLICATE_UPDATE_FIELDS = [
|
||||
'source_group_name',
|
||||
'last_seen_time',
|
||||
'update_time',
|
||||
];
|
||||
|
||||
/** @var array<int, array<string, mixed>>|null */
|
||||
private static ?array $activeChannelRowsCache = null;
|
||||
|
||||
private static float $activeChannelRowsCachedAt = 0.0;
|
||||
|
||||
/**
|
||||
* 与业绩看板「渠道来源」相同的分组(按 source_group_name),不含客户数统计。
|
||||
*
|
||||
@@ -21,11 +33,7 @@ class MediaChannelService
|
||||
*/
|
||||
public static function getOptionGroups(): array
|
||||
{
|
||||
$rows = QywxMediaChannel::where('status', 1)
|
||||
->field('channel_code, channel_name, source_group_name')
|
||||
->order('source_group_name asc, id asc')
|
||||
->select()
|
||||
->toArray();
|
||||
$rows = self::getActiveChannelRows();
|
||||
|
||||
if ($rows === []) {
|
||||
return [];
|
||||
@@ -60,11 +68,17 @@ class MediaChannelService
|
||||
*/
|
||||
public static function getOptions(): array
|
||||
{
|
||||
$rows = QywxMediaChannel::where('status', 1)
|
||||
->field('channel_code, channel_name')
|
||||
->order('channel_name asc, id asc')
|
||||
->select()
|
||||
->toArray();
|
||||
$rows = self::getActiveChannelRows();
|
||||
usort($rows, static function (array $a, array $b): int {
|
||||
$nameCompare = strnatcasecmp(
|
||||
(string) ($a['channel_name'] ?? ''),
|
||||
(string) ($b['channel_name'] ?? '')
|
||||
);
|
||||
|
||||
return $nameCompare !== 0
|
||||
? $nameCompare
|
||||
: strcmp((string) ($a['channel_code'] ?? ''), (string) ($b['channel_code'] ?? ''));
|
||||
});
|
||||
|
||||
return array_map(static fn (array $row): array => [
|
||||
'code' => (string) ($row['channel_code'] ?? ''),
|
||||
@@ -74,9 +88,9 @@ class MediaChannelService
|
||||
|
||||
public static function getDefaultCode(): string
|
||||
{
|
||||
return (string) QywxMediaChannel::where('status', 1)
|
||||
->order('id asc')
|
||||
->value('channel_code');
|
||||
$rows = self::getActiveChannelRows();
|
||||
|
||||
return (string) ($rows[0]['channel_code'] ?? '');
|
||||
}
|
||||
|
||||
public static function isValidCode(string $channelCode): bool
|
||||
@@ -86,9 +100,7 @@ class MediaChannelService
|
||||
return false;
|
||||
}
|
||||
|
||||
return QywxMediaChannel::where('status', 1)
|
||||
->where('channel_code', $channelCode)
|
||||
->count() > 0;
|
||||
return self::getChannelByCode($channelCode) !== null;
|
||||
}
|
||||
|
||||
public static function normalizeStatsCode(string $channelCode): string
|
||||
@@ -106,11 +118,31 @@ class MediaChannelService
|
||||
return null;
|
||||
}
|
||||
|
||||
$row = QywxMediaChannel::where('status', 1)
|
||||
->where('channel_code', $channelCode)
|
||||
->find();
|
||||
$model = QywxMediaChannel::where('channel_code', $channelCode)->find();
|
||||
|
||||
return $row ? $row->toArray() : null;
|
||||
if ($model !== null) {
|
||||
$row = $model->toArray();
|
||||
if ((int) ($row['status'] ?? 0) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$tagId = trim((string) ($row['source_tag_id'] ?? ''));
|
||||
if ($tagId === '') {
|
||||
return $row;
|
||||
}
|
||||
|
||||
$merged = self::mergeConfiguredChannelsWithTags([$row], self::loadCurrentTagRows([$tagId]));
|
||||
|
||||
return $merged[0] ?? $row;
|
||||
}
|
||||
|
||||
foreach (self::getActiveChannelRows() as $row) {
|
||||
if ((string) ($row['channel_code'] ?? '') === $channelCode) {
|
||||
return $row;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static function getNameByCode(string $channelCode): string
|
||||
@@ -134,6 +166,8 @@ class MediaChannelService
|
||||
$names = array_values(array_unique(array_filter([
|
||||
trim((string) ($channel['channel_name'] ?? '')),
|
||||
trim((string) ($channel['source_tag_name'] ?? '')),
|
||||
trim((string) ($channel['legacy_channel_name'] ?? '')),
|
||||
trim((string) ($channel['legacy_source_tag_name'] ?? '')),
|
||||
])));
|
||||
|
||||
if ($names === []) {
|
||||
@@ -233,6 +267,7 @@ class MediaChannelService
|
||||
|
||||
while (true) {
|
||||
$rows = QywxExternalContact::where('id', '>', $lastId)
|
||||
->whereNull('delete_time')
|
||||
->field('id, follow_users')
|
||||
->order('id asc')
|
||||
->limit($batchSize)
|
||||
@@ -276,20 +311,17 @@ class MediaChannelService
|
||||
'update_time' => $now,
|
||||
];
|
||||
|
||||
Db::name('qywx_media_channel')->duplicate([
|
||||
'channel_name',
|
||||
'source_tag_id',
|
||||
'source_tag_name',
|
||||
'source_group_name',
|
||||
'status',
|
||||
'last_seen_time',
|
||||
'update_time',
|
||||
])->insert($rowData);
|
||||
Db::name('qywx_media_channel')
|
||||
->duplicate(self::SCAN_DUPLICATE_UPDATE_FIELDS)
|
||||
->insert($rowData);
|
||||
$upserted++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self::$activeChannelRowsCache = null;
|
||||
self::$activeChannelRowsCachedAt = 0.0;
|
||||
|
||||
return [
|
||||
'scanned_contacts' => $scannedContacts,
|
||||
'discovered_tags' => count($discoveredTags),
|
||||
@@ -297,6 +329,191 @@ class MediaChannelService
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Combine the persistent channel registry with the latest tag snapshots in
|
||||
* the normalized relation table. The registry keeps stable channel codes
|
||||
* and historical name-only channels; relation rows supply newly discovered
|
||||
* tags and names for tags that were renamed in WeCom.
|
||||
*
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
private static function getActiveChannelRows(): array
|
||||
{
|
||||
$now = microtime(true);
|
||||
if (self::$activeChannelRowsCache !== null
|
||||
&& ($now - self::$activeChannelRowsCachedAt) < self::ACTIVE_ROWS_CACHE_TTL_SECONDS) {
|
||||
return self::$activeChannelRowsCache;
|
||||
}
|
||||
|
||||
$configuredRows = QywxMediaChannel::field(
|
||||
'id, channel_code, channel_name, source_tag_id, source_tag_name, source_group_name, '
|
||||
. 'tag_uniq_key, status, last_seen_time, create_time, update_time'
|
||||
)
|
||||
->order('id asc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$rows = self::mergeConfiguredChannelsWithTags($configuredRows, self::loadCurrentTagRows());
|
||||
self::$activeChannelRowsCache = $rows;
|
||||
self::$activeChannelRowsCachedAt = microtime(true);
|
||||
|
||||
return $rows;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[]|null $tagIds null loads all current tags
|
||||
* @return array<int, array{source_tag_id: string, source_tag_name: string, source_group_name: string}>
|
||||
*/
|
||||
private static function loadCurrentTagRows(?array $tagIds = null): array
|
||||
{
|
||||
$bindings = [];
|
||||
$tagFilter = '';
|
||||
if ($tagIds !== null) {
|
||||
$tagIds = array_values(array_unique(array_filter(array_map(
|
||||
static fn ($tagId): string => trim((string) $tagId),
|
||||
$tagIds
|
||||
), static fn (string $tagId): bool => $tagId !== '')));
|
||||
if ($tagIds === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$tagFilter = ' AND newest_tag.tag_id IN (' . implode(', ', array_fill(0, count($tagIds), '?')) . ')';
|
||||
$bindings = $tagIds;
|
||||
}
|
||||
|
||||
$tagTable = self::tableWithPrefix('qywx_external_contact_tag');
|
||||
$sql = <<<SQL
|
||||
SELECT latest_tag.tag_id AS source_tag_id,
|
||||
COALESCE(latest_tag.tag_name, '') AS source_tag_name,
|
||||
COALESCE(latest_tag.group_name, '') AS source_group_name
|
||||
FROM {$tagTable} latest_tag
|
||||
INNER JOIN (
|
||||
SELECT latest_time.tag_id, MAX(tag_at_time.id) AS latest_id
|
||||
FROM (
|
||||
SELECT newest_tag.tag_id, MAX(newest_tag.update_time) AS latest_update_time
|
||||
FROM {$tagTable} newest_tag
|
||||
WHERE newest_tag.tag_id <> ''
|
||||
{$tagFilter}
|
||||
GROUP BY newest_tag.tag_id
|
||||
) latest_time
|
||||
INNER JOIN {$tagTable} tag_at_time
|
||||
ON tag_at_time.tag_id = latest_time.tag_id
|
||||
AND tag_at_time.update_time = latest_time.latest_update_time
|
||||
GROUP BY latest_time.tag_id
|
||||
) selected_tag
|
||||
ON selected_tag.latest_id = latest_tag.id
|
||||
ORDER BY latest_tag.tag_id ASC
|
||||
SQL;
|
||||
|
||||
return array_map(static fn (array $row): array => [
|
||||
'source_tag_id' => trim((string) ($row['source_tag_id'] ?? '')),
|
||||
'source_tag_name' => trim((string) ($row['source_tag_name'] ?? '')),
|
||||
'source_group_name' => trim((string) ($row['source_group_name'] ?? '')),
|
||||
], Db::query($sql, $bindings));
|
||||
}
|
||||
|
||||
/**
|
||||
* Disabled configured tags stay disabled. Active configured rows keep their
|
||||
* stable codes, while automatic display names follow the newest tag name.
|
||||
* Tags not yet present in the registry receive the same deterministic code
|
||||
* that the scanner would create.
|
||||
*
|
||||
* @param array<int, array<string, mixed>> $configuredRows
|
||||
* @param array<int, array<string, mixed>> $tagRows
|
||||
* @return array<int, array<string, mixed>>
|
||||
*/
|
||||
private static function mergeConfiguredChannelsWithTags(array $configuredRows, array $tagRows): array
|
||||
{
|
||||
$tagMap = [];
|
||||
foreach ($tagRows as $tagRow) {
|
||||
$tagId = trim((string) ($tagRow['source_tag_id'] ?? $tagRow['tag_id'] ?? ''));
|
||||
if ($tagId === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tagMap[$tagId] = [
|
||||
'source_tag_id' => $tagId,
|
||||
'source_tag_name' => trim((string) ($tagRow['source_tag_name'] ?? $tagRow['tag_name'] ?? '')),
|
||||
'source_group_name' => trim((string) ($tagRow['source_group_name'] ?? $tagRow['group_name'] ?? '')),
|
||||
];
|
||||
}
|
||||
|
||||
$result = [];
|
||||
$configuredTagIds = [];
|
||||
$configuredCodes = [];
|
||||
foreach ($configuredRows as $configuredRow) {
|
||||
$channelCode = trim((string) ($configuredRow['channel_code'] ?? ''));
|
||||
$tagId = trim((string) ($configuredRow['source_tag_id'] ?? ''));
|
||||
if ($channelCode !== '') {
|
||||
$configuredCodes[$channelCode] = true;
|
||||
}
|
||||
if ($tagId !== '') {
|
||||
// A disabled registry row is an explicit opt-out and must not be
|
||||
// reintroduced as a dynamically discovered channel.
|
||||
$configuredTagIds[$tagId] = true;
|
||||
}
|
||||
if ($channelCode === '' || (int) ($configuredRow['status'] ?? 0) !== 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$row = $configuredRow;
|
||||
$currentTag = $tagId !== '' ? ($tagMap[$tagId] ?? null) : null;
|
||||
if ($currentTag !== null) {
|
||||
$oldTagName = trim((string) ($row['source_tag_name'] ?? ''));
|
||||
$oldChannelName = trim((string) ($row['channel_name'] ?? ''));
|
||||
$currentTagName = (string) $currentTag['source_tag_name'];
|
||||
|
||||
if ($currentTagName !== '') {
|
||||
if ($oldTagName !== '' && $oldTagName !== $currentTagName) {
|
||||
$row['legacy_source_tag_name'] = $oldTagName;
|
||||
}
|
||||
|
||||
$isAutomaticName = $oldChannelName === ''
|
||||
|| $oldChannelName === $oldTagName
|
||||
|| $oldChannelName === $tagId;
|
||||
if ($isAutomaticName) {
|
||||
if ($oldChannelName !== '' && $oldChannelName !== $currentTagName) {
|
||||
$row['legacy_channel_name'] = $oldChannelName;
|
||||
}
|
||||
$row['channel_name'] = $currentTagName;
|
||||
}
|
||||
$row['source_tag_name'] = $currentTagName;
|
||||
}
|
||||
$row['source_group_name'] = (string) $currentTag['source_group_name'];
|
||||
}
|
||||
|
||||
$result[] = $row;
|
||||
}
|
||||
|
||||
foreach ($tagMap as $tagId => $tagRow) {
|
||||
if (isset($configuredTagIds[$tagId])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$channelName = (string) ($tagRow['source_tag_name'] ?? '');
|
||||
$channelCode = self::buildChannelCode($tagId, $channelName);
|
||||
if ($channelCode === '' || isset($configuredCodes[$channelCode])) {
|
||||
continue;
|
||||
}
|
||||
$configuredCodes[$channelCode] = true;
|
||||
$result[] = [
|
||||
'id' => 0,
|
||||
'channel_code' => $channelCode,
|
||||
'channel_name' => $channelName !== '' ? $channelName : $tagId,
|
||||
'source_tag_id' => $tagId,
|
||||
'source_tag_name' => $channelName,
|
||||
'source_group_name' => (string) ($tagRow['source_group_name'] ?? ''),
|
||||
'tag_uniq_key' => self::buildTagUniqKey($tagId, $channelName),
|
||||
'status' => 1,
|
||||
'last_seen_time' => 0,
|
||||
'create_time' => 0,
|
||||
'update_time' => 0,
|
||||
];
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $channel
|
||||
* @return string[]
|
||||
|
||||
Reference in New Issue
Block a user