<?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.TourFansMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.TourFans">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="attention_id" jdbcType="BIGINT" property="attentionId"/>
        <result column="delete_flag" jdbcType="INTEGER" property="deleteFlag"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO tour_fans
            (id,
            attention_id,
            delete_flag,
            create_user_id,
            create_time,
            update_user_id,
            update_time)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.attentionId},
            #{item.deleteFlag},
            #{item.createUserId},
            #{item.createTime},
            #{item.updateUserId},
            #{item.updateTime})
        </foreach>
    </insert>

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

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="tourFansFilter != null">
            <if test="tourFansFilter.attentionId != null">
                AND tour_fans.attention_id = #{tourFansFilter.attentionId}
            </if>
            <if test="tourFansFilter.createUserId != null">
                AND tour_fans.create_user_id = #{tourFansFilter.createUserId}
            </if>
        </if>
    </sql>

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

    <select id="getFouceEachFriendsByName" resultType="com.tourism.webadmin.app.website.model.TourShortUser" parameterType="String">
        SELECT user.user_id, user.show_name, user.head_image_url
        FROM tour_user AS user
        WHERE user.user_id IN (
        SELECT f1.attention_id AS mutual_follow_user_id
        FROM tour_fans f1
        JOIN tour_fans f2
        ON f1.attention_id = f2.create_user_id
        AND f1.create_user_id = f2.attention_id
        WHERE f1.create_user_id = #{userId}
        AND f1.delete_flag = 1
        AND f2.delete_flag = 1
        )
        <if test="name != null and name != ''">
            AND user.show_name LIKE CONCAT('%', #{name}, '%')
        </if>
    </select>
</mapper>
