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

    <select id="selectTotalAppUserInquiryCount" parameterType="hashmap" resultType="hashmap">
        SELECT COUNT(*)  AS "totalCount"
        FROM MU_USER_INQUIRY MUI
        WHERE MUI.USE_YN = 'Y'
        <if test="appUserInquirySearchStartDate != null and appUserInquirySearchStartDate != '' and appUserInquirySearchEndDate != null and appUserInquirySearchEndDate != ''">
            AND DATE_FORMAT(MUI.WRITE_DATE, '%Y-%m-%d') &gt;= #{appUserInquirySearchStartDate}
            AND DATE_FORMAT(MUI.WRITE_DATE, '%Y-%m-%d') &lt;= #{appUserInquirySearchEndDate}
        </if>
        <if test="appUserInquirySearchKeywordParam != null and appUserInquirySearchKeywordParam != ''">
            AND ((MUI.TITLE LIKE CONCAT('%',TRIM(#{appUserInquirySearchKeywordParam}),'%'))
            OR (MUI.CONTENT LIKE CONCAT('%',TRIM(#{appUserInquirySearchKeywordParam}),'%')))
        </if>
    </select>

    <select id="selectListAppUserInquiry" parameterType="hashmap" resultType="hashmap">
        SELECT MUI.*
        FROM (
                SELECT MUI.*
                     , CAST(@RNUM:=@RNUM + 1 AS CHAR) AS "rowNum"
                FROM (
                        SELECT MUI.MU_USER_INQUIRY_ID                   AS "muAppUserInquiryId"
                        , MUI.MU_USER_ID                                AS "muUserId"
                        , MU.USER_NAME                                  AS "userName"
                        , MUI.MEMBERSHIP_ACCOUNT                        AS "membershipAccount"
                        , MUI.PHONE_NUMBER                              AS "phoneNumber"
                        , MUI.TITLE                                     AS "title"
                        , MUI.CONTENT                                   AS "content"
                        , DATE_FORMAT(MUI.WRITE_DATE, '%Y-%m-%d')       AS "writeDate"
                        , IF(MUI.ANSWER_STATUS='Y','답변완료','미답변')    AS "answerStatus"
                        , CASE
                              WHEN MUI.ANSWER_STATUS='Y' THEN DATE_FORMAT(MUI.MOD_DATE, '%Y-%m-%d')
                              ELSE ''
                          END                                            AS "answerDate"
                        , IFNULL((
                                  SELECT MM.NAME
                                    FROM MU_MEMBER AS MM
                                   WHERE MM.USE_YN       = 'Y'
                                     AND MM.MU_MEMBER_ID = MUI.MU_MEMBER_ID
                                   LIMIT 0, 1
                                 ),'')                                   AS "memberName"
                        FROM MU_USER_INQUIRY MUI
                        JOIN MU_USER MU
                          ON MUI.MU_USER_ID = MU.MU_USER_ID
                         AND MU.USE_YN = 'Y'
                       WHERE MUI.USE_YN = 'Y'
                         <if test="appUserInquirySearchStartDate != null and appUserInquirySearchStartDate != '' and appUserInquirySearchEndDate != null and appUserInquirySearchEndDate != ''">
                         AND DATE_FORMAT(MUI.WRITE_DATE, '%Y-%m-%d') &gt;= #{appUserInquirySearchStartDate}
                         AND DATE_FORMAT(MUI.WRITE_DATE, '%Y-%m-%d') &lt;= #{appUserInquirySearchEndDate}
                         </if>
                         <if test="appUserInquirySearchKeywordParam != null and appUserInquirySearchKeywordParam != ''">
                         AND ((MUI.TITLE LIKE CONCAT('%',TRIM(#{appUserInquirySearchKeywordParam}),'%'))
                         OR (MUI.CONTENT LIKE CONCAT('%',TRIM(#{appUserInquirySearchKeywordParam}),'%')))
                         </if>
                         <choose>
                            <when test="appUserInquirySort != null and appUserInquirySort != ''">
                                ORDER BY ${appUserInquirySort}
                            </when>
                            <otherwise>
                                ORDER BY MUI.WRITE_DATE DESC
                            </otherwise>
                         </choose>
                      LIMIT 18446744073709551615
                ) MUI, (SELECT @RNUM:=0) R
            WHERE 1 = 1
        ) MUI
        WHERE 1 = 1
        LIMIT ${appUserInquiryStart}, ${appUserInquiryLimit}
    </select>

    <select id="selectAppUserInquiry" parameterType="hashmap" resultType="hashmap">
        SELECT MUI.MU_USER_INQUIRY_ID                        AS "muAppUserInquiryId"
             , IFNULL((
                       SELECT USER_NAME
                         FROM MU_USER MU
                        WHERE MU.MU_USER_ID = MUI.MU_USER_ID)
                     , '')                                   AS "userName"
             , TITLE                                         AS "title"
             , CONTENT                                       AS "content"
             , ANSWER                                        AS "answer"
          FROM MU_USER_INQUIRY MUI
         WHERE MUI.MU_USER_INQUIRY_ID  = #{muAppUserInquiryId}
         LIMIT 0, 1
    </select>

    <insert id="insertAppUserInquiry" 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>

    <update id="updateAppUserInquiry" parameterType="hashmap">
        UPDATE MU_USER_INQUIRY
           SET MOD_ID              = #{modId}
             , MOD_DATE            = NOW()
             , CUD_FLAG            = 'U'
             , ANSWER             = #{answer}
             , ANSWER_STATUS      = 'Y'
             , MU_MEMBER_ID       = #{muMemberId}
         WHERE USE_YN        = 'Y'
           AND MU_USER_INQUIRY_ID  = #{muAppUserInquiryId}
    </update>

</mapper>