*/ public function listByIntegration(int $integrationId): array { $stmt = $this->pdo->prepare( 'SELECT shoppro_status_code, shoppro_status_name, orderpro_status_code FROM order_status_mappings WHERE integration_id = :integration_id ORDER BY shoppro_status_code ASC' ); $stmt->execute(['integration_id' => $integrationId]); $rows = $stmt->fetchAll(); if (!is_array($rows)) { return []; } $result = []; foreach ($rows as $row) { if (!is_array($row)) { continue; } $code = trim((string) ($row['shoppro_status_code'] ?? '')); if ($code === '') { continue; } $result[$code] = [ 'orderpro_status_code' => trim((string) ($row['orderpro_status_code'] ?? '')), 'shoppro_status_name' => isset($row['shoppro_status_name']) ? trim((string) $row['shoppro_status_name']) : null, ]; } return $result; } /** * @param array $mappings */ public function replaceForIntegration(int $integrationId, array $mappings): void { $deleteStmt = $this->pdo->prepare('DELETE FROM order_status_mappings WHERE integration_id = :integration_id'); $deleteStmt->execute(['integration_id' => $integrationId]); if ($mappings === []) { return; } $insertStmt = $this->pdo->prepare( 'INSERT INTO order_status_mappings ( integration_id, shoppro_status_code, shoppro_status_name, orderpro_status_code, created_at, updated_at ) VALUES ( :integration_id, :shoppro_status_code, :shoppro_status_name, :orderpro_status_code, :created_at, :updated_at )' ); $now = date('Y-m-d H:i:s'); foreach ($mappings as $mapping) { $shopCode = trim((string) ($mapping['shoppro_status_code'] ?? '')); $orderCode = trim((string) ($mapping['orderpro_status_code'] ?? '')); if ($shopCode === '' || $orderCode === '') { continue; } $shopNameRaw = isset($mapping['shoppro_status_name']) ? trim((string) $mapping['shoppro_status_name']) : ''; $shopName = $shopNameRaw === '' ? null : $shopNameRaw; $insertStmt->execute([ 'integration_id' => $integrationId, 'shoppro_status_code' => $shopCode, 'shoppro_status_name' => $shopName, 'orderpro_status_code' => $orderCode, 'created_at' => $now, 'updated_at' => $now, ]); } } /** * @return array */ public function listOrderProToShopProMap(int $integrationId): array { $rows = $this->listByIntegration($integrationId); if ($rows === []) { return []; } $result = []; foreach ($rows as $shopCode => $mapping) { $orderProCode = trim((string) ($mapping['orderpro_status_code'] ?? '')); $normalizedOrderProCode = $this->normalizeCode($orderProCode); $normalizedShopCode = $this->normalizeCode((string) $shopCode); if ($normalizedOrderProCode === '' || $normalizedShopCode === '') { continue; } if (!isset($result[$normalizedOrderProCode])) { $result[$normalizedOrderProCode] = $normalizedShopCode; } } return $result; } private function normalizeCode(string $value): string { return trim(mb_strtolower($value)); } }