<?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="DocTalk">

    <select id="selectUserId" parameterType="hashMap" resultType="hashMap">
        SELECT MU.MU_USER_ID       AS "muUserId"
          FROM MU_USER AS MU
         WHERE MU.USE_YN        = 'Y'
           AND MU.USER_NAME     = #{name}
           AND MU.PHONE_NUMBER  = #{phone}
      ORDER BY MU.WRITE_DATE DESC
              ,MU.WRITE_TIME DESC
         LIMIT 0, 1
    </select>

    <select id="selectConsultingChartNumber" parameterType="hashmap" resultType="hashmap">
        SELECT CONCAT('C', DATE_FORMAT(NOW(), '%Y%m%d'), '-', LPAD(IFNULL(MAX(SUBSTRING(CHART_NUMBER, 11)) + 1, 1), 4, '0')) AS "chartNumber"
          FROM MU_USER_CONSULTING_CHART AS MUCC
         WHERE CHART_NUMBER LIKE CONCAT('C', DATE_FORMAT(NOW(), '%Y%m%d'), '%')
    </select>

    <select id="selectMedicalChartNumber" parameterType="hashmap" resultType="hashmap">
        SELECT CONCAT('M', DATE_FORMAT(NOW(), '%Y%m%d'), '-', LPAD(IFNULL(MAX(SUBSTRING(CHART_NUMBER, 11)) + 1, 1), 4, '0')) AS "chartNumber"
          FROM MU_USER_MEDICAL_CHART AS MUMC
         WHERE CHART_NUMBER LIKE CONCAT('M', DATE_FORMAT(NOW(), '%Y%m%d'), '%')
    </select>

	<insert id="insertUserTemp" parameterType="hashmap">
		<selectKey resultType="string" keyProperty="id" order="BEFORE">
			SELECT CONCAT(#{muUserId},LPAD((SELECT NEXTVAL(MU_USER_SEQ)), 11, 0))
		</selectKey>
			INSERT INTO MU_USER (
						 MU_USER_ID
						,USER_NUMBER
						,USER_NAME
						,USER_TYPE2
						,PHONE_NUMBER
						,CHANNEL
						,EMAIL_YN
						,SMS_YN
						,WRITE_DATE
						,WRITE_TIME
						,CUD_FLAG
						,USE_YN
						,REG_ID
						,REG_DATE
						,MOD_ID
						,MOD_DATE
						,T_ID
						,T_DATE
			) VALUES (
						 #{id}
						,(SELECT CONCAT('U', DATE_FORMAT(NOW(), '%Y%m%d'), '-', LPAD(IFNULL(MAX(SUBSTRING(USER_NUMBER, 11)) + 1, 1), 3, '0'))
							FROM MU_USER AS MU
						   WHERE USER_NUMBER LIKE CONCAT('U', DATE_FORMAT(NOW(), '%Y%m%d'), '%'))
						,#{name}
						,'T'
						,#{phone}
						,(SELECT MCI.MU_CATEGORY_ITEM_ID
						    FROM MU_CATEGORY_ITEM AS MCI
						   WHERE MCI.USE_YN = 'Y'
						 	 AND MCI.CATEGORY_ITEM_NAME LIKE '%네이버%'
						   LIMIT 0, 1)
						,'N'
						,'N'
						,CURDATE()
						,CURTIME()
						,'C'
						,'Y'
						,#{regId}
						,NOW()
						,#{modId}
						,NOW()
						,#{tId}
						,#{tDate}
			)
	</insert>

    <insert id="insertReserve" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muReserveId},LPAD((SELECT NEXTVAL(MU_RESERVE_SEQ)), 11, 0))
        </selectKey>
        INSERT INTO MU_RESERVE(
					MU_RESERVE_ID
				   ,USER_NAME
				   ,MU_USER_ID
				   ,RESERVE_DATE
				   ,RESERVE_TIME
				   ,RESERVE_CATEGORY_ITEM_ID
				   ,RESERVE_CATEGORY_ITEM_NAME
				   ,RESERVE_NAVER_YN
				   ,STATUS
				   ,STATUS2
				   ,WRITE_DATE
				   ,WRITE_TIME
				   ,CUD_FLAG
				   ,USE_YN
				   ,REG_ID
				   ,REG_DATE
				   ,MOD_ID
				   ,MOD_DATE
				   ,T_ID
				   ,T_DATE
				   ,PHONE_NUMBER
				   ,NAVER_RESERVATION_KEY
				   ,NAVER_BOOKING_KEY
				   ,NAVER_DATE_TIME
				   ,NAVER_NAME
				   ,NAVER_PHONE
				   ,NAVER_STATE
				   ,NAVER_USER_ID
        )VALUES(
					#{id}
				   ,#{name}
				   ,#{muUserId}
				   ,#{reserveDate}
				   ,#{reserveTime}
				   ,(SELECT MCI.MU_CATEGORY_ITEM_ID
					   FROM MU_CATEGORY_ITEM AS MCI
					  WHERE MCI.USE_YN = 'Y'
                        AND MCI.CATEGORY_ITEM_NAME LIKE '%네이버%'
					  LIMIT 0, 1)
				   ,'네이버'
				   ,'Y'
				   ,#{status,jdbcType=VARCHAR}
				   ,#{status2}
				   ,CURDATE()
				   ,CURTIME()
				   ,'C'
				   ,'Y'
				   ,#{regId}
				   ,NOW()
				   ,#{modId}
				   ,NOW()
				   ,#{tId}
				   ,#{tDate}
				   ,#{phone}
				   ,#{reservationKey}
				   ,#{bookingKey}
				   ,#{datetime}
				   ,#{name}
				   ,#{phone}
				   ,#{state}
				   ,#{naverUserId}
        )
    </insert>

    <insert id="insertConsultingChart" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muUserConsultingChartId},LPAD((SELECT NEXTVAL(MU_USER_CONSULTING_CHART_SEQ)), 9, 0))
        </selectKey>
        INSERT INTO MU_USER_CONSULTING_CHART(
					MU_USER_CONSULTING_CHART_ID
				   ,MU_RESERVE_ID
				   ,MU_USER_ID
				   ,CHART_NUMBER
				   ,CONTENT
				   ,WRITE_MEMBER_ID
				   ,WRITE_DATE
				   ,WRITE_TIME
				   ,CUD_FLAG
				   ,USE_YN
				   ,REG_ID
				   ,REG_DATE
				   ,MOD_ID
				   ,MOD_DATE
				   ,T_ID
				   ,T_DATE
        )VALUES(
					#{id}
				   ,#{muReserveId}
				   ,#{muUserId}
				   ,#{chartNumber,jdbcType=VARCHAR}
				   ,#{content,jdbcType=VARCHAR}
				   ,#{regId}
				   ,CURDATE()
				   ,CURTIME()
				   ,'C'
				   ,'Y'
				   ,#{regId}
				   ,NOW()
				   ,#{modId}
				   ,NOW()
				   ,#{tId}
				   ,#{tDate}
        )
    </insert>

    <insert id="insertMedicalChart" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muUserMedicalChartId},LPAD((SELECT NEXTVAL(MU_USER_MEDICAL_CHART_SEQ)), 9, 0))
        </selectKey>
        INSERT INTO MU_USER_MEDICAL_CHART(
					MU_USER_MEDICAL_CHART_ID
				   ,MU_RESERVE_ID
				   ,MU_USER_ID
				   ,CHART_NUMBER
				   ,CONTENT
				   ,WRITE_MEMBER_ID
				   ,WRITE_DATE
				   ,WRITE_TIME
				   ,CUD_FLAG
				   ,USE_YN
				   ,REG_ID
				   ,REG_DATE
				   ,MOD_ID
				   ,MOD_DATE
				   ,T_ID
				   ,T_DATE
        )VALUES(
					#{id}
				   ,#{muReserveId}
				   ,#{muUserId}
				   ,#{chartNumber,jdbcType=VARCHAR}
				   ,#{content,jdbcType=VARCHAR}
				   ,#{regId}
				   ,CURDATE()
				   ,CURTIME()
				   ,'C'
				   ,'Y'
				   ,#{regId}
				   ,NOW()
				   ,#{modId}
				   ,NOW()
				   ,#{tId}
				   ,#{tDate}
        )
    </insert>

</mapper>