<?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.TourImGroupMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.TourImGroup">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="belong_type_id" jdbcType="BIGINT" property="belongTypeId"/>
        <result column="leader_id" jdbcType="BIGINT" property="leaderId"/>
        <result column="group_name" jdbcType="VARCHAR" property="groupName"/>
        <result column="group_avatar" jdbcType="VARCHAR" property="groupAvatar"/>
        <result column="description" jdbcType="VARCHAR" property="description"/>
        <result column="is_public" jdbcType="TINYINT" property="isPublic"/>
        <result column="is_need_confirm" jdbcType="TINYINT" property="isNeedConfirm"/>
        <result column="notice_type" jdbcType="INTEGER" property="noticeType"/>
        <result column="banned_status" jdbcType="INTEGER" property="bannedStatus"/>
        <result column="hot_value" jdbcType="INTEGER" property="hotValue"/>
        <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_group
            (id,
            belong_type_id,
            leader_id,
            group_name,
            group_avatar,
            description,
            is_public,
            is_need_confirm,
            notice_type,
            banned_status,
            hot_value,
            data_state,
            create_user_id,
            create_time,
            update_time,
            update_user_id)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.belongTypeId},
            #{item.leaderId},
            #{item.groupName},
            #{item.groupAvatar},
            #{item.description},
            #{item.isPublic},
            #{item.isNeedConfirm},
            #{item.noticeType},
            #{item.bannedStatus},
            #{item.hotValue},
            #{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.TourImGroupMapper.inputFilterRef"/>
        AND tour_im_group.data_state = ${@com.tourism.common.core.constant.GlobalDeletedFlag@NORMAL}
    </sql>

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="tourImGroupFilter != null">
            <if test="tourImGroupFilter.id != null">
                AND tour_im_group.id = #{tourImGroupFilter.id}
            </if>
            <if test="tourImGroupFilter.belongTypeId != null">
                AND tour_im_group.belong_type_id = #{tourImGroupFilter.belongTypeId}
            </if>
            <if test="tourImGroupFilter.leaderId != null">
                AND tour_im_group.leader_id = #{tourImGroupFilter.leaderId}
            </if>
            <if test="tourImGroupFilter.groupName != null and tourImGroupFilter.groupName != ''">
                <bind name = "safeTourImGroupGroupName" value = "'%' + tourImGroupFilter.groupName + '%'" />
                AND tour_im_group.group_name LIKE #{safeTourImGroupGroupName}
            </if>
            <if test="tourImGroupFilter.description != null and tourImGroupFilter.description != ''">
                <bind name = "safeTourImGroupDescription" value = "'%' + tourImGroupFilter.description + '%'" />
                AND tour_im_group.description LIKE #{safeTourImGroupDescription}
            </if>
            <if test="tourImGroupFilter.isPublic != null">
                AND tour_im_group.is_public = #{tourImGroupFilter.isPublic}
            </if>
            <if test="tourImGroupFilter.isNeedConfirm != null">
                AND tour_im_group.is_need_confirm = #{tourImGroupFilter.isNeedConfirm}
            </if>
            <if test="tourImGroupFilter.noticeType != null">
                AND tour_im_group.notice_type = #{tourImGroupFilter.noticeType}
            </if>
            <if test="tourImGroupFilter.bannedStatus != null">
                AND tour_im_group.banned_status = #{tourImGroupFilter.bannedStatus}
            </if>
            <if test="tourImGroupFilter.hotValueStart != null">
                AND tour_im_group.hot_value &gt;= #{tourImGroupFilter.hotValueStart}
            </if>
            <if test="tourImGroupFilter.hotValueEnd != null">
                AND tour_im_group.hot_value &lt;= #{tourImGroupFilter.hotValueEnd}
            </if>
            <if test="tourImGroupFilter.searchString != null and tourImGroupFilter.searchString != ''">
                <bind name = "safeTourImGroupSearchString" value = "'%' + tourImGroupFilter.searchString + '%'" />
                AND CONCAT(IFNULL(tour_im_group.group_name,''), IFNULL(tour_im_group.description,'')) LIKE #{safeTourImGroupSearchString}
            </if>
        </if>
    </sql>

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

    <!-- 支持基于一对一或者一对多从表字段过滤的SQL_ID -->
    <select id="getTourImGroupListEx" resultMap="BaseResultMap" >
        SELECT
            tour_im_group.*
        FROM
            tour_im_group
        <where>
            <include refid="filterRef"/>
            <if test="tourImGroupInvitationFilter != null">
                AND EXISTS (SELECT * FROM tour_im_group_invitation
                <where>
                    tour_im_group.id = tour_im_group_invitation.group_id
                    <include refid="com.tourism.webadmin.back.dao.TourImGroupInvitationMapper.filterRef"/>
                </where>)
            </if>
            <if test="tourImMemberFilter != null">
                AND EXISTS (SELECT * FROM tour_im_member
                <where>
                    tour_im_group.id = tour_im_member.group_id
                    <include refid="com.tourism.webadmin.back.dao.TourImMemberMapper.filterRef"/>
                </where>)
            </if>
            <if test="tourImMessageFilter != null">
                AND EXISTS (SELECT * FROM tour_im_message
                <where>
                    tour_im_group.id = tour_im_message.group_id
                    <include refid="com.tourism.webadmin.back.dao.TourImMessageMapper.filterRef"/>
                </where>)
            </if>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getTakePartImGroupListByName" resultType="com.tourism.webadmin.app.website.model.TourShortImGroup">
        SELECT
            g.id,
            g.group_name,
            g.group_avatar
        FROM
            tour_im_group g
                INNER JOIN
            tour_im_member m
            ON
                g.id = m.group_id
        WHERE
            m.user_id = #{userId} and g.data_state = 1 and m.data_state = 1 and g.notice_type = 2
        <if test="name != null and name != ''">
        AND g.group_name LIKE CONCAT('%', #{name}, '%')
        </if>
    </select>
</mapper>
