<?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.SaveFormMapper">
  <select id="getSequence" parameterType="java.util.HashMap" resultType="int">
    <![CDATA[
      SELECT UD1_NUM AS SEQ
        FROM EMA.EFORM_CODE
       WHERE MAIN_CD = 'SEQUENCE'
         AND SUB_CD = #{subCd}
      FOR UPDATE
    ]]>
  </select>

  <insert id="setSequence" parameterType="java.util.HashMap" useGeneratedKeys="true">
    <![CDATA[
      INSERT INTO EMA.EFORM_CD
        (MAIN_CD, SUB_CD, CD_NM, UD1_NUM, SEQ_NO, USE_YN)
      VALUES
        ('SEQUENCE', #{subCd}, 'count', 1, 1, 'Y')
    ]]>
  </insert>

  <update id="updateSequence" parameterType="java.util.HashMap">
    <![CDATA[
      UPDATE EMA.EFORM_CODE
         SET UD1_NUM = #{ud1Num}
       WHERE MAIN_CD = 'SEQUENCE'
         AND SUB_CD = #{subCd}
    ]]>
  </update>

  <insert id="insertConsentMaster" parameterType="SaveConsentFormDTO" useGeneratedKeys="true">
    <![CDATA[
        insert into EMA.CONSENT_MST
        (
          CONSENT_MST_RID,
          HOS_TYPE, 
          PID,
          VISIT_TYPE,
          OCR_NUMBER,
          CLN_DEPT_CD,
          CLN_DATE,
          CRETNO,
          WARDCD,
          ROOMCD,
          FORM_RID,
          CONSENT_STATE,
          COMPLETE_YN,
          COMPLETE_DATETIME,
          PAPER_YN,
          REISSUE_YN,
          REISSUE_CONSENT_MST_RID,
          REWRITE_YN,
          REWRITE_CONSENT_MST_RID,
          USE_YN,
          MAIN_DR_ID,
          CREATE_USER_ID,
          CREATE_DATETIME,
          MODIFY_USER_ID,
          MODIFY_DATETIME,
          CREATE_USER_NAME,
          MODIFY_USER_NAME,
          OPRSRVNO,
          PAGECNT,
          FORM_CD,
          DEVICE_IDENT_NO
        )
        VALUES
        (
          #{consentMstRid},
          #{instCd},
          #{patientCode},
          #{ordType},
          #{ocrTag},
          #{clnDeptCode},
          #{clnDate},
          #{cretNo},
          #{ward},
          #{roomCd},
          #{formId},
          #{consentState},
          #{completeYn},
          #{completeDateTimeToChars},
          #{paperYn},
          #{reissueYn},
          #{reissueConsentMstRid},
          #{rewriteYn},
          #{reWriteConsentMstRid},
          'Y',
          #{mainDrId},
          #{userId},
          TO_CHAR(SYSDATE,'yyyy-mm-dd hh24:mi:ss'),
          #{userId},
          TO_CHAR(SYSDATE,'yyyy-mm-dd hh24:mi:ss'),
          #{createUserName},
          COM.FN_ZS_GETUSERNM(#{userId}, TO_CHAR(SYSDATE, 'YYYYMMDD')),
          #{opRsrvNo},
          #{formPageCnt},
          #{formCd},
          #{deviceMacAddr}
        )
    ]]>
  </insert>

  <insert id="insertConsentData" parameterType="SaveConsentFormDTO" useGeneratedKeys="true">
    <![CDATA[
      INSERT INTO EMA.CONSENT_DATA
      (
        CONSENT_DATA_RID,
        CONSENT_MST_RID,
        FORM_RID,
        DEVICE_TYPE,
        DEVICE_IDENT_NO,
        COMPLETE_YN,
        CREATE_USER_ID,
        CREATE_DATETIME,
        FORM_XML,
        DATA_XML
      )
      VALUES
      (
        #{consentDataRid},
        #{consentMstRid},
        #{formId},
        #{deviceType},
        #{deviceMacAddr},
        #{completeYn},
        #{userId},
        TO_CHAR(SYSDATE,'yyyy-mm-dd hh24:mi:ss'),
        #{formConsentByte},
        #{dataConsentByte}
      )
    ]]>
  </insert>

  <select id="getFormXmlOrgData" parameterType="int" resultType="string">
    <![CDATA[
      SELECT EMA.blob_to_clob(FORM_XML)
        FROM EMA.CONSENT_DATA 
        WHERE 1 = 1 
          AND CONSENT_MST_RID = #{consentMstRid}
    ]]>
  </select>

</mapper>