CaiWuXiTong/app/Http/Controllers/Business/SupplierNegativeAlertDingTalkActions.php
苏木 675ed67211 fix: 负数预警发送剔除已回传处理结果
自动推送与页面发送统一按账期和供应商剔除已回传处理结果。
Co-authored-by: 苏木 <594326199@qq.com>
Co-committed-by: 苏木 <594326199@qq.com>
2026-11-02 10:19:21 +08:00

117 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\Http\Controllers\Business;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\EcommerceFinance\Services\SupplierNegativeAlertDingTalkWorkflow;
use Modules\EcommerceFinance\View\SupplierNegativeAlertPageData;
use Throwable;
class SupplierNegativeAlertDingTalkActions extends Controller
{
public function __construct(
private readonly SupplierNegativeAlertPageData $pageData,
private readonly SupplierNegativeAlertDingTalkWorkflow $workflow,
) {
$this->middleware('auth');
$this->middleware('company.identify');
}
public function saveRobot(Request $request): RedirectResponse
{
$data = $request->validate([
'dingtalk_webhook' => ['nullable', 'string', 'max:2048'],
'dingtalk_secret' => ['nullable', 'string', 'max:256'],
'dingtalk_group_name' => ['required', 'string', 'max:191'],
]);
$this->workflow->saveConfiguration(
(string) ($data['dingtalk_webhook'] ?? ''),
(string) ($data['dingtalk_secret'] ?? ''),
(string) $data['dingtalk_group_name'],
);
return back()->with('success', '供应商负数预警群机器人已保存。');
}
public function send(Request $request): RedirectResponse
{
$data = $request->validate([
'purchaser' => ['nullable', 'string', 'max:191'],
'month' => ['nullable', 'string', 'regex:/^\d{4}-\d{2}$/'],
'supplier' => ['nullable', 'string', 'max:191'],
'checker' => ['nullable', 'string', 'max:191'],
'settlement_method' => ['nullable', 'string', 'max:191'],
]);
$purchaser = trim((string) ($data['purchaser'] ?? ''));
$filters = [
'month' => (string) ($data['month'] ?? ''),
'supplier' => (string) ($data['supplier'] ?? ''),
'checker' => (string) ($data['checker'] ?? ''),
'settlement_method' => (string) ($data['settlement_method'] ?? ''),
'purchaser' => $purchaser,
];
$filteredRows = $this->pageData->rowsForFilters([
...$filters,
'purchaser' => '',
]);
$rows = $purchaser === ''
? $filteredRows
: array_values(array_filter(
$filteredRows,
static fn (array $row): bool => trim((string) ($row['purchaser'] ?? '')) === $purchaser,
));
if ($rows === []) {
return back()->withErrors([
'dingtalk_send' => '当前筛选没有可发送给该采购的供应商。',
]);
}
$companyId = (int) (company_id() ?: 1);
$rows = $this->workflow->unhandledRows($rows, $companyId);
if ($rows === []) {
return back()->withErrors([
'dingtalk_send' => '当前筛选中的供应商均已回传处理结果,无需重复发送。',
]);
}
try {
if ($purchaser === '') {
$result = $this->workflow->sendGrouped(
$rows,
$filters,
$companyId,
user_id() ? (int) user_id() : null,
);
} else {
$result = $this->workflow->send(
$rows,
$purchaser,
$filters,
$companyId,
user_id() ? (int) user_id() : null,
);
}
} catch (Throwable $exception) {
report($exception);
return back()->withErrors(['dingtalk_send' => '发送失败:' . $exception->getMessage()]);
}
if ($purchaser === '') {
$message = "已一键发送至{$result['group_name']}:{$result['purchaser_count']} 位采购,"
. "{$result['supplier_count']} 家供应商、{$result['row_count']} 条预警。";
if ($result['unassigned_supplier_count'] > 0) {
$message .= " 另有 {$result['unassigned_supplier_count']} 家供应商未分配采购,未发送。";
}
return back()->with('success', $message);
}
return back()->with('success', "已发送至{$result['group_name']}:采购 {$purchaser},"
. "{$result['supplier_count']} 家供应商、{$result['row_count']} 条预警。");
}
}