-- ============================================================
-- 개발관리(PART/E-BOM/설계변경) 운영 DDL — wace_plm 운영DB(211.115.91.141:11133/waceplm) 추출
-- 추출일: 2026-05-12
-- 추출 방법: information_schema + pg_indexes + pg_description 쿼리
--            (pg_dump 14.19 ↔ PG 16.8 mismatch로 pg_dump 사용 불가)
--
-- 대상 테이블 9개 (운영 카운트):
--   admin_supply_mng         28 cols /      7건  ← 공급업체 마스터(관리자)
--   bom_part_qty             19 cols /    835건  ← BOM 수량 트리
--   order_spec_mng           12 cols / 12,522건  ← 발주 스펙 이력
--   part_bom_report          23 cols /     40건  ← BOM 리포트 헤더
--   part_mng_history         59 cols /    263건  ← 파트 이력(설계변경)
--   product_mgmt_upg_detail   7 cols /     87건  ← 제품 업그레이드 디테일
--   product_mgmt_upg_master   5 cols /      6건  ← 제품 업그레이드 마스터
--   sales_bom_report         16 cols /  1,529건  ← 영업 BOM 단가
--   supply_mng               29 cols /      1건  ← 공급업체(고객)
--
-- 비고:
--  · 운영 스키마 1:1 보존 — 컬럼 순서/타입/길이/default 모두 운영과 동일.
--  · objid 컬럼은 wace Java 측에서 UUID/임의 채번(연관 시퀀스 없음 — 확인 완료).
--  · admin_supply_mng.objid·supply_mng.objid는 numeric, default 0(운영 그대로).
--  · admin_supply_mng.employee_email 운영 타입이 'xid'(PostgreSQL 시스템 타입, transaction id)
--    → 데이터 의미(이메일)와 무관한 운영 측 추정 실수. character varying 으로 정정 적용.
--  · part_mng_history.objid는 numeric NOT NULL (PK)이지만 default 미지정.
--  · product_mgmt_upg_master PK는 (objid, target_objid) 복합키.
--  · sales_bom_report 의 supply_objid*/price* 컬럼 length 가 들쭉날쭉한 부분은 운영 그대로.
--  · sales_bom_report_parent_objid_idx 는 UNIQUE 인덱스(운영 정의 그대로 — 1 BOM당 1 단가).
--  · company_code 분기 없음(vexplor_rps는 COMPANY_16 단독).
-- ============================================================

BEGIN;

-- ------------------------------------------------------------
-- 1) admin_supply_mng (공급업체 마스터 — 관리자 측 입력)
-- ------------------------------------------------------------
DROP TABLE IF EXISTS admin_supply_mng CASCADE;
CREATE TABLE admin_supply_mng (
    objid               numeric           NOT NULL DEFAULT '0'::numeric,
    supply_code         character varying(100) DEFAULT 'NULL::character varying'::character varying,
    supply_name         character varying(100) DEFAULT 'NULL::character varying'::character varying,
    reg_no              character varying(100) DEFAULT 'NULL::character varying'::character varying,
    supply_address      character varying(500) DEFAULT 'NULL::character varying'::character varying,
    supply_busname      character varying(100) DEFAULT 'NULL::character varying'::character varying,
    supply_stockname    character varying(100) DEFAULT 'NULL::character varying'::character varying,
    supply_tel_no       character varying     DEFAULT 'NULL::character varying'::character varying,
    supply_fax_no       character varying     DEFAULT 'NULL::character varying'::character varying,
    charge_user_name    character varying     DEFAULT 'NULL::character varying'::character varying,
    payment_method      character varying,
    reg_id              character varying     DEFAULT 'NULL::character varying'::character varying,
    reg_date            timestamp,
    status              character varying     DEFAULT 'NULL::character varying'::character varying,
    area_cd             character varying     DEFAULT 'NULL::character varying'::character varying,
    bus_reg_no          character varying     DEFAULT 'NULL::character varying'::character varying,
    office_no           character varying     DEFAULT 'NULL::character varying'::character varying,
    email               character varying     DEFAULT 'NULL::character varying'::character varying,
    account_code        character varying,
    remark              character varying,
    account_bank        character varying,
    account_number      character varying,
    account_user_name   character varying,
    employee_name       character varying,
    employee_position   character varying,
    employee_number     character varying,
    -- 운영은 'xid' 시스템 타입으로 잘못 정의됨. 의미상 이메일 → character varying 으로 정정.
    employee_email      character varying,
    david               character varying(50),
    CONSTRAINT admin_supply_mng_pkey PRIMARY KEY (objid)
);

-- ------------------------------------------------------------
-- 2) bom_part_qty (BOM 수량 트리 — 835건)
-- ------------------------------------------------------------
DROP TABLE IF EXISTS bom_part_qty CASCADE;
CREATE TABLE bom_part_qty (
    bom_report_objid    character varying(64)  NOT NULL,
    objid               character varying(64)  NOT NULL,
    parent_objid        character varying(64)  DEFAULT 'NULL::character varying'::character varying,
    child_objid         character varying(64)  DEFAULT 'NULL::character varying'::character varying,
    parent_part_no      character varying(64)  DEFAULT 'NULL::character varying'::character varying,
    part_no             character varying(64)  DEFAULT 'NULL::character varying'::character varying,
    qty                 character varying,
    regdate             timestamp,
    seq                 integer,
    status              character varying,
    deploy_date         character varying,
    deploy_user_id      character varying,
    edit_date           character varying,
    writer              character varying,
    qty_temp            character varying,
    last_part_objid     character varying,
    editer              character varying,
    item_qty            character varying,
    supplier            character varying,
    CONSTRAINT bom_part_qty_pkey PRIMARY KEY (objid)
);
CREATE INDEX bom_part_qty_bom_report_objid2_idx ON bom_part_qty USING btree (bom_report_objid, last_part_objid, part_no);
CREATE INDEX bom_part_qty_bom_report_objid_idx  ON bom_part_qty USING btree (bom_report_objid);
CREATE INDEX bom_part_qty_last_part_objid_idx   ON bom_part_qty USING btree (last_part_objid);
CREATE INDEX bom_part_qty_parent_objid_idx      ON bom_part_qty USING btree (parent_objid);
CREATE INDEX idx_bom_part_qty_part_no           ON bom_part_qty USING btree (part_no) WHERE ((part_no IS NOT NULL) AND ((part_no)::text <> ''::text));

COMMENT ON COLUMN bom_part_qty.status          IS '상태';
COMMENT ON COLUMN bom_part_qty.deploy_date     IS '배포일';
COMMENT ON COLUMN bom_part_qty.deploy_user_id  IS '배포자';
COMMENT ON COLUMN bom_part_qty.edit_date       IS '수정일';
COMMENT ON COLUMN bom_part_qty.writer          IS '등록자';
COMMENT ON COLUMN bom_part_qty.qty_temp        IS '수량(설변중)';
COMMENT ON COLUMN bom_part_qty.last_part_objid IS '마지막 품번키';
COMMENT ON COLUMN bom_part_qty.editer          IS '수정자';
COMMENT ON COLUMN bom_part_qty.item_qty        IS '항목수량';
COMMENT ON COLUMN bom_part_qty.supplier        IS '공급업체';

-- ------------------------------------------------------------
-- 3) order_spec_mng (발주 스펙 이력 — 12,522건, 운영 최다)
-- ------------------------------------------------------------
DROP TABLE IF EXISTS order_spec_mng CASCADE;
CREATE TABLE order_spec_mng (
    objid           character varying  NOT NULL,
    seq             character varying  NOT NULL,
    part_objid      character varying  NOT NULL,
    partner_rank    character varying,
    partner_objid   character varying,
    partner_price   character varying,
    partner_qty     character varying,
    apply_date      character varying,
    remark          character varying,
    regdate         timestamp,
    is_last         character varying,
    writer          character varying,
    CONSTRAINT order_spec_mng_pkey PRIMARY KEY (objid)
);

-- ------------------------------------------------------------
-- 4) part_bom_report (BOM 리포트 헤더 — 40건)
-- ------------------------------------------------------------
DROP TABLE IF EXISTS part_bom_report CASCADE;
CREATE TABLE part_bom_report (
    objid               character varying      NOT NULL DEFAULT ''::character varying,
    customer_objid      character varying,
    contract_objid      character varying,
    unit_code           character varying,
    revision            character varying,
    writer              character varying(64),
    regdate             timestamp,
    status              character varying(64),
    deploy_date         character varying(64),
    eo_no               character varying(100),
    eo_date             character varying(100),
    note                character varying(2000),
    edit_date           timestamp,
    editer              character varying,
    unit_code_old       character varying,
    multi_break_yn      character varying,
    multi_yn            character varying,
    multi_master_yn     character varying,
    multi_master_objid  character varying,
    product_cd          character varying,
    part_no             character varying,
    part_name           character varying,
    version             character varying,
    CONSTRAINT part_bom_report_pkey PRIMARY KEY (objid)
);
CREATE INDEX idx_part_bom_report_customer         ON part_bom_report USING btree (customer_objid) WHERE ((customer_objid IS NOT NULL) AND ((customer_objid)::text <> ''::text));
CREATE INDEX idx_part_bom_report_regdate          ON part_bom_report USING btree (regdate DESC);
CREATE INDEX idx_part_bom_report_writer           ON part_bom_report USING btree (writer);
CREATE INDEX part_bom_report_contract_objid_idx   ON part_bom_report USING btree (contract_objid);
CREATE INDEX part_bom_report_unit_code_idx        ON part_bom_report USING btree (unit_code, contract_objid);

COMMENT ON COLUMN part_bom_report.objid              IS 'OBJECT ID';
COMMENT ON COLUMN part_bom_report.customer_objid     IS '고객사 OBJID';
COMMENT ON COLUMN part_bom_report.contract_objid     IS '계약objid';
COMMENT ON COLUMN part_bom_report.unit_code          IS 'unit';
COMMENT ON COLUMN part_bom_report.revision           IS 'rev';
COMMENT ON COLUMN part_bom_report.writer             IS '작성자';
COMMENT ON COLUMN part_bom_report.regdate            IS '등록일';
COMMENT ON COLUMN part_bom_report.status             IS '상태';
COMMENT ON COLUMN part_bom_report.deploy_date        IS '배포일';
COMMENT ON COLUMN part_bom_report.edit_date          IS '수정일';
COMMENT ON COLUMN part_bom_report.editer             IS '수정자';
COMMENT ON COLUMN part_bom_report.unit_code_old      IS 'UNIT_CODE';
COMMENT ON COLUMN part_bom_report.multi_break_yn    IS '동시적용프로젝트 깨짐 여부';
COMMENT ON COLUMN part_bom_report.multi_yn          IS '동시적용프로젝트 여부';
COMMENT ON COLUMN part_bom_report.multi_master_yn   IS '동시적용프로젝트 마스터 여부';
COMMENT ON COLUMN part_bom_report.multi_master_objid IS '동시적용프로젝트 마스터 키';

-- ------------------------------------------------------------
-- 5) part_mng_history (파트 이력 — 설계변경 핵심, 59 cols / 263건)
-- ------------------------------------------------------------
DROP TABLE IF EXISTS part_mng_history CASCADE;
CREATE TABLE part_mng_history (
    objid                     numeric           NOT NULL,
    product_mgmt_objid        character varying(100) DEFAULT NULL::character varying,
    upg_no                    character varying(100) DEFAULT NULL::character varying,
    part_no                   character varying(100) DEFAULT NULL::character varying,
    part_name                 character varying(100) DEFAULT NULL::character varying,
    unit                      character varying(50)  DEFAULT NULL::character varying,
    qty                       character varying(50)  DEFAULT NULL::character varying,
    spec                      character varying(100) DEFAULT 'NULL::character varying'::character varying,
    material                  character varying(100) DEFAULT NULL::character varying,
    weight                    character varying(50)  DEFAULT NULL::character varying,
    part_type                 character varying(100) DEFAULT NULL::character varying,
    remark                    character varying(1000) DEFAULT NULL::character varying,
    es_spec                   character varying(100) DEFAULT NULL::character varying,
    ms_spec                   character varying(100) DEFAULT NULL::character varying,
    change_option             character varying(50)  DEFAULT NULL::character varying,
    design_apply_point        character varying(50)  DEFAULT NULL::character varying,
    management_flag           character varying(50)  DEFAULT NULL::character varying,
    revision                  character varying(50)  DEFAULT NULL::character varying,
    status                    character varying(30)  DEFAULT NULL::character varying,
    reg_date                  timestamp,
    edit_date                 timestamp,
    writer                    character varying(30)  DEFAULT NULL::character varying,
    is_last                   character varying(5)   DEFAULT NULL::character varying,
    eo_no                     character varying,
    eo_temp                   character varying,
    excel_upload_seq          character varying,
    sourcing_code             character varying,
    sub_material              character varying(100) DEFAULT 'NULL::character varying'::character varying,
    parent_part_no            character varying,
    design_date               character varying,
    eo_date                   character varying,
    deploy_date               timestamp,
    thickness                 character varying,
    width                     character varying,
    height                    character varying,
    out_diameter              character varying,
    in_diameter               character varying,
    length                    character varying,
    supply_code               character varying,
    change_type               character varying,
    contract_objid            character varying,
    maker                     character varying,
    qty_temp                  character varying,
    bom_report_objid          character varying,
    parent_part_objid         character varying,
    parent_qty_child_objid    character varying,
    bom_qty_status            character varying,
    his_reg_date              timestamp,
    his_writer                character varying,
    his_status                character varying,
    qty_child_objid           character varying,
    bom_status                character varying,
    bom_deploy_date           timestamp,
    chg_part_objid            character varying,
    chg_part_no               character varying,
    chg_part_rev              character varying,
    heat_treatment_hardness   character varying,
    heat_treatment_method     character varying,
    surface_treatment         character varying,
    CONSTRAINT part_mng_history_pkey PRIMARY KEY (objid)
);

COMMENT ON COLUMN part_mng_history.qty_temp               IS '수량(설변중)';
COMMENT ON COLUMN part_mng_history.bom_report_objid       IS 'BOM 키';
COMMENT ON COLUMN part_mng_history.parent_part_objid      IS '부모 파트 키';
COMMENT ON COLUMN part_mng_history.parent_qty_child_objid IS '부모 구조 키';
COMMENT ON COLUMN part_mng_history.bom_qty_status         IS 'BOM 상태';
COMMENT ON COLUMN part_mng_history.his_reg_date           IS '등록일';
COMMENT ON COLUMN part_mng_history.his_writer             IS '등록자';
COMMENT ON COLUMN part_mng_history.his_status             IS '상태';
COMMENT ON COLUMN part_mng_history.qty_child_objid        IS '구조 키';
COMMENT ON COLUMN part_mng_history.bom_status             IS 'BOM 상태';
COMMENT ON COLUMN part_mng_history.bom_deploy_date        IS '배포일';

-- ------------------------------------------------------------
-- 6) product_mgmt_upg_detail (제품 업그레이드 디테일 — 87건)
-- ------------------------------------------------------------
DROP TABLE IF EXISTS product_mgmt_upg_detail CASCADE;
CREATE TABLE product_mgmt_upg_detail (
    objid           integer       NOT NULL,
    target_objid    integer,
    upg_name        character varying(100),
    upg_code        character varying(100),
    vc              character varying(100),
    note            character varying(1000),
    product_objid   integer,
    CONSTRAINT product_mgmt_upg_detail_pkey PRIMARY KEY (objid)
);

COMMENT ON COLUMN product_mgmt_upg_detail.objid        IS 'objid';
COMMENT ON COLUMN product_mgmt_upg_detail.target_objid IS 'upg_masterobjid';
COMMENT ON COLUMN product_mgmt_upg_detail.upg_name     IS 'upg명';
COMMENT ON COLUMN product_mgmt_upg_detail.upg_code     IS 'upg코드';
COMMENT ON COLUMN product_mgmt_upg_detail.vc           IS 'vc';
COMMENT ON COLUMN product_mgmt_upg_detail.note         IS '비고';

-- ------------------------------------------------------------
-- 7) product_mgmt_upg_master (제품 업그레이드 마스터 — 6건, 복합 PK)
-- ------------------------------------------------------------
DROP TABLE IF EXISTS product_mgmt_upg_master CASCADE;
CREATE TABLE product_mgmt_upg_master (
    objid          integer            NOT NULL,
    target_objid   integer            NOT NULL,
    spec_name      character varying  NOT NULL,
    writer         character varying,
    regdate        timestamp,
    CONSTRAINT product_mgmt_upg_master_pkey PRIMARY KEY (objid, target_objid)
);

COMMENT ON COLUMN product_mgmt_upg_master.objid        IS '제품사양마스터코드';
COMMENT ON COLUMN product_mgmt_upg_master.target_objid IS '양산마스터코드';
COMMENT ON COLUMN product_mgmt_upg_master.spec_name    IS '사양명';
COMMENT ON COLUMN product_mgmt_upg_master.writer       IS '작성자';
COMMENT ON COLUMN product_mgmt_upg_master.regdate      IS '동록일';

-- ------------------------------------------------------------
-- 8) sales_bom_report (영업 BOM 단가 — 1,529건)
-- ------------------------------------------------------------
DROP TABLE IF EXISTS sales_bom_report CASCADE;
CREATE TABLE sales_bom_report (
    objid           character varying       NOT NULL DEFAULT ''::character varying,
    parent_objid    character varying,
    supply_objid    character varying,
    price           character varying,
    supply_objid1   character varying,
    price1          character varying(64),
    supply_objid2   character varying(100),
    price2          character varying(64),
    supply_objid3   character varying(64),
    price3          character varying(100),
    supply_objid4   character varying(100),
    price4          character varying(2000),
    writer          character varying,
    regdate         timestamp,
    update_date     timestamp,
    modifier        character varying,
    CONSTRAINT sales_bom_report_pkey PRIMARY KEY (objid)
);
-- 운영 인덱스는 UNIQUE — 1 BOM(parent_objid) 당 1 단가행
CREATE UNIQUE INDEX sales_bom_report_parent_objid_idx ON sales_bom_report USING btree (parent_objid);

COMMENT ON COLUMN sales_bom_report.objid         IS '키';
COMMENT ON COLUMN sales_bom_report.parent_objid  IS 'bom_report_objid';
COMMENT ON COLUMN sales_bom_report.supply_objid  IS '공급업체key';
COMMENT ON COLUMN sales_bom_report.price         IS '단가';
COMMENT ON COLUMN sales_bom_report.supply_objid1 IS '레이져업체명';
COMMENT ON COLUMN sales_bom_report.price1        IS '단가';
COMMENT ON COLUMN sales_bom_report.supply_objid2 IS '용접업체명';
COMMENT ON COLUMN sales_bom_report.price2        IS '단가';
COMMENT ON COLUMN sales_bom_report.supply_objid3 IS '가공업체명';
COMMENT ON COLUMN sales_bom_report.price3        IS '단가';
COMMENT ON COLUMN sales_bom_report.supply_objid4 IS '후처리';
COMMENT ON COLUMN sales_bom_report.price4        IS '단가';
COMMENT ON COLUMN sales_bom_report.writer        IS '담당자';
COMMENT ON COLUMN sales_bom_report.regdate       IS '작성일';
COMMENT ON COLUMN sales_bom_report.update_date   IS '수정일';
COMMENT ON COLUMN sales_bom_report.modifier      IS '수정자';

-- ------------------------------------------------------------
-- 9) supply_mng (공급업체/고객 — 1건)
-- ------------------------------------------------------------
DROP TABLE IF EXISTS supply_mng CASCADE;
CREATE TABLE supply_mng (
    objid              numeric           NOT NULL DEFAULT 0,
    supply_code        character varying(100) DEFAULT NULL::character varying,
    supply_name        character varying(100) DEFAULT NULL::character varying,
    reg_no             character varying(100) DEFAULT NULL::character varying,
    supply_address     character varying(500) DEFAULT NULL::character varying,
    supply_busname     character varying(100) DEFAULT NULL::character varying,
    supply_stockname   character varying(100) DEFAULT NULL::character varying,
    supply_tel_no      character varying(30)  DEFAULT NULL::character varying,
    supply_fax_no      character varying(30)  DEFAULT NULL::character varying,
    charge_user_name   character varying(100) DEFAULT NULL::character varying,
    payment_method     character varying(100),
    reg_id             character varying(100) DEFAULT NULL::character varying,
    reg_date           timestamp,
    status             character varying(32)  DEFAULT NULL::character varying,
    area_cd            character varying(32)  DEFAULT NULL::character varying,
    bus_reg_no         character varying(100) DEFAULT NULL::character varying,
    office_no          character varying(32)  DEFAULT 'NULL::character varying'::character varying,
    email              character varying(32)  DEFAULT 'NULL::character varying'::character varying,
    cus_no             character varying,
    manager1_name      character varying(100),
    manager1_email     character varying(100),
    manager2_name      character varying(100),
    manager2_email     character varying(100),
    manager3_name      character varying(100),
    manager3_email     character varying(100),
    manager4_name      character varying(100),
    manager4_email     character varying(100),
    manager5_name      character varying(100),
    manager5_email     character varying(100),
    CONSTRAINT supply_mng_pkey PRIMARY KEY (objid)
);

COMMENT ON COLUMN supply_mng.supply_code      IS '구분';
COMMENT ON COLUMN supply_mng.supply_name      IS '고객명';
COMMENT ON COLUMN supply_mng.reg_no           IS '법인/주민번호';
COMMENT ON COLUMN supply_mng.supply_address   IS '주소';
COMMENT ON COLUMN supply_mng.supply_busname   IS '업태';
COMMENT ON COLUMN supply_mng.supply_stockname IS '업종';
COMMENT ON COLUMN supply_mng.supply_tel_no    IS '핸드폰';
COMMENT ON COLUMN supply_mng.supply_fax_no    IS '팩스번호';
COMMENT ON COLUMN supply_mng.charge_user_name IS '대표자명';
COMMENT ON COLUMN supply_mng.reg_id           IS '실사용자명';
COMMENT ON COLUMN supply_mng.reg_date         IS '등록일';
COMMENT ON COLUMN supply_mng.status           IS '상태';
COMMENT ON COLUMN supply_mng.area_cd          IS '지역';
COMMENT ON COLUMN supply_mng.bus_reg_no       IS '사업자등록번호';
COMMENT ON COLUMN supply_mng.office_no        IS '오피스no';
COMMENT ON COLUMN supply_mng.email            IS '이메일';
COMMENT ON COLUMN supply_mng.cus_no           IS '고객번호';
COMMENT ON COLUMN supply_mng.manager1_name    IS '담당자1 이름';
COMMENT ON COLUMN supply_mng.manager1_email   IS '담당자1 이메일';
COMMENT ON COLUMN supply_mng.manager2_name    IS '담당자2 이름';
COMMENT ON COLUMN supply_mng.manager2_email   IS '담당자2 이메일';
COMMENT ON COLUMN supply_mng.manager3_name    IS '담당자3 이름';
COMMENT ON COLUMN supply_mng.manager3_email   IS '담당자3 이메일';
COMMENT ON COLUMN supply_mng.manager4_name    IS '담당자4 이름';
COMMENT ON COLUMN supply_mng.manager4_email   IS '담당자4 이메일';
COMMENT ON COLUMN supply_mng.manager5_name    IS '담당자5 이름';
COMMENT ON COLUMN supply_mng.manager5_email   IS '담당자5 이메일';

COMMIT;

-- ============================================================
-- vexplor_rps 적용 방법 (메인 agent 검토 후 직접 실행):
--   PGPASSWORD='vexplor0909!!' psql -h 211.115.91.141 -p 11134 -U postgres -d vexplor_rps \
--     -f /Users/jhj/vexplor_rps/docs/migration/development/ddl-extracted/300_part_bom.sql
--
-- 적용 후 검증:
--   PGPASSWORD='vexplor0909!!' psql -h 211.115.91.141 -p 11134 -U postgres -d vexplor_rps -c "
--     SELECT relname, (SELECT count(*) FROM pg_attribute WHERE attrelid = c.oid AND attnum > 0 AND NOT attisdropped) AS cols
--     FROM pg_class c WHERE relkind='r' AND relnamespace=(SELECT oid FROM pg_namespace WHERE nspname='public')
--     AND relname IN ('admin_supply_mng','bom_part_qty','order_spec_mng','part_bom_report','part_mng_history','product_mgmt_upg_detail','product_mgmt_upg_master','sales_bom_report','supply_mng')
--     ORDER BY relname;"
--   기대값: 28 / 19 / 12 / 23 / 59 / 7 / 5 / 16 / 29
-- ============================================================
