<?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.TourTravelNotesTopicMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.TourTravelNotesTopic">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="enable" jdbcType="TINYINT" property="enable"/>
        <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"/>
        <result column="data_state" jdbcType="TINYINT" property="dataState"/>
        <result column="hot_value" jdbcType="INTEGER" property="hotValue"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO tour_travel_notes_topic
            (id,
            name,
            enable,
            create_user_id,
            create_time,
            update_user_id,
            update_time,
            data_state,
            hot_value)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.name},
            #{item.enable},
            #{item.createUserId},
            #{item.createTime},
            #{item.updateUserId},
            #{item.updateTime},
            #{item.dataState},
            #{item.hotValue})
        </foreach>
    </insert>

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

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="tourTravelNotesTopicFilter != null">
            <if test="tourTravelNotesTopicFilter.name != null and tourTravelNotesTopicFilter.name != ''">
                <bind name = "safeTourTravelNotesTopicName" value = "'%' + tourTravelNotesTopicFilter.name + '%'" />
                AND tour_travel_notes_topic.name LIKE #{safeTourTravelNotesTopicName}
            </if>
            <if test="tourTravelNotesTopicFilter.enable != null">
                AND tour_travel_notes_topic.enable = #{tourTravelNotesTopicFilter.enable}
            </if>
            <if test="tourTravelNotesTopicFilter.hotValueStart != null">
                AND tour_travel_notes_topic.hot_value &gt;= #{tourTravelNotesTopicFilter.hotValueStart}
            </if>
            <if test="tourTravelNotesTopicFilter.hotValueEnd != null">
                AND tour_travel_notes_topic.hot_value &lt;= #{tourTravelNotesTopicFilter.hotValueEnd}
            </if>
        </if>
    </sql>

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

    <select id="getViewCountTopicByName" resultType="Integer" parameterType="String">
        SELECT
            SUM(ttn.page_view_count) AS total_page_views
        FROM
            tour_travel_notes_topic_relation ttnr
                JOIN
            tour_tourism_project_travel_notes_writer ttn
            ON
                ttnr.travel_notes_id = ttn.id
        WHERE
            ttnr.topic_name = #{name} and ttn.data_state = 1
        <if test="titleName != null and titleName !=''">
            and ttn.project_title LIKE CONCAT('%', #{titleName}, '%');
        </if>
    </select>
    <resultMap id="viewCountMap" type="java.util.Map">
        <id property="id" column="id" />
        <result property="totalPageViews" column="total_page_views"/>
    </resultMap>

    <select id="getViewCountListTopicByName" resultType="com.tourism.webadmin.app.website.vo.NotesViewsVo" parameterType="java.util.List">
        SELECT
        ttnr.topic_id AS id,
        SUM(ttn.page_view_count) AS total_page_views
        FROM
        tour_travel_notes_topic_relation ttnr
        JOIN
        tour_tourism_project_travel_notes_writer ttn
        ON ttnr.travel_notes_id = ttn.id
        WHERE
        ttnr.topic_id IN
        <foreach collection="idList" item="id" open="(" close=")" separator=",">
            #{id}
        </foreach>
        AND ttn.data_state = 1
        GROUP BY
        ttnr.topic_id
    </select>

    <select id="getCommentCountTopicByName" resultType="Integer" parameterType="String">
        SELECT
            COUNT(tnc.id) AS total_comment_count
        FROM
            tour_travel_notes_topic_relation ttnr
                JOIN
            tour_tourism_project_travel_notes_writer ttn
            ON ttnr.travel_notes_id = ttn.id
                LEFT JOIN
            tour_travel_notes_comment tnc
            ON ttn.id = tnc.travel_note_id
        WHERE
            ttnr.topic_name = #{name} and ttn.data_state = 1
        <if test="titleName != null and titleName !=''">
            and ttn.project_title LIKE CONCAT('%', #{titleName}, '%');
        </if>
    </select>

    <select id="getCountTravelNotesByName" resultType="Integer" parameterType="String">
        SELECT
            COUNT(DISTINCT ttnr.travel_notes_id) AS travel_note_count
        FROM
            tour_travel_notes_topic_relation ttnr
        left join tour_tourism_project_travel_notes_writer ttn on ttnr.travel_notes_id = ttn.id
        WHERE
            ttnr.topic_name = #{name}
            and ttn.data_state = 1
            AND ttn.state = 3
            AND ttn.unmount_state = 1
        <if test="titleName != null and titleName !=''">
            and ttn.project_title LIKE CONCAT('%', #{titleName}, '%');
        </if>
    </select>

    <select id="getTravelNotesByTopicName" resultType="TourismProjectTravelNotesWrite" parameterType="String">
        SELECT
            DISTINCT tttw.*
        FROM
            tour_tourism_project_travel_notes_writer tttw
                JOIN
            tour_travel_notes_topic_relation ttnr
            ON
                tttw.id = ttnr.travel_notes_id
        WHERE
            ttnr.topic_name = #{name}
          AND tttw.state = 3
          AND tttw.unmount_state = 1
          AND tttw.data_state = 1
        <if test="orderBy == null or orderBy == 0">
            ORDER BY tttw.hot_value DESC
        </if>
        <if test="orderBy == 1">
            ORDER BY tttw.create_time DESC
        </if>
    </select>
</mapper>
