<?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.message.frequency.interactionmessagerecord.mapper.InteractionMessageRecordMapper">

    <resultMap type="com.ruoyi.message.frequency.interactionmessagerecord.entity.InteractionMessageRecord" id="InteractionMessageRecordResult">
        <result property="id"    column="id"    />
        <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="delFlag"    column="del_flag"    />
        <result property="userId"    column="user_id"    />
        <result property="userType"    column="user_type"    />
        <result property="messageType"    column="message_type"    />
        <result property="title"    column="title"    />
        <result property="content"    column="content"    />
        <result property="isRead"    column="is_read"    />
        <result property="isShow"    column="is_show"    />
        <result property="skipId"    column="skip_id"    />
        <result property="interactionUserType"    column="interaction_user_type"    />
        <result property="interactionUserId"    column="interaction_user_id"    />
        <result property="objectId"    column="object_id"    />
    </resultMap>

    <sql id="selectInteractionMessageRecordVo">
        select id, create_by, create_time, update_by, update_time, del_flag, user_id, user_type, message_type, title, content, is_read, is_show, skip_id, interaction_user_type, interaction_user_id, object_id from t_interaction_message_record
    </sql>

    <select id="selectInteractionMessageRecordList" parameterType="com.ruoyi.message.frequency.interactionmessagerecord.entity.InteractionMessageRecord" resultMap="InteractionMessageRecordResult">
        <include refid="selectInteractionMessageRecordVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="userType != null "> and user_type = #{userType}</if>
            <if test="messageType != null "> and message_type = #{messageType}</if>
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="isRead != null  and isRead != ''"> and is_read = #{isRead}</if>
            <if test="isShow != null  and isShow != ''"> and is_show = #{isShow}</if>
            <if test="skipId != null "> and skip_id = #{skipId}</if>
            <if test="interactionUserType != null "> and interaction_user_type = #{interactionUserType}</if>
            <if test="interactionUserId != null "> and interaction_user_id = #{interactionUserId}</if>
            <if test="objectId != null "> and object_id = #{objectId}</if>
        </where>
    </select>

    <select id="selectInteractionMessageRecordById" parameterType="Long" resultMap="InteractionMessageRecordResult">
        <include refid="selectInteractionMessageRecordVo"/>
        where id = #{id}
    </select>

    <insert id="insertInteractionMessageRecord" parameterType="com.ruoyi.message.frequency.interactionmessagerecord.entity.InteractionMessageRecord">
        insert into t_interaction_message_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</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="delFlag != null">del_flag,</if>
            <if test="userId != null">user_id,</if>
            <if test="userType != null">user_type,</if>
            <if test="messageType != null">message_type,</if>
            <if test="title != null">title,</if>
            <if test="content != null">content,</if>
            <if test="isRead != null">is_read,</if>
            <if test="isShow != null">is_show,</if>
            <if test="skipId != null">skip_id,</if>
            <if test="interactionUserType != null">interaction_user_type,</if>
            <if test="interactionUserId != null">interaction_user_id,</if>
            <if test="objectId != null">object_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</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="delFlag != null">#{delFlag},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userType != null">#{userType},</if>
            <if test="messageType != null">#{messageType},</if>
            <if test="title != null">#{title},</if>
            <if test="content != null">#{content},</if>
            <if test="isRead != null">#{isRead},</if>
            <if test="isShow != null">#{isShow},</if>
            <if test="skipId != null">#{skipId},</if>
            <if test="interactionUserType != null">#{interactionUserType},</if>
            <if test="interactionUserId != null">#{interactionUserId},</if>
            <if test="objectId != null">#{objectId},</if>
        </trim>
    </insert>

    <update id="updateInteractionMessageRecord" parameterType="com.ruoyi.message.frequency.interactionmessagerecord.entity.InteractionMessageRecord">
        update t_interaction_message_record
        <trim prefix="SET" suffixOverrides=",">
            <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="delFlag != null">del_flag = #{delFlag},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="messageType != null">message_type = #{messageType},</if>
            <if test="title != null">title = #{title},</if>
            <if test="content != null">content = #{content},</if>
            <if test="isRead != null">is_read = #{isRead},</if>
            <if test="isShow != null">is_show = #{isShow},</if>
            <if test="skipId != null">skip_id = #{skipId},</if>
            <if test="interactionUserType != null">interaction_user_type = #{interactionUserType},</if>
            <if test="interactionUserId != null">interaction_user_id = #{interactionUserId},</if>
            <if test="objectId != null">object_id = #{objectId},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteInteractionMessageRecordById" parameterType="Long">
        delete from t_interaction_message_record where id = #{id}
    </delete>

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

    <select id="interactionMessagePage" resultType="com.ruoyi.message.controller.resp.InteractionMessagePageResp">
        SELECT
            t.id,
            t.create_time,
            t.message_type,
            t.title,
            t.content,
            t.is_read,
            t.is_show,
            t.skip_id,
            t.interaction_user_type user_type,
            t.interaction_user_id user_id,
            t.object_id,
            CASE

                WHEN t.type IN ( 1, 3, 4, 7 ) THEN
                    1
                WHEN t.type in (2,5,8,11) THEN
                    2
                WHEN t.type = 6 THEN
                    3
                WHEN t.type in (9,10) THEN
                    4
                ELSE 0
                END type,
            CASE

                WHEN t.type IN ( 1, 3, 4, 7 ) THEN
                    IFNULL( p.del_flag, 2 )
                WHEN t.type in (2,5,8,11) THEN
                    w.del_flag
                WHEN t.type in (9,10) THEN
                    n.del_flag
                WHEN t.type = 6 THEN
                    1 ELSE 0
                END del_flag
        FROM
            t_interaction_message_record AS t
                LEFT JOIN t_post p ON t.object_id = p.id
                AND t.type IN ( 1, 3, 4, 7 )
                LEFT JOIN t_works w ON t.object_id = w.id
                AND t.type in (2,5,8,11)
                LEFT JOIN t_news n ON t.object_id = n.id
                AND t.type in (9,10)
        WHERE
            t.del_flag = 1
          AND t.is_show = 1
          AND t.user_type = #{userVo.userType}
          AND t.user_id = #{userVo.userId}
        ORDER BY
            t.create_time DESC
    </select>
</mapper>