/commission.json)初始化。 * 存为 storage/app/planting-reimbursement-references/<公司>/commission-people.json(持久卷),不建表。 */ class PlantingCommissionPeople { public const FILE = 'commission-people.json'; /** @var array> */ private array $loaded = []; public function __construct(private readonly PlantingReimbursementReferences $references) {} /** @return array{people:list,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 姓名集合 */ public function nameSet(int $companyId): array { return array_map(static fn (): bool => true, $this->platformMap($companyId)); } /** @return array> 花名 → [报销平台 → 每单佣金上限(分,null = 不限)];空 = 没登记,不能报佣金 */ public function platformMap(int $companyId): array { $map = []; foreach ($this->document($companyId)['people'] as $person) { $map[PlantingReimbursementAuditor::normName($person['name'])] = self::platformCaps((string) ($person['platform'] ?? '')); } return $map; } /** * 「京东15,天猫,拼多多6元/单」→ ['京东' => 1500, '天猫' => null, '拼多多' => 600](分)。 * * @return array */ public static function platformCaps(string $text): array { $caps = []; $text = mb_convert_kana($text, 'as'); preg_match_all('/(京喜|京东|天猫|拼多多|PDD|抖音|淘宝(?:C店)?|阿里(?:巴巴)?|1688|ALIBABA)\s*(?:[¥¥]\s*)?(\d+(?:\.\d+)?)?/iu', $text, $matches, PREG_SET_ORDER); foreach ($matches as $match) { $platform = PlantingQuotaCalculator::platform($match[1]); $cap = isset($match[2]) && $match[2] !== '' ? (int) round((float) $match[2] * 100) : null; if (! array_key_exists($platform, $caps) || ($caps[$platform] !== null && ($cap === null || $cap > $caps[$platform]))) { $caps[$platform] = $cap; } } return $caps; } /** * 「京东,天猫,拼多多5元」「淘宝C店」「京东15」→ 规范平台名列表(同 PlantingQuotaCalculator::platform)。 * * @return list */ public static function platforms(string $text): array { return array_keys(self::platformCaps($text)); } 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 个字)。'); } if (self::platforms($platform) === []) { throw new InvalidArgumentException('请填写报销平台(如:京东,天猫)。'); } $people = $this->document($companyId)['people']; foreach ($people as $index => $person) { if ($person['name'] === $name) { // 已在名单里:更新报销平台(和备注)。 $people[$index]['platform'] = mb_substr(trim($platform), 0, 50); if (trim($remark) !== '') { $people[$index]['remark'] = mb_substr(trim($remark), 0, 200); } $this->store($companyId, $people, $userId); return $name; } } $people[] = ['name' => $name, 'platform' => mb_substr(trim($platform), 0, 50), 'remark' => mb_substr(trim($remark), 0, 200), 'added_at' => PlantingClock::stamp()]; $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,updated: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; $updated = 0; foreach ($rows as $row) { $name = PlantingReimbursementAuditor::normName((string) $row['key']); $platform = mb_substr(trim((string) ($row['payload']['platform'] ?? '')), 0, 50); if ($name === '') { continue; } if (isset($existing[$name])) { $index = $existing[$name]; if ($platform !== '' && ($people[$index]['platform'] ?? '') !== $platform) { $people[$index]['platform'] = $platform; $updated++; } continue; } $people[] = ['name' => $name, 'platform' => $platform, 'remark' => '导入:' . mb_substr($fileName, 0, 80), 'added_at' => PlantingClock::stamp()]; $existing[$name] = count($people) - 1; $added++; } $this->store($companyId, $people, $userId); return ['added' => $added, 'updated' => $updated, 'skipped' => count($rows) - $added - $updated]; } 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> $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' => PlantingClock::stamp(), '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; } }