master
lion 3 months ago
parent b7698c4d85
commit e5e221a61d

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

Loading…
Cancel
Save