135 lines
6.0 KiB
PHP
135 lines
6.0 KiB
PHP
<?php
|
||
|
||
namespace Modules\EcommerceFinance\Services;
|
||
|
||
use Illuminate\Support\Facades\Storage;
|
||
use InvalidArgumentException;
|
||
use RuntimeException;
|
||
|
||
/**
|
||
* 种菜报销「报销佣金人员」名单:发起人在名单里才能报佣金。
|
||
*
|
||
* 2026-12-01 起在工作台维护(新增 / 删除 / 导入),不再每次上传整表。第一次读取时,用以前上传过的历史名单
|
||
* (planting-reimbursement-references/<公司>/commission.json)初始化。
|
||
* 存为 storage/app/planting-reimbursement-references/<公司>/commission-people.json(持久卷),不建表。
|
||
*/
|
||
class PlantingCommissionPeople
|
||
{
|
||
public const FILE = 'commission-people.json';
|
||
|
||
/** @var array<int,array<string,mixed>> */
|
||
private array $loaded = [];
|
||
|
||
public function __construct(private readonly PlantingReimbursementReferences $references) {}
|
||
|
||
/** @return array{people:list<array{name:string,platform:string,remark:string,added_at:?string}>,source:string,updated_at:?string} */
|
||
public function document(int $companyId): array
|
||
{
|
||
if (isset($this->loaded[$companyId])) {
|
||
return $this->loaded[$companyId];
|
||
}
|
||
$disk = Storage::disk('local');
|
||
$document = $disk->exists($this->path($companyId)) ? json_decode((string) $disk->get($this->path($companyId)), true) : null;
|
||
if (! is_array($document) || ! is_array($document['people'] ?? null)) {
|
||
// 首次:用历史上传的名单初始化。
|
||
$people = [];
|
||
foreach (array_keys($this->references->nameSet($companyId, PlantingReimbursementReferences::KIND_COMMISSION)) as $name) {
|
||
$people[] = ['name' => (string) $name, 'platform' => '', 'remark' => '历史上传名单导入', 'added_at' => null];
|
||
}
|
||
$document = ['people' => $people, 'source' => $people === [] ? 'empty' : 'history', 'updated_at' => null];
|
||
}
|
||
|
||
return $this->loaded[$companyId] = $document;
|
||
}
|
||
|
||
/** @return array<string,bool> 姓名集合(给校验用) */
|
||
public function nameSet(int $companyId): array
|
||
{
|
||
$set = [];
|
||
foreach ($this->document($companyId)['people'] as $person) {
|
||
$set[PlantingReimbursementAuditor::normName($person['name'])] = true;
|
||
}
|
||
|
||
return $set;
|
||
}
|
||
|
||
public function add(int $companyId, string $name, string $platform, string $remark, ?int $userId): string
|
||
{
|
||
$name = PlantingReimbursementAuditor::normName($name);
|
||
if ($name === '' || mb_strlen($name) > 50 || preg_match('/^\d+$/', $name)) {
|
||
throw new InvalidArgumentException('请填写发起人花名(不超过 50 个字)。');
|
||
}
|
||
$people = $this->document($companyId)['people'];
|
||
foreach ($people as $person) {
|
||
if ($person['name'] === $name) {
|
||
throw new InvalidArgumentException($name . ' 已在名单里。');
|
||
}
|
||
}
|
||
$people[] = ['name' => $name, 'platform' => mb_substr(trim($platform), 0, 50), 'remark' => mb_substr(trim($remark), 0, 200), 'added_at' => now()->format('Y-m-d H:i:s')];
|
||
$this->store($companyId, $people, $userId);
|
||
|
||
return $name;
|
||
}
|
||
|
||
public function remove(int $companyId, string $name, ?int $userId): void
|
||
{
|
||
$name = PlantingReimbursementAuditor::normName($name);
|
||
$people = array_values(array_filter($this->document($companyId)['people'], static fn (array $person): bool => $person['name'] !== $name));
|
||
if (count($people) === count($this->document($companyId)['people'])) {
|
||
throw new InvalidArgumentException($name . ' 不在名单里,请刷新页面后重试。');
|
||
}
|
||
$this->store($companyId, $people, $userId);
|
||
}
|
||
|
||
/**
|
||
* 用表格追加名单(格式同以前上传的「报销佣金人员」表,取右侧「人员 / 平台」)。已有的不重复加。
|
||
*
|
||
* @return array{added:int,skipped:int}
|
||
*/
|
||
public function import(int $companyId, string $path, string $fileName, ?int $userId): array
|
||
{
|
||
$rows = $this->references->parse(PlantingReimbursementReferences::KIND_COMMISSION, $path, $fileName);
|
||
$people = $this->document($companyId)['people'];
|
||
$existing = array_flip(array_column($people, 'name'));
|
||
$added = 0;
|
||
foreach ($rows as $row) {
|
||
$name = PlantingReimbursementAuditor::normName((string) $row['key']);
|
||
if ($name === '' || isset($existing[$name])) {
|
||
continue;
|
||
}
|
||
$people[] = ['name' => $name, 'platform' => '', 'remark' => '导入:' . mb_substr($fileName, 0, 80), 'added_at' => now()->format('Y-m-d H:i:s')];
|
||
$existing[$name] = true;
|
||
$added++;
|
||
}
|
||
$this->store($companyId, $people, $userId);
|
||
|
||
return ['added' => $added, 'skipped' => count($rows) - $added];
|
||
}
|
||
|
||
public function exportCsv(int $companyId): string
|
||
{
|
||
$lines = ["\xEF\xBB\xBF人员,平台,备注,加入时间"];
|
||
foreach ($this->document($companyId)['people'] as $person) {
|
||
$lines[] = implode(',', array_map(static fn (?string $value): string => '"' . str_replace('"', '""', (string) $value) . '"', [$person['name'], $person['platform'], $person['remark'], $person['added_at']]));
|
||
}
|
||
|
||
return implode("\n", $lines) . "\n";
|
||
}
|
||
|
||
/** @param list<array<string,mixed>> $people */
|
||
private function store(int $companyId, array $people, ?int $userId): void
|
||
{
|
||
usort($people, static fn (array $a, array $b): int => strcmp($a['name'], $b['name']));
|
||
$document = ['people' => array_values($people), 'source' => 'edited', 'updated_at' => now()->format('Y-m-d H:i:s'), 'updated_by' => $userId];
|
||
if (! Storage::disk('local')->put($this->path($companyId), json_encode($document, JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR))) {
|
||
throw new RuntimeException('报销佣金人员名单保存失败。');
|
||
}
|
||
$this->loaded[$companyId] = $document;
|
||
}
|
||
|
||
private function path(int $companyId): string
|
||
{
|
||
return PlantingReimbursementReferences::DIRECTORY . '/' . $companyId . '/' . self::FILE;
|
||
}
|
||
}
|