<?xml version="1.0" encoding="utf-8" ?>
<!--<sqlMap namespace="CLIP.e-Form.Consent.Dfh" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >-->
<sqlMap namespace="CLIP.e-Form.Consent.Knuh" xmlns="http://ibatis.apache.org/mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <statements>
    <statement id="GET_CATEGORY_FOR_DROPDOWN" parameterClass="System.Collections.Hashtable" resultMap="CategoryForDropdownMap">
      <![CDATA[
        SELECT  CATEGORY_TYPE, 
                CATEGORY_ID, 
                CATEGORY_NAME  
          FROM  EFORM_CATEGORY
         WHERE  USE_YN = 'Y' 
           AND  P_CATEGORY_RID = (SELECT CATEGORY_RID FROM EFORM_CATEGORY WHERE CATEGORY_ID = #category_id#)
         ORDER BY CATEGORY_NAME
      ]]>
      <!--isEqual property="category_id" compareValue="1">
        <![CDATA[
         WHERE  USE_YN = 'Y' 
           AND  P_CATEGORY_RID = '1'
        ]]>
      </isEqual>
      <isNotEqual property="category_id" compareValue="1">
        <![CDATA[
         WHERE  USE_YN = 'Y' 
           AND  P_CATEGORY_RID = (SELECT CATEGORY_RID FROM EFORM_CATEGORY WHERE CATEGORY_ID = #category_id#)
        ]]>
      </isNotEqual><![CDATA[
         ORDER BY CATEGORY_NAME]]-->
    </statement>

    <!-- statement id="GET_CONSENT_BY_SEARCH_LIST" parameterClass="System.Collections.Hashtable" resultMap="ConsentBySearchMap">
      <![CDATA[
        SELECT form_cd
             , form_guid
             , form_name
             , full_category_name
             , form_rid
             , prntcnt
             , print_only
             , ocrtagprntyn
             , ( SELECT formprntnm
                   FROM emr.mrfmform b
                  WHERE b.instcd = #instcd#
                    AND b.formcd = a.form_cd
                    AND TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') BETWEEN b.formfromdt AND b.formtodt
               )                                                                                    AS formprntnm
             , b.opdiagnm
             , b.opnm
             , CASE WHEN EXISTS ( SELECT 1
                                    FROM emr.mmbdhrcd c
                                   WHERE c.instcd   = #instcd#
                                     AND c.hardcdno = '6920'
                                     AND c.trgtcd   = a.form_cd
                                ) THEN
                         'N'
                    ELSE 'Y'
                END                                                                                 AS userdrflag
             , ( SELECT c.rmk
                   FROM emr.mmbmhrcd c
                      , emr.mmbdhrcd d
                  WHERE c.instcd              = #instcd#
                    AND c.hardcdno            = '6011'
                    AND d.instcd              = c.instcd
                    AND d.hardcdno            = c.hardcdno 
                    AND d.trgtcd             != c.rmk
                    AND d.trgtcd              = a.form_cd
                    AND NVL(d.hardspec, 'Y')  = 'Y'
                    AND TO_CHAR(SYSDATE, 'YYYYMMDD')  BETWEEN d.valifromdd AND d.valitodd
                    AND ROWNUM                = 1
               )                                                                                    AS lnkformcd
          FROM ( SELECT a.form_cd
                      , a.form_guid
                      , a.form_name
                      , c.full_category_name
                      , a.form_rid
                      , a.prntcnt
                      , a.print_only
                      , a.ocrtagprntyn
                   FROM v_eform_formdoc a
                      , ( SELECT category_rid
                               , LEVEL
                            FROM eform_category
                           START with category_rid = ( SELECT category_rid
                                                         FROM eform_category
                                                        WHERE category_id = #category_id#
                                                     )
                         CONNECT BY p_category_rid = PRIOR category_rid
                        ) b
                      , ( SELECT category_rid
                               , category_name
                               , LTRIM(SYS_CONNECT_BY_PATH(category_name, '>'), '>') AS full_category_name
                               , LEVEL
                            FROM eform_category
                           START WITH category_rid = '1'
                         CONNECT BY p_category_rid = PRIOR category_rid
                        ) c
                  WHERE b.category_rid = a.category_rid
                    AND c.category_rid = b.category_rid
                    AND ( REPLACE(UPPER(a.form_name), ' ', '') LIKE REPLACE(UPPER(#key_word#), ' ', '')
                       OR UPPER(a.form_cd) LIKE UPPER(#key_word#)
                        )
               ) a
             , ( SELECT trgtcd
                      , trgtcdnm  AS opdiagnm
                      , trgtcdsnm AS opnm
                   FROM emr.mmbdhrcd
                  WHERE instcd   = #instcd#
                    AND hardcdno = '6011'
                    AND TO_CHAR(SYSDATE, 'YYYYMMDD') BETWEEN valifromdd AND valitodd
               ) b
         WHERE b.trgtcd(+) = a.form_cd
         ORDER BY form_name]]>
    </statement -->

    <statement id="GET_CONSENT_BY_SEARCH_LIST" parameterClass="System.Collections.Hashtable" resultMap="ConsentFormMap">
      <![CDATA[
        SELECT ''                                                                         AS CONSENT_MST_RID,
               ''                                                                         AS CREATE_USER_ID,
               ''                                                                         AS CREATE_USER_NAME,
               ''                                                                         AS CREATE_DATETIME,
               ''                                                                         AS OCR_TAG,
               ''                                                                         AS PID,
               ''                                                                         AS INDD,
               ''                                                                         AS ORD_TYPE,
               ''                                                                         AS ORD_DEPT_CD,
               ''                                                                         AS CRET_NO,
               ''                                                                         AS OPRSRVNO,
               ''                                                                         AS PATIENT_NAME,
               ''                                                                         AS CONSENT_STATE,
               ''                                                                         AS CONSENT_STATE_ENG,
               VF.FORM_CD                                                                 AS FORM_CD,
               VF.FORM_GUID                                                               AS FORM_GUID,
               VF.FORM_NAME                                                               AS FORM_NAME,
               VF.FORM_RID                                                                AS FORM_RID,
               VF.PRINT_ONLY                                                              AS PRNT_ONLY,
               VF.PRNTCNT                                                                 AS PRINT_CNT,
               VF.OCRTAGPRNTYN                                                            AS OCR_TAG_PRNT_YN,
               MRFM.FORMPRNTNM                                                            AS FORM_PRNT_NM,
               DECODE(MRFM.CERTUSEYN, 'Y', MRFM.CERTUSEYN, 'N')                           AS CERT_USE_YN,
               OP.OPDIAGNM                                                                AS OP_DIAG_NM,
               OP.OPNM                                                                    AS OP_NM,
               CASE WHEN EXISTS (SELECT 1
                                   FROM EMR.MMBDHRCD C
                                  WHERE C.INSTCD   = #instCd#
                                    AND C.HARDCDNO = '6920'
                                    AND C.TRGTCD   = VF.FORM_CD
                                ) THEN
                           'N'
                      ELSE 'Y'
                    END                                                                   AS USERDRFLAG,
               (SELECT C.RMK
                  FROM EMR.MMBMHRCD C,
                       EMR.MMBDHRCD D
                 WHERE C.INSTCD              = #instCd#
                   AND C.HARDCDNO            = '6011'
                   AND D.INSTCD              = C.INSTCD
                   AND D.HARDCDNO            = C.HARDCDNO 
                   AND D.TRGTCD             != C.RMK
                   AND D.TRGTCD              = VF.FORM_CD
                   AND NVL(D.HARDSPEC, 'Y')  = 'Y'
                   AND TO_CHAR(SYSDATE, 'YYYYMMDD')  BETWEEN D.VALIFROMDD AND D.VALITODD
                   AND ROWNUM                = 1
               )                                                                          AS LNKFORMCD,
               (SELECT DECODE(COUNT(FORM.FORMCD), 0, '-', '1100010977')
                  FROM EMR.MRFMCALCMECH MECH,
                       EMR.MRFMFORM FORM
                 WHERE MECH.INSTCD = #instCd#
                   AND FORM.FORMCD = VF.FORM_CD
                   AND MECH.INSTCD = FORM.INSTCD
                   AND MECH.FORMCD = FORM.FORMCD
                   AND TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') BETWEEN FORM.FORMFROMDT AND FORM.FORMTODT
                   AND MECH.BIZFLAG='JINJEONG')                                           AS JINJEONGCD
          FROM v_eform_formdoc VF, 
               EMA.EFORM_CATEGORY EC, 
               EMR.MRFMFORM MRFM,
               (SELECT TRGTCD,
                       TRGTCDNM AS OPDIAGNM,
                       TRGTCDSNM AS OPNM
                  FROM EMR.MMBDHRCD
                 WHERE INSTCD   = #instCd#
                   AND HARDCDNO = '6011'
                   AND TO_CHAR(SYSDATE, 'YYYYMMDD') BETWEEN VALIFROMDD AND VALITODD
               ) OP
         WHERE 1 = 1
           AND MRFM.INSTCD = #instCd#
           AND MRFM.FORMPROGFLAG LIKE 'OCR%'
           AND TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') BETWEEN MRFM.FORMFROMDT AND MRFM.FORMTODT
           AND VF.CATEGORY_RID = EC.CATEGORY_RID
           AND VF.FORM_CD = MRFM.FORMCD
           AND OP.TRGTCD(+) = VF.FORM_CD
      ]]>

      <isNotEmpty property="keyWord">
        <![CDATA[
          AND REPLACE(UPPER(VF.FORM_NAME), ' ', '') LIKE REPLACE(UPPER(#keyWord#), ' ', '')
        ]]>
      </isNotEmpty>
      <isNotEmpty property="categoryId">
        <![CDATA[
          AND EC.CATEGORY_ID = #categoryId#
        ]]>
      </isNotEmpty>
      <![CDATA[
        ORDER BY VF.FORM_NAME
      ]]>
    </statement>

    <statement id="GET_FORM_GUID_BY_FORM_CD" parameterClass="System.Collections.Hashtable" resultClass="String">
      <![CDATA[
        SELECT form_guid
          FROM v_eform_formdoc
         WHERE form_cd = #formCd#]]>
    </statement>

    <statement id="GET_CONSENT_LIST" parameterClass="System.Collections.Hashtable" resultMap="ConsentFormMap">
      <![CDATA[
        SELECT CMT.CONSENT_MST_RID                                           AS CONSENT_MST_RID,
               CMT.CREATE_USER_ID                                            AS CREATE_USER_ID,
               CMT.CREATE_USER_NAME                                          AS CREATE_USER_NAME,
               CMT.CREATE_DATETIME                                           AS CREATE_DATETIME,
               CMT.OCR_NUMBER                                                AS OCR_TAG,
               CMT.PID                                                       AS PID,
               CMT.CLN_DATE                                                  AS INDD,
               CMT.VISIT_TYPE                                                AS ORD_TYPE,
               CMT.CLN_DEPT_CD                                               AS ORD_DEPT_CD,
               CMT.CRETNO                                                    AS CRET_NO,
               CMT.OPRSRVNO                                                  AS OPRSRVNO,
               (SELECT ptbs.hngnm
                  FROM PAM.PMCMPTBS PTBS
                 WHERE PTBS.INSTCD = #instCd#
                  AND PTBS.PID    = CMT.PID)                                 AS PATIENT_NAME,
               CASE
                 WHEN CMT.CONSENT_STATE = 'PAPER_OUT'
                   THEN 
                     (SELECT CASE WHEN B.STATCD IN ('P','CR') THEN '출력'
                                  WHEN B.STATCD IN ('R','CC') THEN '반납'
                                  WHEN B.STATCD = 'C' THEN '반납확인' ELSE NULL END AS STATUS
                        FROM EMR.MRFMOCRPRNT A, EMR.MRFHOCRPRNT B
                       WHERE A.INSTCD = B.INSTCD
                         AND A.OCRTAG = B.OCRTAG
                         AND A.UPDTDT = B.UPDTDT
                         AND NVL(A.EOCRYN,'N') = 'N'
                         AND B.STATCD IN ('P','R','C','CR','CC')
                         AND A.INSTCD = #instCd#
                         AND A.OCRTAG = CMT.OCR_NUMBER)
                   ELSE
                     DECODE(CMT.CONSENT_STATE, 'TEMP',        '임시'
                                             , 'ELECTR_CMP',  '확인'
                                             , 'PAPER_OUT',   '출력'
                                             , 'CERTIFY_CMP', '인증'
                                             , 'UNFINISHED',  '연동'
                                             , 'VERBAL',      '구두'
                                             , CMT.CONSENT_STATE)
               END                                                           AS CONSENT_STATE,
               CMT.CONSENT_STATE                                             AS CONSENT_STATE_ENG,
               EF.FORM_CD                                                    AS FORM_CD,
               EF.FORM_GUID                                                  AS FORM_GUID,
               EF.FORM_NAME                                                  AS FORM_NAME,
               EF.FORM_RID                                                   AS FORM_RID,
               MRFM.PRINT_ONLY                                               AS PRNT_ONLY,
               MRFM.PRNTCNT                                                  AS PRINT_CNT,
               MRFM.OCRTAGPRNTYN                                             AS OCR_TAG_PRNT_YN,
               MRFM.FORMPRNTNM                                               AS FORM_PRNT_NM,
               DECODE(MRFM.CERTUSEYN, 'Y', MRFM.CERTUSEYN, 'N')              AS CERT_USE_YN,
               OP.OPDIAGNM                                                   AS OP_DIAG_NM,
               OP.OPNM                                                       AS OP_NM,
               CASE WHEN EXISTS (SELECT 1
                                   FROM EMR.MMBDHRCD C
                                  WHERE C.INSTCD   = #instCd#
                                    AND C.HARDCDNO = '6920'
                                    AND C.TRGTCD   = EF.FORM_CD
                                ) THEN
                           'N'
                      ELSE 'Y'
                    END                                                      AS USERDRFLAG,
               ''                                                            AS LNKFORMCD,
               ''                                                            AS JINJEONGCD
          FROM EMA.CONSENT_MST    CMT
             , EMA.EFORM_DOCUMENT EF
             , EMR.MRFMFORM       MRFM
             , ( SELECT TRGTCD
                      , TRGTCDNM  AS OPDIAGNM
                      , TRGTCDSNM AS OPNM
                   FROM EMR.MMBDHRCD
                  WHERE INSTCD   = #instCd#
                    AND HARDCDNO = '6011'
                    AND TO_CHAR(SYSDATE, 'YYYYMMDD') BETWEEN VALIFROMDD AND VALITODD
               ) OP
         WHERE CMT.HOS_TYPE                =      #instCd#
      ]]>
      <isNotEmpty property="patientCode">
        <isEmpty property="userId">
          <![CDATA[
           AND CMT.PID                     =      #patientCode#
          ]]>
        </isEmpty>
      </isNotEmpty>
      <isNotEmpty property="consentMstRid">
        <![CDATA[
          AND CMT.CONSENT_MST_RID = #consentMstRid#
        ]]>
      </isNotEmpty>
      <isNotEmpty property="userId">
        <![CDATA[
           AND #userId#                  IN     (CMT.CREATE_USER_ID, CMT.MODIFY_USER_ID)
        ]]>
      </isNotEmpty>
      <![CDATA[
           AND CMT.USE_YN                   =      'Y'
           AND TO_CHAR(TO_DATE(CMT.CREATE_DATETIME, 'YYYY-MM-DD HH24:MI:SS'), 'YYYYMMDD') BETWEEN #startDt# AND #endDt#
           AND EF.FORM_RID                  =      CMT.FORM_RID
           AND MRFM.INSTCD                  =      CMT.HOS_TYPE
           AND MRFM.FORMCD                  =      EF.FORM_CD
           AND MRFM.FORMPROGFLAG            LIKE   'OCR%'
           AND MRFM.FORMLEVLCD             !=      '-'
           AND TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') BETWEEN MRFM.FORMFROMDT AND MRFM.FORMTODT
        ]]>
      <isNotEqual property="consentState" compareValue="ALL">
        <![CDATA[
           AND CMT.CONSENT_STATE           IN     ($consentState$)
        ]]>
      </isNotEqual><![CDATA[
           AND CASE WHEN EXISTS ( SELECT 1
                                    FROM EMR.MRFMOCRPRNT E
                                   WHERE E.INSTCD = #instCd#
                                     AND E.OCRTAG = CMT.OCR_NUMBER ) THEN
                         CASE WHEN CMT.CONSENT_STATE IN ('UNFINISHED', 'TEMP', 'PAPER_OUT', 'ELECTR_CMP')
                               AND EXISTS ( SELECT 1
                                              FROM EMR.MRFMOCRPRNT E
                                                 , EMR.MRFHOCRPRNT F
                                             WHERE E.INSTCD = #instCd#
                                               AND E.OCRTAG = CMT.OCR_NUMBER
                                               AND F.INSTCD = E.INSTCD
                                               AND F.OCRTAG = E.OCRTAG
                                               AND F.UPDTDT = E.UPDTDT
                                               AND F.STATCD IN ('C', 'R', 'CC', 'CR', 'P')
                                          ) THEN 'Y'
                              WHEN CMT.CONSENT_STATE = 'CERTIFY_CMP'
                               AND EXISTS ( SELECT 1
                                              FROM EMR.MRFMOCRPRNT E
                                                 , EMR.MRFHOCRPRNT F
                                             WHERE E.INSTCD = #instCd#
                                               AND E.OCRTAG = CMT.OCR_NUMBER
                                               AND F.INSTCD = E.INSTCD
                                               AND F.OCRTAG = E.OCRTAG
                                               AND F.UPDTDT = E.UPDTDT
                                               AND F.STATCD = 'C'
                                          ) THEN 'Y'
                              ELSE 'N'
                         END
                    ELSE 'N'
                END = 'Y'
           AND OP.TRGTCD(+)               = EF.FORM_CD
      ]]>
      <![CDATA[
          ORDER BY EF.FORM_NAME ASC, OCR_NUMBER DESC
      ]]>
      <!--![CDATA[
      AND F.STATCD = 'P'
         ORDER BY MRFM.FORMPRNTNM ASC
                , ocr_number    DESC
                , consent_state ASC
      ]]-->
    </statement>

    <statement id="GET_CONSENT_IMAGE" parameterClass="System.Collections.Hashtable" resultMap="ConsentImageMap">
      <![CDATA[
        SELECT  CONSENT_MST_RID
              , CONSENT_IMAGE_RID
              , FORM_RID
              , CASE WHEN SUBSTR(IMAGE_PATH,0,6) = 'Image1' THEN '\\192.168.100.32\image_a01' || SUBSTR(IMAGE_PATH,7)
                     WHEN SUBSTR(IMAGE_PATH,0,6) = 'Image2' THEN '\\192.168.100.32\image_a02' || SUBSTR(IMAGE_PATH,7)
                     WHEN SUBSTR(IMAGE_PATH,0,6) = 'Image3' THEN '\\192.168.100.32\image_a03' || SUBSTR(IMAGE_PATH,7)
                     ELSE IMAGE_PATH
                 END IMAGE_PATH
              , IMAGE_FILENAME
              , COMPLETE_YN
              , CREATE_USER_ID
              , CREATE_DATETIME
          FROM  CONSENT_IMAGE
         WHERE  COMPLETE_YN = 'Y'
           AND  CONSENT_MST_RID = #consent-mst-rid#
         ORDER BY CONSENT_IMAGE_RID   
      ]]>
    </statement>
    <statement id="GetConsentStateCheck" parameterClass="System.Collections.Hashtable" resultClass="System.String">
      <![CDATA[
        select mst.consent_state
         from EMA.CONSENT_MST mst
        where mst.consent_mst_rid = #consentmstrid#
           and mst.use_yn = 'Y'
     ]]>
    </statement>

    <statement id="getImageServerProps" parameterClass="System.Collections.Hashtable" resultMap="ImageServerPropsMap">
      <![CDATA[ 
        SELECT
          IPADDRESS,
          LOCALPATH,
          ACTIVE,
          CIFSUSER,
          CIFSPASS
        FROM cmr.patht
        WHERE
          active IN ('A', 'R', 'C', 'V')
        order by active
      ]]>
    </statement>

    <!-- tatement id="GET_CONSENT_BY_FORMCD" parameterClass="System.Collections.Hashtable" resultMap="ConsentBySearchMap">
      <![CDATA[
        SELECT a.form_cd
             , a.form_guid
             , a.form_name
             , ''                                                                                 AS full_category_name
             , a.form_rid
             , b.prntcnt
             , b.print_only
             , b.ocrtagprntyn
             , ( SELECT formprntnm
                   FROM emr.mrfmform d
                  WHERE d.instcd = #instcd#
                    AND d.formcd = b.formcd
                    AND TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') BETWEEN d.formfromdt AND d.formtodt
               )                                                                                  AS formprntnm
             , c.opdiagnm
             , c.opnm
             , CASE WHEN EXISTS ( SELECT 1
                                    FROM emr.mmbdhrcd d
                                   WHERE d.instcd   = b.instcd
                                     AND d.hardcdno = '6920'
                                     AND d.trgtcd   = b.formcd
                                ) THEN
                         'N'
                    ELSE 'Y'
                END                                                                               AS userdrflag
             , ( SELECT c.rmk
                   FROM emr.mmbmhrcd c
                      , emr.mmbdhrcd d
                  WHERE c.instcd              = #instcd#
                    AND c.hardcdno            = '6011'
                    AND d.instcd              = c.instcd
                    AND d.hardcdno            = c.hardcdno 
                    AND d.trgtcd             != c.rmk
                    AND NVL(d.hardspec, '-') != 'N'
                    AND TO_CHAR(SYSDATE, 'YYYYMMDD')  BETWEEN d.valifromdd AND d.valitodd
                    AND ROWNUM                = 1
               )                                                                                    AS lnkformcd
          FROM ema.eform_document a
             , emr.mrfmform b
             , ( SELECT trgtcd
                      , trgtcdnm  AS opdiagnm
                      , trgtcdsnm AS opnm
                   FROM emr.mmbdhrcd
                  WHERE instcd   = #instcd#
                    AND hardcdno = '6011'
                    AND TO_CHAR(SYSDATE, 'YYYYMMDD') BETWEEN valifromdd AND valitodd
               ) c
         WHERE a.form_cd      IN    ($formcdarr$)
           AND a.use_yn       =     'Y'
           AND a.deploy_yn    =     'Y'
           AND a.form_version =     ( SELECT MAX(form_version)
                                        FROM ema.eform_document c
                                       WHERE c.form_cd   = a.form_cd
                                         AND c.use_yn    = 'Y'
                                         AND c.deploy_yn = 'Y' )
           AND b.instcd        =    #instcd#
           AND b.formcd        =    a.form_cd
           AND b.formprogflag  LIKE 'OCR%'
           AND b.formlevlcd   !=    '-'
           AND TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') BETWEEN b.formfromdt AND b.formtodt
           AND c.trgtcd(+)     =    b.formcd]]>
    </statement -->

    <statement id="GET_CONSENT_BY_FORMCD" parameterClass="System.Collections.Hashtable" resultMap="ConsentFormMap">
      <![CDATA[
        SELECT ''                                                                                 AS CONSENT_MST_RID,
               ''                                                                                 AS CREATE_USER_ID,
               ''                                                                                 AS CREATE_USER_NAME,
               ''                                                                                 AS CREATE_DATETIME,
               ''                                                                                 AS OCR_TAG,
               ''                                                                                 AS PID,
               ''                                                                                 AS INDD,
               ''                                                                                 AS ORD_TYPE,
               ''                                                                                 AS ORD_DEPT_CD,
               ''                                                                                 AS CRET_NO,
               ''                                                                                 AS OPRSRVNO,
               ''                                                                                 AS PATIENT_NAME,
               ''                                                                                 AS CONSENT_STATE,
               ''                                                                                 AS CONSENT_STATE_ENG,
               EF.FORM_CD                                                                         AS FORM_CD,
               EF.FORM_GUID                                                                       AS FORM_GUID,
               EF.FORM_NAME                                                                       AS FORM_NAME,
               EF.FORM_RID                                                                        AS FORM_RID,
               MRF.PRINT_ONLY                                                                     AS PRNT_ONLY,
               MRF.PRNTCNT                                                                        AS PRINT_CNT,
               MRF.OCRTAGPRNTYN                                                                   AS OCR_TAG_PRNT_YN,
               DECODE(MRF.CERTUSEYN, 'Y', MRF.CERTUSEYN, 'N')                                     AS CERT_USE_YN,
               MRF.FORMPRNTNM                                                                     AS FORM_PRNT_NM,
               OP.OPDIAGNM                                                                        AS OP_DIAG_NM,
               OP.OPNM                                                                            AS OP_NM,
               CASE WHEN EXISTS (SELECT 1
                                   FROM EMR.MMBDHRCD D
                                  WHERE D.INSTCD   = MRF.INSTCD
                                    AND D.HARDCDNO = '6920'
                                    AND D.TRGTCD   = MRF.FORMCD
                                ) THEN
                         'N'
                    ELSE 'Y'
                END                                                                               AS USERDRFLAG,
                (SELECT C.RMK
                  FROM EMR.MMBMHRCD C,
                       EMR.MMBDHRCD D
                 WHERE C.INSTCD              = #instCd#
                   AND C.HARDCDNO            = '6011'
                   AND D.INSTCD              = C.INSTCD
                   AND D.HARDCDNO            = C.HARDCDNO 
                   AND D.TRGTCD             != C.RMK
                   AND NVL(D.HARDSPEC, '-') != 'N'
                   AND TO_CHAR(SYSDATE, 'YYYYMMDD')  BETWEEN D.VALIFROMDD AND D.VALITODD
                   AND ROWNUM                = 1
               )                                                                                 AS LNKFORMCD,
               (SELECT DECODE(COUNT(FORM.FORMCD), 0, '-', '1100010977')
                  FROM EMR.MRFMCALCMECH MECH,
                       EMR.MRFMFORM FORM
                 WHERE MECH.INSTCD = #instCd#
                   AND FORM.FORMCD = EF.FORM_CD
                   AND MECH.INSTCD = FORM.INSTCD
                   AND MECH.FORMCD = FORM.FORMCD
                   AND TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') BETWEEN FORM.FORMFROMDT AND FORM.FORMTODT
                   AND MECH.BIZFLAG='JINJEONG')                                                  AS JINJEONGCD
          FROM EMA.EFORM_DOCUMENT EF
             , EMR.MRFMFORM MRF
             , ( SELECT TRGTCD
                      , TRGTCDNM  AS OPDIAGNM
                      , TRGTCDSNM AS OPNM
                   FROM EMR.MMBDHRCD
                  WHERE INSTCD   = #instCd#
                    AND HARDCDNO = '6011'
                    AND TO_CHAR(SYSDATE, 'YYYYMMDD') BETWEEN VALIFROMDD AND VALITODD
               ) OP
         WHERE EF.FORM_CD      =    #formCd#
           AND EF.USE_YN       =     'Y'
           AND EF.DEPLOY_YN    =     'Y'
           AND EF.FORM_VERSION =     ( SELECT MAX(FORM_VERSION)
                                        FROM EMA.EFORM_DOCUMENT C
                                       WHERE C.FORM_CD   = EF.FORM_CD
                                         AND C.USE_YN    = 'Y'
                                         AND C.DEPLOY_YN = 'Y' )
           AND MRF.INSTCD        =    #instCd#
           AND MRF.FORMCD        =    EF.FORM_CD
           AND MRF.FORMPROGFLAG  LIKE 'OCR%'
           AND MRF.FORMLEVLCD   !=    '-'
           AND TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') BETWEEN MRF.FORMFROMDT AND MRF.FORMTODT
           AND OP.TRGTCD(+)     =    MRF.FORMCD
      ]]>
    </statement>
    
    <statement id="GET_OCR_STRING" parameterClass="System.Collections.Hashtable" resultClass="System.String">
      <![CDATA[
        SELECT A.TRGTCDNM
          FROM emr.MMBDHRCD A
          WHERE INSTCD = #instcd#
              AND trgtcd = #formcd#
              AND hardcdno = '6938'
              AND TO_CHAR (SYSDATE, 'YYYYMMDDHH') BETWEEN A.valifromdd and a.valitodd
              AND rownum = 1]]>
    </statement>
    <statement id="GET_CERTUSEYN" parameterClass="System.Collections.Hashtable" resultClass="System.String">
      <![CDATA[
        SELECT A.CERTUSEYN
          from emr.mrfmform a
          where 1=1
              and rownum = 1
              and instcd = #instcd#
              and A.FORMCD = #formcd#
              and A.FORMPROGFLAG like 'OCR%'
              and to_char(sysdate, 'YYYYMMDDHH24MISS') between a.formfromdt and a.formtodt]]>
    </statement>

    <statement id="GET_CERTUSEYN_MAP" parameterClass="System.Collections.Hashtable" resultClass="System.String">
      <![CDATA[
        SELECT LISTAGG(DECODE(a.certuseyn,'Y', a.certuseyn,'N'), ', ') WITHIN GROUP (ORDER BY a.certuseyn) as result
          FROM emr.mrfmform A
         WHERE instcd = #instcd#
           AND A.FORMCD IN ($formcd$)
           AND A.FORMPROGFLAG LIKE 'OCR%'`
           AND to_char(sysdate, 'YYYYMMDDHH24MISS') between a.formfromdt and a.formtodt]]>
    </statement>

    <statement id="GET_RESULT" parameterClass="System.Collections.Hashtable" resultClass="System.String">
      <![CDATA[
       SELECT NVL(LISTAGG(c.formcd, ',') WITHIN GROUP (ORDER BY c.formcd), 'N') AS RESULT
         FROM (SELECT DISTINCT a.formcd
                 FROM emr.mrfmocrprnt a, emr.mrfhocrprnt b
                WHERE a.instcd = b.instcd
                  AND a.ocrtag = b.ocrtag
                  AND a.updtdt = b.updtdt
                  AND a.instcd = '031'
                  AND a.formcd IN ('1300013648')
                  AND a.pid    = '1783676'
                  AND substr(a.FSTPRNTDT,1,8) = to_char(sysdate,'yyyymmdd')
                  AND a.eocryn = 'Y'
                  AND b.statcd = 'C') c
       ]]>
    </statement>
	  
    <statement id="GET_DUAL_MODE" parameterClass="System.Collections.Hashtable" resultClass="System.String">
      <![CDATA[
      SELECT CASE
        WHEN count(*) > 0 THEN 'Y' 
        ELSE 'N' 
        END AS result
        FROM emr.MMBDHRCD A
        WHERE a.instcd = #instcd#
            AND A.hardcdno = '6960'
            AND a.trgtcd = #ipaddr#
            AND TO_CHAR (SYSDATE, 'YYYYMMDDHH') BETWEEN A.valifromdd AND a.valitodd]]>
    </statement>
    <statement id="TO_CERTIFY_CONSENT_MST" parameterClass="System.Collections.Hashtable">
      <![CDATA[
      UPDATE ema.consent_mst
        SET consent_state = 'CERTIFY_CMP'
            ,complete_datetime = TO_CHAR(SYSDATE,'yyyy-mm-dd hh24:mi:ss')
            , modify_datetime = TO_CHAR(SYSDATE,'yyyy-mm-dd hh24:mi:ss')
        WHERE consent_mst_rid in ($consent_rids$)
            AND hos_type = #instcd#
            AND consent_state = 'ELECTR_CMP']]>
    </statement>
    <statement  id="TO_CERTIFY_OCRPRNT" parameterClass="System.Collections.Hashtable">
    <![CDATA[
    UPDATE emr.mrfhocrprnt
      SET statcd = 'C'
          , scanyn = 'Y'
          , lastupdtdt = TO_CHAR(SYSDATE,'yyyy-mm-dd hh24:mi:ss')
      WHERE ocrtag IN ($ocrtags$)
          AND instcd = #instcd#
          AND statcd = 'P'
          AND scanyn = 'N']]>
    </statement>
    <statement id="UPDATE_CONSENT_MASTER" parameterClass="System.Collections.Hashtable">
      <![CDATA[
      update EMA.consent_mst
        set use_yn = 'N'
        where hos_type = #instcd#
            and pid = #pid#
            and use_yn = 'Y'
            and ocr_number = #ocrtag#]]>
    </statement>
    <statement id="UPDATE_OCR_HISTORY" parameterClass="System.Collections.Hashtable">
      <![CDATA[
      update emr.mrfmocrprnt
        set instcd = '999'
        where instcd = #instcd#
            and pid = #pid#
            and ocrtag = #ocrtag#]]>
    </statement>
    <statement id="CHECK_JINJEONG_DOCUMENT" parameterClass="System.Collections.Hashtable">
      <![CDATA[
        SELECT COUNT(form.FORMCD)
          FROM emr.mrfmcalcmech mech,
               emr.mrfmform form
          WHERE mech.instcd = #instcd#
            AND form.formcd = #formcd#
            AND mech.instcd = form.instcd
            AND mech.formcd = form.formcd
            AND TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') between form.formfromdt and form.formtodt
            AND mech.bizflag='JINJEONG'
      ]]>
    </statement>
    <statement id="GET_LINKED_ANST_DOCUMENT"  parameterClass="System.Collections.Hashtable">
      <![CDATA[
      SELECT RMK AS LINKFORMCD
        FROM EMR.MMBMHRCD
        WHERE INSTCD = #instcd#
          AND HARDCDNO = 6011
          AND ROWNUM = 1
      ]]>
    </statement>
    <statement id="CHECK_OPERATION_DOCUMENT" parameterClass="System.Collections.Hashtable">
      <![CDATA[
      select case when count(*) > 0 then 'Y'
        else 'N'
        end as result
        from emr.mmbdhrcd
        where hardcdno = 6011
            and instcd = #instcd#
            and trgtcd = #formCd#
            and to_char(sysdate, 'yyyyMMdd') between valifromdd and valitodd
      ]]>
    </statement>
    <statement id="GetUnfinishedListPerUserName" parameterClass="System.Collections.Hashtable" resultMap="ConsentMap">
      <![CDATA[
      SELECT a.consent_mst_rid
              , DECODE(a.consent_state, 'TEMP',        '임시'
                                      , 'ELECTR_CMP',  '확인'
                                      , 'PAPER_OUT',   '출력'
                                      , 'CERTIFY_CMP', '인증'
                                      , 'UNFINISHED',  '임시'
                                      , 'VERBAL',      '구두'
                                      , a.consent_state)                     AS consent_state_disp
              , b.form_name                                                  AS consent_name
              , a.hos_type
              , a.pid
              , ( SELECT hngnm
                  FROM pam.pmcmptbs e
                  WHERE e.instcd = a.hos_type
                  AND e.pid    = a.pid
              )                                                            AS hngnm
              , a.visit_type
              , DECODE(a.visit_type, 'O', '외래'
                                  , 'I', '입원'
                                  , 'D', 'DSC'
                                  , 'E', '응급'
                                  , 'S', '건진'
                                  , a.visit_type)                           AS visit_type_nm
              , a.order_no
              , a.order_cd
              , a.order_name
              , a.cln_dept_cd
              , ( SELECT depthngnm
                  FROM com.zsdddept e
                  WHERE e.instcd   = #instcd#
                  AND e.deptcd   = a.cln_dept_cd
                  AND e.valitodd = '99991231'
              )                                                            AS cln_dept_nm
              , COM.FN_ZS_GETDEPTNAME(#instcd#
                                  , ( SELECT deptcd
                                          FROM com.zsdddept d
                                      WHERE d.instcd      = #instcd#
                                          AND d.deptengabbr = a.wardcd
                                          AND d.valitodd    = '99991231'
                                      )
                                  , '99991231')                            AS wardcd
              , a.roomcd
              , a.form_rid
              , b.form_cd
              , b.form_guid
              , c.prntcnt
              , a.consent_state
              , a.ocr_number 
              , a.cosign_dc_id
              , a.cosign_dc_name
              , a.modify_user_id                                             AS create_user_id
              , a.modify_user_name                                           AS create_user_name
              , a.modify_datetime                                            AS create_datetime
              , a.cln_date
              , a.cretno
              , c.ocrtagprntyn
              , c.formprntnm
              , CASE WHEN EXISTS ( SELECT 1
                                  FROM emr.mmbdhrcd e
                                  WHERE e.instcd   = c.instcd
                                      AND e.hardcdno = '6920'
                                      AND e.trgtcd   = c.formcd
                              ) THEN
                          'N'
                  ELSE 'Y'
              END                                                         AS userdrflag
              , c.print_only
              , d.opdiagnm
              , d.opnm
              , a.oprsrvno
       FROM ema.consent_mst    a
        , ema.eform_document b
        , emr.mrfmform       c
        , ( SELECT trgtcd
                , trgtcdnm  AS opdiagnm
                , trgtcdsnm AS opnm
            FROM emr.mmbdhrcd
            WHERE instcd   = #instcd#
            AND hardcdno = '6011'
            AND TO_CHAR(SYSDATE, 'YYYYMMDD') BETWEEN valifromdd AND valitodd
        ) d
         WHERE a.hos_type                =      #instcd#
          and a.MODIFY_USER_name = #usename#
          AND a.use_yn                  =      'Y'
          AND TO_CHAR(TO_DATE(a.create_datetime, 'YYYY-MM-DD HH24:MI:SS'), 'YYYYMMDD') BETWEEN #startDt# AND #endDt#
          AND b.form_rid                =      a.form_rid
          AND c.instcd                  =      a.hos_type
          AND c.formcd                  =      b.form_cd
          AND c.formprogflag            LIKE   'OCR%'
          AND c.formlevlcd             !=      '-'
          AND TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') BETWEEN c.formfromdt AND c.formtodt]]>
      <isNotEqual property="status" compareValue="ALL">
        <![CDATA[
           AND a.consent_state  IN ($status$)]]>
      </isNotEqual><![CDATA[
           AND CASE WHEN EXISTS ( SELECT 1
                                    FROM emr.mrfmocrprnt d
                                   WHERE d.instcd = #instcd#
                                     AND d.ocrtag = a.ocr_number ) THEN
                         CASE WHEN a.consent_state IN ('TEMP', 'PAPER_OUT')
                               AND EXISTS ( SELECT 1
                                              FROM emr.mrfmocrprnt d
                                                 , emr.mrfhocrprnt e
                                             WHERE d.instcd = #instcd#
                                               AND d.ocrtag = a.ocr_number
                                               AND e.instcd = d.instcd
                                               AND e.ocrtag = d.ocrtag
                                               AND e.updtdt = d.updtdt
                                               AND e.statcd = 'P'
                                          ) THEN 'Y'
                              WHEN a.consent_state = 'CERTIFY_CMP'
                               AND EXISTS ( SELECT 1
                                              FROM emr.mrfmocrprnt d
                                                 , emr.mrfhocrprnt e
                                             WHERE d.instcd = #instcd#
                                               AND d.ocrtag = a.ocr_number
                                               AND e.instcd = d.instcd
                                               AND e.ocrtag = d.ocrtag
                                               AND e.updtdt = d.updtdt
                                               AND e.statcd = 'C'
                                          ) THEN 'Y'
                              ELSE 'N'
                         END
                    ELSE 'Y'
                END = 'Y'
                AND d.trgtcd(+)               = b.form_cd
      ]]>
    </statement>
  </statements>
</sqlMap>