<?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.TourImMemberMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.TourImMember">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="group_id" jdbcType="BIGINT" property="groupId"/>
        <result column="group_role" jdbcType="INTEGER" property="groupRole"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="is_show" jdbcType="INTEGER" property="isShow"/>
        <result column="partner_id" jdbcType="BIGINT" property="partnerId"/>
        <result column="is_top" jdbcType="INTEGER" property="isTop"/>
        <result column="is_not_disturb" jdbcType="INTEGER" property="isNotDisturb"/>
        <result column="group_nickname" jdbcType="VARCHAR" property="groupNickname"/>
        <result column="group_remark" jdbcType="VARCHAR" property="groupRemark"/>
        <result column="data_state" jdbcType="TINYINT" property="dataState"/>
        <result column="group_back_image" jdbcType="VARCHAR" property="groupBackImage"/>
        <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_member
            (id,
            group_id,
            group_role,
            user_id,
            is_show,
            partner_id,
            is_top,
            is_not_disturb,
            group_nickname,
            group_remark,
            data_state,
            group_back_image,
            create_user_id,
            create_time,
            update_time,
            update_user_id)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.groupId},
            #{item.groupRole},
            #{item.userId},
            #{item.isShow},
            #{item.partnerId},
            #{item.isTop},
            #{item.isNotDisturb},
            #{item.groupNickname},
            #{item.groupRemark},
            #{item.dataState},
            #{item.groupBackImage},
            #{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.TourImMemberMapper.inputFilterRef"/>
        AND tour_im_member.data_state = ${@com.tourism.common.core.constant.GlobalDeletedFlag@NORMAL}
    </sql>

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="tourImMemberFilter != null">
            <if test="tourImMemberFilter.id != null">
                AND tour_im_member.id = #{tourImMemberFilter.id}
            </if>
            <if test="tourImMemberFilter.groupId != null">
                AND tour_im_member.group_id = #{tourImMemberFilter.groupId}
            </if>
            <if test="tourImMemberFilter.groupRole != null">
                AND tour_im_member.group_role = #{tourImMemberFilter.groupRole}
            </if>
            <if test="tourImMemberFilter.userId != null">
                AND tour_im_member.user_id = #{tourImMemberFilter.userId}
            </if>
            <if test="tourImMemberFilter.isShow != null">
                AND tour_im_member.is_show = #{tourImMemberFilter.isShow}
            </if>
            <if test="tourImMemberFilter.isTop != null">
                AND tour_im_member.is_top = #{tourImMemberFilter.isTop}
            </if>
            <if test="tourImMemberFilter.isNotDisturb != null">
                AND tour_im_member.is_not_disturb = #{tourImMemberFilter.isNotDisturb}
            </if>
            <if test="tourImMemberFilter.groupNickname != null and tourImMemberFilter.groupNickname != ''">
                <bind name = "safeTourImMemberGroupNickname" value = "'%' + tourImMemberFilter.groupNickname + '%'" />
                AND tour_im_member.group_nickname LIKE #{safeTourImMemberGroupNickname}
            </if>
            <if test="tourImMemberFilter.groupRemark != null and tourImMemberFilter.groupRemark != ''">
                <bind name = "safeTourImMemberGroupRemark" value = "'%' + tourImMemberFilter.groupRemark + '%'" />
                AND tour_im_member.group_remark LIKE #{safeTourImMemberGroupRemark}
            </if>
            <if test="tourImMemberFilter.searchString != null and tourImMemberFilter.searchString != ''">
                <bind name = "safeTourImMemberSearchString" value = "'%' + tourImMemberFilter.searchString + '%'" />
                AND CONCAT(IFNULL(tour_im_member.group_nickname,''), IFNULL(tour_im_member.group_remark,'')) LIKE #{safeTourImMemberSearchString}
            </if>
        </if>
    </sql>

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

    <select id="getTourMemberList" resultType="com.tourism.webadmin.app.website.vo.MainMemberVo">
        SELECT
            t2.id,
            t1.id as groupId,
            t1.group_avatar as headImage,
            t2.group_back_image as backgroundImage,
            t2.is_top,
            t1.group_name,
            t1.notice_type,
            t2.create_time,
            t2.group_remark as groupRemark,
            t2.group_nickname,
            t2.is_show,
            t1.notice_type,
            t2.is_not_disturb,
            t2.partner_id AS toUserId
        FROM
            tour_im_group as t1
        INNER JOIN
            tour_im_member as t2
            ON  t2.group_id=t1.id
        WHERE t2.data_state='1'
            AND t1.data_state='1'
            AND t2.is_show= '1'
            AND t2.user_id=#{userId}
    </select>

    <select id="selectLastMessage" resultType="com.tourism.webadmin.back.model.TourImMessage">
        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
        (
        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,
        ROW_NUMBER() OVER ( PARTITION BY group_id ORDER BY create_time DESC ) AS row_num
        FROM
        tour_im_message
        WHERE group_id
        <foreach collection="ids" item="id" open="IN(" separator="," close=")">
            #{id}
        </foreach>
        ) AS t1
        WHERE
        deleted_by_users not like concat('%',#{userId},'%')
        AND t1.row_num = '1'
    </select>

    <select id="getLast" resultType="com.tourism.webadmin.back.model.TourImMessage">
        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
        WHERE  message_content like concat('%',#{userId},'%') AND
        notice_type
        <foreach collection="types" item="type" open="IN(" separator="," close=")">
            #{type}
        </foreach>
        ORDER BY create_time DESC
        limit 1
    </select>

    <select id="getLastSysMessage" resultType="com.tourism.webadmin.back.model.TourImMessage">
        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
        WHERE  (message_content LIKE concat('%"getUserId":"',#{userId},'"%')
        OR message_content LIKE concat('%"getUserId":"','null','"%') )
        AND
        notice_type = #{type}
        ORDER BY create_time DESC
        limit 1
    </select>

    <select id="getCount" resultType="com.tourism.webadmin.back.model.TourImMessage">
        SELECT
            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_user_id,
            update_time
        FROM
            tour_im_message
        WHERE
            data_state = 1
          AND (
            revocation_tag = "0"
                AND group_id
                <foreach collection="list" item="item" open="IN(" separator="," close=")">
                    #{item}
                </foreach>
                AND read_by_users NOT LIKE concat("%",#{userId},"%")
                AND create_time >= #{time})
          AND (
            message_content LIKE concat('%"getUserId":"',#{userId},'"%')
                OR message_content LIKE concat('%"getUserId":"','null','"%'))
    </select>
</mapper>
