parent
e2572c9a4c
commit
59e0e46b8a
@ -20,6 +20,12 @@ class SupplierDingTalkNotifier
|
||||
|
||||
private const MAX_MESSAGES = 20;
|
||||
|
||||
private const MAX_MESSAGE_BODY_BYTES = 20000;
|
||||
|
||||
private const TARGET_MESSAGE_BODY_BYTES = 18500;
|
||||
|
||||
private const DETAIL_LINES_PER_ROW = 3;
|
||||
|
||||
public function __construct(
|
||||
private readonly SupplierReturnShareLink $returnShareLinks,
|
||||
private readonly SupplierSummaryAICard $summaryAICard,
|
||||
@ -222,16 +228,17 @@ class SupplierDingTalkNotifier
|
||||
}
|
||||
$this->assertWebhook($webhook);
|
||||
|
||||
$chunks = array_chunk(array_values($differences), self::ROWS_PER_MESSAGE);
|
||||
$summary = $this->summarize($differences);
|
||||
$chunks = $this->chunkDifferenceMessages($supplier, $differences, $summary, $filters);
|
||||
if (count($chunks) > self::MAX_MESSAGES) {
|
||||
throw ValidationException::withMessages([
|
||||
'dingtalk_push' => '当前差异超过 ' . (self::ROWS_PER_MESSAGE * self::MAX_MESSAGES)
|
||||
. ' 条,请先缩小月份或对账人范围后再分批推送。',
|
||||
'dingtalk_push' => '当前差异正文需要超过 ' . self::MAX_MESSAGES
|
||||
. ' 条钉钉消息,请缩小月份、对账人或差异范围后再分批推送。',
|
||||
]);
|
||||
}
|
||||
|
||||
$summary = $this->summarize($differences);
|
||||
$total = max(count($chunks), 1);
|
||||
$rowOffset = 0;
|
||||
foreach ($chunks as $index => $chunk) {
|
||||
$title = $supplier . ' · 货款对账差异';
|
||||
$this->send(
|
||||
@ -239,8 +246,9 @@ class SupplierDingTalkNotifier
|
||||
trim((string) ($config['secret'] ?? '')),
|
||||
(bool) ($config['at_all'] ?? false),
|
||||
$title,
|
||||
$this->message($supplier, $chunk, $summary, $filters, $index + 1, $total, $index * self::ROWS_PER_MESSAGE),
|
||||
$this->message($supplier, $chunk, $summary, $filters, $index + 1, $total, $rowOffset),
|
||||
);
|
||||
$rowOffset += count($chunk);
|
||||
}
|
||||
|
||||
$config['last_pushed_at'] = $this->platformTimestamp();
|
||||
@ -959,18 +967,23 @@ class SupplierDingTalkNotifier
|
||||
static fn (mixed $detail): bool => is_array($detail),
|
||||
));
|
||||
if ($differenceDetails !== []) {
|
||||
$detailCount = count($differenceDetails);
|
||||
$lines[] = sprintf(
|
||||
'%d. **%s**',
|
||||
$rowOffset + $index + 1,
|
||||
(string) ($row['type'] ?? '差异'),
|
||||
);
|
||||
$prefix = count($differenceDetails) > 1 ? ' - ' : ' ';
|
||||
foreach ($differenceDetails as $detail) {
|
||||
foreach (array_slice($differenceDetails, 0, self::DETAIL_LINES_PER_ROW) as $detail) {
|
||||
$lines[] = $prefix . $this->differenceDetailLine(
|
||||
$detail,
|
||||
(string) ($row['type'] ?? ''),
|
||||
);
|
||||
}
|
||||
if ($detailCount > self::DETAIL_LINES_PER_ROW) {
|
||||
$lines[] = ' - 其余 ' . ($detailCount - self::DETAIL_LINES_PER_ROW)
|
||||
. ' 项明细请登录厂家对账门户查看';
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
@ -981,10 +994,10 @@ class SupplierDingTalkNotifier
|
||||
$theirsAmount = (float) ($row['theirsAmount'] ?? 0);
|
||||
$mineUnitPrice = (float) ($row['mineUnitPrice'] ?? $row['unitPrice'] ?? 0);
|
||||
$theirsUnitPrice = (float) ($row['theirsUnitPrice'] ?? $row['unitPrice'] ?? 0);
|
||||
$purchaseNo = trim((string) ($row['purchaseNo'] ?? ''));
|
||||
$date = trim((string) ($row['date'] ?? ''));
|
||||
$barcode = trim((string) ($row['barcode'] ?? ''));
|
||||
$supplierBarcode = trim((string) ($row['supplierBarcode'] ?? ''));
|
||||
$purchaseNo = $this->clipText($row['purchaseNo'] ?? '', 100);
|
||||
$date = $this->clipText($row['date'] ?? '', 32);
|
||||
$barcode = $this->clipText($row['barcode'] ?? '', 100);
|
||||
$supplierBarcode = $this->clipText($row['supplierBarcode'] ?? '', 100);
|
||||
|
||||
$lines[] = sprintf(
|
||||
'%d. **%s**|采购单号:%s|日期:%s|商品编码:%s%s',
|
||||
@ -1001,7 +1014,7 @@ class SupplierDingTalkNotifier
|
||||
$lines[] = ' 厂家:数量 ' . $this->number($theirsQty)
|
||||
. '|单价 ¥' . number_format($theirsUnitPrice, 2)
|
||||
. '|金额 ¥' . number_format($theirsAmount, 2);
|
||||
$lines[] = ' 处理:' . (string) ($row['note'] ?? '');
|
||||
$lines[] = ' 处理:' . $this->clipText($row['note'] ?? '', 180);
|
||||
}
|
||||
|
||||
$lines[] = '';
|
||||
@ -1014,9 +1027,9 @@ class SupplierDingTalkNotifier
|
||||
/** @param array<string,mixed> $detail */
|
||||
private function differenceDetailLine(array $detail, string $type): string
|
||||
{
|
||||
$purchaseNo = trim((string) ($detail['purchaseNo'] ?? ''));
|
||||
$expressNo = trim((string) ($detail['expressNo'] ?? ''));
|
||||
$barcode = trim((string) ($detail['barcode'] ?? ''));
|
||||
$purchaseNo = $this->clipText($detail['purchaseNo'] ?? '', 100);
|
||||
$expressNo = $this->clipText($detail['expressNo'] ?? '', 100);
|
||||
$barcode = $this->clipText($detail['barcode'] ?? '', 100);
|
||||
$mineQty = (float) ($detail['mineQty'] ?? 0);
|
||||
$mineUnitPrice = (float) ($detail['mineUnitPrice'] ?? 0);
|
||||
$mineAmount = (float) ($detail['mineAmount'] ?? 0);
|
||||
@ -1024,8 +1037,8 @@ class SupplierDingTalkNotifier
|
||||
$theirsUnitPrice = (float) ($detail['theirsUnitPrice'] ?? 0);
|
||||
$theirsAmount = (float) ($detail['theirsAmount'] ?? 0);
|
||||
$amountDifference = (float) ($detail['amountDifference'] ?? 0);
|
||||
$referenceLabel = trim((string) ($detail['referenceLabel'] ?? ''));
|
||||
$referenceNo = trim((string) ($detail['referenceNo'] ?? ''));
|
||||
$referenceLabel = $this->clipText($detail['referenceLabel'] ?? '', 20);
|
||||
$referenceNo = $this->clipText($detail['referenceNo'] ?? '', 100);
|
||||
if ($referenceLabel === '' || $referenceNo === '') {
|
||||
$useExpress = (int) ($detail['matchedByLayer'] ?? 0) === 4
|
||||
? $expressNo !== ''
|
||||
@ -1066,11 +1079,12 @@ class SupplierDingTalkNotifier
|
||||
$url .= (str_contains($url, '?') ? '&' : '?') . 'timestamp=' . $timestamp . '&sign=' . $sign;
|
||||
}
|
||||
|
||||
$response = Http::timeout(10)->post($url, [
|
||||
'msgtype' => 'markdown',
|
||||
'markdown' => ['title' => $title, 'text' => $text],
|
||||
'at' => ['atMobiles' => [], 'isAtAll' => $atAll],
|
||||
]);
|
||||
$payload = $this->markdownPayload($title, $text, $atAll);
|
||||
if ($this->payloadBytes($payload) > self::MAX_MESSAGE_BODY_BYTES) {
|
||||
throw new RuntimeException('钉钉消息正文超过 20000 字节,已阻止发送。');
|
||||
}
|
||||
|
||||
$response = Http::asJson()->timeout(10)->post($url, $payload);
|
||||
|
||||
if (! $response->successful()) {
|
||||
throw new RuntimeException('钉钉机器人推送返回 HTTP ' . $response->status());
|
||||
@ -1083,6 +1097,138 @@ class SupplierDingTalkNotifier
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按钉钉请求体实际字节数分批,避免主差异条数不多但子明细过多时超限。
|
||||
*
|
||||
* @param list<array<string,mixed>> $differences
|
||||
* @param array{count:int,extra:int,mismatch:int,missing:int,unpriced:int,difference_amount:float} $summary
|
||||
* @param array<string,mixed> $filters
|
||||
* @return list<list<array<string,mixed>>>
|
||||
*/
|
||||
private function chunkDifferenceMessages(
|
||||
string $supplier,
|
||||
array $differences,
|
||||
array $summary,
|
||||
array $filters,
|
||||
): array {
|
||||
$chunks = [];
|
||||
$current = [];
|
||||
$rowOffset = 0;
|
||||
$title = $supplier . ' · 货款对账差异';
|
||||
|
||||
foreach ($differences as $difference) {
|
||||
$candidate = [...$current, $difference];
|
||||
$fits = count($candidate) <= self::ROWS_PER_MESSAGE
|
||||
&& $this->differenceMessageFits(
|
||||
$title,
|
||||
$supplier,
|
||||
$candidate,
|
||||
$summary,
|
||||
$filters,
|
||||
count($chunks) + 1,
|
||||
self::MAX_MESSAGES,
|
||||
$rowOffset,
|
||||
);
|
||||
if ($fits) {
|
||||
$current = $candidate;
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($current !== []) {
|
||||
$chunks[] = $current;
|
||||
$rowOffset += count($current);
|
||||
$current = [];
|
||||
}
|
||||
|
||||
$singleFits = $this->differenceMessageFits(
|
||||
$title,
|
||||
$supplier,
|
||||
[$difference],
|
||||
$summary,
|
||||
$filters,
|
||||
count($chunks) + 1,
|
||||
self::MAX_MESSAGES,
|
||||
$rowOffset,
|
||||
);
|
||||
if (! $singleFits) {
|
||||
$difference = $this->minimalDifferenceRow($difference);
|
||||
}
|
||||
$current[] = $difference;
|
||||
}
|
||||
|
||||
if ($current !== []) {
|
||||
$chunks[] = $current;
|
||||
}
|
||||
|
||||
return $chunks;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<array<string,mixed>> $rows
|
||||
* @param array{count:int,extra:int,mismatch:int,missing:int,unpriced:int,difference_amount:float} $summary
|
||||
* @param array<string,mixed> $filters
|
||||
*/
|
||||
private function differenceMessageFits(
|
||||
string $title,
|
||||
string $supplier,
|
||||
array $rows,
|
||||
array $summary,
|
||||
array $filters,
|
||||
int $part,
|
||||
int $parts,
|
||||
int $rowOffset,
|
||||
): bool {
|
||||
return $this->payloadBytes($this->markdownPayload(
|
||||
$title,
|
||||
$this->message($supplier, $rows, $summary, $filters, $part, $parts, $rowOffset),
|
||||
)) <= self::TARGET_MESSAGE_BODY_BYTES;
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $row */
|
||||
private function minimalDifferenceRow(array $row): array
|
||||
{
|
||||
return [
|
||||
'type' => $this->clipText($row['type'] ?? '差异', 40),
|
||||
'purchaseNo' => $this->clipText($row['purchaseNo'] ?? '', 100),
|
||||
'date' => $this->clipText($row['date'] ?? '', 32),
|
||||
'barcode' => $this->clipText($row['barcode'] ?? '', 100),
|
||||
'supplierBarcode' => $this->clipText($row['supplierBarcode'] ?? '', 100),
|
||||
'mineQty' => (float) ($row['mineQty'] ?? 0),
|
||||
'theirsQty' => (float) ($row['theirsQty'] ?? 0),
|
||||
'mineAmount' => (float) ($row['mineAmount'] ?? 0),
|
||||
'theirsAmount' => (float) ($row['theirsAmount'] ?? 0),
|
||||
'mineUnitPrice' => (float) ($row['mineUnitPrice'] ?? 0),
|
||||
'theirsUnitPrice' => (float) ($row['theirsUnitPrice'] ?? 0),
|
||||
'note' => '该行明细较多,请登录厂家对账门户查看完整差异。',
|
||||
'differenceDetails' => [],
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array<string,mixed> */
|
||||
private function markdownPayload(string $title, string $text, bool $atAll = false): array
|
||||
{
|
||||
return [
|
||||
'msgtype' => 'markdown',
|
||||
'markdown' => ['title' => $title, 'text' => $text],
|
||||
'at' => ['atMobiles' => [], 'isAtAll' => $atAll],
|
||||
];
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $payload */
|
||||
private function payloadBytes(array $payload): int
|
||||
{
|
||||
$encoded = json_encode($payload, JSON_INVALID_UTF8_SUBSTITUTE);
|
||||
|
||||
return strlen(is_string($encoded) ? $encoded : '');
|
||||
}
|
||||
|
||||
private function clipText(mixed $value, int $maxLength): string
|
||||
{
|
||||
$text = preg_replace('/[\r\n]+/u', ' ', trim((string) $value)) ?? '';
|
||||
|
||||
return mb_strimwidth($text, 0, $maxLength, '…', 'UTF-8');
|
||||
}
|
||||
|
||||
private function assertWebhook(string $webhook): void
|
||||
{
|
||||
$parts = parse_url($webhook);
|
||||
|
||||
@ -855,6 +855,65 @@ class SupplierReconciliationDingTalkTest extends FeatureTestCase
|
||||
Http::assertSentCount(2);
|
||||
}
|
||||
|
||||
public function test_difference_messages_stay_below_dingtalk_body_limit_when_details_are_large(): void
|
||||
{
|
||||
$this->saveRobot();
|
||||
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
|
||||
$rows = [];
|
||||
for ($index = 1; $index <= 38; $index++) {
|
||||
$details = [];
|
||||
for ($detail = 1; $detail <= 50; $detail++) {
|
||||
$details[] = [
|
||||
'purchaseNo' => sprintf('PO-%03d-%03d', $index, $detail),
|
||||
'expressNo' => sprintf('SF-%03d-%03d', $index, $detail),
|
||||
'barcode' => sprintf('BARCODE-%03d-%03d', $index, $detail),
|
||||
'mineQty' => 2,
|
||||
'mineUnitPrice' => 58.29,
|
||||
'mineAmount' => 116.58,
|
||||
'theirsQty' => 1,
|
||||
'theirsUnitPrice' => 58.29,
|
||||
'theirsAmount' => 58.29,
|
||||
'amountDifference' => 58.29,
|
||||
];
|
||||
}
|
||||
$rows[] = [
|
||||
'barcode' => sprintf('BULK-LONG-%03d', $index),
|
||||
'purchaseNo' => sprintf('PO-LONG-%03d', $index),
|
||||
'date' => '2026-11-30',
|
||||
'mineQty' => 100,
|
||||
'theirsQty' => 50,
|
||||
'mineAmount' => 5829,
|
||||
'theirsAmount' => 2914.5,
|
||||
'mineUnitPrice' => 58.29,
|
||||
'theirsUnitPrice' => 58.29,
|
||||
'type' => '数量/金额不一致',
|
||||
'note' => '厂家数量少,请修改后重新上传',
|
||||
'differenceDetails' => $details,
|
||||
];
|
||||
}
|
||||
|
||||
$result = app(SupplierDingTalkNotifier::class)->push('晨丰', $rows, [
|
||||
'month' => '2026-11',
|
||||
'reconciler' => '张三',
|
||||
]);
|
||||
|
||||
$this->assertSame(38, $result['rows']);
|
||||
$this->assertGreaterThan(1, $result['messages']);
|
||||
$this->assertLessThanOrEqual(20, $result['messages']);
|
||||
Http::assertSentCount($result['messages']);
|
||||
|
||||
$texts = [];
|
||||
foreach (Http::recorded() as [$request]) {
|
||||
$this->assertLessThanOrEqual(20000, strlen($request->body()));
|
||||
$texts[] = (string) ($request['markdown']['text'] ?? '');
|
||||
}
|
||||
$combined = implode("\n", $texts);
|
||||
$this->assertStringContainsString('差异共 **38** 条', $combined);
|
||||
$this->assertStringContainsString('其余 47 项明细请登录厂家对账门户查看', $combined);
|
||||
$this->assertStringContainsString('1. **数量/金额不一致**', $combined);
|
||||
$this->assertStringContainsString('38. **数量/金额不一致**', $combined);
|
||||
}
|
||||
|
||||
public function test_finance_page_shows_monthly_return_push_entry(): void
|
||||
{
|
||||
$this->seedReconciliation();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user