CaiWuXiTong/app/Http/Controllers/Business/SupplierNegativeAlertDingTalkActions.php

83 lines
3.0 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' => ['required', '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']);
$rows = $this->pageData->rowsForFilters([
'purchaser' => $purchaser,
'month' => (string) ($data['month'] ?? ''),
'supplier' => (string) ($data['supplier'] ?? ''),
'checker' => (string) ($data['checker'] ?? ''),
'settlement_method' => (string) ($data['settlement_method'] ?? ''),
]);
if ($rows === []) {
return back()->withErrors([
'dingtalk_send' => '当前筛选没有可发送给该采购的供应商。',
]);
}
try {
$result = $this->workflow->send(
$rows,
$purchaser,
$request->only(['month', 'supplier', 'checker', 'settlement_method', 'purchaser']),
(int) (company_id() ?: 1),
user_id() ? (int) user_id() : null,
);
} catch (Throwable $exception) {
report($exception);
return back()->withErrors(['dingtalk_send' => '发送失败:' . $exception->getMessage()]);
}
return back()->with(
'success',
"已发送至{$result['group_name']}:采购 {$purchaser},"
. "{$result['supplier_count']} 家供应商、{$result['row_count']} 条预警。",
);
}
}