CaiWuXiTong/app/Support/CourierBills/WorkPlatformCourierRobots.php

93 lines
4.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Support\CourierBills;
use RuntimeException;
/** 按财务分组名称匹配工作平台的快递名称或群名;组合分组任一名称命中即可。 */
final class WorkPlatformCourierRobots
{
/** 负责人明确指定的对应关系,目标必须命中工作平台完整业务名称。 */
private const DESIGNATED_NAMES = [
'北京韵达-乐鑫达' => '北京大件韵达',
'樵舍中通' => '樵舍中通',
'樵舍中通-上楼' => '樵舍中通',
'樵舍中通上楼' => '樵舍中通',
'江西中通快运' => '樵舍中通',
'江西新建中通' => '樵舍中通',
'山东中通快运' => '樵舍中通',
'南昌樵舍仓仓储费' => '樵舍中通',
'中通快运专线' => '常州中通快运',
'宿迁中通快运' => '沭阳中通快运',
'山东顺丰干配' => '顺德德邦',
'河北中通-江三' => '顺德德邦',
'苏北顺丰干配大件' => '顺德德邦',
];
public function match(array $groups, array $rows): array
{
$platformRows = [];
foreach ($rows as $row) {
if (! is_array($row) || ! isset($row['Id'])) {
throw new RuntimeException('机器人配置缺少有效的记录编号。');
}
if (trim((string) ($row['forModel'] ?? '')) !== '快递应付') continue;
$platformRows[] = $row;
}
$robots = $unmatched = $ambiguous = [];
foreach ($groups as $group) {
$name = (string) $group->name;
$designatedName = self::DESIGNATED_NAMES[$name] ?? null;
$candidates = [];
$bestScore = 0;
$names = preg_split('/[|++、,,;;]+/u', $name, -1, PREG_SPLIT_NO_EMPTY) ?: [];
foreach ($platformRows as $row) {
$keys = array_filter(array_map('trim', explode('|', (string) ($row['businessKey'] ?? ''))));
if ($designatedName !== null) {
if (in_array($designatedName, $keys, true)) $candidates[(string) $row['Id']] = $row;
continue;
}
$platformGroup = (string) ($row['groupname'] ?? '');
$score = 0;
foreach ($names as $part) {
$part = trim($part);
if ($part === '') continue;
// 负责人明确这两家独立,缺少本家配置时也不能借用另一家。
if (in_array($part, ['常州德邦', '常州德邦逆向'], true)) {
if (in_array($part, $keys, true)) $score = max($score, 100000 + mb_strlen($part));
continue;
}
if (str_contains($platformGroup, $part)) $score = max($score, mb_strlen($part));
foreach ($keys as $key) {
if ($part === $key) {
$score = max($score, 100000 + mb_strlen($key));
} elseif (str_contains($part, $key) || str_contains($key, $part)) {
$score = max($score, min(mb_strlen($part), mb_strlen($key)));
}
}
}
if ($score > $bestScore) {
$candidates = [];
$bestScore = $score;
}
if ($score > 0 && $score === $bestScore) $candidates[(string) $row['Id']] = $row;
}
if ($candidates === []) { $unmatched[] = $name; continue; }
if (count($candidates) !== 1) { $ambiguous[] = $name; continue; }
$row = reset($candidates);
$webhook = trim((string) ($row['webAccesstoken'] ?? ''));
if ($webhook === '') { $unmatched[] = $name; continue; }
if (! str_starts_with($webhook, 'https://')) {
$webhook = 'https://oapi.dingtalk.com/robot/send?access_token=' . rawurlencode($webhook);
}
$robots[$name] = [
'webhook' => $webhook, 'secret' => trim((string) ($row['Secret'] ?? '')),
'group_name' => trim(preg_replace('/[\x00-\x1F\x7F]/u', '', (string) ($row['groupname'] ?? '')) ?? ''),
'source_id' => (string) $row['Id'], 'source_updated_at' => (string) ($row['modifytime'] ?? ''),
];
}
return compact('robots', 'unmatched', 'ambiguous');
}
}