<?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.tourism.webadmin.back.dao.TourImMessageMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.TourImMessage">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="message_content" jdbcType="LONGVARCHAR" property="messageContent"/>
        <result column="message_type" jdbcType="INTEGER" property="messageType"/>
        <result column="group_id" jdbcType="BIGINT" property="groupId"/>
        <result column="notice_type" jdbcType="INTEGER" property="noticeType"/>
        <result column="is_top" jdbcType="INTEGER" property="isTop"/>
        <result column="revocation_tag" jdbcType="INTEGER" property="revocationTag"/>
        <result column="deleted_by_users" jdbcType="LONGVARCHAR" property="deletedByUsers"/>
        <result column="read_by_users" jdbcType="LONGVARCHAR" property="readByUsers"/>
        <result column="data_state" jdbcType="TINYINT" property="dataState"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO tour_im_message
            (id,
            message_content,
            message_type,
            group_id,
            notice_type,
            is_top,
            revocation_tag,
            deleted_by_users,
            read_by_users,
            data_state,
            create_user_id,
            create_time,
            update_time,
            update_user_id)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.messageContent},
            #{item.messageType},
            #{item.groupId},
            #{item.noticeType},
            #{item.isTop},
            #{item.revocationTag},
            #{item.deletedByUsers},
            #{item.readByUsers},
            #{item.dataState},
            #{item.createUserId},
            #{item.createTime},
            #{item.updateTime},
            #{item.updateUserId})
        </foreach>
    </insert>

    <!-- 如果有逻辑删除字段过滤，请写到这里 -->
    <sql id="filterRef">
        <!-- 这里必须加上全包名，否则当filterRef被其他Mapper.xml包含引用的时候，就会调用Mapper.xml中的该SQL片段 -->
        <include refid="com.tourism.webadmin.back.dao.TourImMessageMapper.inputFilterRef"/>
        AND tour_im_message.data_state = ${@com.tourism.common.core.constant.GlobalDeletedFlag@NORMAL}
    </sql>

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="tourImMessageFilter != null">
            <if test="tourImMessageFilter.id != null">
                AND tour_im_message.id = #{tourImMessageFilter.id}
            </if>
            <if test="tourImMessageFilter.messageContent != null and tourImMessageFilter.messageContent != ''">
                <bind name = "safeTourImMessageMessageContent" value = "'%' + tourImMessageFilter.messageContent + '%'" />
                AND tour_im_message.message_content LIKE #{safeTourImMessageMessageContent}
            </if>
            <if test="tourImMessageFilter.messageType != null">
                AND tour_im_message.message_type = #{tourImMessageFilter.messageType}
            </if>
            <if test="tourImMessageFilter.groupId != null">
                AND tour_im_message.group_id = #{tourImMessageFilter.groupId}
            </if>
            <if test="tourImMessageFilter.noticeType != null">
                AND tour_im_message.notice_type = #{tourImMessageFilter.noticeType}
            </if>
            <if test="tourImMessageFilter.isTop != null">
                AND tour_im_message.is_top = #{tourImMessageFilter.isTop}
            </if>
            <if test="tourImMessageFilter.revocationTag != null">
                AND tour_im_message.revocation_tag = #{tourImMessageFilter.revocationTag}
            </if>
            <if test="tourImMessageFilter.searchString != null and tourImMessageFilter.searchString != ''">
                <bind name = "safeTourImMessageSearchString" value = "'%' + tourImMessageFilter.searchString + '%'" />
                AND IFNULL(tour_im_message.message_content,'') LIKE #{safeTourImMessageSearchString}
            </if>
        </if>
    </sql>

    <select id="getTourImMessageList" resultMap="BaseResultMap" parameterType="com.tourism.webadmin.back.model.TourImMessage">
        SELECT * FROM tour_im_message
        <where>
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getMessageByGroupId" resultMap="BaseResultMap">
        SELECT
        id,
        message_content,
        message_type,
        notice_type,
        group_id,
        is_top,
        revocation_tag,
        deleted_by_users,
        read_by_users,
        data_state,
        create_user_id,
        create_time,
        update_time,
        update_user_id
        FROM tour_im_message as t1
        <where>
            group_id = #{groupId}
            AND data_state = 1
            AND deleted_by_users not like CONCAT('%',#{userId},'%')
            <if test="messageId !=null and messageId!='' "> AND
                create_time &lt;= (SELECT create_time FROM tour_im_message WHERE id = #{messageId})
            </if>
            <if test="searchMessage != null and searchMessage !='' ">
                AND message_content like CONCAT('%',#{searchMessage},'%')
            </if>
        </where>
        ORDER BY create_time desc
        LIMIT #{pageSize};
    </select>

    <select id="getMessageByGroupIdCount" resultType="java.lang.Integer">
        SELECT
            count(*)
        FROM tour_im_message as t1
        <where>
            group_id = #{groupId}
            AND data_state = 1
            AND deleted_by_users not like CONCAT('%',#{userId},'%')
            <if test="searchMessage != null and searchMessage !='' ">
                AND message_content like CONCAT('%',#{searchMessage},'%')
            </if>
        </where>
        ORDER BY create_time desc
    </select>
</mapper>
