<?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.iit.mapper.IitPushUserRelMapper">

    <resultMap type="IitPushUserRel" id="IitPushUserRelResult">
        <result property="id" column="id"/>
        <result property="pushId" column="push_id"/>
        <result property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="pushTime" column="push_time"/>
        <result property="isView" column="is_view"/>
        <result property="viewTime" column="view_time"/>
        <result property="pushName" column="push_name"/>
        <result property="fileId" column="file_id"/>
        <result property="fileName" column="file_name"/>
        <result property="fileType" column="file_type"/>
        <result property="fileRemark" column="file_remark"/>
        <result property="fileUrl" column="file_url"/>
    </resultMap>

    <sql id="selectIitPushUserRelVo">
        select id, push_id, user_id, user_name, push_time, is_view, view_time
        from iit_push_user_rel
    </sql>

    <sql id="messageInfo">
        SELECT
            u.id,
            u.push_id,
            u.user_id,
            u.user_name,
            u.push_time,
            u.is_view,
            u.view_time,
            p.user_name as push_name,
            i.id as file_id,
            i.file_name,
            i.file_type,
            i.remark as file_remark,
            i.file_url
        FROM
            iit_push_user_rel u
                LEFT JOIN
            iit_file_push p on u.push_id = p.id
                LEFT JOIN
            iit_file_info i on p.file_id = i.id
    </sql>

    <select id="selectIitPushUserRelList" parameterType="IitPushUserRel" resultMap="IitPushUserRelResult">
        <include refid="selectIitPushUserRelVo"/>
        <where>
            <if test="pushId != null ">and push_id = #{pushId}</if>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''">and user_name like concat('%', #{userName}, '%')</if>
            <if test="pushTime != null ">and push_time = #{pushTime}</if>
            <if test="isView != null ">and is_view = #{isView}</if>
            <if test="viewTime != null ">and view_time = #{viewTime}</if>
        </where>
    </select>

    <select id="selectMyselfMessage" parameterType="IitPushUserRel" resultMap="IitPushUserRelResult">
        <include refid="messageInfo" />
        where u.user_id = #{userId}
        <if test="fileName != null  and fileName != ''"> and i.file_name like concat('%', #{fileName}, '%')</if>
        <if test="fileType != null "> and i.file_type = #{fileType}</if>
        <if test="pushName != null and pushName != ''"> and p.user_name like concat('%', #{pushName}, '%')</if>
        order by u.push_time desc
    </select>

    <select id="getMessageInfo" parameterType="Long" resultMap="IitPushUserRelResult">
        <include refid="messageInfo" />
        where u.id = #{id}
    </select>

    <select id="selectIitPushUserRelById" parameterType="Long" resultMap="IitPushUserRelResult">
        <include refid="selectIitPushUserRelVo"/>
        where id = #{id}
    </select>

    <insert id="insertIitPushUserRel" parameterType="IitPushUserRel">
        insert into iit_push_user_rel
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="pushId != null">push_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="pushTime != null">push_time,</if>
            <if test="isView != null">is_view,</if>
            <if test="viewTime != null">view_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="pushId != null">#{pushId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="pushTime != null">#{pushTime},</if>
            <if test="isView != null">#{isView},</if>
            <if test="viewTime != null">#{viewTime},</if>
        </trim>
    </insert>

    <insert id="insertIitPushUserRelBatch" parameterType="java.util.List">
        insert into iit_push_user_rel (push_id, user_id, user_name, push_time, is_view) values
        <foreach collection="list" item="item" separator=",">
            (#{item.pushId}, #{item.userId}, #{item.userName}, #{item.pushTime}, #{item.isView})
        </foreach>
    </insert>

    <update id="updateIitPushUserRel" parameterType="IitPushUserRel">
        update iit_push_user_rel
        <trim prefix="SET" suffixOverrides=",">
            <if test="pushId != null">push_id = #{pushId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="pushTime != null">push_time = #{pushTime},</if>
            <if test="isView != null">is_view = #{isView},</if>
            <if test="viewTime != null">view_time = #{viewTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteIitPushUserRelById" parameterType="Long">
        delete
        from iit_push_user_rel
        where id = #{id}
    </delete>

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