CaiWuXiTong/app/Console/Commands/ImportWorkPlatformCourierRobots.php

45 lines
2.7 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\Console\Commands;
use App\Support\CourierBills\CourierBillStore;
use App\Support\CourierBills\WorkPlatformCourierRobots;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Storage;
use Modules\EcommerceFinance\Services\SupplierDingTalkNotifier;
use RuntimeException;
class ImportWorkPlatformCourierRobots extends Command
{
protected $signature = 'courier-bills:import-platform-robots {file : Private JSON export of courier payable robot rows} {--company=1} {--apply : Save the reviewed exact matches}';
protected $description = '预演或导入工作平台快递应付群机器人配置;不发送消息';
public function handle(CourierBillStore $store, WorkPlatformCourierRobots $matcher, SupplierDingTalkNotifier $notifier): int
{
$path = (string) $this->argument('file');
if (! is_file($path) || filesize($path) > 2 * 1024 * 1024) throw new RuntimeException('配置文件不存在或超过 2MB。');
$rows = json_decode(file_get_contents($path), true, 32, JSON_THROW_ON_ERROR);
if (! is_array($rows) || ! array_is_list($rows) || count($rows) > 10000) throw new RuntimeException('配置文件必须是最多 10000 条的数组。');
\App\Models\Common\Company::findOrFail((int) $this->option('company'))->makeCurrent(true);
$result = $matcher->match($store->groups(), $rows);
$this->line('名称匹配:' . count($result['robots']) . ';未匹配:' . count($result['unmatched']) . ';有歧义:' . count($result['ambiguous']));
foreach ($result['robots'] as $name => $robot) $this->line($name . ' → ' . ($robot['group_name'] ?: '工作平台已配置群') . '(配置 ' . $robot['source_id'] . ')');
$this->line('未匹配:' . implode('、', $result['unmatched']));
$this->line('有歧义:' . implode('、', $result['ambiguous']));
if (! $this->option('apply')) return self::SUCCESS;
$backup = 'private/courier-robot-backups/' . now()->format('Ymd-His') . '-' . bin2hex(random_bytes(4)) . '.enc';
$backedUp = Storage::disk('local')->put($backup, Crypt::encryptString(json_encode([
'company_id' => (int) $this->option('company'),
'value' => setting('supplier_reconciliation.dingtalk_robots', []),
], JSON_THROW_ON_ERROR)));
if (! $backedUp || ! chmod(Storage::disk('local')->path($backup), 0600)) {
throw new RuntimeException('原配置备份失败,未修改机器人配置。');
}
$notifier->importCourierRobots($result['robots']);
$this->info('已保存 ' . count($result['robots']) . ' 家;原配置已加密备份:' . $backup . ';未发送消息。');
return self::SUCCESS;
}
}