diff --git a/database/migrations/2026_03_12_100001_ensure_equipment_tables_exist.php b/database/migrations/2026_03_12_100001_ensure_equipment_tables_exist.php new file mode 100644 index 0000000..1bcc97e --- /dev/null +++ b/database/migrations/2026_03_12_100001_ensure_equipment_tables_exist.php @@ -0,0 +1,203 @@ +id(); + $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID'); + $table->string('equipment_code', 20)->comment('설비코드 (KD-M-001 형식)'); + $table->string('name', 100)->comment('설비명'); + $table->string('equipment_type', 50)->nullable()->comment('설비유형'); + $table->string('specification', 255)->nullable()->comment('규격'); + $table->string('manufacturer', 100)->nullable()->comment('제조사'); + $table->string('model_name', 100)->nullable()->comment('모델명'); + $table->string('serial_no', 100)->nullable()->comment('제조번호'); + $table->string('location', 100)->nullable()->comment('위치'); + $table->string('production_line', 50)->nullable()->comment('생산라인'); + $table->date('purchase_date')->nullable()->comment('구입일'); + $table->date('install_date')->nullable()->comment('설치일'); + $table->decimal('purchase_price', 15, 2)->nullable()->comment('구입가격'); + $table->integer('useful_life')->nullable()->comment('내용연수'); + $table->string('status', 20)->default('active')->comment('상태: active/idle/disposed'); + $table->date('disposed_date')->nullable()->comment('폐기일'); + $table->foreignId('manager_id')->nullable()->comment('담당자 ID'); + $table->foreignId('sub_manager_id')->nullable()->comment('부 담당자 ID'); + $table->string('photo_path', 500)->nullable()->comment('설비사진 경로'); + $table->text('memo')->nullable()->comment('비고'); + $table->json('options')->nullable()->comment('확장 속성 JSON'); + $table->tinyInteger('is_active')->default(1)->comment('사용여부'); + $table->integer('sort_order')->default(0)->comment('정렬순서'); + $table->foreignId('created_by')->nullable()->comment('생성자 ID'); + $table->foreignId('updated_by')->nullable()->comment('수정자 ID'); + $table->foreignId('deleted_by')->nullable()->comment('삭제자 ID'); + $table->timestamps(); + $table->softDeletes(); + + $table->unique(['tenant_id', 'equipment_code'], 'uq_equipment_code'); + $table->index(['tenant_id', 'status'], 'idx_equipment_status'); + $table->index(['tenant_id', 'production_line'], 'idx_equipment_line'); + $table->index(['tenant_id', 'equipment_type'], 'idx_equipment_type'); + }); + } + + // 2. equipment_inspection_templates (점검항목 템플릿) + if (! Schema::hasTable('equipment_inspection_templates')) { + Schema::create('equipment_inspection_templates', function (Blueprint $table) { + $table->id(); + $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID'); + $table->unsignedBigInteger('equipment_id')->comment('설비 ID'); + $table->string('inspection_cycle', 20)->default('daily') + ->comment('점검주기: daily/weekly/monthly/bimonthly/quarterly/semiannual'); + $table->integer('item_no')->comment('항목번호'); + $table->string('check_point', 50)->comment('점검개소'); + $table->string('check_item', 100)->comment('점검항목'); + $table->string('check_timing', 20)->nullable()->comment('시기: operating/stopped'); + $table->string('check_frequency', 50)->nullable()->comment('주기'); + $table->text('check_method')->nullable()->comment('점검방법 및 기준'); + $table->integer('sort_order')->default(0)->comment('정렬순서'); + $table->tinyInteger('is_active')->default(1)->comment('사용여부'); + $table->timestamps(); + + $table->unique(['equipment_id', 'inspection_cycle', 'item_no'], 'uq_equipment_cycle_item_no'); + $table->index('tenant_id', 'idx_insp_tmpl_tenant'); + $table->index('inspection_cycle', 'idx_insp_tmpl_cycle'); + + $table->foreign('equipment_id') + ->references('id') + ->on('equipments') + ->onDelete('cascade'); + }); + } + + // 3. equipment_inspections (점검 헤더) + if (! Schema::hasTable('equipment_inspections')) { + Schema::create('equipment_inspections', function (Blueprint $table) { + $table->id(); + $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID'); + $table->unsignedBigInteger('equipment_id')->comment('설비 ID'); + $table->string('inspection_cycle', 20)->default('daily') + ->comment('점검주기: daily/weekly/monthly/bimonthly/quarterly/semiannual'); + $table->string('year_month', 7)->comment('점검년월 (2026-02)'); + $table->string('overall_judgment', 10)->nullable()->comment('종합판정: OK/NG'); + $table->foreignId('inspector_id')->nullable()->comment('점검자 ID'); + $table->text('repair_note')->nullable()->comment('수리내역'); + $table->text('issue_note')->nullable()->comment('이상내용'); + $table->foreignId('created_by')->nullable()->comment('생성자 ID'); + $table->foreignId('updated_by')->nullable()->comment('수정자 ID'); + $table->timestamps(); + + $table->unique(['tenant_id', 'equipment_id', 'inspection_cycle', 'year_month'], 'uq_inspection_cycle_period'); + $table->index(['tenant_id', 'inspection_cycle', 'year_month'], 'idx_inspection_cycle_period'); + + $table->foreign('equipment_id') + ->references('id') + ->on('equipments') + ->onDelete('cascade'); + }); + } + + // 4. equipment_inspection_details (점검 상세) + if (! Schema::hasTable('equipment_inspection_details')) { + Schema::create('equipment_inspection_details', function (Blueprint $table) { + $table->id(); + $table->unsignedBigInteger('inspection_id')->comment('점검 헤더 ID'); + $table->unsignedBigInteger('template_item_id')->comment('점검항목 템플릿 ID'); + $table->date('check_date')->comment('점검일'); + $table->string('result', 10)->nullable()->comment('결과: good/bad/repaired'); + $table->string('note', 500)->nullable()->comment('비고'); + $table->timestamps(); + + $table->unique(['inspection_id', 'template_item_id', 'check_date'], 'uq_inspection_detail'); + + $table->foreign('inspection_id') + ->references('id') + ->on('equipment_inspections') + ->onDelete('cascade'); + + $table->foreign('template_item_id') + ->references('id') + ->on('equipment_inspection_templates') + ->onDelete('cascade'); + }); + } + + // 5. equipment_repairs (수리이력) + if (! Schema::hasTable('equipment_repairs')) { + Schema::create('equipment_repairs', function (Blueprint $table) { + $table->id(); + $table->unsignedBigInteger('tenant_id')->comment('테넌트 ID'); + $table->unsignedBigInteger('equipment_id')->comment('설비 ID'); + $table->date('repair_date')->comment('수리일'); + $table->string('repair_type', 20)->comment('보전구분: internal/external'); + $table->decimal('repair_hours', 5, 1)->nullable()->comment('수리시간'); + $table->text('description')->nullable()->comment('수리내용'); + $table->decimal('cost', 15, 2)->nullable()->comment('수리비용'); + $table->string('vendor', 100)->nullable()->comment('외주업체'); + $table->foreignId('repaired_by')->nullable()->comment('수리자 ID'); + $table->text('memo')->nullable()->comment('비고'); + $table->json('options')->nullable()->comment('확장 속성 JSON'); + $table->foreignId('created_by')->nullable()->comment('생성자 ID'); + $table->foreignId('updated_by')->nullable()->comment('수정자 ID'); + $table->timestamps(); + $table->softDeletes(); + + $table->index(['tenant_id', 'repair_date'], 'idx_repair_date'); + $table->index(['tenant_id', 'equipment_id'], 'idx_repair_equipment'); + + $table->foreign('equipment_id') + ->references('id') + ->on('equipments') + ->onDelete('cascade'); + }); + } + + // 6. equipment_process (설비-공정 매핑) + if (! Schema::hasTable('equipment_process')) { + Schema::create('equipment_process', function (Blueprint $table) { + $table->id(); + $table->unsignedBigInteger('equipment_id')->comment('설비 ID'); + $table->unsignedBigInteger('process_id')->comment('공정 ID'); + $table->tinyInteger('is_primary')->default(0)->comment('주 설비 여부'); + $table->timestamps(); + + $table->unique(['equipment_id', 'process_id'], 'uq_equipment_process'); + + $table->foreign('equipment_id') + ->references('id') + ->on('equipments') + ->onDelete('cascade'); + + $table->foreign('process_id') + ->references('id') + ->on('processes') + ->onDelete('cascade'); + }); + } + } + + public function down(): void + { + // 역순으로 삭제 (FK 의존성) + Schema::dropIfExists('equipment_process'); + Schema::dropIfExists('equipment_inspection_details'); + Schema::dropIfExists('equipment_repairs'); + Schema::dropIfExists('equipment_inspections'); + Schema::dropIfExists('equipment_inspection_templates'); + Schema::dropIfExists('equipments'); + } +};