import 'package:superport/models/company_model.dart'; /// Company 엔터티를 기반으로 본사/지점을 통합 관리하는 래퍼 모델 /// parentCompanyId 기반으로 본사/지점 구분 (Clean Architecture) class CompanyItem { final Company company; // 본사 또는 지점 (통합) final String? parentCompanyName; // 지점인 경우만 본사명 (조회된 데이터) /// 지점 여부 (parentCompanyId != null) bool get isBranch => company.parentCompanyId != null; /// 본사 여부 (parentCompanyId == null) bool get isHeadquarters => company.parentCompanyId == null; CompanyItem({ required this.company, this.parentCompanyName, // 지점인 경우만 설정 }) : assert( // 지점인 경우 parentCompanyName이 있어야 함 company.parentCompanyId == null || parentCompanyName != null, 'Branch must have parentCompanyName' ); /// 본사 생성자 CompanyItem.headquarters(this.company) : parentCompanyName = null; /// 지점 생성자 CompanyItem.branch(Company branchCompany, this.parentCompanyName) : company = branchCompany; /// 표시용 이름 (계층적 구조) String get displayName { if (isBranch) { return '$parentCompanyName > ${company.name}'; } else { return company.name; } } /// 실제 이름 (본사명 또는 지점명) String get name => company.name; /// ID (본사 ID 또는 지점 ID) int? get id => company.id; /// 주소 String get address => company.address.toString(); /// 담당자명 String? get contactName => company.contactName; /// 담당자 직급 String? get contactPosition => company.contactPosition; /// 연락처 String? get contactPhone => company.contactPhone; /// 이메일 String? get contactEmail => company.contactEmail; /// 회사 유형 (본사만, 지점은 빈 리스트) List get companyTypes { return isBranch ? [] : company.companyTypes; } /// 비고 String? get remark => company.remark; /// 생성일 DateTime? get createdAt => company.createdAt; /// 수정일 DateTime? get updatedAt => company.updatedAt; /// 활성 상태 bool get isActive => company.isActive; /// 파트너사 플래그 (지점은 부모 회사의 속성 상속) bool get isPartner => isBranch ? false : company.isPartner; /// 고객사 플래그 (지점은 부모 회사의 속성 상속) bool get isCustomer => isBranch ? false : company.isCustomer; /// 부모 회사 ID (지점인 경우만) int? get parentCompanyId => company.parentCompanyId; /// JSON 직렬화 Map toJson() { return { 'company': company.toJson(), 'parentCompanyName': parentCompanyName, 'isBranch': isBranch, }; } /// JSON 역직렬화 factory CompanyItem.fromJson(Map json) { final company = Company.fromJson(json['company']); final parentCompanyName = json['parentCompanyName'] as String?; if (company.parentCompanyId != null) { return CompanyItem.branch(company, parentCompanyName!); } else { return CompanyItem.headquarters(company); } } @override bool operator ==(Object other) { if (identical(this, other)) return true; return other is CompanyItem && other.company.id == company.id; } @override int get hashCode => company.id.hashCode; @override String toString() { if (isBranch) { return 'CompanyItem.branch(${company.name} of $parentCompanyName)'; } else { return 'CompanyItem.headquarters(${company.name})'; } } }