一次点击按采购拆分任务,并用一条钉钉群消息发送全部采购独立处理链接。 Co-authored-by: 苏木 <594326199@qq.com> Co-committed-by: 苏木 <594326199@qq.com>
109 lines
4.1 KiB
PHP
109 lines
4.1 KiB
PHP
<?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' => '当前筛选没有可发送给该采购的供应商。',
|
||
]);
|
||
}
|
||
|
||
try {
|
||
if ($purchaser === '') {
|
||
$result = $this->workflow->sendGrouped(
|
||
$rows,
|
||
$filters,
|
||
(int) (company_id() ?: 1),
|
||
user_id() ? (int) user_id() : null,
|
||
);
|
||
} else {
|
||
$result = $this->workflow->send(
|
||
$rows,
|
||
$purchaser,
|
||
$filters,
|
||
(int) (company_id() ?: 1),
|
||
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']} 条预警。");
|
||
}
|
||
}
|