<?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.yeejoin.amos.knowledgebase.face.orm.dao.MessageMapper">

    <select id="selectMessageListByOwner" parameterType="string" resultType="map">
        SELECT
          km.rec_user_id AS sponsor,
          km.rec_date AS starttingTime,
          km.message_type AS messageType,
          km.message_title AS messageTitle,
          km.message_content AS messageContent,
          km.target_seq AS targetSeq,
          kmp.message_owner AS owner,
          kmp.message_status AS messageStatus,
          kmp.sequence_nbr AS sequenceNbr
        FROM
          knowledge_message_personal kmp
          LEFT JOIN knowledge_message km
            ON kmp.message_seq = km.sequence_nbr
        where kmp.message_owner = #{owner}
          <if test="messageType!=null and messageType.length!=0">
              and km.message_type = #{messageType}
          </if>
        ORDER BY
          kmp.message_status ASC,
          km.rec_date DESC
    </select>

    <select id="selectMessageBySeq" parameterType="long" resultType="map">
        SELECT
          km.rec_user_id AS sponsor,
          km.rec_date AS starttingTime,
          km.message_type AS messageType,
          km.message_title AS messageTitle,
          km.message_content AS messageContent,
          km.target_seq AS targetSeq,
          kmp.message_owner AS owner,
          kmp.message_status AS messageStatus,
          kmp.sequence_nbr AS sequenceNbr
        FROM
          knowledge_message_personal kmp
          LEFT JOIN knowledge_message km
            ON kmp.message_seq = km.sequence_nbr
        where kmp.sequence_nbr = #{sequenceNbr}
    </select>

    <select id="selectMessageListByPage" parameterType="java.util.Map" resultType="map">
        SELECT
        km.rec_user_id AS sponsor,
        km.rec_date AS starttingTime,
        km.message_type AS messageType,
        km.message_title AS messageTitle,
        km.message_content AS messageContent,
        km.target_seq AS targetSeq,
        kmp.message_owner AS owner,
        kmp.message_status AS messageStatus,
        kmp.sequence_nbr AS sequenceNbr
        FROM
        knowledge_message_personal kmp
        LEFT JOIN knowledge_message km
        ON kmp.message_seq = km.sequence_nbr
        where  kmp.message_owner = #{owner}
        <if test="messageType!=null and messageType.length!=0">
            and km.message_type = #{messageType}
        </if>
        ORDER BY
        kmp.message_status ASC,
        km.rec_date DESC
        LIMIT  #{current},#{size}
    </select>

    <select id="selectMessageListByCount" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT
        COUNT(*)
        FROM
        knowledge_message_personal kmp
        LEFT JOIN knowledge_message km
        ON kmp.message_seq = km.sequence_nbr
        where kmp.message_owner = #{owner}
        <if test="messageType!=null and messageType.length!=0">
            and km.message_type = #{messageType}
        </if>
        ORDER BY
        kmp.message_status ASC,
        km.rec_date DESC
    </select>

</mapper>
