>> */ private array $cache = []; public function __construct(private readonly SupplierPayableOverrideStore $overrides) { } /** @param list $rows * @return list */ public function decorateRows(array $rows, int $companyId): array { foreach ($rows as $row) { $this->decorateRow($row, $companyId); } return $rows; } public function decorateRow(object $row, int $companyId): object { $id = (int) ($row->Id ?? 0); $values = $this->values($companyId); $row->{self::FIELD} = $id > 0 && (bool) ($values[$this->key($id)][self::FIELD] ?? false); return $row; } /** * @param list $supplierIds * @return array */ public function flagsBySupplierIds(array $supplierIds, int $companyId): array { $values = $this->values($companyId); $flags = []; foreach (array_unique(array_filter($supplierIds, static fn (int $id): bool => $id > 0)) as $supplierId) { $flags[$supplierId] = (bool) ($values[$this->key($supplierId)][self::FIELD] ?? false); } return $flags; } /** @return array{created:int,updated:int} */ public function save(int $supplierId, bool $isFreeShipping, int $companyId, ?int $userId, string $source = 'manual'): array { $result = $this->overrides->save( self::DATASET, $this->key($supplierId), [self::FIELD => $isFreeShipping], $companyId, $userId, $source, ); unset($this->cache[$companyId]); return $result; } /** * @param array $values 供应商 ID => 是否包邮厂家 * @return array{created:int,updated:int} */ public function saveMany(array $values, int $companyId, ?int $userId, string $source = 'import'): array { $records = []; foreach ($values as $supplierId => $isFreeShipping) { if ((int) $supplierId <= 0) { continue; } $records[$this->key((int) $supplierId)] = [self::FIELD => (bool) $isFreeShipping]; } $result = $this->overrides->saveBatch(self::DATASET, $records, $companyId, $userId, $source); unset($this->cache[$companyId]); return $result; } /** @return array> */ private function values(int $companyId): array { if (! isset($this->cache[$companyId])) { $this->cache[$companyId] = []; foreach ($this->overrides->listRows(self::DATASET, $companyId) as $row) { $key = (string) ($row['_action_key'] ?? ''); if ($key !== '') { $this->cache[$companyId][$key] = $row; } } } return $this->cache[$companyId]; } private function key(int $supplierId): string { return 'supplier:' . $supplierId; } }