create table TB_ENS_KT_GBS
(
    SNDNG_MASTR_ID         VARCHAR2(20) not null,
    SNDNG_DETAIL_ID        VARCHAR2(20) not null,
    UNITY_SNDNG_DETAIL_ID  VARCHAR2(20),
    SIGNGU_CODE            CHAR(5),
    FFNLG_CODE             VARCHAR2(2),
    SERVICE_CD             VARCHAR2(5),
    MSG_CD                 VARCHAR2(5),
    MAKE_DT                VARCHAR2(14),
    DATA_CNT               NUMBER(8),
    SNDN_EX_TIME           VARCHAR2(14),
    EX_TIME                VARCHAR2(14),
    SND_TEL_NO             VARCHAR2(20),
    M_TYPE                 VARCHAR2(10),
    MSG_TYPE               VARCHAR2(1),
    OPT_TYPE               VARCHAR2(1),
    SRC_KEY                VARCHAR2(50),
    SRC_SEQ                VARCHAR2(8),
    SCI                    VARCHAR2(88),
    MMS_DTL_CNTS           VARCHAR2(4000),
    MMS_TITLE              VARCHAR2(40),
    RCS_DTL_CNTS           VARCHAR2(4000),
    URL                    VARCHAR2(1000),
    DOC_HASH               VARCHAR2(100),
    MDN                    VARCHAR2(11),
    MMS_SNDG_RSLT_SQNO     VARCHAR2(8),
    PRCS_DT                VARCHAR2(8),
    MMS_BSNS_DVCD          VARCHAR2(5),
    MBL_BZOWR_DVCD         VARCHAR2(2),
    RL_MMS_SNDG_TELNO      VARCHAR2(20),
    MMS_RSLT_DVCD          VARCHAR2(20),
    MMS_SNDG_TMST          VARCHAR2(14),
    MMS_RCV_TMST           VARCHAR2(14),
    MMS_RDG_TMST           VARCHAR2(14),
    PREV_APPROVE_YN        VARCHAR2(4),
    RESULT_CD              VARCHAR2(2),
    RESULT_DT              VARCHAR2(14),
    ERROR_MSG              VARCHAR2(255),
    REGIST_DT              DATE,
    REGISTER               VARCHAR2(20),
    UPDT_DT                DATE,
    UPDUSR                 VARCHAR2(20),
    constraint PK_TB_ENS_KT_BC_GBS
        primary key (SNDNG_MASTR_ID, SNDNG_DETAIL_ID)
);

comment on table TB_ENS_KT_GBS is 'GBS 공인 알림 문자';
comment on column TB_ENS_KT_GBS.SNDNG_MASTR_ID is '발송 마스터 ID';
comment on column TB_ENS_KT_GBS.SNDNG_DETAIL_ID is '발송 상세 ID';
comment on column TB_ENS_KT_GBS.UNITY_SNDNG_DETAIL_ID is '통합 발송 상세 ID';
comment on column TB_ENS_KT_GBS.SIGNGU_CODE is '시군구 코드';
comment on column TB_ENS_KT_GBS.FFNLG_CODE is '과태료 코드';
comment on column TB_ENS_KT_GBS.SERVICE_CD is '기관코드';
comment on column TB_ENS_KT_GBS.MSG_CD is '문서코드';
comment on column TB_ENS_KT_GBS.MAKE_DT is '생성일시';
comment on column TB_ENS_KT_GBS.DATA_CNT is '회차전체데이터건수';
comment on column TB_ENS_KT_GBS.SNDN_EX_TIME is '발송마감시간';
comment on column TB_ENS_KT_GBS.EX_TIME is '열람마감시간';
comment on column TB_ENS_KT_GBS.SND_TEL_NO is '발신번호(서비스기관)(발송전화번호)';
comment on column TB_ENS_KT_GBS.M_TYPE is '문서종류(sms,lms,mms)';
comment on column TB_ENS_KT_GBS.MSG_TYPE is '발송메세지타입';
comment on column TB_ENS_KT_GBS.OPT_TYPE is '메시지 발송구분';
comment on column TB_ENS_KT_GBS.SRC_KEY is '관리키';
comment on column TB_ENS_KT_GBS.SRC_SEQ is 'SRC_SEQ';
comment on column TB_ENS_KT_GBS.SCI is 'CI';
comment on column TB_ENS_KT_GBS.MMS_DTL_CNTS is 'MMS상세내용';
comment on column TB_ENS_KT_GBS.MMS_TITLE is 'MMS제목';
comment on column TB_ENS_KT_GBS.RCS_DTL_CNTS is 'RCS 메시지 상세내용';
comment on column TB_ENS_KT_GBS.URL is '연결URL';
comment on column TB_ENS_KT_GBS.DOC_HASH is '문서해시';
comment on column TB_ENS_KT_GBS.MDN is '개인휴대전화번호';
comment on column TB_ENS_KT_GBS.MMS_SNDG_RSLT_SQNO is 'MMS발송결과상태순번 (1:수신,2:열람)';
comment on column TB_ENS_KT_GBS.PRCS_DT is '처리일자';
comment on column TB_ENS_KT_GBS.MMS_BSNS_DVCD is '문서코드';
comment on column TB_ENS_KT_GBS.MBL_BZOWR_DVCD is '모바일사업자구분 (01:KT,02:SKT,03:LGT)';
comment on column TB_ENS_KT_GBS.RL_MMS_SNDG_TELNO is '실제발송번호(일부)';
comment on column TB_ENS_KT_GBS.MMS_RSLT_DVCD is '발송결과코드';
comment on column TB_ENS_KT_GBS.MMS_SNDG_TMST is '발송 타임 스탬프';
comment on column TB_ENS_KT_GBS.MMS_RCV_TMST is '수신 타임 스탬프';
comment on column TB_ENS_KT_GBS.MMS_RDG_TMST is '열람 타임 스탬프';
comment on column TB_ENS_KT_GBS.PREV_APPROVE_YN is '기동의발송여부';
comment on column TB_ENS_KT_GBS.RESULT_CD is '처리결과';
comment on column TB_ENS_KT_GBS.RESULT_DT is '응답처리일자';
comment on column TB_ENS_KT_GBS.ERROR_MSG is '오류 메시지';
comment on column TB_ENS_KT_GBS.REGIST_DT is '등록 일시';
comment on column TB_ENS_KT_GBS.REGISTER is '등록자';
comment on column TB_ENS_KT_GBS.UPDT_DT is '수정 일시';
comment on column TB_ENS_KT_GBS.UPDUSR is '수정자';

-- 자료 이관

insert into tb_ens_mobile_page_manage
select concat('KKO', send_detail_id)
     , 'KKO-MY-DOC'
     , details
     , to_char(regist_dt, 'yyyymmddhh24miss')
     , 'system'
from ens_snd_dtl_mbl_page
where post_se='kkoMydoc';

insert into tb_ens_mobile_page_manage
select concat('KTG', send_detail_id)
     , 'KT-GBS'
     , details
     , to_char(regist_dt, 'yyyymmddhh24miss')
     , 'system'
from ens_snd_dtl_mbl_page
where post_se='ktGibis';

insert into tb_ens_unity_sndng_mastr
select (select c.elctrn_ntic_sndng_id from ens_snd_dtl_kko_md b inner join tb_elctrn_ntic_sndng_detail c on b.prop_external_document_uuid=c.external_document_uuid where a.send_mast_id = b.send_mast_id and rownum = 1)
     , org_cd
     , '11'
     , tmplt_cd
     , 'ENS'
     , send_cnt
     , 'send-ok'  -- stat_cd
     , to_char(send_dt, 'yyyymmddhh24miss')
     , ''
     , ''
     , 'KKO-MY-DOC'
     , ''
     , ''
     , '1'
     , to_char(close_dt, 'yyyymmddhh24miss')
     , ''
     , ''
     , regist_dt
     , 'system'
     , last_updt_dt
     , 'system'
from ens_snd_mast a
where stat_cd IN ('open', 'sendcmplt')
  and tmplt_cd like '%2';


insert into tb_ens_unity_sndng_mastr
select (select c.elctrn_ntic_sndng_id from ens_snd_dtl_kt_gbs b inner join tb_elctrn_ntic_sndng_detail c on b.LINKED_UUID=c.external_document_uuid where a.send_mast_id = b.send_mast_id and rownum = 1)
     , org_cd
     , '11'
     , tmplt_cd
     , 'ENS'
     , send_cnt
     , 'send-ok'  -- stat_cd
     , to_char(send_dt, 'yyyymmddhh24miss')
     , ''
     , ''
     , 'KT-GBS'
     , ''
     , ''
     , '1'
     , to_char(close_dt, 'yyyymmddhh24miss')
     , ''
     , ''
     , regist_dt
     , 'system'
     , last_updt_dt
     , 'system'
from ens_snd_mast a
where stat_cd IN ('open', 'sendcmplt')
  and tmplt_cd like '%3';


insert into tb_ens_unity_sndng_detail
select (select b.elctrn_ntic_sndng_detail_id from tb_elctrn_ntic_sndng_detail b where a.prop_external_document_uuid=b.external_document_uuid)
     , (select c.elctrn_ntic_sndng_id from ens_snd_dtl_kko_md b inner join tb_elctrn_ntic_sndng_detail c on b.prop_external_document_uuid=c.external_document_uuid where a.send_mast_id = b.send_mast_id and rownum= 1)
     , bb.org_cd
     , '11'
     , ''
     , ''
     , ''
     , ''
     , ''
     , ''
     , ''
     , ''
     , ''
     , (select mobile_page_cn from tb_ens_mobile_page_manage c where c.sndng_detail_id=concat('KKO', a.send_detail_id))
     , ''
     , a.prop_external_document_uuid
     , a.regist_dt
     , 'system'
     , a.last_updt_dt
     , 'system'
from ens_snd_dtl_kko_md a
         inner join ens_snd_mast bb on (a.SEND_MAST_ID=bb.send_mast_id)
where bb.stat_cd IN ('open', 'sendcmplt') and bb.tmplt_cd like '%2';


insert into tb_ens_unity_sndng_detail
select (select b.elctrn_ntic_sndng_detail_id from tb_elctrn_ntic_sndng_detail b where a.LINKED_UUID=b.external_document_uuid)
     , (select c.elctrn_ntic_sndng_id from ens_snd_dtl_kt_gbs b inner join tb_elctrn_ntic_sndng_detail c on b.LINKED_UUID=c.external_document_uuid where a.send_mast_id = b.send_mast_id and rownum= 1)
     , bb.org_cd
     , '11'
     , ''
     , ''
     , ''
     , ''
     , ''
     , ''
     , ''
     , ''
     , ''
     , (select mobile_page_cn from tb_ens_mobile_page_manage c where c.sndng_detail_id=concat('KTG', a.send_detail_id))
     , ''
     , a.LINKED_UUID
     , null
     , 'system'
     , null
     , 'system'
from ens_snd_dtl_kt_gbs a
         inner join ens_snd_mast bb on (a.SEND_MAST_ID=bb.send_mast_id)
where bb.stat_cd IN ('open', 'sendcmplt') and bb.tmplt_cd like '%3';



insert into tb_ens_sndng_mastr
select concat('KKO', send_mast_id)
     , (select c.elctrn_ntic_sndng_id from ens_snd_dtl_kko_md b inner join tb_elctrn_ntic_sndng_detail c on b.prop_external_document_uuid=c.external_document_uuid where a.send_mast_id = b.send_mast_id and rownum=1)
     , org_cd
     , '11'
     , tmplt_cd
     , 'ENS'
     , 'KKO-MY-DOC'
     , send_cnt
     , '1'
     , '1'
     , 'send-ok'
     , to_char(send_dt, 'yyyymmddhh24miss')
     , to_char(close_dt, 'yyyymmddhh24miss')
     , ''
     , ''
     , ''
     , null
     , ''
     , regist_dt
     , 'system'
     , last_updt_dt
     , 'system'
from ens_snd_mast a
where stat_cd IN ('open', 'sendcmplt')
  and tmplt_cd like '%2';


insert into tb_ens_sndng_mastr
select concat('KTG', send_mast_id)
     , (select c.elctrn_ntic_sndng_id from ens_snd_dtl_kt_gbs b inner join tb_elctrn_ntic_sndng_detail c on b.LINKED_UUID=c.external_document_uuid where a.send_mast_id = b.send_mast_id and rownum=1)
     , org_cd
     , '11'
     , tmplt_cd
     , 'ENS'
     , 'KT-GBS'
     , send_cnt
     , '1'
     , '1'
     , 'send-ok'
     , to_char(send_dt, 'yyyymmddhh24miss')
     , to_char(close_dt, 'yyyymmddhh24miss')
     , ''
     , ''
     , ''
     , null
     , ''
     , regist_dt
     , 'system'
     , last_updt_dt
     , 'system'
from ens_snd_mast a
where stat_cd IN ('open', 'sendcmplt')
  and tmplt_cd like '%3';


insert into tb_ens_kakao_my_doc
select concat('KKO', a.send_detail_id)
     , (select b.elctrn_ntic_sndng_detail_id from tb_elctrn_ntic_sndng_detail b where a.prop_external_document_uuid=b.external_document_uuid)
     , concat('KKO', a.send_mast_id)
     , bb.org_cd
     , '11'
     , a.title
     , a.hash
     , a.common_categories
     , a.read_expiread_at
     , a.recv_ci
     , ''
     , ''
     , ''
     , a.recv_is_require_vfy_name
     , a.prop_link
     , a.prop_payload
     , a.prop_message
     , a.prop_cs_number
     , a.prop_cs_name
     , a.prop_external_document_uuid
     , a.document_binder_uuid
     , a.error_code
     , a.error_message
     , a.kko_doc_stat
     , FN_GET_DATETOUNIXTIME(a.kko_doc_sent_dt)
     , FN_GET_DATETOUNIXTIME(a.kko_doc_received_dt)
     , FN_GET_DATETOUNIXTIME(a.kko_doc_auth_frst_dt)
     , FN_GET_DATETOUNIXTIME(a.kko_doc_token_vrfy_frst_dt)
     , FN_GET_DATETOUNIXTIME(a.kko_doc_read_frst_dt)
     , FN_GET_DATETOUNIXTIME(a.kko_doc_user_notied_dt)
     , a.regist_dt
     , 'system'
     , a.last_updt_dt
     , 'system'
from ens_snd_dtl_kko_md a
         inner join ens_snd_mast bb on (a.SEND_MAST_ID=bb.send_mast_id)
where bb.stat_cd IN ('open', 'sendcmplt') and bb.tmplt_cd like '%2';


insert into tb_ens_kt_gbs
select concat('KTG', b.SEND_MAST_ID)
     , concat('KTG', b.send_detail_id)
     , (select bb.elctrn_ntic_sndng_detail_id from tb_elctrn_ntic_sndng_detail bb where b.LINKED_UUID=bb.external_document_uuid)
     , E.ORG_CD
     , '11'
     , E.KT_ST_SERVICE_CD
     , ''
     , ''
     , A.SEND_CNT
     , TO_CHAR(A.CLOSE_DT, 'YYYYMMDDHH24MISS')
     , TO_CHAR(A.CLOSE_DT, 'YYYYMMDDHH24MISS')
     , ''
     , D.M_TYPE
     , D.MSG_TYPE
     , D.OPT_TYPE
     , B.SRC_KEY
     , B.SRC_SEQ
     , B.CI
     , B.MMS_DTL_CNTS
     , B.MMS_TITLE
     , ''
     , B.URL
     , B.HASH
     , ''
     , ''
     , ''
     , ''
     , ''
     , ''
     , B.DOC_STAT
     , B.DOC_SENT_DT
     , B.DOC_RECEIVED_DT
     , B.DOC_READ_FRST_DT
     , ''
     , ''
     , ''
     , ''
     , sysdate
     , ''
     , ''
     , ''
FROM ENS_SND_MAST A
         INNER JOIN ENS_SND_DTL_KT_GBS B ON (A.SEND_MAST_ID=B.SEND_MAST_ID)
         INNER JOIN ENS_TMPLT_MNG C ON (A.TMPLT_CD=C.TMPLT_CD)
         INNER JOIN ENS_TMPLT_MNG_KT_GBS D ON (A.TMPLT_CD=D.TMPLT_CD)
         INNER JOIN ENS_ORG_MNG E ON (A.ORG_CD=E.ORG_CD)
where a.STAT_CD in ('open', 'sendcmplt');

