<?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.zsc.edu.gateway.modules.notice.repo.UserMessageRepository">
    <resultMap id="userMessageMap" type="com.zsc.edu.gateway.modules.notice.vo.UserMessageVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="is_read" jdbcType="BOOLEAN" property="isRead"/>
        <result column="read_time" jdbcType="TIMESTAMP" property="readTime"/>
        <result column="username" jdbcType="VARCHAR" property="username"/>
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <result column="avatar" jdbcType="VARCHAR" property="avatar"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="email" jdbcType="VARCHAR" property="email"/>
        <result column="type" jdbcType="INTEGER" property="type"/>
        <result column="system" jdbcType="BOOLEAN" property="system"/>
        <result column="email" jdbcType="BOOLEAN" property="email"/>
        <result column="sms" jdbcType="BOOLEAN" property="sms"/>
        <result column="html" jdbcType="BOOLEAN" property="html"/>
        <result column="title" jdbcType="VARCHAR" property="title"/>
        <result column="content" jdbcType="VARCHAR" property="content"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <result column="create_time" jdbcType="DATE" property="createTime"/>
        <result column="update_time" jdbcType="DATE" property="updateTime"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <collection property="attachments" ofType="com.zsc.edu.gateway.modules.attachment.entity.Attachment">
            <id column="id" jdbcType="VARCHAR" property="id"/>
            <result column="file_name" jdbcType="VARCHAR" property="fileName"/>
            <result column="mime_type" jdbcType="VARCHAR" property="mimeType"/>
            <result column="upload_time" jdbcType="TIMESTAMP" property="uploadTime"/>
            <result column="url" jdbcType="VARCHAR" property="url"/>
        </collection>
    </resultMap>
    <select id="selectByMessageIdAndUserId" resultType="com.zsc.edu.gateway.modules.notice.vo.UserMessageVo"
            resultMap="userMessageMap">
        select sum.id as user_message_id,sum.user_id as user_message_user_id,sum.message_id as
        user_message_message_id,sum.is_read as user_message_is_read,sum.read_time as user_message_read_time,su.id as
        user_id,su.username as user_username,su.address as user_address,su.avatar as user_avatar,su.name as
        user_name,su.phone as user_phone,sm.*,sm.email as message_email,a.*
        from sys_user_message sum
        left join sys_user su on sum.id=su.id
        left join sys_message sm on sm.id=sum.id
        left join sys_message_attachment sma on sm.id=sma.message_id
        left join attachment a on sma.attachment_id=a.id
        <where>
            sm.id=#{messageId}
            and su.id=#{userId}
        </where>
    </select>

    <select id="query" resultType="com.zsc.edu.gateway.modules.notice.vo.UserMessageVo" resultMap="userMessageMap">
        select sum.id as user_message_id,sum.user_id as user_message_user_id,sum.message_id as
        user_message_message_id,sum.is_read as user_message_is_read,sum.read_time as user_message_read_time,su.id as
        user_id,su.username as user_username,su.address as user_address,su.avatar as user_avatar,su.name as
        user_name,su.email as user_email,su.phone as user_phone,sm.*,sm.email as message_email,a.*
        from sys_user_message sum
        left join sys_user su on sum.id=su.id
        left join sys_message sm on sm.id=sum.id
        left join sys_message_attachment sma on sm.id=sma.message_id
        left join attachment a on sma.attachment_id=a.id
        where 1=1
        <if test="query.userId != null">
            AND sum.user_id = #{query.userId}
        </if>
        <if test="query.title != null and query.title != ''">
            AND sm.title LIKE CONCAT('%', #{query.title}, '%')
        </if>
        <if test="query.type != null">
            AND sm.type = #{query.type}
        </if>
        <if test="query.name != null and query.name != ''">
            AND su.username LIKE CONCAT('%', #{query.name}, '%')
        </if>
        <if test="query.system != null">
            AND sm.system = #{query.system}
        </if>
        <if test="query.isRead != null">
            AND sum.is_read = #{query.isRead}
        </if>
        <if test="query.createAtBegin != null and query.createAtEnd != null">
            AND sm.create_time BETWEEN #{query.createAtBegin} AND #{query.createAtEnd}
        </if>
        ORDER BY
        sm.create_time DESC
    </select>
</mapper>