<?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.ruoyi.project.common.mapper.MessageUserMapper">
    
    <resultMap type="com.ruoyi.project.common.domain.MessageUser" id="MessageUserResult">
        <result property="id"    column="id"    />
        <result property="messageId"    column="message_id"    />
        <result property="userId"    column="user_id"    />
        <result property="readTime"    column="read_time"    />
        <result property="readStatus"    column="read_status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <resultMap type="com.ruoyi.project.common.dto.MessageRes" id="MessageResult">
        <result property="id"    column="id"    />
        <result property="messageId"    column="message_id"    />
        <result property="userId"    column="user_id"    />
        <result property="readTime"    column="read_time"    />
        <result property="readStatus"    column="read_status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="messageType"    column="message_type"    />
        <result property="messageContent"    column="message_content"    />
        <result property="pushTime"    column="push_time"    />
        <result property="pushStatus"    column="push_status"    />
        <result property="messageContentES"    column="message_contentES"    />
        <result property="messageContentRU"    column="message_contentRU"    />
        <result property="messageContentUS"    column="message_contentUS"    />
    </resultMap>

    <sql id="selectMessageUserVo">
        select id, message_id, user_id, read_time, read_status, del_flag, create_by, create_time, update_by, update_time, remark from message_user
    </sql>

    <select id="selectMessageUserList" parameterType="com.ruoyi.project.common.domain.MessageUser" resultMap="MessageUserResult">
        <include refid="selectMessageUserVo"/>
        <where>
            del_flag = 0
            <if test="messageId != null "> and message_id = #{messageId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="readTime != null "> and read_time = #{readTime}</if>
            <if test="readStatus != null  and readStatus != ''"> and read_status = #{readStatus}</if>
        </where>
    </select>

    <select id="selectMessageContantList" parameterType="com.ruoyi.project.common.domain.MessageUser" resultMap="MessageResult">
        select mu.id, message_id, user_id, read_time, read_status, msg.create_by, msg.create_time, msg.update_by, msg.update_time,msg.remark,
        message_type, message_content, msg.message_contentES, msg.message_contentRU, msg.message_contentUS
        from message_user mu
        left join message msg on msg.id = mu.message_id
        <where>
            mu.del_flag = 0
            <if test="messageId != null "> and mu.message_id = #{messageId}</if>
            <if test="userId != null "> and mu.user_id = #{userId}</if>
            <if test="readStatus != null  and readStatus != ''"> and mu.read_status = #{readStatus}</if>
        </where>
    </select>
    
    <select id="selectMessageUserById" parameterType="Long" resultMap="MessageUserResult">
        <include refid="selectMessageUserVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertMessageUser" parameterType="com.ruoyi.project.common.domain.MessageUser" useGeneratedKeys="true" keyProperty="id">
        insert into message_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="messageId != null">message_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="readTime != null">read_time,</if>
            <if test="readStatus != null">read_status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="messageId != null">#{messageId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="readTime != null">#{readTime},</if>
            <if test="readStatus != null">#{readStatus},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateMessageUser" parameterType="com.ruoyi.project.common.domain.MessageUser">
        update message_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="messageId != null">message_id = #{messageId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="readTime != null">read_time = #{readTime},</if>
            <if test="readStatus != null">read_status = #{readStatus},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMessageUserById" parameterType="Long">
        delete from message_user where id = #{id}
    </delete>

    <delete id="deleteMessageUserByIds" parameterType="String">
        delete from message_user where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <insert id="InsertBySQL" parameterType="java.util.List"   useGeneratedKeys="true"
            keyProperty="id">
        insert into message_user (
        message_id,
        user_id,
        create_by,
        create_time,
        remark
        )values
        <foreach collection="list" item="item" index="index" separator=",">
            (
            #{item.messageId},
            #{item.userId},
            #{item.createBy},
            #{item.createTime},
            #{item.remark}
            )
        </foreach>
    </insert>

    <select id="selectUnReadById" parameterType="Long" resultType="long">
      select count(*)
      from message_user
      where user_id = #{userId} and del_flag = 0 and read_status = '0'
    </select>
</mapper>