diff --git a/app/Services/VenueImportService.php b/app/Services/VenueImportService.php index 9eac533..a1655f0 100644 --- a/app/Services/VenueImportService.php +++ b/app/Services/VenueImportService.php @@ -13,14 +13,8 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet; class VenueImportService { - /** @var array 中文主题名 => item_value */ - public const THEME_LABEL_TO_VALUE = [ - '自然生态' => 'theme_nature_ecology', - '科技产业' => 'theme_tech_industry', - '苏工苏艺' => 'theme_su_craft', - '生命健康' => 'theme_life_health', - '弘扬科学家精神' => 'theme_scientist_spirit', - ]; + /** @var array|null 主题:item_label => item_value(与数据字典 venue_type 同步,按请求缓存) */ + private ?array $themeLabelToValueMapCache = null; private const TICKET_TYPE_LABEL = [ '免费' => 'free', @@ -38,6 +32,27 @@ class VenueImportService '预约开放' => 'appointment', ]; + /** + * @return array 中文标签 => item_value + */ + private function themeLabelToValueMap(): array + { + if ($this->themeLabelToValueMapCache !== null) { + return $this->themeLabelToValueMapCache; + } + + $this->themeLabelToValueMapCache = DictItem::query() + ->where('dict_type', 'venue_type') + ->where('is_active', true) + ->orderBy('sort') + ->orderBy('id') + ->get(['item_label', 'item_value']) + ->mapWithKeys(fn ($r) => [trim((string) $r->item_label) => (string) $r->item_value]) + ->all(); + + return $this->themeLabelToValueMapCache; + } + public function buildTemplateSpreadsheet(): Spreadsheet { $spreadsheet = new Spreadsheet(); @@ -96,7 +111,7 @@ class VenueImportService $opt->setCellValue('B1', '主题'); $tr = 2; - foreach (array_keys(self::THEME_LABEL_TO_VALUE) as $label) { + foreach (array_keys($this->themeLabelToValueMap()) as $label) { $opt->setCellValue('B' . $tr, $label); $tr++; } @@ -197,10 +212,11 @@ class VenueImportService $themeRaw = str_replace(',', ',', $g(1)); $themeParts = array_filter(array_map('trim', explode(',', $themeRaw)), fn ($s) => $s !== ''); + $themeMap = $this->themeLabelToValueMap(); $venueTypes = []; foreach ($themeParts as $part) { - if (isset(self::THEME_LABEL_TO_VALUE[$part])) { - $venueTypes[] = self::THEME_LABEL_TO_VALUE[$part]; + if (isset($themeMap[$part])) { + $venueTypes[] = $themeMap[$part]; } }