CaiWuXiTong/app/Business/ServiceCompanyReconciliation/CustomerServiceMappingWorkbookImporter.php
DoDo 00728c5212 feat: 固定客服公司映射并自动平衡未分配店铺 (#1096)
固定已有映射,收入店铺自动平衡后锁定。
2026-11-11 17:45:18 +08:00

91 lines
2.8 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\Business\ServiceCompanyReconciliation;
use App\Business\XlsxSheetReader;
use RuntimeException;
class CustomerServiceMappingWorkbookImporter
{
public function __construct(private readonly XlsxSheetReader $reader) {}
/**
* @return array{
* rows:list<array{service_company_name:string,store_company_name:string,fee_rate:float,source_row:int}>,
* total_rows:int,
* assigned_rows:int,
* unassigned_rows:int,
* unassigned:list<array{store_company_name:string,fee_rate:float,source_row:int}>,
* invalid_rows:list<string>
* }
*/
public function rows(string $path): array
{
if (! is_file($path)) {
throw new RuntimeException('客服公司映射文件不存在:' . $path);
}
$result = [
'rows' => [],
'total_rows' => 0,
'assigned_rows' => 0,
'unassigned_rows' => 0,
'unassigned' => [],
'invalid_rows' => [],
];
foreach ($this->reader->rows(
$path,
['公司名称', '结算比例', '对应客服公司'],
sheetName: '公司盘点',
) as $row) {
$result['total_rows']++;
$sourceRow = $result['total_rows'] + 1;
$storeName = trim((string) ($row['公司名称'] ?? ''));
$serviceName = trim((string) ($row['对应客服公司'] ?? ''));
if ($storeName === '') {
continue;
}
$feeRate = $this->feeRate((string) ($row['结算比例'] ?? ''));
if ($feeRate === null) {
$result['invalid_rows'][] = "第 {$sourceRow} 行结算比例无效。";
continue;
}
if ($serviceName === '') {
$result['unassigned_rows']++;
$result['unassigned'][] = [
'store_company_name' => $storeName,
'fee_rate' => $feeRate,
'source_row' => $sourceRow,
];
continue;
}
$result['rows'][] = [
'service_company_name' => $serviceName,
'store_company_name' => $storeName,
'fee_rate' => $feeRate,
'source_row' => $sourceRow,
];
$result['assigned_rows']++;
}
return $result;
}
private function feeRate(string $value): ?float
{
$normalized = trim(str_replace(['%', ',', ','], ['', '', ''], $value));
if ($normalized === '' || ! is_numeric($normalized)) {
return null;
}
$rate = (float) $normalized;
if ($rate > 1) {
$rate /= 100;
}
return $rate > 0 && $rate <= 1 ? round($rate, 6) : null;
}
}