CaiWuXiTong/app/Support/CourierBills/WorkPlatformCourierRobots.php

73 lines
3.5 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
{
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;
// 负责人指定北京乐鑫达使用北京大件韵达的现有群机器人。
$matchName = $name === '北京韵达-乐鑫达' ? '北京大件韵达' : $name;
$candidates = [];
$bestScore = 0;
$names = preg_split('/[|++、,,;;]+/u', $matchName, -1, PREG_SPLIT_NO_EMPTY) ?: [];
foreach ($platformRows as $row) {
$keys = array_filter(array_map('trim', explode('|', (string) ($row['businessKey'] ?? ''))));
$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');
}
}