<?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.HospitalMapper">
  <select id="getBaseData" resultType="string">
    <![CDATA[
      SELECT LISTAGG(DECODE(HARDCDNO, 6580, 'SESSION_TIME|'||TRGTCDNM,
                                      6581, 'ELECTR_CMP|'||TRGTCDNM,
                                      6582, 'MY_PATIENT|'||TRGTCDNM,
                                      6583, 'MOBILE_VERSION|'||TRGTCDNM,
                                      6584, 'PC_VERSION|'||TRGTCDNM), ',')
                                      WITHIN GROUP(ORDER BY HARDCDNO) AS BASE_DATA
        FROM EMR.MMBDHRCD
       WHERE INSTCD   = #{instCd}
         AND HARDCDNO IN (6580, 6581, 6582, 6583, 6584)
    ]]>
  </select>

  <select id="doLogin" resultType="string">
   <![CDATA[
     SELECT UPWD.PWD_SHA256              AS PWD_SHA256
       FROM (
             SELECT USERB.USERID AS USERID, USERB.USERNM AS USERNM
               FROM COM.ZSUMUSRB USERB, 
                    (SELECT USERID, MAX(USERFROMDD) AS USERFROMDD 
                       FROM COM.ZSUMUSRB
                      WHERE USERID = #{userId}
                      GROUP BY USERID
                    ) MAX_USERB
              WHERE USERB.USERID = MAX_USERB.USERID
                AND USERB.USERFROMDD = MAX_USERB.USERFROMDD
            ) USRB,
            COM.ZSUMUPWD UPWD,
            COM.ZSUMPWLG PWLG
      WHERE UPWD.USERID(+) = USRB.USERID
        AND PWLG.USERID(+) = USRB.USERID
    ]]>
  </select>
  
  <select id="getUserDeptList" parameterType="java.util.HashMap" resultType="com.dbs.consentServer.VO.hospitalVO.UserDeptVO">
    <![CDATA[
      SELECT CODE.CDID                                                                 AS DUTPLCEINSTCD,
             CODE.CDNM                                                                 AS DUTPLCEINSTNM,
             DEPT.DEPTCD                                                               AS DUTPLCECD,
             DEPT.DEPTHNGNM                                                            AS DEPTHNGNM,
             CODE.CDNM || ':'|| DEPT.DEPTHNGNM                                         AS DEPTNM,
             USRD.FROMDD                                                               AS FROMDD,
             SEQ.SORTNO                                                                AS SORTNO,
             CODE.CDID || ':' || DEPT.DEPTCD                                           AS DEPTCD,
             NVL(USRD.JOBKINDCD, '1140')                                               AS JOBKINDCD,
             NVL((SELECT CDNM
                    FROM COM.ZBCMCODE 
                   WHERE CDGRUPID = 'R0061'
                     AND CDID = USRD.JOBKINDCD), '-')                                  AS JOBKINDNM,
             NVL((SELECT HOSP.HOSPAPPSTATUS
                    FROM EMA.EFORM_HOSPUSERS HOSP
                   WHERE HOSP.HOSPUSER_ID = #{userId}
                 ), 'I')                                                               AS INDEXPAGE,
             USRD.USERID AS USERID,
             USRB.USERNM AS USERNAME,
             CASE WHEN JOBKINDCD LIKE '03%' AND JOBPOSCD IN ('1799','1800') THEN 'A'
             WHEN JOBKINDCD LIKE '03%' THEN 'B' ELSE 'C' END                           DRKIND
        FROM COM.ZBCMCODE CODE,
             COM.ZSDDDEPT DEPT,
             (
              SELECT USERB.USERID AS USERID, USERB.USERNM AS USERNM
                FROM COM.ZSUMUSRB USERB, 
                     (SELECT USERID, MAX(USERFROMDD) AS USERFROMDD 
                        FROM COM.ZSUMUSRB
                       WHERE USERID = #{userId}
                       GROUP BY USERID
                     ) MAX_USERB
               WHERE USERB.USERID = MAX_USERB.USERID
                 AND USERB.USERFROMDD = MAX_USERB.USERFROMDD
             ) USRB,
             COM.ZSUMUSRD USRD LEFT OUTER JOIN COM.ZSUMSORT SEQ ON (
                  USRD.USERID = SEQ.USERID
              AND USRD.DUTPLCEINSTCD = SEQ.DUTPLCEINSTCD
              AND USRD.DUTPLCECD = SEQ.DUTPLCECD), 
             (SELECT CDRL.SUBCDSID
                FROM COM.ZBCDCDRL CDRL,
                     COM.ZBCMCODE CODE
               WHERE CODE.CDGRUPID = 'Z0011'
                 AND TO_CHAR(SYSDATE, 'YYYYMMDD') BETWEEN CODE.VALIFROMDD AND CODE.VALITODD
                 AND CODE.CDID       = #{sysCd}
                 AND CODE.CDSID      = CDRL.SUPCDSID) A
            WHERE A.SUBCDSID         = CODE.CDSID
              AND CODE.CDID          = USRD.DUTPLCEINSTCD
              AND USRD.USERID        = #{userId}
              AND USRD.USERID        = USRB.USERID
              AND USRD.DUTPLCECD     = DEPT.DEPTCD
              AND USRD.DUTPLCEINSTCD = DEPT.INSTCD
              AND USRD.PSNWORKYN = 'N'
              AND TO_CHAR(SYSDATE, 'YYYYMMDD') BETWEEN CODE.VALIFROMDD AND CODE.VALITODD
              AND TO_CHAR(SYSDATE, 'YYYYMMDD') BETWEEN DEPT.VALIFROMDD AND DEPT.VALITODD
              AND TO_CHAR(SYSDATE, 'YYYYMMDD') BETWEEN USRD.FROMDD AND USRD.TODD
            ORDER BY SORTNO ASC
    ]]>
  </select>
  
  <select id="getWardList" resultType="com.dbs.consentServer.VO.hospitalVO.WardDeptVO">
    <![CDATA[
      SELECT ''               AS deptCd,
             '병동(전체)'       AS deptNm,
             1                 AS sort
        FROM DUAL
       UNION ALL
      SELECT dept.deptcd       AS deptcd,
             dept.depthngnm    AS deptnm,
             2                 AS sort
        FROM com.zsdddept dept
       WHERE dept.instcd      = #{instCd}
         AND dept.orddeptflag = 'W'
         AND dept.orduseyn    = 'Y'
         AND TO_CHAR(SYSDATE, 'YYYYMMDD') BETWEEN dept.valifromdd AND dept.valitodd
       ORDER BY sort, deptnm
    ]]>
  </select>

  <select id="getDeptList" parameterType="java.util.HashMap" resultType="com.dbs.consentServer.VO.hospitalVO.WardDeptVO">
    <![CDATA[
      SELECT ''                    AS deptCd,
             '진료과(전체)'          AS deptNm,
             0                      AS sort
        FROM DUAL
       UNION ALL
      SELECT deptcd                 AS deptCd,
             depthngnm              AS deptNm,
             1                      AS sort
        FROM com.zsdddept
       WHERE instcd      = #{instCd}
         AND TO_CHAR(CURRENT_DATE,'YYYYMMDD') BETWEEN valifromdd AND valitodd
    ]]>
    <if test='ordType == "OP"'>
      <![CDATA[
        AND orddeptflag = 'D'
        AND orduseyn    = 'Y'
      ORDER BY sort, deptnm
      ]]>
    </if>
    <if test='ordType != "OP"'>
      <![CDATA[
        AND rcptuseyn = 'Y'
      ORDER BY sort, deptnm
      ]]>
    </if>
  </select>
  
  <select id="getDoctorList" parameterType="java.util.HashMap" resultType="com.dbs.consentServer.VO.hospitalVO.GetDoctorVO">
    <![CDATA[
      SELECT ''                                   AS doctorId,
             '진료의(전체)'                         AS doctorNm,
             '일반의'                               AS doctorKind,
             ''                                   AS drkind,
             1                                     AS sort
        FROM DUAL
       UNION
      SELECT b.userid                              AS doctorId,
             b.usernm                              AS doctorNm,
             DECODE(a.jobposcd, '1800', '전공의'
                              , '1799', '전공의'
                              , '전문의')          AS doctorKind,
             CASE 
               WHEN jobkindcd like '03%' and jobposcd in ('1799','1800')
                 THEN 'A'
               WHEN jobkindcd like '03%' then 'B' else 'C'
             END drKind,
             2                                     AS sort
        FROM com.zsumusrd a
             , com.zsumusrb b
       WHERE a.dutplceinstcd     =       #{instCd}
         AND a.psnworkyn         =       'N'
         AND NVL(a.tempyn, '-') !=       'C'
         AND a.jobkindcd         LIKE    '03%'
    ]]>
    <if test='ordDeptCd != "" and ordDeptCd != null'>
      <![CDATA[
        AND a.dutplcecd         =       #{ordDeptCd}
      ]]>
    </if>
    <![CDATA[
         
         AND #{srchDd}            BETWEEN a.fromdd AND a.todd
         AND b.userid            =       a.userid
         AND #{srchDd}            BETWEEN b.userfromdd AND b.usertodd
       ORDER BY sort, doctorid
    ]]>
  </select>

  <select id="getInPatientList" parameterType="PatientListDTO" resultType="GetPatientVO">
    <if test='patientState == "C"'>
      <![CDATA[
        with tmp as
        (SELECT   /*+ leading(h m c) materialize */ m.instcd,m.pid ,m.cretno,m.orddd
           FROM ema.consent_mst c,
                emr.mrfmocrprnt m,
                emr.mrfhocrprnt h
          WHERE c.use_yn = 'Y'
            AND c.consent_state IN ('UNFINISHED', 'TEMP', 'ELECTR_CMP')
            AND m.instcd = c.hos_type
            AND m.ocrtag = c.ocr_number
            AND m.instcd = h.instcd
            AND m.ocrtag = h.ocrtag
            AND m.updtdt = h.updtdt
            AND h.statcd = 'P'
            AND h.updtdt like #{srchDd}||'%'
            AND h.instcd = #{instCd} )
      ]]>
    </if>
    <![CDATA[
      SELECT ptbs.pid                                                                        AS pid,
             inpt.indd                                                                       AS inDd,
             inpt.cretno                                                                     AS cretNo,
             inpt.instcd                                                                     AS instCd,
             inpt.ordtype                                                                    AS ordType,
             ptbs.hngnm                                                                      AS hngNm,
             inpt.orddeptcd                                                                  AS ordDeptCd,
             inpt.medispclid                                                                 AS doctorId,
             ptbs.sex ||'/'||com.Fn_zz_getage('','',TO_CHAR(SYSDATE, 'yyyymmdd'),'B',brthdd) AS sa,
             com.fn_zs_getdeptnm(inpt.instcd,inpt.orddeptcd,inpt.dschdd)                     AS deptNm,
             com.fn_zs_getusernm(inpt.medispclid,inpt.dschdd)                                AS doctorNm,
             inpt.roomCd                                                                     AS roomCd,
             ''                                                                              AS opRoomNm,
             ''                                                                              AS oprsRvNo
        FROM pam.pmihinpt inpt,
              pam.pmcmptbs ptbs
    ]]>
    <if test='srchYn == "N"'>
      <if test='myPatient == "Y"'>
        <![CDATA[
          , emr.mnwhsetp setp
        ]]>
      </if>
    </if>
    <![CDATA[
      WHERE  inpt.instcd = #{instCd}
    ]]>
    <if test='pid != "" and pid != null'>
      <![CDATA[
        AND inpt.pid = #{pid}
      ]]>
    </if>
    <if test='patientState == "A"'>
      <!-- 재원 -->
      <![CDATA[
        and inpt.indschacptstat = 'A'
      ]]>
    </if>
    <if test='patientState == "0"'>
      <!-- 입원 -->
      <![CDATA[
        AND inpt.careinrmdd = #{srchDd}
      ]]>
    </if>
    <if test='patientState == "1"'>
      <!-- 퇴원 -->
      <![CDATA[
        AND inpt.indschacptstat in ('T','D')
        AND inpt.dschdd = #{srchDd}
      ]]>
    </if>
    <if test='patientState == "C"'>
      <![CDATA[
        AND EXISTS (SELECT 1
                      FROM tmp a
                     where a.instcd = inpt.instcd
                       and a.pid    = inpt.pid
                       and a.orddd  = inpt.indd
                       and a.cretno = inpt.cretno )
      ]]>
    </if>
    <if test='patientState != "" and patientState != null'>
      <if test='wardCd != "" and wardCd != null'>
        <![CDATA[
          AND inpt.wardcd = #{wardCd}
        ]]>
      </if>
      <if test='myPatient == "N"'>
        <if test='doctorId != "" and doctorId != null'>
          <!-- 전공의(A), 담당교수(B), 그외 직종(C) 구분코드-->
          <![CDATA[
          AND CASE
                WHEN #{drKind} = 'A'
                  THEN inpt.atdoctid
                ELSE inpt.medispclid
              END = #{doctorId}
        ]]>
        </if>
      </if>

      <if test='ordDeptCd != "" and ordDeptCd != null'>
        <![CDATA[
          AND inpt.orddeptcd = #{ordDeptCd}
        ]]>
      </if>
    </if>
    <if test='srchYn == "N"'>
      <if test='myPatient == "Y"'>
        <![CDATA[
          AND setp.nursid = #{userId}
          AND setp.pid = inpt.pid
          AND setp.fromdt < '99999999999999'
          AND setp.todt > SYSTIMESTAMP
          AND setp.instcd =  #{instCd}
        ]]>
      </if>
    </if>

    <![CDATA[
        AND inpt.mskind = 'M'
        AND inpt.histstat = 'Y'
        AND inpt.ordtype in ('I','D')
        AND ptbs.instcd = inpt.instcd
        AND ptbs.pid = inpt.pid
      ORDER BY inpt.roomcd,
              ptbs.hngnm,
              inpt.indd
    ]]>
  </select>

  <select id="getOutPatientList" parameterType="PatientListDTO" resultType="GetPatientVO">
    <if test='patientState == "3"'>
      <![CDATA[
        with tmp as
        (SELECT /*+ leading(h m c) materialize */ m.instcd,m.pid ,m.cretno,m.orddd
          FROM ema.consent_mst c,
               emr.mrfmocrprnt m,
               emr.mrfhocrprnt h
         WHERE c.use_yn = 'Y'
           AND c.consent_state IN ('UNFINISHED', 'TEMP', 'ELECTR_CMP')
           AND m.instcd = c.hos_type
           AND m.ocrtag = c.ocr_number
           AND m.instcd = h.instcd
           AND m.ocrtag = h.ocrtag
           AND m.updtdt = h.updtdt
           AND h.statcd = 'P'
           AND h.updtdt like #{srchDd}||'%'
           AND h.instcd = #{instCd} )
      ]]>
    </if>
    <![CDATA[
      SELECT ptbs.pid                                                   AS pid,
             otpt.orddd                                                 AS inDd,
             otpt.cretno                                                AS cretNo,
             otpt.instcd                                                AS instCd,
             otpt.ordtype                                               AS ordType,
             ptbs.hngnm                                                 AS hngNm,
             otpt.orddeptcd                                             AS ordDeptCd,
             otpt.orddrid                                               AS doctorId,
             ptbs.sex ||'/'||com.Fn_zz_getage('','',orddd,'B',brthdd)   AS sa,
             com.fn_zs_getdeptnm(otpt.instcd,otpt.orddeptcd,otpt.orddd) AS deptnm,
             com.fn_zs_getusernm(otpt.orddrid,otpt.orddd)               AS doctorNm,
             ''                                                         AS roomCd,
             ''                                                         AS opRoomNm,
             ''                                                         AS oprsRvNo
        FROM pam.pmohotpt otpt,
             pam.pmcmptbs ptbs
       WHERE otpt.instcd = #{instCd}
         AND otpt.mskind = 'M'
         AND otpt.histstat IN ('R', 'T')
         AND otpt.ordtype IN ('O','S')
         AND ptbs.instcd = otpt.instcd
         AND ptbs.pid = otpt.pid
    ]]>
    <if test='srchYn == "N"'>
      <![CDATA[
        AND otpt.orddd = #{srchDd}
      ]]>
    </if>
    <if test='ordDeptCd != "" and ordDeptCd != null'>
      <![CDATA[
        AND otpt.orddeptcd = #{ordDeptCd}
      ]]>
    </if>
    <if test='doctorId != "" and doctorId != null'>
      <![CDATA[
        AND otpt.orddrid= #{doctorId}
      ]]>
    </if>
    <if test='patientState == "3"'>
      <if test='patientState != "" and patientState != null'>
        <if test='srchYn == "N"'>
          <if test='patientState != "A"'>
            <![CDATA[
              AND otpt.elbulbodstat = #{patientState}
            ]]>
          </if>
        </if>
      </if>
    </if>
    <if test='patientState == "3"'>
      <![CDATA[
        AND EXISTS (SELECT 1
                      FROM tmp a
                     where a.instcd = otpt.instcd
                       and a.pid    = otpt.pid
                       and a.orddd  = otpt.orddd
                       and a.cretno = otpt.cretno )
      ]]>
    </if>
    <if test='pid != "" and pid != null'>
      <![CDATA[
        AND otpt.pid = #{pid}
      ]]>
    </if>
    <![CDATA[
      ORDER BY ptbs.hngnm, otpt.orddd DESC
    ]]>
  </select>

  <select id="getOpPatientList" parameterType="PatientListDTO" resultType="GetPatientVO">
    <![CDATA[
      SELECT oprs.pid                                                       AS pid,
             to_char(to_date(inpt.indd,'yyyymmdd'),'yyyymmdd')              AS inDd,
             inpt.cretno                                                    AS cretNo,
             inpt.instcd                                                    AS instcd,
             inpt.ordtype                                                   AS ordType,
             ptbs.hngnm                                                     AS hngNm,
             oprs.perfdeptcd                                                AS ordDeptCd,
             oprs.perfdrid                                                  AS doctorId,
             ptbs.sex ||'/'||com.Fn_zz_getage('','',dschdd,'B',brthdd)      AS sa,
             com.fn_zs_getdeptnm(oprs.instcd,oprs.perfdeptcd,oprs.opcnfmdd) AS deptNm,
             com.fn_zs_getusernm(oprs.perfdrid,oprs.opcnfmdd)               AS doctorNm,
             inpt.roomcd                                                    AS roomCd,
             oproomcd                                                       AS opRoomNm,
             oprs.oprsrvno                                                  AS oprsRvNo
        FROM emr.mmohoprs oprs
             ,pam.pmcmptbs ptbs
             ,pam.pmihinpt inpt
       WHERE oprs.ophistcd = 'O'
         AND inpt.histstat = 'Y'
         AND inpt.mskind = 'M'
         AND ptbs.instcd = oprs.instcd
         AND ptbs.pid = oprs.pid
         AND inpt.instcd = oprs.instcd
         AND inpt.pid = oprs.pid
         AND oprs.opcnfmdd BETWEEN inpt.indd AND inpt.dschdd
         AND oprs.instcd = #{instCd}
         AND oprs.opcnfmdd = #{srchDd}
    ]]>
    <if test='ordDeptCd !="" and ordDeptCd != null'>
      <!-- 과 선택시 -->
      <![CDATA[
        AND oprs.perfdeptcd = #{ordDeptCd}
      ]]>
    </if>
    <if test='doctorId != "" and doctorId != null'>
      <!-- 의사 선택시 -->
      <![CDATA[
        AND oprs.perfdrid = #{doctorId}
      ]]>
    </if>
    <if test= 'pid != "" and pid != null'>
      <!-- 환자 등록번호 -->
      <![CDATA[
        AND oprs.pid = #{pid}
      ]]>
    </if>
    <if test='patientState =="0"'>
      <!-- 미착 -->
      <![CDATA[
        AND oprs.opstatcd   IN      ('10', '20')
      ]]>
    </if>
    <if test='patientState =="1"'>
      <!-- 준비 -->
      <![CDATA[
        AND oprs.opstatcd =       '30'
      ]]>
    </if>
    <if test='patientState == "2"'>
      <!-- 진행 -->
      <![CDATA[
        AND oprs.opstatcd = '40'
      ]]>
    </if>
    <if test='patientState == "3"'>
      <!-- 완료 -->
      <![CDATA[
        AND oprs.opstatcd IN ('50', '60', '70')
      ]]>
    </if>
  </select>

  <select id="getErPatientList" parameterType="PatientListDTO" resultType="GetPatientVO">
    <if test='patientState == "C"'>
      <![CDATA[
        with tmp as
        (SELECT /*+ leading(h m c) materialize */ m.instcd,m.pid ,m.cretno,m.orddd
           FROM ema.consent_mst c,
                emr.mrfmocrprnt m,
                emr.mrfhocrprnt h
          WHERE c.use_yn = 'Y'
            AND c.consent_state IN ('UNFINISHED', 'TEMP', 'ELECTR_CMP')
            AND m.instcd = c.hos_type
            AND m.ocrtag = c.ocr_number
            AND m.instcd = h.instcd
            AND m.ocrtag = h.ocrtag
            AND m.updtdt = h.updtdt
            AND h.statcd = 'P'
            AND h.updtdt like #{srchDd}||'%'
            AND h.instcd = #{instCd} )
      ]]>
    </if>
    <![CDATA[
      SELECT ptbs.pid                                                                        AS pid,
             inpt.indd                                                                       AS indd,
             inpt.cretno                                                                     AS cretNo,
             inpt.instCd                                                                     AS instCD,
             inpt.ordtype                                                                    AS ordType,
             ptbs.hngnm                                                                      AS hngNm,
             inpt.orddeptcd                                                                  AS ordDeptCd,
             inpt.medispclid                                                                 AS doctorId,
             ptbs.sex ||'/'||com.Fn_zz_getage('','',TO_CHAR(SYSDATE, 'yyyymmdd'),'B',brthdd) AS sa,
             com.fn_zs_getdeptnm(inpt.instcd,inpt.orddeptcd,inpt.dschdd)                     AS deptNm,
             com.fn_zs_getusernm(inpt.medispclid,inpt.dschdd)                                AS doctorNm,
             inpt.roomcd                                                                     AS roomCd,
             ''                                                                              AS opRoomNm,
             ''                                                                              AS oprsRvNo
        FROM pam.pmihinpt inpt,
             pam.pmcmptbs ptbs
       WHERE inpt.instcd = #{instCd}
         AND inpt.mskind = 'M'
         AND inpt.histstat = 'Y'
         AND inpt.ordtype = 'E'
         AND ptbs.instcd = inpt.instcd
         AND ptbs.pid = inpt.pid
    ]]>
    <if test='patientState == "A"'>
      <!-- 재원 -->
      <![CDATA[
        AND inpt.indschacptstat = 'A'
        AND inpt.eroutdt = '-'
      ]]>
    </if>
    <if test='patientState == "0"'>
      <!-- 퇴원 -->
      <![CDATA[
        AND inpt.indschacptstat in ('T','D')
        AND inpt.dschdd = #{srchDd}
      ]]>
    </if>
    <if test='patientState == "1"'>
      <!-- 퇴실정리 -->
      <![CDATA[
        AND inpt.indschacptstat = 'A'
        AND inpt.eroutdt       !=       '-' 
        AND inpt.eroutdt       !=       '99991231235959'
        AND inpt.dschdd         =       '99991231'
      ]]>
    </if>
    <if test='patientState == "C"'>
      <![CDATA[
        AND EXISTS (SELECT 1
                      FROM tmp a
                     where a.instcd = inpt.instcd
                       and a.pid    = inpt.pid
                       and a.orddd  = inpt.indd
                       and a.cretno = inpt.cretno )
      ]]>
    </if>
    <if test='pid != "" and pid != null'>
      <![CDATA[
        AND inpt.pid = #{pid}
      ]]>
    </if>
    <!-- 상태가 전체가 아니면 -->
    <if test='patientState != "" and patientState != null'>
      <if test='ordDeptCd != "" and ordDeptCd != null'>
        <![CDATA[
          AND inpt.orddeptcd = #{ordDeptCd}
        ]]>
      </if>
      <if test='doctorId != "" and doctorId != null'>
        <![CDATA[
          AND CASE WHEN #{drKind} = 'A' THEN inpt.atdoctid ELSE inpt.medispclid END = #{doctorId}
        ]]>
      </if>
    </if>
    <![CDATA[
      ORDER BY ptbs.hngnm,inpt.indd,inpt.intm
    ]]>
  </select>

  <select id="getPatientInfoIn" parameterType="PatientInfoDTO" resultType="GetPatientInfoVO">
    <![CDATA[
        SELECT INPT.ORDDEPTCD                                                               AS ordDeptCd,
               (SELECT DEPTENGABBR 
                  FROM COM.ZSDDDEPT DEPT
                 WHERE DEPT.INSTCD = INPT.INSTCD
                   AND DEPT.DEPTCD = INPT.ORDDEPTCD
                   AND INPT.INDD BETWEEN DEPT.VALIFROMDD AND DEPT.VALITODD)                 AS deptEngAbbr,
               COM.FN_ZS_GETDEPTNAME(INPT.INSTCD, INPT.ORDDEPTCD, INPT.INDD)                AS ordDeptNm,
               INPT.PID                                                                     AS pid,
               TO_CHAR(INPT.CRETNO)                                                         AS cretNo,
               INPT.ROOMCD                                                                  AS roomCd,
               INPT.INDD                                                                    AS inDd,
               INPT.DSCHDD                                                                  AS dschDd,
               INPT.ORDTYPE                                                                 AS ordType,
               INPT.INSTCD                                                                  AS instCd,
               INPT.MEDISPCLID                                                              AS mainDrId,
               COM.FN_ZS_GETUSERNM(INPT.MEDISPCLID, INPT.INDD)                              AS mainDrName,
               COM.FN_ZS_GETUSERNM(INPT.ERMEDISPCLID, INPT.INDD)                            AS erDrNm,
               TO_CHAR(INPT.FSTRGSTDT)                                                      AS firstRgstDt,
               PTBS.SEX || '/' || COM.FN_ZZ_GETAGEAS(PTBS.RRGSTNO1,
                                                     PTBS.RRGSTNO2,
                                                     TO_CHAR(SYSDATE, 'YYYYMMDD'),
                                                     'A', PTBS.BRTHDD)                      AS sa,
               PTBS.HNGNM                                                                   AS patientName,
               PTBS.RRGSTNO1 || '-' || SUBSTR(PTBS.RRGSTNO2, 1, 1) || '******'              AS juminNo,
               PTBS.ZIPCDADDR || ' ' || DETLADDR                                            AS zipCdAddr,
               PTBS.MPPHONTEL                                                               AS telNum,
               (SELECT CDNM
                  FROM COM.ZBCMCODE
                 WHERE CDGRUPID = 'P0008'
                   AND CDID     = INPT.INSUKIND)                                            AS insuKind,
               (SELECT DEPT.DEPTENGABBR
                  FROM COM.ZSDDDEPT DEPT
                 WHERE DEPT.INSTCD =       INPT.INSTCD
                   AND DEPT.DEPTCD =       INPT.WARDCD
                   AND INPT.INDD      BETWEEN DEPT.VALIFROMDD AND DEPT.VALITODD)               AS ward,
               (SELECT DIAG.DIAGCD
                  FROM EMR.MMOHDIAG DIAG
                 WHERE DIAG.INSTCD         = #{instCd}
                   AND DIAG.PID            = INPT.PID
                   AND DIAG.ORDDD          = INPT.INDD
                   AND DIAG.CRETNO         = INPT.CRETNO
                   AND DIAG.ORDDEPTCD      = INPT.ORDDEPTCD
                   AND DIAG.DIAGHISTCD     = 'O'
                   AND DIAG.DIAGTYPECD     = 'D'
                   AND DIAG.DIAGKINDCDFLAG = 'M'
                   AND ROWNUM = 1
                   AND DIAG.DIAGHISTNO     = (SELECT MAX(DIAGH.DIAGHISTNO)
                                                FROM EMR.MMOHDIAG DIAGH
                                               WHERE DIAGH.INSTCD         = #{instCd}
                                                 AND DIAGH.PID            = INPT.PID
                                                 AND DIAGH.ORDDD          = INPT.INDD
                                                 AND DIAGH.CRETNO         = INPT.CRETNO
                                                 AND DIAGH.ORDDEPTCD      = INPT.ORDDEPTCD
                                                 AND DIAGH.DIAGHISTCD     = 'O'
                                                 AND DIAGH.DIAGTYPECD     = 'D'
                                                 AND DIAGH.DIAGKINDCDFLAG = 'M')
               )                                                                            AS dxCd,
               (SELECT TERM.DIAGHNGNM
                  FROM EMR.MMOHDIAG DIAG,
                       EMR.MMBVDIAG TERM
                 WHERE DIAG.INSTCD         =       #{instCd}
                   AND DIAG.PID            =       INPT.PID
                   AND DIAG.ORDDD          =       INPT.INDD
                   AND DIAG.CRETNO         =       INPT.CRETNO
                   AND DIAG.ORDDEPTCD      =       INPT.ORDDEPTCD
                   AND DIAG.DIAGHISTCD     =       'O'
                   AND DIAG.DIAGTYPECD     =       'D'
                   AND DIAG.DIAGKINDCDFLAG =       'M'
                   AND TERM.INSTCD         =       DIAG.INSTCD
                   AND TERM.ORGDIAGCD      =       DIAG.DIAGCD
                   AND DIAG.DIAGDD         BETWEEN TERM.TERMFROMDD AND TERM.TERMTODD
                   AND DIAG.DIAGDD         BETWEEN TERM.DIAGATTRFROMDD AND TERM.DIAGATTRTODD
                   AND ROWNUM = 1
                   AND DIAG.DIAGHISTNO     =       (SELECT MAX(DIAGH.DIAGHISTNO)
                                                      FROM EMR.MMOHDIAG DIAGH
                                                     WHERE diagh.instcd         = #{instCd}
                                                       AND DIAGH.PID            = INPT.PID
                                                       AND DIAGH.ORDDD          = INPT.INDD
                                                       AND DIAGH.CRETNO         = INPT.CRETNO
                                                       AND DIAGH.ORDDEPTCD      = INPT.ORDDEPTCD
                                                       AND DIAGH.DIAGHISTCD     = 'O'
                                                       AND DIAGH.DIAGTYPECD     = 'D'
                                                       AND DIAGH.DIAGKINDCDFLAG = 'M')
               )                                                                     AS dxNm,
               (SELECT LISTAGG(DECODE(ISOLDISPCD, 'A','공기주의(A)',
                                                  'B','혈액주의(B)',
                                                  'C','접촉주의(C)',
                                                  'D','비말주의(D)'), ', ') WITHIN GROUP (ORDER BY ISOLDISPCD) AS gbn
                  FROM EMA.ASBHISPT
                 WHERE INSTCD                 = #{instCd}
                   AND PID                    = #{pid}
                   AND ISOLDISPCD             IN ('A', 'B','C', 'D')
                   AND ((TO_CHAR(SYSDATE,'YYYYMMDD') < SUBSTR(DISPTODT, 1, 8))
                    OR ((SUBSTR(DISPFROMDT, 1, 8) <= TO_CHAR(SYSDATE,'YYYYMMDD'))
                   AND SUBSTR(DISPTODT, 1, 8) IS NULL))
               )                                                                                            AS gbn,
               OPRS.PERFDRDEPTCD                                                     AS opDeptCd,
               OPRS.PERFDRDEPT                                                       AS opDeptNm,
               OPRS.PERFDRNM                                                         AS opDrNm,
               OPRS.PERFDRID                                                         AS opDrId,
               OPRS.OPCNFMDD                                                         AS opCnfmDd,
               OPRS.PERFDRNM                                                         AS perfDrNm,
               OPRS.PERFDRFLAG                                                       AS perfDrFlag,
               OPRS.PERFDRDEPT                                                       AS perfDrDept,
               OPRS.OPDIAGNM                                                         AS opDiagNm,
               OPRS.OPNM                                                             AS opNm,
               OPRS.ANSTDRNM1                                                        AS anstDrNm1,
               OPRS.ANSTDRFLAG1                                                      AS anstDrFlag1,
               OPRS.ANSTDEPTNM1                                                      AS anstDeptNm1,
               OPRS.ANSTDRNM2                                                        AS anstDrNm2,
               OPRS.ANSTDRFLAG2                                                      AS anstDrFlag2,
               OPRS.ANSTDEPTNM2                                                      AS anstDeptNm2,
               OPRS.ANSTDRNM3                                                        AS anstDrNm3,
               OPRS.ANSTDRFLAG3                                                      AS anstDrFlag3,
               OPRS.ANSTDEPTNM3                                                      AS anstDeptNm3,
               #{opRsrvNo}                                                           AS opRsrvNo,
               ''                                                                    AS orderNo,
               ''                                                                    AS printTime,
               ''                                                                    AS ocrCd,
               ''                                                                    AS inputId,
               ''                                                                    AS inputNm,
               ''                                                                    AS signImg,

               ''                                                                    AS bp,
               ''                                                                    AS dm,
               ''                                                                    AS heart,
               ''                                                                    AS kidney,
               ''                                                                    AS respiration,
               ''                                                                    AS hx,
               ''                                                                    AS allergy,
               ''                                                                    AS drug,
               ''                                                                    AS smoking,
               ''                                                                    AS idio,
               ''                                                                    AS nacrotics,
               ''                                                                    AS airway,
               ''                                                                    AS hemorrhage,
               ''                                                                    AS statusEtc
          FROM PAM.PMIHINPT        INPT,
               PAM.PMCMPTBS PTBS
    ]]>
    <if test='opRsrvNo != "" and opRsrvNo != null'>
      <![CDATA[
            , ( SELECT a.opcnfmdd
                    , a.perfdrid
                    , a.perfdrnm
                    , a.perfdrflag
                    , a.perfdeptcd                                                               AS perfdrdeptcd
                    , a.perfdeptnm                                                               AS perfdrdept
                    , a.opdiagnm
                    , a.opnm
                    , MAX(CASE WHEN a.opcnfmno = 1 AND NVL(a.anstdrid, '-') != '-' THEN
                                    COM.FN_ZS_GETUSERNM(a.anstdrid, a.opcnfmdd)
                                WHEN a.opcnfmno = 1 AND NVL(a.anstdrid1, '-') NOT IN ('-', 'undefined') THEN
                                    COM.FN_ZS_GETUSERNM(a.anstdrid1, a.opcnfmdd)
                                ELSE ''
                            END)                                                                  AS anstdrnm1
                    , MAX(CASE WHEN a.opcnfmno = 1 AND NVL(a.anstdrid, '-') != '-' THEN
                                    CASE WHEN b.jobposcd IN ('1800', '1799') THEN
                                              '전공의'
                                          ELSE '전문의'
                                      END
                                WHEN a.opcnfmno = 1 AND NVL(a.anstdrid1, '-') NOT IN ('-', 'undefined') THEN
                                    ( SELECT CASE WHEN c.jobposcd IN ('1800', '1799') THEN
                                                        '전공의'
                                                  ELSE '전문의'
                                              END
                                        FROM com.zsumusrd c
                                        WHERE c.dutplceinstcd =       a.instcd
                                          AND c.userid        =       a.anstdrid1
                                          AND a.opcnfmdd      BETWEEN c.fromdd AND c.todd
                                          AND c.dutplcecd     =       '2090000000'
                                          AND c.psnworkyn     =       'N'
                                    )
                                ELSE ''
                            END)                                                                  AS anstdrflag1
                    , MAX(CASE WHEN a.opcnfmno = 1 AND NVL(a.anstdrid, '-') != '-' THEN
                                    a.anstdeptnm
                                WHEN a.opcnfmno = 1 AND NVL(a.anstdrid1, '-') NOT IN ('-', 'undefined') THEN
                                    a.anstdeptnm
                                ELSE ''
                            END)                                                                  AS anstdeptnm1
                    , MAX(CASE WHEN a.opcnfmno = 2 AND NVL(a.anstdrid, '-') != '-' THEN
                                    COM.FN_ZS_GETUSERNM(a.anstdrid, a.opcnfmdd)
                                WHEN a.opcnfmno = 2 AND NVL(a.anstdrid2, '-') NOT IN ('-', 'undefined') THEN
                                    COM.FN_ZS_GETUSERNM(a.anstdrid2, a.opcnfmdd)
                                ELSE ''
                            END)                                                                  AS anstdrnm2
                    , MAX(CASE WHEN a.opcnfmno = 2 AND NVL(a.anstdrid, '-') != '-' THEN
                                    CASE WHEN b.jobposcd IN ('1800', '1799') THEN
                                              '전공의'
                                          ELSE '전문의'
                                      END
                                WHEN a.opcnfmno = 2 AND NVL(a.anstdrid2, '-') NOT IN ('-', 'undefined') THEN
                                    ( SELECT CASE WHEN c.jobposcd IN ('1800', '1799') THEN
                                                        '전공의'
                                                  ELSE '전문의'
                                              END
                                        FROM com.zsumusrd c
                                        WHERE c.dutplceinstcd =       a.instcd
                                          AND c.userid        =       a.anstdrid2
                                          AND a.opcnfmdd      BETWEEN c.fromdd AND c.todd
                                          AND c.dutplcecd     =       '2090000000'
                                          AND c.psnworkyn     =       'N'
                                    )
                                ELSE ''
                            END)                                                                  AS anstdrflag2
                    , MAX(CASE WHEN a.opcnfmno = 2 AND NVL(a.anstdrid, '-') != '-' THEN
                                    a.anstdeptnm
                                WHEN a.opcnfmno = 2 AND NVL(a.anstdrid2, '-') NOT IN ('-', 'undefined') THEN
                                    a.anstdeptnm
                                ELSE ''
                            END)                                                                  AS anstdeptnm2
                    , MAX(CASE WHEN a.opcnfmno = 3 AND NVL(a.anstdrid, '-') != '-' THEN
                                    COM.FN_ZS_GETUSERNM(a.anstdrid, a.opcnfmdd)
                                WHEN a.opcnfmno = 3 AND NVL(a.anstdrid3, '-') NOT IN ('-', 'undefined') THEN
                                    COM.FN_ZS_GETUSERNM(a.anstdrid3, a.opcnfmdd)
                                ELSE ''
                            END)                                                                  AS anstdrnm3
                    , MAX(CASE WHEN a.opcnfmno = 3 AND NVL(a.anstdrid, '-') != '-' THEN
                                    CASE WHEN b.jobposcd IN ('1800', '1799') THEN
                                              '전공의'
                                          ELSE '전문의'
                                      END
                                WHEN a.opcnfmno = 3 AND NVL(a.anstdrid3, '-') NOT IN ('-', 'undefined') THEN
                                    ( SELECT CASE WHEN c.jobposcd IN ('1800', '1799') THEN
                                                        '전공의'
                                                  ELSE '전문의'
                                              END
                                        FROM com.zsumusrd c
                                        WHERE c.dutplceinstcd =       a.instcd
                                          AND c.userid        =       a.anstdrid3
                                          AND a.opcnfmdd      BETWEEN c.fromdd AND c.todd
                                          AND c.dutplcecd     =       '2090000000'
                                          AND c.psnworkyn     =       'N'
                                    )
                                ELSE ''
                            END)                                                                  AS anstdrflag3
                    , MAX(CASE WHEN a.opcnfmno = 3 AND NVL(a.anstdrid, '-') != '-' THEN
                                    a.anstdeptnm
                                WHEN a.opcnfmno = 3 AND NVL(a.anstdrid3, '-') NOT IN ('-', 'undefined') THEN
                                    a.anstdeptnm
                                ELSE ''
                            END)                                                                  AS anstdeptnm3
                  FROM ( SELECT a.instcd
                              , a.opcnfmdd
                              , a.perfdrid
                              , COM.FN_ZS_GETUSERNM(a.perfdrid, a.opcnfmdd)                    AS perfdrnm
                              , CASE WHEN b.jobposcd IN ('1800', '1799') THEN
                                          '전공의'
                                    ELSE '전문의'
                                  END                                                          AS perfdrflag
                              , a.perfdeptcd
                              , COM.FN_ZS_GETDEPTNM(a.instcd, a.perfdeptcd, a.opcnfmdd)        AS perfdeptnm
                              , MAX(CASE WHEN c.termflag = '0' AND c.termcd = a.diagcd THEN
                                              NVL(c.termhngnm, c.termengnm)
                                        ELSE ''
                                    END)                                                      AS opdiagnm
                              , MAX(CASE WHEN c.termflag = '9' AND c.termcd = a.opcd THEN
                                              NVL(c.termhngnm, c.termengnm)
                                        ELSE ''
                                    END)                                                      AS opnm
                              , d.opcnfmno
                              , d.anstdrid
                              , COM.FN_ZS_GETDEPTNM(a.instcd, '2090000000', a.opcnfmdd)        AS anstdeptnm
                              , MAX(e.anstdrid)                                                AS anstdrid1
                              , MAX(e.anstdrid2)                                               AS anstdrid2
                              , MAX(e.anstdrid3)                                               AS anstdrid3
                          FROM emr.mmohoprs a
                              , com.zsumusrd b
                              , emr.mrtmterm c
                              , emr.mmodocad d
                              , emr.mmodoprs e
                          WHERE a.instcd        =       #{instCd}
                            AND a.oprsrvno      =       #{opRsrvNo}
                            AND a.ophistcd      =       'O'
                            AND b.dutplceinstcd =       a.instcd
                            AND b.userid        =       a.perfdrid
                            AND b.dutplcecd     =       a.perfdeptcd
                            AND a.opcnfmdd      BETWEEN b.fromdd AND b.todd
                            AND c.instcd        =       a.instcd
                            AND c.termflag      IN      ('0', '9')
                            AND c.termcd        IN      (a.diagcd, a.opcd)
                            AND a.opcnfmdd      BETWEEN c.termfromdd AND c.termtodd
                            AND d.instcd(+)     =       a.instcd
                            AND d.oprsrvno(+)   =       a.oprsrvno
                            AND d.opcnfmno(+)   IN      (1, 2, 3)
                            AND e.instcd(+)     =       a.instcd
                            AND a.opcnfmdd      BETWEEN e.appstartdd(+) AND e.appenddd(+)
                            AND e.usedwflag(+)  =       TO_CHAR(TO_DATE(a.opcnfmdd, 'YYYYMMDD'), 'd')
                            AND e.oproomcd(+)   =       a.oproomcd
                            AND CASE WHEN SUBSTR(a.opfromtmcd, 1, 4) BETWEEN '0000' AND '1200' THEN
                                          '1'
                                    ELSE '2'
                                END            =       e.usescheflag(+)
                          GROUP BY a.instcd
                                , a.opcnfmdd
                                , a.perfdrid
                                , a.perfdeptcd
                                , b.jobposcd
                                , d.opcnfmno
                                , d.anstdrid
                      ) a
                    , com.zsumusrd b
                WHERE b.dutplceinstcd(+) =       a.instcd
                  AND b.userid(+)        =       a.anstdrid
                  AND a.opcnfmdd         BETWEEN b.fromdd(+) AND b.todd(+)
                  AND b.dutplcecd(+)     =       '2090000000'
                  AND b.psnworkyn(+)     =       'N'
                GROUP BY a.opcnfmdd
                        , a.perfdrid
                        , a.perfdrnm
                        , a.perfdrflag
                        , a.perfdeptcd
                        , a.perfdeptnm
                        , a.opdiagnm
                        , a.opnm
              ) oprs
      ]]>
    </if>
    <if test='opRsrvNo == ""'>
      <![CDATA[
            , ( SELECT ''    AS opcnfmdd
                     , ''    AS perfdrid
                     , ''    AS perfdrnm
                     , ''    AS perfdrflag
                     , ''    AS perfdrdeptcd
                     , ''    AS perfdrdept
                     , ''    AS opdiagnm
                     , ''    AS opnm
                     , ''    AS anstdrnm1
                     , ''    AS anstdrflag1
                     , ''    AS anstdeptnm1
                     , ''    AS anstdrnm2
                     , ''    AS anstdrflag2
                     , ''    AS anstdeptnm2
                     , ''    AS anstdrnm3
                     , ''    AS anstdrflag3
                     , ''    AS anstdeptnm3
                 FROM DUAL
              ) oprs
      ]]>
    </if>
    <![CDATA[
        WHERE 1 = 1
          AND INPT.INSTCD       = #{instCd}
          AND INPT.PID          = #{pid}
          AND INPT.HISTSTAT     = 'Y'
          AND INPT.MSKIND       = 'M'
          AND INPT.CRETNO       = #{cretNo}
          AND INPT.INDD         = #{inDd}
          AND PTBS.INSTCD(+)    = INPT.INSTCD
          AND PTBS.PID(+)       = INPT.PID
    ]]>
    <if test='ordDeptCd != "" and ordDeptCd != null'>
      <if test='opRsrvNo == ""'>
        <![CDATA[
          AND INPT.ORDDEPTCD    = #{ordDeptCd}
      ]]>
      </if>
    </if>
    <![CDATA[
      ORDER BY INPT.FSTRGSTDT DESC
    ]]>
  </select>

  <select id="getPatientInfoOut" parameterType="PatientInfoDTO" resultType="GetPatientInfoVO">
      <![CDATA[
        SELECT OTPT.ORDDEPTCD                                                        AS ordDeptCd,
               (SELECT DEPTENGABBR 
                  FROM COM.ZSDDDEPT DEPT
                 WHERE DEPT.INSTCD = OTPT.INSTCD
                   AND DEPT.DEPTCD = OTPT.ORDDEPTCD
                   AND OTPT.ORDDD BETWEEN DEPT.VALIFROMDD AND DEPT.VALITODD)          AS deptEngAbbr,
               COM.FN_ZS_GETDEPTNAME(OTPT.INSTCD, OTPT.ORDDEPTCD, OTPT.ORDDD)        AS ordDeptNm,
               OTPT.PID                                                              AS pid,
               TO_CHAR(OTPT.cretno)                                                  AS cretNo,
               ''                                                                    AS roomCd,
               OTPT.ORDDD                                                            AS inDd,
               ''                                                                    AS dschDd, 
               OTPT.ORDTYPE                                                          AS ordType,
               OTPT.INSTCD                                                           AS instCd,
               OTPT.ORDDRID                                                          AS mainDrId,
               COM.FN_ZS_GETUSERNM(OTPT.ORDDRID, OTPT.ORDDD)                         AS mainDrName,
               ''                                                                    AS erDrNm,
               TO_CHAR(OTPT.FSTRGSTDT)                                               AS firstRgstDt,
               PTBS.SEX || '/' || COM.FN_ZZ_GETAGEAS(PTBS.RRGSTNO1
                                                   , PTBS.RRGSTNO2
                                                   , TO_CHAR(SYSDATE, 'YYYYMMDD')
                                                   , 'A'
                                                   , PTBS.BRTHDD)                    AS sa,
               PTBS.HNGNM                                                            AS patientName,
               PTBS.RRGSTNO1 || '-' || SUBSTR(PTBS.RRGSTNO2, 1, 1) || '******'       AS juminNo,
               PTBS.ZIPCDADDR || ' ' || PTBS.DETLADDR                                AS zipCdAddr,
               PTBS.MPPHONTEL                                                        AS telNum,
               (SELECT CDNM
                  FROM COM.ZBCMCODE
                 WHERE CDGRUPID = 'P0008'
                   AND CDID     = OTPT.INSUKIND)                                     AS insuKind,
               ''                                                                    AS ward,
               (SELECT DIAG.DIAGCD
                  FROM EMR.MMOHDIAG DIAG
                 WHERE DIAG.INSTCD         = #{instCd}
                   AND DIAG.PID            = OTPT.PID
                   AND DIAG.ORDDD          = OTPT.ORDDD
                   AND DIAG.CRETNO         = OTPT.CRETNO
                   AND DIAG.DIAGHISTCD     = 'O'
                   AND DIAG.DIAGTYPECD     = 'D'
                   AND DIAG.DIAGKINDCDFLAG = 'M'
                   AND ROWNUM = 1
                   AND DIAG.DIAGHISTNO     = (SELECT MAX(DIAGH.DIAGHISTNO)
                                                FROM EMR.MMOHDIAG DIAGH
                                               WHERE DIAGH.INSTCD         = #{instCd}
                                                 AND DIAGH.PID            = OTPT.PID
                                                 AND DIAGH.ORDDD          = OTPT.ORDDD
                                                 AND DIAGH.CRETNO         = OTPT.CRETNO
                                                 AND DIAGH.DIAGHISTCD     = 'O'
                                                 AND DIAGH.DIAGTYPECD     = 'D'
                                                 AND DIAGH.DIAGKINDCDFLAG = 'M')
               )                                                                  AS dxCd,
               ( SELECT TERM.DIAGHNGNM
                   FROM EMR.MMOHDIAG DIAG
                      , EMR.MMBVDIAG TERM
                  WHERE DIAG.INSTCD         = #{instCd}
                    AND DIAG.PID            = OTPT.PID
                    AND DIAG.ORDDD          = OTPT.ORDDD
                    AND DIAG.CRETNO         = OTPT.CRETNO
                    AND DIAG.DIAGHISTCD     = 'O'
                    AND DIAG.DIAGTYPECD     = 'D'
                    AND DIAG.DIAGKINDCDFLAG = 'M'
                    AND TERM.INSTCD         = DIAG.INSTCD
                    AND TERM.ORGDIAGCD      = DIAG.DIAGCD
                    AND DIAG.DIAGDD         BETWEEN TERM.TERMFROMDD AND TERM.TERMTODD
                    AND DIAG.DIAGDD         BETWEEN TERM.DIAGATTRFROMDD AND TERM.DIAGATTRTODD
                    AND ROWNUM = 1
                    AND DIAG.DIAGHISTNO     = (SELECT MAX(DIAGH.DIAGHISTNO)
                                                 FROM EMR.MMOHDIAG DIAGH
                                                WHERE DIAGH.INSTCD         = #{instCd}
                                                  AND DIAGH.PID            = OTPT.PID
                                                  AND DIAGH.ORDDD          = OTPT.ORDDD
                                                  AND DIAGH.CRETNO         = OTPT.CRETNO
                                                  AND DIAGH.DIAGHISTCD     = 'O'
                                                  AND DIAGH.DIAGTYPECD     = 'D'
                                                  AND DIAGH.DIAGKINDCDFLAG = 'M')
               )                                                                  AS dxNm,
               (SELECT LISTAGG(DECODE(ISOLDISPCD, 'A','공기주의(A)',
                                                  'B','혈액주의(B)',
                                                  'C','접촉주의(C)',
                                                  'D','비말주의(D)'), ', ') WITHIN GROUP (ORDER BY ISOLDISPCD) AS gbn
                  FROM EMA.ASBHISPT
                 WHERE INSTCD                 = #{instCd}
                   AND PID                    = #{pid}
                   AND ISOLDISPCD             IN ('A', 'B','C', 'D')
                   AND ((TO_CHAR(SYSDATE,'YYYYMMDD') < SUBSTR(DISPTODT, 1, 8))
                    OR ((SUBSTR(DISPFROMDT, 1, 8) <= TO_CHAR(SYSDATE,'YYYYMMDD'))
                   AND SUBSTR(DISPTODT, 1, 8) IS NULL)))                              AS gbn,
                ''                                                                    AS opDeptCd,
                ''                                                                    AS opDeptNm,
                ''                                                                    AS opDrNm,
                ''                                                                    AS opDrId,
                ''                                                                    AS opCnfmDd,
                ''                                                                    AS perfDrNm,
                ''                                                                    AS perfDrFlag,
                ''                                                                    AS perfDrDept,
                ''                                                                    AS opDiagNm,
                ''                                                                    AS opNm,
                ''                                                                    AS anstDrNm1,
                ''                                                                    AS anstDrFlag1,
                ''                                                                    AS anstDeptNm1,
                ''                                                                    AS anstDrNm2,
                ''                                                                    AS anstDrFlag2,
                ''                                                                    AS anstDeptNm2,
                ''                                                                    AS anstDrNm3,
                ''                                                                    AS anstDrFlag3,
                ''                                                                    AS anstDeptNm3,
                ''                                                                    AS opRsrvNo,
                ''                                                                    AS orderNo,
                ''                                                                    AS printTime,
                ''                                                                    AS ocrCd,
                ''                                                                    AS inputId,
                ''                                                                    AS inputNm,
                ''                                                                    AS signImg,

                ''                                                                    AS bp,
                ''                                                                    AS dm,
                ''                                                                    AS heart,
                ''                                                                    AS kidney,
                ''                                                                    AS respiration,
                ''                                                                    AS hx,
                ''                                                                    AS allergy,
                ''                                                                    AS drug,
                ''                                                                    AS smoking,
                ''                                                                    AS idio,
                ''                                                                    AS nacrotics,
                ''                                                                    AS airway,
                ''                                                                    AS hemorrhage,
                ''                                                                    AS statusEtc
           FROM PAM.PMOHOTPT OTPT
                LEFT OUTER JOIN PAM.PMCMPTBS PTBS
                  ON PTBS.PID    = OTPT.PID
                 AND PTBS.INSTCD = OTPT.INSTCD
          WHERE OTPT.INSTCD    =  #{instCd}
            AND OTPT.PID       =  #{pid}
            AND OTPT.ORDDD     =  #{inDd}
            AND OTPT.HISTSTAT  IN ('R', 'T')
            AND OTPT.MSKIND    =  'M'
            AND OTPT.CRETNO    = #{cretNo}
      ]]>
  </select>

  <insert id="setUserFormSet" parameterType="java.util.HashMap" useGeneratedKeys="true">
    <![CDATA[ 
      INSERT INTO emr.mrfmsetindx(
              instcd,
              itemindxseq,
              supitemindxseq,
              indxflag,
              indxnm,
              indxcnts,
              deptcd,
              userid,
              linkflag,
              linkcd,
              comnseq,
              fstrgstrid,
              fstrgstdt,
              lastupdtrid,
              lastupdtdt,
              jobkind,
              slipno
              )
      VALUES(
              #{instCd},
              EMR.SQ_MRFMSETINDX_01.NEXTVAL,
              472,
              'O',
              '',
              '',
              '',
              #{userId},
              'Y',
              #{formCd},
              '',
              #{userId},
              systimestamp,
              #{userId},
              systimestamp,
              '',
              '')
      ]]>
  </insert>

  <delete id="delUserFormSet" parameterType="java.util.HashMap">
    <![CDATA[ 
      UPDATE EMR.MRFMSETINDX
          SET INSTCD      = '999'
        WHERE ITEMINDXSEQ = #{idx}
          AND USERID      = #{userId}
          AND INSTCD      = #{instCd}
          AND LINKCD      = #{formCd}
    ]]>
  </delete>

  <select id="getConsentFormCategory" parameterType="string" resultType="ConsentFormCategoryVO">
    <![CDATA[
      SELECT FORM_CATEGORY_ID         AS categoryId,
             FORM_CATEGORY_CODE       AS categoryCode,
             PARENT_FORM_CATEGORY_ID  AS parentCategoryId,
             FORM_CATEGORY_NAME       AS categoryName
        FROM EMA.CLIP_FORM_CATEGORY
       WHERE USE_YN = 'Y'
       ORDER BY PARENT_FORM_CATEGORY_ID, FORM_CATEGORY_CODE
    ]]>
  </select>

  <update id="updateUserSetUp" parameterType="java.util.HashMap">
    <![CDATA[ 
      MERGE INTO EMA.EFORM_HOSPUSERS HOSP
      USING DUAL ON (HOSP.HOSPUSER_ID = #{userId} AND HOSP.HOSPITALCD = #{instCd})
       WHEN MATCHED 
       THEN UPDATE
               SET HOSP.HOSPAPPSTATUS = #{status}
       WHEN NOT MATCHED 
       THEN INSERT (HOSP.HOSPITALCD,
                    HOSP.HOSPAPPSTATUS,
                    HOSP.HOSPUSER_ID)
            VALUES (#{instCd},
                    #{status},
                    #{userId})
    ]]>
  </update>
  
</mapper>