增加采购筛选、群机器人任务发送、钉钉内逐供应商处理和结果回传。 Co-authored-by: 苏木 <594326199@qq.com> Co-committed-by: 苏木 <594326199@qq.com>
83 lines
3.0 KiB
PHP
83 lines
3.0 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' => ['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']} 条预警。",
|
||
);
|
||
}
|
||
}
|