*/ public static function search($keyword, $cityCode = '', $limit = 20) { $keyword = trim((string) $keyword); if ($keyword === '') { return []; } $limit = max(1, min(50, (int) $limit)); $cityCode = HuifuOnboardAreaHelper::toCode($cityCode); $rows = self::searchDatabase($keyword, $cityCode, $limit); if (count($rows) >= $limit) { return $rows; } $jsonRows = self::searchJson($keyword, $cityCode, $limit - count($rows)); $existing = array_column($rows, 'branch_code'); foreach ($jsonRows as $row) { if (in_array($row['branch_code'], $existing, true)) { continue; } $rows[] = $row; if (count($rows) >= $limit) { break; } } return $rows; } protected static function searchDatabase($keyword, $cityCode, $limit) { try { $query = Db::name('shopro_huifu_bank_branch')->where('status', 1); if ($cityCode !== '') { $query->where('area_id', $cityCode); } $query->where(function ($q) use ($keyword) { $q->whereLike('branch_name', '%' . $keyword . '%') ->whereOr('branch_code', 'like', '%' . $keyword . '%') ->whereOr('bank_name', 'like', '%' . $keyword . '%'); }); $list = $query->limit($limit)->select(); if (!$list) { return []; } $rows = []; foreach ($list as $item) { $rows[] = [ 'branch_code' => (string) $item['branch_code'], 'branch_name' => (string) $item['branch_name'], 'bank_name' => (string) ($item['bank_name'] ?? ''), 'prov_id' => (string) ($item['prov_id'] ?? ''), 'area_id' => (string) ($item['area_id'] ?? ''), ]; } return $rows; } catch (\Throwable $e) { return []; } } protected static function searchJson($keyword, $limit) { if ($limit <= 0) { return []; } $all = self::loadJson(); $keywordLower = mb_strtolower($keyword, 'UTF-8'); $rows = []; foreach ($all as $item) { $hay = mb_strtolower( ($item['branch_name'] ?? '') . ' ' . ($item['bank_name'] ?? '') . ' ' . ($item['branch_code'] ?? ''), 'UTF-8' ); if (mb_strpos($hay, $keywordLower, 0, 'UTF-8') === false) { continue; } $rows[] = $item; if (count($rows) >= $limit) { break; } } return $rows; } protected static function loadJson() { if (self::$jsonCache !== null) { return self::$jsonCache; } $file = ADDON_PATH . 'shopro_huifu/data/bank_branches.json'; if (!is_file($file)) { self::$jsonCache = []; return self::$jsonCache; } $decoded = json_decode(file_get_contents($file), true); self::$jsonCache = is_array($decoded) ? $decoded : []; return self::$jsonCache; } }