--------------------------------------------------------------------------------
-- 적용시스템명 : 운영계(ORACLE)
-- 테이블 : ZT_통합코드
-- 테이블ID : ZT_UNFC_CD
--------------------------------------------------------------------------------
CREATE TABLE S_NNZ.ZT_UNFC_CD
(
 UNFC_CD_ID VARCHAR2(50) NOT NULL,
 UNFC_CD_NM VARCHAR2(100) NULL,
 UNFC_CD_HAN_NM VARCHAR2(100) NULL,
 UNFC_CD_ENG_NM VARCHAR2(100) NULL,
 UNFC_CD_DS VARCHAR2(4000) NULL,
 META_SYST_CD VARCHAR2(3) NOT NULL,
 META_DUTJ_TYPE_CD VARCHAR2(5) NOT NULL,
 PUSE_YN VARCHAR2(1) NOT NULL,
 SPPO_UNFC_CD_ID VARCHAR2(50) NULL,
 DATA_LOAD_DT DATE NULL,
 SYST_RGI_DT DATE NOT NULL,
 SYST_RGI_PRAF_NO VARCHAR2(8) NOT NULL,
 SYST_RGI_OGNZ_NO VARCHAR2(7) NOT NULL,
 SYST_RGI_SYST_CD VARCHAR2(3) NOT NULL,
 SYST_RGI_PRGR_ID VARCHAR2(100) NOT NULL,
 SYST_CHG_DT DATE NOT NULL,
 SYST_CHG_PRAF_NO VARCHAR2(8) NOT NULL,
 SYST_CHG_OGNZ_NO VARCHAR2(7) NOT NULL,
 SYST_CHG_SYST_CD VARCHAR2(3) NOT NULL,
 SYST_CHG_PRGR_ID VARCHAR2(100) NOT NULL
)
TABLESPACE TS_ZT_D01;



COMMENT ON TABLE S_NNZ.ZT_UNFC_CD IS 'ZT_통합코드';

COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.UNFC_CD_ID IS '통합코드ID';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.UNFC_CD_NM IS '통합코드명';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.UNFC_CD_HAN_NM IS '통합코드한글명';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.UNFC_CD_ENG_NM IS '통합코드영문명';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.UNFC_CD_DS IS '통합코드설명';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.META_SYST_CD IS '메타시스템코드';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.META_DUTJ_TYPE_CD IS '메타업무유형코드';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.PUSE_YN IS '사용여부';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.SPPO_UNFC_CD_ID IS '상위통합코드ID';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.DATA_LOAD_DT IS '데이터적재일시';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.SYST_RGI_DT IS '시스템등록일시';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.SYST_RGI_PRAF_NO IS '시스템등록인사번호';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.SYST_RGI_OGNZ_NO IS '시스템등록조직번호';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.SYST_RGI_SYST_CD IS '시스템등록시스템코드';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.SYST_RGI_PRGR_ID IS '시스템등록프로그램ID';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.SYST_CHG_DT IS '시스템변경일시';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.SYST_CHG_PRAF_NO IS '시스템변경인사번호';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.SYST_CHG_OGNZ_NO IS '시스템변경조직번호';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.SYST_CHG_SYST_CD IS '시스템변경시스템코드';
COMMENT ON COLUMN S_NNZ.ZT_UNFC_CD.SYST_CHG_PRGR_ID IS '시스템변경프로그램ID';



-- 개별권한 Role
GRANT SELECT ON S_NNZ.ZT_UNFC_CD TO RL_A_EAI_NNZ_SEL;

GRANT INSERT,UPDATE,DELETE ON S_NNZ.ZT_UNFC_CD TO RL_A_EAI_NNZ_IUD;

-- Default Role
GRANT SELECT ON S_NNZ.ZT_UNFC_CD TO RL_NNZ_SEL;

GRANT INSERT,UPDATE,DELETE ON S_NNZ.ZT_UNFC_CD TO RL_NNZ_IUD;



CREATE INDEX S_NNZ.IX_ZT_UNFC_CD_01 ON S_NNZ.ZT_UNFC_CD
(UNFC_CD_NM)
 TABLESPACE TS_ZT_I01;

CREATE INDEX S_NNZ.IX_ZT_UNFC_CD_02 ON S_NNZ.ZT_UNFC_CD
(UNFC_CD_HAN_NM)
 TABLESPACE TS_ZT_I01;

CREATE INDEX S_NNZ.IX_ZT_UNFC_CD_03 ON S_NNZ.ZT_UNFC_CD
(UNFC_CD_ENG_NM)
 TABLESPACE TS_ZT_I01;


CREATE UNIQUE INDEX S_NNZ.PK_ZT_UNFC_CD ON S_NNZ.ZT_UNFC_CD
 (UNFC_CD_ID)
 TABLESPACE TS_ZT_I01;

ALTER TABLE S_NNZ.ZT_UNFC_CD ADD (
 CONSTRAINT PK_ZT_UNFC_CD
 PRIMARY KEY (UNFC_CD_ID)
USING INDEX S_NNZ.PK_ZT_UNFC_CD);

--------------------------------------------------------------------------------
-- 적용시스템명 : 개발계(ORACLE)
-- 테이블 : ZT_통합코드상세
-- 테이블ID : ZT_UNFC_CD_DTPT
--------------------------------------------------------------------------------
CREATE TABLE S_TFG.ZT_UNFC_CD_DTPT
(
 UNFC_CD_ID VARCHAR2(50) NOT NULL,
 CD_VLDT_VALU VARCHAR2(50) NOT NULL,
 CD_VLDT_VALU_INQR_SEQ NUMBER(5) NULL,
 CD_VLDT_VALU_NM VARCHAR2(400) NULL,
 CD_VLDT_VALU_HNGL_ABR_NM VARCHAR2(200) NULL,
 CD_VLDT_VALU_ENGC_ABR_NM VARCHAR2(200) NULL,
 CD_VLDT_VALU_HAN_NM VARCHAR2(400) NULL,
 CD_VLDT_VALU_ENG_NM VARCHAR2(400) NULL,
 CD_VLDT_VALU_DS VARCHAR2(4000) NULL,
 CD_ASRT_TYPE_BIT_CD VARCHAR2(20) NOT NULL,
 SPPO_CD_VLDT_VALU VARCHAR2(50) NULL,
 SPPO_UNFC_CD_ID VARCHAR2(50) NULL,
 VLDT_STRT_YMD DATE NOT NULL,
 VLDT_END_YMD DATE NOT NULL,
 USER_DEF_VALU_1 VARCHAR2(100) NULL,
 USER_DEF_VALU_2 VARCHAR2(100) NULL,
 SYST_RGI_DT DATE NOT NULL,
 SYST_RGI_PRAF_NO VARCHAR2(8) NOT NULL,
 SYST_RGI_OGNZ_NO VARCHAR2(7) NOT NULL,
 SYST_RGI_SYST_CD VARCHAR2(3) NOT NULL,
 SYST_RGI_PRGR_ID VARCHAR2(100) NOT NULL,
 SYST_CHG_DT DATE NOT NULL,
 SYST_CHG_PRAF_NO VARCHAR2(8) NOT NULL,
 SYST_CHG_OGNZ_NO VARCHAR2(7) NOT NULL,
 SYST_CHG_SYST_CD VARCHAR2(3) NOT NULL,
 SYST_CHG_PRGR_ID VARCHAR2(100) NOT NULL,
 DATA_LOAD_DT DATE NOT NULL
)
TABLESPACE TS_TFG_D01;



COMMENT ON TABLE S_TFG.ZT_UNFC_CD_DTPT IS 'ZT_통합코드상세';

COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.UNFC_CD_ID IS '통합코드ID';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.CD_VLDT_VALU IS '코드유효값';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.CD_VLDT_VALU_INQR_SEQ IS '코드유효값조회순서';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.CD_VLDT_VALU_NM IS '코드유효값명';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.CD_VLDT_VALU_HNGL_ABR_NM IS '코드유효값한글약어명';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.CD_VLDT_VALU_ENGC_ABR_NM IS '코드유효값영문약어명';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.CD_VLDT_VALU_HAN_NM IS '코드유효값한글명';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.CD_VLDT_VALU_ENG_NM IS '코드유효값영문명';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.CD_VLDT_VALU_DS IS '코드유효값설명';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.CD_ASRT_TYPE_BIT_CD IS '코드분류유형비트코드';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.SPPO_CD_VLDT_VALU IS '상위코드유효값';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.SPPO_UNFC_CD_ID IS '상위통합코드ID';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.VLDT_STRT_YMD IS '유효시작일자';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.VLDT_END_YMD IS '유효종료일자';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.USER_DEF_VALU_1 IS '사용자정의값1';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.USER_DEF_VALU_2 IS '사용자정의값2';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.SYST_RGI_DT IS '시스템등록일시';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.SYST_RGI_PRAF_NO IS '시스템등록인사번호';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.SYST_RGI_OGNZ_NO IS '시스템등록조직번호';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.SYST_RGI_SYST_CD IS '시스템등록시스템코드';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.SYST_RGI_PRGR_ID IS '시스템등록프로그램ID';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.SYST_CHG_DT IS '시스템변경일시';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.SYST_CHG_PRAF_NO IS '시스템변경인사번호';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.SYST_CHG_OGNZ_NO IS '시스템변경조직번호';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.SYST_CHG_SYST_CD IS '시스템변경시스템코드';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.SYST_CHG_PRGR_ID IS '시스템변경프로그램ID';
COMMENT ON COLUMN S_TFG.ZT_UNFC_CD_DTPT.DATA_LOAD_DT IS '데이터적재일시';



-- Default Role
GRANT INSERT,UPDATE,DELETE ON S_TFG.ZT_UNFC_CD_DTPT TO RL_TFG_IUD;

GRANT SELECT ON S_TFG.ZT_UNFC_CD_DTPT TO RL_TFG_SEL;



CREATE UNIQUE INDEX S_TFG.PK_ZT_UNFC_CD_DTPT ON S_TFG.ZT_UNFC_CD_DTPT
 (UNFC_CD_ID,CD_VLDT_VALU)
 TABLESPACE TS_TFG_I01;

ALTER TABLE S_TFG.ZT_UNFC_CD_DTPT ADD (
 CONSTRAINT PK_ZT_UNFC_CD_DTPT
 PRIMARY KEY (UNFC_CD_ID,CD_VLDT_VALU)
USING INDEX S_TFG.PK_ZT_UNFC_CD_DTPT);

-------------------------------------------------------------------------------------------
-- ZT_MENU 테이블 생성 (메뉴 기본 정보 저장)
CREATE TABLE ZT_MENU (
    MENU_ID          INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,  -- 자동 증가 ID (PK)
    PARENT_MENU_ID   INTEGER,                                           -- 부모 메뉴 ID (FK, NULL 가능 for 루트 메뉴)
    NAME             VARCHAR(100),                                      -- 컴포넌트 이름 (옵셔널)
    PATH             VARCHAR(255) NOT NULL,                             -- 라우트 경로 (필수)
    TITLE            VARCHAR(100) NOT NULL,                             -- 메뉴 이름 (meta.title, 필수)
    ICON             VARCHAR(50),                                       -- 메뉴 아이콘 (옵셔널)
    SHOW_BADGE       VARCHAR(1) DEFAULT 'N' CHECK (SHOW_BADGE IN ('Y', 'N')), -- 배지 표시 여부 (Y/N, 옵셔널)
    SHOW_TEXT_BADGE  VARCHAR(50),                                       -- 텍스트 배지 표시 (옵셔널)
    IS_HIDE          VARCHAR(1) DEFAULT 'N' CHECK (IS_HIDE IN ('Y', 'N')),    -- 메뉴 숨김 여부 (Y/N, 옵셔널)
    IS_HIDE_TAB      VARCHAR(1) DEFAULT 'N' CHECK (IS_HIDE_TAB IN ('Y', 'N')),-- 탭 숨김 여부 (Y/N, 옵셔널)
    LINK             VARCHAR(255),                                      -- 링크 (옵셔널)
    IS_IFRAME        VARCHAR(1) DEFAULT 'N' CHECK (IS_IFRAME IN ('Y', 'N')),  -- iframe 여부 (Y/N, 옵셔널)
    KEEP_ALIVE       VARCHAR(1) DEFAULT 'N' CHECK (KEEP_ALIVE IN ('Y', 'N')), -- 캐시 여부 (Y/N, 옵셔널)
    ORDER_SEQ        INTEGER DEFAULT 0 NOT NULL,                        -- 메뉴 표시 순서 (정렬용, 필수)
    IS_ACTIVE        VARCHAR(1) DEFAULT 'Y' CHECK (IS_ACTIVE IN ('Y', 'N')),  -- 활성 여부 (Y/N, 기본 Y)
    SYST_RGI_DT      DATE,                                              -- 시스템등록일시
    SYST_RGI_PRAF_NO VARCHAR(50),                                       -- 시스템등록인사번호
    SYST_RGI_OGNZ_NO VARCHAR(50),                                       -- 시스템등록조직번호
    SYST_RGI_SYST_CD VARCHAR(50),                                       -- 시스템등록시스템코드
    SYST_RGI_PRGR_ID VARCHAR(50),                                       -- 시스템등록프로그램ID
    SYST_CHG_DT      DATE,                                              -- 시스템변경일시
    SYST_CHG_PRAF_NO VARCHAR(50),                                       -- 시스템변경인사번호
    SYST_CHG_OGNZ_NO VARCHAR(50),                                       -- 시스템변경조직번호
    SYST_CHG_SYST_CD VARCHAR(50),                                       -- 시스템변경시스템코드
    SYST_CHG_PRGR_ID VARCHAR(50)                                        -- 시스템변경프로그램ID
);

-- 제약 조건 추가
ALTER TABLE ZT_MENU ADD CONSTRAINT FK_ZT_MENU_PARENT FOREIGN KEY (PARENT_MENU_ID) REFERENCES ZT_MENU (MENU_ID) ON DELETE CASCADE;  -- 부모 삭제 시 자식도 삭제 (계층 삭제 지원)

-- 인덱스 생성 (조회 최적화)
CREATE INDEX IDX_ZT_MENU_PARENT ON ZT_MENU (PARENT_MENU_ID);
CREATE INDEX IDX_ZT_MENU_PATH ON ZT_MENU (PATH);

-- 컬럼 코멘트 추가
COMMENT ON COLUMN ZT_MENU.MENU_ID IS '메뉴 ID (PK)';
COMMENT ON COLUMN ZT_MENU.PARENT_MENU_ID IS '부모 메뉴 ID (FK)';
COMMENT ON COLUMN ZT_MENU.NAME IS '컴포넌트 이름';
COMMENT ON COLUMN ZT_MENU.PATH IS '라우트 경로';
COMMENT ON COLUMN ZT_MENU.TITLE IS '메뉴 이름';
COMMENT ON COLUMN ZT_MENU.ICON IS '메뉴 아이콘';
COMMENT ON COLUMN ZT_MENU.SHOW_BADGE IS '배지 표시 여부 (Y/N)';
COMMENT ON COLUMN ZT_MENU.SHOW_TEXT_BADGE IS '텍스트 배지 표시';
COMMENT ON COLUMN ZT_MENU.IS_HIDE IS '메뉴 숨김 여부 (Y/N)';
COMMENT ON COLUMN ZT_MENU.IS_HIDE_TAB IS '탭 숨김 여부 (Y/N)';
COMMENT ON COLUMN ZT_MENU.LINK IS '링크';
COMMENT ON COLUMN ZT_MENU.IS_IFRAME IS 'iframe 여부 (Y/N)';
COMMENT ON COLUMN ZT_MENU.KEEP_ALIVE IS '캐시 여부 (Y/N)';
COMMENT ON COLUMN ZT_MENU.ORDER_SEQ IS '메뉴 표시 순서';
COMMENT ON COLUMN ZT_MENU.IS_ACTIVE IS '활성 여부 (Y/N)';
COMMENT ON COLUMN ZT_MENU.SYST_RGI_DT IS '시스템등록일시';
COMMENT ON COLUMN ZT_MENU.SYST_RGI_PRAF_NO IS '시스템등록인사번호';
COMMENT ON COLUMN ZT_MENU.SYST_RGI_OGNZ_NO IS '시스템등록조직번호';
COMMENT ON COLUMN ZT_MENU.SYST_RGI_SYST_CD IS '시스템등록시스템코드';
COMMENT ON COLUMN ZT_MENU.SYST_RGI_PRGR_ID IS '시스템등록프로그램ID';
COMMENT ON COLUMN ZT_MENU.SYST_CHG_DT IS '시스템변경일시';
COMMENT ON COLUMN ZT_MENU.SYST_CHG_PRAF_NO IS '시스템변경인사번호';
COMMENT ON COLUMN ZT_MENU.SYST_CHG_OGNZ_NO IS '시스템변경조직번호';
COMMENT ON COLUMN ZT_MENU.SYST_CHG_SYST_CD IS '시스템변경시스템코드';
COMMENT ON COLUMN ZT_MENU.SYST_CHG_PRGR_ID IS '시스템변경프로그램ID';

-- ZT_MENU_AUTH 테이블 생성 (메뉴 권한 목록 저장)
CREATE TABLE ZT_MENU_AUTH (
    AUTH_ID          INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,  -- 자동 증가 ID (PK)
    MENU_ID          INTEGER NOT NULL,                                  -- 메뉴 ID (FK)
    TITLE            VARCHAR(100) NOT NULL,                             -- 권한 제목 (e.g., ''추가'', ''편집'')
    AUTH_MARK        VARCHAR(100) NOT NULL,                             -- 권한 마크 (e.g., ''add'', ''edit'')
    ORDER_SEQ        INTEGER DEFAULT 0 NOT NULL,                        -- 권한 표시 순서 (정렬용)
    SYST_RGI_DT      DATE,                                              -- 시스템등록일시
    SYST_RGI_PRAF_NO VARCHAR(50),                                       -- 시스템등록인사번호
    SYST_RGI_OGNZ_NO VARCHAR(50),                                       -- 시스템등록조직번호
    SYST_RGI_SYST_CD VARCHAR(50),                                       -- 시스템등록시스템코드
    SYST_RGI_PRGR_ID VARCHAR(50),                                       -- 시스템등록프로그램ID
    SYST_CHG_DT      DATE,                                              -- 시스템변경일시
    SYST_CHG_PRAF_NO VARCHAR(50),                                       -- 시스템변경인사번호
    SYST_CHG_OGNZ_NO VARCHAR(50),                                       -- 시스템변경조직번호
    SYST_CHG_SYST_CD VARCHAR(50),                                       -- 시스템변경시스템코드
    SYST_CHG_PRGR_ID VARCHAR(50)                                        -- 시스템변경프로그램ID
);

-- 제약 조건 추가
ALTER TABLE ZT_MENU_AUTH ADD CONSTRAINT FK_ZT_MENU_AUTH_MENU FOREIGN KEY (MENU_ID) REFERENCES ZT_MENU (MENU_ID) ON DELETE CASCADE;  -- 메뉴 삭제 시 권한도 삭제

-- 인덱스 생성
CREATE INDEX IDX_ZT_MENU_AUTH_MENU ON ZT_MENU_AUTH (MENU_ID);

-- 컬럼 코멘트 추가
COMMENT ON COLUMN ZT_MENU_AUTH.AUTH_ID IS '권한 ID (PK)';
COMMENT ON COLUMN ZT_MENU_AUTH.MENU_ID IS '메뉴 ID (FK)';
COMMENT ON COLUMN ZT_MENU_AUTH.TITLE IS '권한 제목';
COMMENT ON COLUMN ZT_MENU_AUTH.AUTH_MARK IS '권한 마크';
COMMENT ON COLUMN ZT_MENU_AUTH.ORDER_SEQ IS '권한 표시 순서';
COMMENT ON COLUMN ZT_MENU_AUTH.SYST_RGI_DT IS '시스템등록일시';
COMMENT ON COLUMN ZT_MENU_AUTH.SYST_RGI_PRAF_NO IS '시스템등록인사번호';
COMMENT ON COLUMN ZT_MENU_AUTH.SYST_RGI_OGNZ_NO IS '시스템등록조직번호';
COMMENT ON COLUMN ZT_MENU_AUTH.SYST_RGI_SYST_CD IS '시스템등록시스템코드';
COMMENT ON COLUMN ZT_MENU_AUTH.SYST_RGI_PRGR_ID IS '시스템등록프로그램ID';
COMMENT ON COLUMN ZT_MENU_AUTH.SYST_CHG_DT IS '시스템변경일시';
COMMENT ON COLUMN ZT_MENU_AUTH.SYST_CHG_PRAF_NO IS '시스템변경인사번호';
COMMENT ON COLUMN ZT_MENU_AUTH.SYST_CHG_OGNZ_NO IS '시스템변경조직번호';
COMMENT ON COLUMN ZT_MENU_AUTH.SYST_CHG_SYST_CD IS '시스템변경시스템코드';
COMMENT ON COLUMN ZT_MENU_AUTH.SYST_CHG_PRGR_ID IS '시스템변경프로그램ID';