Files
sam-api/app/Console/Commands/GenerateSimpleRelationships.php

150 lines
6.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
class GenerateSimpleRelationships extends Command
{
protected $signature = 'db:generate-simple-relationships';
protected $description = '기본 논리적 관계 문서 생성';
public function handle()
{
$this->info('🔄 기본 논리적 관계 문서 생성 중...');
$relationships = $this->getKnownRelationships();
$this->generateDocument($relationships);
$this->info('✅ 논리적 관계 문서 생성 완료!');
}
private function getKnownRelationships(): array
{
return [
'users' => [
'description' => '사용자 계정',
'relationships' => [
'user_tenants (hasMany)' => 'user_tenants.user_id → users.id',
'user_roles (hasMany)' => 'user_roles.user_id → users.id',
'audit_logs (hasMany)' => 'audit_logs.actor_id → users.id (생성자)',
],
],
'tenants' => [
'description' => '테넌트 (회사/조직)',
'relationships' => [
'user_tenants (hasMany)' => 'user_tenants.tenant_id → tenants.id',
'classifications (hasMany)' => 'classifications.tenant_id → tenants.id (논리적)',
'departments (hasMany)' => 'departments.tenant_id → tenants.id',
'products (hasMany)' => 'products.tenant_id → tenants.id',
'orders (hasMany)' => 'orders.tenant_id → tenants.id',
],
],
'categories' => [
'description' => '제품 카테고리 (계층구조)',
'relationships' => [
'parent (belongsTo)' => 'categories.parent_id → categories.id',
'children (hasMany)' => 'categories.parent_id → categories.id',
'products (hasMany)' => 'products.category_id → categories.id',
'estimates (hasMany)' => 'estimates.model_set_id → categories.id (논리적)',
],
],
'products' => [
'description' => '제품 마스터',
'relationships' => [
'category (belongsTo)' => 'products.category_id → categories.id',
'tenant (belongsTo)' => 'products.tenant_id → tenants.id',
'product_components (hasMany)' => 'product_components.parent_product_id → products.id (논리적)',
'order_items (hasMany)' => 'order_items.product_id → products.id',
],
],
'departments' => [
'description' => '부서 관리 (계층구조)',
'relationships' => [
'parent (belongsTo)' => 'departments.parent_id → departments.id (논리적)',
'children (hasMany)' => 'departments.parent_id → departments.id (논리적)',
'tenant (belongsTo)' => 'departments.tenant_id → tenants.id',
],
],
'estimates' => [
'description' => '견적서 (스냅샷 데이터)',
'relationships' => [
'category (belongsTo)' => 'estimates.model_set_id → categories.id (논리적)',
'tenant (belongsTo)' => 'estimates.tenant_id → tenants.id',
'estimate_items (hasMany)' => 'estimate_items.estimate_id → estimates.id (논리적)',
],
],
'estimate_items' => [
'description' => '견적 아이템',
'relationships' => [
'estimate (belongsTo)' => 'estimate_items.estimate_id → estimates.id (논리적)',
'tenant (belongsTo)' => 'estimate_items.tenant_id → tenants.id',
],
],
'product_components' => [
'description' => 'BOM 구성요소 (통합 참조구조)',
'relationships' => [
'parent_product (belongsTo)' => 'product_components.parent_product_id → products.id (논리적)',
'material_or_product (polymorphic)' => 'product_components.ref_id → materials.id OR products.id (ref_type 기반)',
'tenant (belongsTo)' => 'product_components.tenant_id → tenants.id',
],
],
'classifications' => [
'description' => '분류 코드',
'relationships' => [
'tenant (belongsTo)' => 'classifications.tenant_id → tenants.id (논리적)',
],
],
];
}
private function generateDocument(array $relationships): void
{
$timestamp = now()->format('Y-m-d H:i:s');
$content = "# 논리적 데이터베이스 관계 문서 (간소화)\n\n";
$content .= "> **생성일**: {$timestamp}\n";
$content .= "> **소스**: 알려진 비즈니스 관계 기반\n";
$content .= "> **참고**: FK 제거 후 논리적 관계 명세\n\n";
$content .= "## 📊 테이블별 논리적 관계\n\n";
foreach ($relationships as $table => $info) {
$content .= "### 📋 `{$table}` - {$info['description']}\n\n";
foreach ($info['relationships'] as $relationName => $definition) {
$content .= "- **{$relationName}**: `{$definition}`\n";
}
$content .= "\n";
}
$content .= "## 🚨 중요 사항\n\n";
$content .= "### 논리적 관계 (FK 제거됨)\n";
$content .= "- `classifications.tenant_id → tenants.id`\n";
$content .= "- `departments.parent_id → departments.id`\n";
$content .= "- `estimates.model_set_id → categories.id`\n";
$content .= "- `estimate_items.estimate_id → estimates.id`\n";
$content .= "- `product_components` 모든 관계 (통합 구조)\n\n";
$content .= "### 물리적 FK 유지됨\n";
$content .= "- 모든 `tenant_id` 관계 (멀티테넌트 보안)\n";
$content .= "- 권한 관리 시스템 FK\n";
$content .= "- 기타 중요 비즈니스 FK\n\n";
$content .= "## 📝 개발 가이드\n\n";
$content .= "1. **Service 레이어**에서 논리적 무결성 검증 필수\n";
$content .= "2. **Eloquent 관계 메서드** 적극 활용\n";
$content .= "3. **Soft Delete**로 데이터 보호\n";
$content .= "4. **BelongsToTenant** 트레잇으로 테넌트 격리\n\n";
$content .= "---\n";
$content .= "*이 문서는 개발 참조용입니다. 모델 변경 시 업데이트 해주세요.*\n";
File::put(base_path('LOGICAL_RELATIONSHIPS_SIMPLE.md'), $content);
$this->info('📄 문서 생성: LOGICAL_RELATIONSHIPS_SIMPLE.md');
}
}