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

    <select id="selectTotalUserInquiryCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*)  AS "totalCount"
          FROM MU_USER_INQUIRY MUI
         WHERE MUI.USE_YN = 'Y'
          AND MU_USER_ID = #{muUserId}
    </select>

    <select id="selectListUserInquiry" parameterType="hashmap" resultType="hashmap">
            SELECT MUI.MU_USER_INQUIRY_ID                        AS "muUserInquiryId"
                 , MUI.TITLE                                     AS "title"
                 , DATE_FORMAT(MUI.WRITE_DATE, '%Y-%m-%d')       AS "writeDate"
                 , IF(MUI.ANSWER_STATUS='Y','답변 완료','답변 대기')    AS "answerStatus"
            FROM MU_USER_INQUIRY MUI
           WHERE MUI.USE_YN = 'Y'
             AND MU_USER_ID = #{muUserId}
    </select>

    <select id="selectUserInquiry" parameterType="hashmap" resultType="hashmap">
        SELECT MUI.MU_USER_INQUIRY_ID                        AS "muAppUserInquiryId"
             , CASE
                   WHEN MUI.ANSWER_STATUS='Y' THEN DATE_FORMAT(MUI.MOD_DATE, '%Y-%m-%d')
                   ELSE ''
               END                                           AS "answerDate"
             , CASE
                   WHEN MUI.ANSWER_STATUS='Y' THEN DATE_FORMAT(MUI.MOD_DATE, '%H:%i')
                   ELSE ''
               END                                           AS "answerTime"
             , DATE_FORMAT(MUI.WRITE_DATE, '%Y-%m-%d')       AS "writeDate"
             , DATE_FORMAT(MUI.WRITE_TIME, '%H:%i')          AS "writeTime"
             , TITLE                                         AS "title"
             , CONTENT                                       AS "content"
             , CASE
                   WHEN MUI.ANSWER_STATUS='Y' THEN ANSWER
                   ELSE ''
               END                                           AS "answer"
          FROM MU_USER_INQUIRY MUI
         WHERE MUI.MU_USER_INQUIRY_ID  = #{muUserInquiryId}
         LIMIT 0, 1
    </select>

    <insert id="insertUserInquiry" parameterType="hashmap">
        <selectKey resultType="string" keyProperty="id" order="BEFORE">
            SELECT CONCAT(#{muAppUserInquiryId},LPAD((SELECT NEXTVAL(MU_USER_INQUIRY_SEQ)), 11, 0))
        </selectKey>
        INSERT INTO MU_USER_INQUIRY(
        MU_USER_INQUIRY_ID
        ,MU_USER_ID
        ,MEMBERSHIP_ACCOUNT
        ,PHONE_NUMBER
        ,TITLE
        ,CONTENT
        ,ANSWER_STATUS
        ,WRITE_DATE
        ,WRITE_TIME
        ,USE_YN
        ,REG_ID
        ,REG_DATE
        ,MOD_ID
        ,MOD_DATE
        ,T_ID
        ,T_DATE
        )VALUES(
        #{id}
        ,#{muUserId}
        ,#{membershipAccount}
        ,#{phoneNumber}
        ,#{title}
        ,#{content}
        ,'N'
        ,CURDATE()
        ,CURTIME()
        ,'Y'
        ,#{regId}
        ,NOW()
        ,#{modId}
        ,NOW()
        ,#{tId}
        ,#{tDate}
        )
    </insert>
</mapper>