<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.dbs.consentServer.mapper.ConsentMapper">

  <select id="getConsentBySearch" parameterType="java.util.HashMap" resultType="ConsentFormListVO">
    <![CDATA[
      SELECT ''                                          AS idx,
             CF.FORM_ID                                  AS formId,
             CF.FORM_CODE                                AS formCode,
             CF.FORM_NAME                                AS formName,
             ''                                          AS formPrntNm,
             ''                                          AS printCnt,
             ''                                          AS certPass,
             ''                                          AS pid,
             ''                                          AS deptEngAggr,
             ''                                          AS consentState,
             ''                                          AS consentStateEng,
             ''                                          AS consentMstRid,
             ''                                          AS ocrTag,
             ''                                          AS modifyUserId,
             ''                                          AS modifyUserNm,
             ''                                          AS modifyDateTime,
             ''                                          AS createDateTime,
             ''                                          AS clnDate,
             ''                                          AS cretNo,
             ''                                          AS ordType,
             ''                                          AS deptCd,
             ''                                          AS mainDrId,
             ''                                          AS patientName
        FROM EMA.CLIP_FORM CF
        LEFT OUTER JOIN EMA.CLIP_FORM_DATA CFD
          ON CF.FORM_ID = CFD.FORM_ID
        WHERE 1 = 1
    ]]>
    <if test='categoryId != "" and categoryId != null'>
      <if test='categoryId != "1"'>
        <![CDATA[
          AND CF.FORM_CATEGORY_ID = #{categoryId}
        ]]>
      </if>
    </if>
    <if test='keyWord != "" and keyWord != null'>
      <![CDATA[
        AND UPPER(CF.FORM_NAME) LIKE #{keyWord}
      ]]>
    </if>
    <![CDATA[
      AND CF.PRODUCT_ID = 1
      AND CF.FORM_TYPE = 'FORM_TYPE_001'
      AND CF.USE_YN = 'Y'
      AND CFD.PUBLICATION_YN = 'Y'
      AND CFD.FORM_VERSION = ( SELECT MAX(FORM_VERSION)
                                  FROM EMA.CLIP_FORM_DATA A
                                WHERE A.FORM_ID = CFD.FORM_ID
                                  AND A.PUBLICATION_YN = 'Y'
                                  AND A.product_id = '1' )
    ]]>
  </select>

  <select id="getUserFormSet" parameterType="java.util.HashMap" resultType="ConsentFormListVO">
    <![CDATA[
      SELECT MRFI.ITEMINDXSEQ                                                           AS idx,
             CF.FORM_ID                                                                 AS formId,
             CF.FORM_CODE                                                               AS formCode,
             CF.FORM_NAME                                                               AS formName,
    ]]>
    <if test='instCd != "031"'>
      <![CDATA[
             MF.FORMPRNTNM                                                              AS formPrntNm,
             MF.PRNTCNT                                                                 AS printCnt,
      ]]>
    </if>
    <![CDATA[
             (SELECT DECODE(COUNT(FORMCD), 0, 'N', 'Y') AS CERT_PASS
                FROM EMR.MRFMFORM
               WHERE INSTCD = #{instCd}
                 AND TO_CHAR(SYSDATE, 'YYYYMMDD') BETWEEN FORMFROMDT AND FORMTODT
                 AND FORMTODT = '99991231'
                 AND FORMCD = CF.FORM_CODE)                                             AS certPass,
             ''                                                                         AS pid,
             ''                                                                         AS deptEngAggr,
             ''                                                                         AS consentState,
             ''                                                                         AS consentStateEng,
             ''                                                                         AS consentMstRid,
             ''                                                                         AS ocrTag,
             ''                                                                         AS modifyUserId,
             ''                                                                         AS modifyUserNm,
             ''                                                                         AS modifyDateTime,
             ''                                                                         AS createDateTime,
             ''                                                                         AS clnDate,
             ''                                                                         AS cretNo,
             ''                                                                         AS ordType,
             ''                                                                         AS deptCd,
             ''                                                                         AS mainDrId,
             ''                                                                         AS patientName
        FROM EMR.MRFMSETINDX MRFI
       INNER JOIN EMA.CLIP_FORM CF
          ON MRFI.LINKCD = CF.FORM_CODE
        LEFT OUTER JOIN EMA.CLIP_FORM_DATA CFD
          ON CF.FORM_ID = CFD.FORM_ID
          ]]>

    <if test='instCd != "031"'>
      <![CDATA[
        INNER JOIN EMR.MRFMFORM MF
            ON MRFI.LINKCD = MF.FORMCD
            AND MF.INSTCD = '101'
      ]]>
    </if>
    <![CDATA[
       WHERE 1 = 1
         AND MRFI.INSTCD = #{instCd}
         AND MRFI.USERID = #{userId}
         AND CF.FORM_TYPE = 'FORM_TYPE_001'
         AND CF.PRODUCT_ID = '1'
         AND CF.USE_YN = 'Y'
         AND MRFI.VALIYN = 'Y'
         AND MRFI.INDXFLAG = 'O'
         ]]>
    <if test='instCd != "031"'>
      <![CDATA[
         AND TO_CHAR(SYSDATE, 'YYYYMMDDHH24MISS') BETWEEN MF.FORMFROMDT AND MF.FORMTODT
         AND MF.FORMPROGFLAG LIKE 'OCR%'
      ]]>
    </if>
    <![CDATA[
         AND CFD.PUBLICATION_YN = 'Y'
         AND CFD.FORM_VERSION = ( SELECT MAX(FORM_VERSION) 
                                    FROM EMA.CLIP_FORM_DATA A 
                                   WHERE A.FORM_ID = CFD.FORM_ID
                                     AND A.PUBLICATION_YN = 'Y'
                                     AND A.PRODUCT_ID = '1' )
       ORDER BY CF.FORM_NAME
    ]]>
  </select>

  <select id="getConsentList" parameterType="java.util.HashMap" resultType="ConsentFormListVO">
    <!-- CMT.CONSENT_STATE                                                         AS consentStateEng -->
    <![CDATA[
      SELECT ''                                                                        AS idx,
             CF.FORM_ID                                                                AS formId,
             CF.FORM_CODE                                                              AS formCode,
             CF.FORM_NAME                                                              AS formName,
             ''                                                                        AS formPrntNm,
             CMT.PAGECNT                                                               AS printCnt,
             ''                                                                        AS certPass,
             CMT.PID                                                                   AS pid,
             ''                                                                        AS deptEngAggr,
             DECODE(CMT.CONSENT_STATE, 'TEMP',        '임시',
                                       'ELECTR_CMP',  '확인',
                                       'PAPER_OUT',   '출력',
                                       'CERTIFY_CMP', '인증',
                                       'UNFINISHED',  '연동',
                                       'VERBAL',      '구두',
                                       CMT.CONSENT_STATE)                              AS consentState,
             CMT.CONSENT_STATE                                                         AS consentStateEng,
             CONSENT_MST_RID                                                           AS consentMstRid,
             CMT.OCR_NUMBER                                                            AS ocrTag,
             MODIFY_USER_ID                                                            AS modifyUserId,
             MODIFY_USER_NAME                                                          AS modifyUserNm,
             MODIFY_DATETIME                                                           AS modifyDateTime,
             CREATE_DATETIME                                                           AS createDateTime,
             CMT.CLN_DATE                                                              AS clnDate,
             CMT.CRETNO                                                                AS certNo,
             CMT.VISIT_TYPE                                                            AS ordType,
             CMT.CLN_DEPT_CD                                                           AS deptCd,
             CMT.MAIN_DR_ID                                                            AS mainDrId,
             (SELECT HNGNM 
               FROM PAM.PMCMPTBS PTBS 
               WHERE INSTCD = '031'
                 AND PID    = CMT.PID)                                                 AS patientName
        FROM EMA.CLIP_FORM CF
        LEFT OUTER JOIN EMA.CLIP_FORM_DATA CFD
          ON CF.FORM_ID = CFD.FORM_ID,
             EMA.CONSENT_MST CMT
        WHERE 1 = 1
          ]]>
    <if test='consentState != "ALL"'>
      <![CDATA[
          AND CMT.CONSENT_STATE IN
      ]]>

       <foreach collection='consentState' item='item' index="index" separator=',' open='(' close=')'>
        <![CDATA[
          ${item}
        ]]>
       </foreach>
      
    </if>
    <if test='patientCode != "" and patientCode != null'>
      <if test='userId == "" or userId == null'>
        <![CDATA[
          AND CMT.PID = #{patientCode}
        ]]>
      </if>
    </if>
    <if test='userId != "" and userId != null'>
      <![CDATA[
        AND CMT.MODIFY_USER_ID = #{userId}
      ]]>
    </if>
    <![CDATA[
          AND TO_CHAR(TO_DATE(CMT.MODIFY_DATETIME, 'YYYY-MM-DD HH24:MI:SS'), 'YYYYMMDD') BETWEEN #{startDt} AND #{endDt}
          AND CMT.USE_YN = 'Y'
          AND CMT.FORM_CD = CF.FORM_CODE
          AND CF.PRODUCT_ID = 1
          AND CF.FORM_TYPE = 'FORM_TYPE_001'
          AND CF.USE_YN = 'Y'
          AND CFD.PUBLICATION_YN = 'Y'
          AND CFD.FORM_VERSION = ( SELECT MAX(FORM_VERSION)
                                    FROM EMA.CLIP_FORM_DATA A
                                    WHERE A.FORM_ID = CFD.FORM_ID
                                      AND A.PUBLICATION_YN = 'Y'
                                      AND A.product_id = '1' )
    ]]>
  </select>

  <update id="updateConsentMstForDelete" parameterType="java.util.HashMap">
    <![CDATA[ 
      UPDATE EMA.CONSENT_MST
         SET MODIFY_USER_ID = #{userId},
             MODIFY_DATETIME = TO_CHAR(SYSDATE, 'yyyy-mm-dd hh24:mi:ss'),
             USE_YN = 'N',
             REASON_FOR_USE_N = #{reasonForUseN},
             MODIFY_USER_NAME = COM.FN_ZS_GETUSERNM(#{userId}, TO_CHAR(SYSDATE, 'YYYYMMDD'))
       WHERE CONSENT_MST_RID = #{consentMstRid}
    ]]>
  </update>

  <select id="getConsentMstForDeleteOcrTag" parameterType="java.util.HashMap" resultType="string">
    <![CDATA[ 
      SELECT OCR_NUMBER AS OCR_TAG
        FROM EMA.CONSENT_MST
       WHERE CONSENT_MST_RID = #{consentMstRid}
    ]]>
  </select>

  <update id="updateMrfhOcrPrntStatCd" parameterType="java.util.HashMap">
    <![CDATA[ 
      UPDATE EMR.MRFHOCRPRNT
         SET STATCD = 'CP',
             LASTUPDTRID = #{userId},
             LASTUPDTDT = TO_CHAR(SYSDATE, 'yyyyMMdd hh24:mi:ss')
       WHERE OCRTAG = #{ocrTag}
    ]]>
  </update>

  <select id="getOcrTag" resultType="string">
    <![CDATA[
      SELECT TO_CHAR(SYSDATE, 'YYYYMMDD') || SUBSTR(LPAD(EMR.SQ_MRFMOCRP_01.NEXTVAL, 10, '0'), 6, 5) AS OCR_TAG
        FROM DUAL
    ]]>
  </select>

</mapper>