<?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.yonge.cooleshow.bbs.dao.BbsPrivateMessageDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.bbs.entity.BbsPrivateMessage">
            <result column="id" property="id" />
	        <result column="send_user_id_" property="sendUserId" />
	        <result column="receiver_user_id_" property="receiverUserId" />
	        <result column="message_content_" property="messageContent" />
	        <result column="created_time_" property="createdTime" />
		</resultMap>  
    
    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id as id
        , t.send_user_id_ as sendUserId
        , t.receiver_user_id_ as receiverUserId
        , t.message_content_ as messageContent
        , t.created_time_ as createdTime
        , t.read_flag_ as readFlag
        </sql> 
    
    <select id="detail" resultType="com.yonge.cooleshow.bbs.vo.BbsPrivateMessageVo">
        SELECT
            <include refid="baseColumns"/>
        FROM bbs_private_message t
        where t.id = #{id}
    </select>
    
    <select id="selectPage" resultType="com.yonge.cooleshow.bbs.vo.BbsPrivateMessageVo">
		SELECT         
        	<include refid="baseColumns" />
		FROM bbs_private_message t
	</select>

	<select id="selectPersonPage" resultType="com.yonge.cooleshow.bbs.dto.PrivateMessagePersonVo">

        select
            bpm.message_content_ as messageContext,
            bpm.created_time_ as messageTime,
            bpm.read_flag_ as readFlag,
            if(bpm.send_user_id_ = #{query.userId},bpm.receiver_user_id_,bpm.send_user_id_) as messageUserId,
            if(su.id_ = #{query.userId},su2.username_,su.username_) as username,
            if(su.id_ = #{query.userId},su2.avatar_,su.avatar_) as avatar
        from bbs_private_message bpm
        left join sys_user su on bpm.send_user_id_ = su.id_
        left  join sys_user su2 on su2.id_ = bpm.receiver_user_id_
        <where>
            <if test="query.userId != null">
                and (#{query.userId} = bpm.send_user_id_ or #{query.userId} = bpm.receiver_user_id_)
            </if>
        </where>
        group by bpm.send_user_id_

    </select>

	<update id="read">
        update bbs_private_message bpm set bpm.read_flag_ = 1
        where #{readDto.sendUserId} = bpm.send_user_id_
        and #{readDto.userId} = bpm.receiver_user_id_
        and #{readDto.readTime} &gt; bpm.created_time_
    </update>

	<select id="selectMessagePage" resultType="com.yonge.cooleshow.bbs.entity.BbsPrivateMessage">

        select
        bpm.id as id
        , bpm.send_user_id_ as sendUserId
        , bpm.receiver_user_id_ as receiverUserId
        , bpm.message_content_ as messageContent
        , bpm.created_time_ as createdTime
        , bpm.read_flag_ as readFlag
        from bbs_private_message bpm
        <where>
            bpm.read_flag_ = 1
            <if test="query.userId != null and query.receiverUserId != null">
                and ((#{query.userId} = bpm.send_user_id_ and #{query.receiverUserId} = bpm.receiver_user_id_)
                or (#{query.userId} = bpm.receiver_user_id_ and #{query.receiverUserId} = bpm.send_user_id_))
            </if>
        </where>

        order by bpm.created_time_ desc
    </select>

	<select id="selectUnreadMessage" resultMap="BaseResultMap">

        select
        bpm.id as id
        , bpm.send_user_id_ as sendUserId
        , bpm.receiver_user_id_ as receiverUserId
        , bpm.message_content_ as messageContent
        , bpm.created_time_ as createdTime
        , bpm.read_flag_ as readFlag
        from bbs_private_message bpm
        <where>
            bpm.read_flag_ = 0
            <if test="query.userId != null and query.receiverUserId != null">
                and
             (#{query.userId} = bpm.receiver_user_id_ and #{query.receiverUserId} = bpm.send_user_id_)
            </if>
        </where>
    </select>
</mapper>