Files
sam-api/app/Console/Commands/BendingFillSamItemIds.php
강영보 c29090a0b8 feat: [bending] 절곡품 전용 테이블 분리 API
- bending_items 전용 테이블 생성 (items.options → 정규 컬럼 승격)
- bending_models 전용 테이블 생성 (가이드레일/케이스/하단마감재 통합)
- bending_data JSON 통합 (별도 테이블 → bending_items.bending_data 컬럼)
- bending_item_mappings 테이블 DROP (bending_items.code에 흡수)
- BendingItemService/BendingCodeService → BendingItem 모델 전환
- GuiderailModelService component 이미지 자동 복사
- ItemsFileController bending_items/bending_models 폴백 지원
- Swagger 스키마 업데이트
2026-03-19 20:00:18 +09:00

102 lines
3.4 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Models\Items\Item;
use Illuminate\Console\Attributes\AsCommand;
use Illuminate\Console\Command;
/**
* 모델(GUIDERAIL/SHUTTERBOX/BOTTOMBAR) components에 sam_item_id 일괄 채우기
* legacy_bending_num → SAM BENDING item ID 매핑
*/
#[AsCommand(name: 'bending:fill-sam-item-ids', description: '모델 components의 sam_item_id 일괄 매핑')]
class BendingFillSamItemIds extends Command
{
protected $signature = 'bending:fill-sam-item-ids
{--tenant_id=287 : Target tenant ID}
{--dry-run : 실제 저장 없이 미리보기}';
public function handle(): int
{
$tenantId = (int) $this->option('tenant_id');
$dryRun = $this->option('dry-run');
$this->info('=== sam_item_id 일괄 매핑 ===');
$this->info('Mode: ' . ($dryRun ? 'DRY-RUN' : 'LIVE'));
// 1. legacy_bending_num → SAM item ID 매핑 테이블 구축
$bendingItems = Item::withoutGlobalScopes()
->where('tenant_id', $tenantId)
->where('item_category', 'BENDING')
->whereNull('deleted_at')
->get();
$legacyMap = [];
foreach ($bendingItems as $item) {
$legacyNum = $item->getOption('legacy_bending_num');
if ($legacyNum !== null) {
$legacyMap[(string) $legacyNum] = $item->id;
}
}
$this->info("BENDING items: {$bendingItems->count()}건, legacy_bending_num 매핑: " . count($legacyMap) . '건');
// 2. 모델 items의 components 순회
$models = Item::withoutGlobalScopes()
->where('tenant_id', $tenantId)
->whereIn('item_category', ['GUIDERAIL_MODEL', 'SHUTTERBOX_MODEL', 'BOTTOMBAR_MODEL'])
->whereNull('deleted_at')
->get();
$this->info("모델: {$models->count()}");
$updated = 0;
$mapped = 0;
$notFound = 0;
foreach ($models as $model) {
$components = $model->getOption('components', []);
if (empty($components)) {
continue;
}
$changed = false;
foreach ($components as &$comp) {
// 이미 sam_item_id가 있으면 스킵
if (! empty($comp['sam_item_id'])) {
continue;
}
$legacyNum = $comp['legacy_bending_num'] ?? null;
if ($legacyNum === null) {
continue;
}
$samId = $legacyMap[(string) $legacyNum] ?? null;
if ($samId) {
$comp['sam_item_id'] = $samId;
$changed = true;
$mapped++;
} else {
$notFound++;
$this->warn(" [{$model->id}] legacy_bending_num={$legacyNum} → SAM ID 없음 ({$comp['itemName']})");
}
}
unset($comp);
if ($changed && ! $dryRun) {
$model->setOption('components', $components);
$model->save();
$updated++;
} elseif ($changed) {
$updated++;
}
}
$this->info('');
$this->info("결과: 모델 {$updated}건 업데이트, 컴포넌트 {$mapped}건 매핑, {$notFound}건 미매핑");
return self::SUCCESS;
}
}