CREATE SCHEMA DFXCONSOLE;

CREATE TABLE TB_CODE (
    CODE_GROUP VARCHAR(64) NOT NULL
    , CODE_NAME VARCHAR(64) NOT NULL
    , CODE_VALUE VARCHAR(1024) NOT NULL
    , CODE_DESCRIPTION VARCHAR(1024)
    , ORDER_NO DECIMAL(5) NOT NULL
    , PARENT_CODE_NAME VARCHAR(64)
    , USE_YN VARCHAR(1) NOT NULL
    , CONSTRAINT PK_CODE PRIMARY KEY (CODE_GROUP, CODE_NAME)
);

CREATE TABLE TB_DFX_USER (
    USER_UUID VARCHAR(64) NOT NULL
    , USER_EMAIL VARCHAR(320) NOT NULL
    , USER_PASSWORD VARCHAR(512)
    , USER_NICK VARCHAR(512) NOT NULL
    , USER_REGISTER_TIME VARCHAR(64)
    , LAST_LOGIN_TIME VARCHAR(64)
    , CONSTRAINT PK_DFX_USER PRIMARY KEY (USER_UUID)
);
COMMENT ON TABLE TB_DFX_USER IS 'DFXCONSOLE 사용자';
COMMENT ON COLUMN TB_DFX_USER.USER_UUID IS '사용자 UUID - 자동생성됨';
COMMENT ON COLUMN TB_DFX_USER.USER_EMAIL IS '사용자 email - 로그인 아이디로 사용됨';
COMMENT ON COLUMN TB_DFX_USER.USER_PASSWORD IS '사용자 비밀번호';
COMMENT ON COLUMN TB_DFX_USER.USER_NICK IS '닉네임';
COMMENT ON COLUMN TB_DFX_USER.USER_REGISTER_TIME IS '가입일시';
COMMENT ON COLUMN TB_DFX_USER.LAST_LOGIN_TIME IS '마지막 로그인 일시';
CREATE UNIQUE INDEX IX_DFX_USER_01 ON TB_DFX_USER(USER_EMAIL);


CREATE TABLE TB_DFX_AGENT_INFO (
    AGENT_ID VARCHAR(256) NOT NULL
    , HOST_NAME VARCHAR(256) NOT NULL
    , LISTEN_PORT DECIMAL(5) NOT NULL
    , DESCRIPTION VARCHAR(2048)
    , POSTMAN_COUNT DECIMAL(9)
    , DROPBOX_COUNT DECIMAL(9)
    , STATUS_CODE VARCHAR(64) NOT NULL
    , LAST_STATUS_TS TIMESTAMPTZ(3) NOT NULL
    , SETTINGS_DATA TEXT
    , CONSTRAINT PK_DFX_AGENT_CONFIG PRIMARY KEY (AGENT_ID)
);
COMMENT ON TABLE TB_DFX_AGENT_INFO IS '에이전트 설정';
COMMENT ON COLUMN TB_DFX_AGENT_INFO.AGENT_ID IS '에이전트ID';
COMMENT ON COLUMN TB_DFX_AGENT_INFO.HOST_NAME IS '에이전트가 설치된 HOSTNAME 또는 IP';
COMMENT ON COLUMN TB_DFX_AGENT_INFO.LISTEN_PORT IS '에이전트 수신 포트';
COMMENT ON COLUMN TB_DFX_AGENT_INFO.DESCRIPTION IS '에이전트 설명';
COMMENT ON COLUMN TB_DFX_AGENT_INFO.POSTMAN_COUNT IS 'POSTMAN 갯수';
COMMENT ON COLUMN TB_DFX_AGENT_INFO.DROPBOX_COUNT IS 'DROPBOX 갯수';
COMMENT ON COLUMN TB_DFX_AGENT_INFO.STATUS_CODE IS '상태코드 CODE_GROUP: AGENT_STATUS_CODE';
COMMENT ON COLUMN TB_DFX_AGENT_INFO.LAST_STATUS_TS IS '최종상태확인 시간';
COMMENT ON COLUMN TB_DFX_AGENT_INFO.SETTINGS_DATA IS '에이전스 설정 파일 내용';


CREATE TABLE TB_DFX_DROPBOX (
    AGENT_ID VARCHAR(256) NOT NULL
    , DROPBOX_ID VARCHAR(256) NOT NULL
    , TASK_TYPE_CODE VARCHAR(64) NOT NULL
    , DATA_SOURCE_ID VARCHAR(256)
    , SQL_ID VARCHAR(256)
    , DESCRIPTION VARCHAR(2048)
    , CONSTRAINT PK_DFX_DROPBOX PRIMARY KEY (AGENT_ID, DROPBOX_ID)
);
COMMENT ON TABLE TB_DFX_DROPBOX IS 'DROPBOX 설정';
COMMENT ON COLUMN TB_DFX_DROPBOX.AGENT_ID IS '에이전트ID';
COMMENT ON COLUMN TB_DFX_DROPBOX.DROPBOX_ID IS 'DROPBOX ID';
COMMENT ON COLUMN TB_DFX_DROPBOX.TASK_TYPE_CODE IS 'CODE DROPBOX_TASK_TYPE';
COMMENT ON COLUMN TB_DFX_DROPBOX.DATA_SOURCE_ID IS 'DATA SOURCE ID';
COMMENT ON COLUMN TB_DFX_DROPBOX.SQL_ID IS 'SQL_ID';
COMMENT ON COLUMN TB_DFX_DROPBOX.DESCRIPTION IS 'DESCRIPTION';

CREATE TABLE TB_DFX_POSTMAN (
    AGENT_ID VARCHAR(256) NOT NULL
    , POSTMAN_ID VARCHAR(256) NOT NULL
    , POSTMAN_TASK_TYPE_CODE VARCHAR(64) NOT NULL
    , POSTMAN_ACTION_TYPE_CODE VARCHAR(64) NOT NULL
    , POSTMAN_ACTION_CRON VARCHAR(32)
    , DATA_SOURCE_ID VARCHAR(256)
    , SQL_ID VARCHAR(256)
    , POST_PROCESSING_SQL_ID VARCHAR(256)
    , RECIPIENT_AGENT_ID VARCHAR(256)
    , RECIPIENT_DROPBOX_ID VARCHAR(256)
    , DESCRIPTION VARCHAR(2048)
    , CONSTRAINT PK_DFX_POSTMAN PRIMARY KEY (AGENT_ID, POSTMAN_ID)
);
COMMENT ON TABLE TB_DFX_POSTMAN IS 'DROPBOX 설정';
COMMENT ON COLUMN TB_DFX_POSTMAN.AGENT_ID IS '에이전트ID';
COMMENT ON COLUMN TB_DFX_POSTMAN.POSTMAN_ID IS 'POSTMAN ID';
COMMENT ON COLUMN TB_DFX_POSTMAN.POSTMAN_TASK_TYPE_CODE IS 'CODE_GROUP: POSTMAN_TASK_TYPE';
COMMENT ON COLUMN TB_DFX_POSTMAN.POSTMAN_ACTION_TYPE_CODE IS 'CODE_GROUP: POSTMAN_ACTION_TYPE';
COMMENT ON COLUMN TB_DFX_POSTMAN.POSTMAN_ACTION_CRON IS 'POSTMAN ACTION CRON';
COMMENT ON COLUMN TB_DFX_POSTMAN.DATA_SOURCE_ID IS 'POSTMAN DATA_SOURCE_ID';
COMMENT ON COLUMN TB_DFX_POSTMAN.SQL_ID IS '전송데이터 조회 SQL_ID';
COMMENT ON COLUMN TB_DFX_POSTMAN.POST_PROCESSING_SQL_ID IS '후처리 SQL ID';
COMMENT ON COLUMN TB_DFX_POSTMAN.RECIPIENT_AGENT_ID IS '수신자 에이전트ID';
COMMENT ON COLUMN TB_DFX_POSTMAN.RECIPIENT_DROPBOX_ID IS '수신자 DROPBOX ID';
COMMENT ON COLUMN TB_DFX_POSTMAN.DESCRIPTION IS '설명';


CREATE TABLE TB_DFX_AGENT_MESSAGE (
    MESSAGE_UUID VARCHAR(36) NOT NULL
    , SENDER_AGENT_ID VARCHAR(256) NOT NULL
    , SENDER_POSTMAN_ID VARCHAR(256) NOT NULL
    , SENDER_TS TIMESTAMPTZ(3)
    , RECIPIENT_AGENT_ID VARCHAR(256) NOT NULL
    , RECIPIENT_DROPBOX_ID VARCHAR(256) NOT NULL
    , RECIPIENT_TS TIMESTAMPTZ(3)
    , MESSAGE_TYPE_CODE VARCHAR(64)
    , CONSOLE_RECEIVE_TS TIMESTAMPTZ(3)
    , PROCESS_STATUS_CODE VARCHAR(64)
    , MESSAGE_DATA TEXT
    , MESSAGE_DATA_COUNT DECIMAL(9) DEFAULT 0
    , PROCESS_ACK_TS TIMESTAMPTZ(3)
    , CONSTRAINT PK_DFX_AGENT_MESSAGE PRIMARY KEY (MESSAGE_UUID, SENDER_AGENT_ID)
);
CREATE INDEX IX_DFX_AGENT_MESSAGE_1 ON TB_DFX_AGENT_MESSAGE (RECIPIENT_AGENT_ID);
CREATE INDEX IX_DFX_AGENT_MESSAGE_2 ON TB_DFX_AGENT_MESSAGE (CONSOLE_RECEIVE_TS);
COMMENT ON TABLE TB_DFX_AGENT_MESSAGE IS '에이전트간 메시지';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.MESSAGE_UUID IS 'MESSAGE UUID';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.SENDER_AGENT_ID IS '송신 에이전트 ID';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.SENDER_POSTMAN_ID IS '송신 에이전트 POSTMAN ID';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.SENDER_TS IS '송신 시간';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.RECIPIENT_AGENT_ID IS '수신 에이전트 ID';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.RECIPIENT_DROPBOX_ID IS '수신 DROPBOX ID';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.RECIPIENT_TS IS '수신 시간';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.MESSAGE_TYPE_CODE IS '메시지 타입 코드 CODE_GROUP: MESSAGE_TYPE';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.CONSOLE_RECEIVE_TS IS '콘솔 수신 시간';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.PROCESS_STATUS_CODE IS '프로세스 상태 코드 CODE_GROUP: PROCESS_STATUS';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.MESSAGE_DATA IS '메시지 데이터';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.MESSAGE_DATA_COUNT IS '메시지 데이터 갯수';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE.PROCESS_ACK_TS IS '처리결과 수신 시간';


CREATE SEQUENCE SQ_DFX_AGENT_MESSAGE_HISTORY START WITH 1 MINVALUE 1 MAXVALUE 999999999999999999 INCREMENT BY 1 CACHE 1;
CREATE TABLE TB_DFX_AGENT_MESSAGE_HISTORY (
    AGENT_MESSAGE_HISTORY_SEQ DECIMAL(18) NOT NULL
    , MESSAGE_UUID VARCHAR(36) NOT NULL
    , SENDER_AGENT_ID VARCHAR(256) NOT NULL
    , SENDER_POSTMAN_ID VARCHAR(256) NOT NULL
    , SENDER_TS TIMESTAMPTZ(3)
    , RECIPIENT_AGENT_ID VARCHAR(256) NOT NULL
    , RECIPIENT_DROPBOX_ID VARCHAR(256) NOT NULL
    , RECIPIENT_TS TIMESTAMPTZ(3)
    , MESSAGE_TYPE_CODE VARCHAR(64)
    , CONSOLE_RECEIVE_TS TIMESTAMPTZ(3)
    , PROCESS_STATUS_CODE VARCHAR(64)
    , MESSAGE_DATA TEXT
    , MESSAGE_DATA_COUNT DECIMAL(9) DEFAULT 0
    , PROCESS_ACK_TS TIMESTAMPTZ(3)
    , CONSTRAINT PK_DFX_AGENT_MESSAGE_HISTORY PRIMARY KEY (AGENT_MESSAGE_HISTORY_SEQ)
);
CREATE INDEX IX_DFX_AGENT_MESSAGE_HISTORY_1 ON TB_DFX_AGENT_MESSAGE_HISTORY (SENDER_AGENT_ID);
CREATE INDEX IX_DFX_AGENT_MESSAGE_HISTORY_2 ON TB_DFX_AGENT_MESSAGE_HISTORY (MESSAGE_UUID);
CREATE INDEX IX_DFX_AGENT_MESSAGE_HISTORY_3 ON TB_DFX_AGENT_MESSAGE_HISTORY (RECIPIENT_AGENT_ID);
CREATE INDEX IX_DFX_AGENT_MESSAGE_HISTORY_4 ON TB_DFX_AGENT_MESSAGE_HISTORY (CONSOLE_RECEIVE_TS);
COMMENT ON TABLE TB_DFX_AGENT_MESSAGE_HISTORY IS '에이전트간 메시지 송수신 이력';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.AGENT_MESSAGE_HISTORY_SEQ IS '메시지 송신 이력 순번 SQ_DFX_AGENT_MESSAGE_HISTORY';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.MESSAGE_UUID IS 'MESSAGE UUID';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.SENDER_AGENT_ID IS '송신 에이전트 ID';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.SENDER_POSTMAN_ID IS '송신 에이전트 POSTMAN ID';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.SENDER_TS IS '송신 시간';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.RECIPIENT_AGENT_ID IS '수신 에이전트 ID';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.RECIPIENT_DROPBOX_ID IS '수신 DROPBOX ID';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.RECIPIENT_TS IS '수신 시간';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.MESSAGE_TYPE_CODE IS '메시지 타입 코드 CODE_GROUP: MESSAGE_TYPE';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.CONSOLE_RECEIVE_TS IS '콘솔 수신 시간';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.PROCESS_STATUS_CODE IS '프로세스 상태 코드 CODE_GROUP: PROCESS_STATUS';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.MESSAGE_DATA IS '메시지 데이터';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.MESSAGE_DATA_COUNT IS '메시지 데이터 갯수';
COMMENT ON COLUMN TB_DFX_AGENT_MESSAGE_HISTORY.PROCESS_ACK_TS IS '처리결과 수신 시간';




