<?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="cn.bunny.services.mapper.message.MessageMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.bunny.dao.entity.system.Message">
        <id column="id" property="id"/>
        <id column="create_time" property="createTime"/>
        <id column="update_time" property="updateTime"/>
        <id column="create_user" property="createUser"/>
        <id column="update_user" property="updateUser"/>
        <id column="is_deleted" property="isDeleted"/>
        <id column="title" property="title"/>
        <id column="send_user_id" property="sendUserId"/>
        <id column="message_type" property="messageType"/>
        <id column="content" property="content"/>
        <id column="cover" property="cover"/>
        <id column="summary" property="summary"/>
        <id column="editor_type" property="editorType"/>
        <id column="level" property="level"/>
        <id column="extra" property="extra"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, create_time, update_time, create_user, update_user, is_deleted, title, send_user_id, message_type, content, cover, summary, editor_type, level,extra
    </sql>

    <!-- 分页查询发送消息 -->
    <select id="selectListByPage" resultType="cn.bunny.dao.vo.system.message.MessageReceivedWithMessageVo">
        SELECT
        message.id,
        message.create_time,
        message.update_time,
        message.create_user,
        message.update_user,
        message.is_deleted,
        message.title,
        message.send_user_id,
        message.content,
        message.cover,
        message.summary,
        message.editor_type,
        message.level,
        message.extra,
        send_user.nickname AS send_nickname,
        create_user.username AS create_username,
        update_user.username AS update_username,
        message_type.message_name AS message_type,
        message_type.id AS message_type_id,
        message_received.received_user_id AS received_user_id,
        message_received.id AS message_received_id,
        received_user.nickname received_user_nickname
        FROM sys_message message
        LEFT JOIN sys_user create_user ON create_user.id = message.create_user
        LEFT JOIN sys_user update_user ON update_user.id = message.update_user
        LEFT JOIN sys_user send_user ON send_user.id = message.send_user_id
        LEFT JOIN sys_message_type message_type ON message_type.id = message.message_type
        LEFT JOIN sys_message_received message_received ON message.id = message_received.message_id
        LEFT JOIN sys_user received_user ON received_user.id = message_received.received_user_id
        <where>
            message.is_deleted = 0
            <if test="dto.title != null and dto.title != ''">
                and message.title like CONCAT('%',#{dto.title},'%')
            </if>
            <if test="dto.sendNickname != null and dto.sendNickname != ''">
                and send_user.nickname like CONCAT('%',#{dto.sendNickname},'%')
            </if>
            <if test="dto.messageType != null and dto.messageType != ''">
                and message_type.message_type = #{dto.messageType}
            </if>
            <if test="dto.editorType != null and dto.editorType != ''">
                and message.editor_type = #{dto.editorType}
            </if>
            <if test="dto.level != null and dto.level != ''">
                and message.level = #{dto.level}
            </if>
            <if test="dto.extra != null and dto.extra != ''">
                and message.extra like CONCAT('%',#{dto.extra},'%')
            </if>
        </where>
    </select>

    <!-- 根据消息id查询消息详情 -->
    <select id="selectMessageVoById" resultType="cn.bunny.dao.vo.system.message.MessageDetailVo">
        SELECT message.*,
               send_user.nickname AS send_nickname
        FROM sys_message message
                 LEFT JOIN sys_user update_user ON update_user.id = message.update_user
                 LEFT JOIN sys_user send_user ON send_user.id = message.send_user_id
        WHERE message.id = #{id}
          and message.is_deleted = 0
    </select>

    <!-- 物理删除系统消息 -->
    <delete id="deleteBatchIdsWithPhysics">
        delete
        from sys_message
        where is_deleted=0 and id in
        <foreach collection="ids" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>

    <!-- 根据消息id获取接收人信息 -->
    <select id="selectUserinfoListByMessageId"
            resultType="cn.bunny.dao.vo.system.message.MessageReceivedWithUserVo">
        SELECT message_received.message_id,
               message_received.received_user_id,
               USER.nickname,
               USER.username
        FROM sys_message_received message_received
                 LEFT JOIN sys_user user ON user.id = message_received.received_user_id
        where message_received.message_id = #{messageId}
    </select>

</mapper>
