Skip to content

Commit cac8327

Browse files
committed
chore: import improvements
1 parent 4d3df74 commit cac8327

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

src/Jobs/ImportTariffCodes.php

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,30 @@ private function flushChunk(array $chunk): void
208208
return;
209209
}
210210

211+
$codes = array_keys($chunk);
212+
$existingRecords = DB::table('world_tariff_codes')
213+
->where('year', $chunk[reset($codes)]['year'])
214+
->whereIn('code', $codes)
215+
->get(['code', 'name', 'measure_unit'])
216+
->keyBy('code');
217+
211218
$payload = [];
212219
foreach ($chunk as $rec) {
213-
$names = array_intersect_key($rec['name'], array_flip($this->locales));
214-
$units = array_intersect_key($rec['measure_unit'], array_flip($this->locales));
220+
$existingName = [];
221+
$existingUnits = [];
222+
223+
if (isset($existingRecords[$rec['code']])) {
224+
$existing = $existingRecords[$rec['code']];
225+
if ($existing->name) {
226+
$existingName = json_decode($existing->name, true) ?? [];
227+
}
228+
if ($existing->measure_unit) {
229+
$existingUnits = json_decode($existing->measure_unit, true) ?? [];
230+
}
231+
}
232+
233+
$names = array_merge($existingName, $rec['name']);
234+
$units = array_merge($existingUnits, $rec['measure_unit']);
215235

216236
$payload[] = [
217237
'year' => $rec['year'],
@@ -315,17 +335,17 @@ private function transformCnName(string $name, string $code, array $codeNamesLoo
315335

316336
$hierarchicalParts = [];
317337

318-
$hierarchicalParts[] = ucfirst($name);
338+
$hierarchicalParts[] = strtoupper($name);
319339

320340
$currentCode = $code;
321341
while (strlen($currentCode) > 2) {
322342
$parentCode = substr($currentCode, 0, -2);
323-
if (strlen($parentCode) >= 2) {
343+
if (strlen($parentCode) > 2) {
324344
$parentName = $codeNamesLookup[$parentCode] ?? null;
325345
if ($parentName) {
326346
$cleanParentName = ltrim($parentName, '-');
327347
if (! empty($cleanParentName)) {
328-
$hierarchicalParts[] = ucfirst($cleanParentName);
348+
$hierarchicalParts[] = strtoupper($cleanParentName);
329349
}
330350
}
331351
}

0 commit comments

Comments
 (0)