<?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.TourismProjectTravelNotesWriteMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.TourismProjectTravelNotesWrite">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="project_title" jdbcType="VARCHAR" property="projectTitle"/>
        <result column="departure_time" jdbcType="DATE" property="departureTime"/>
        <result column="count_times" jdbcType="VARCHAR" property="countTimes"/>
        <result column="role" jdbcType="VARCHAR" property="role"/>
        <result column="recommendation_rate" jdbcType="TINYINT" property="recommendationRate"/>
        <result column="average_cost" jdbcType="VARCHAR" property="averageCost"/>
        <result column="state" jdbcType="TINYINT" property="state"/>
        <result column="reason" jdbcType="VARCHAR" property="reason"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="travel_notes_banner" jdbcType="VARCHAR" property="travelNotesBanner"/>
        <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="tourism_tavel_notes_url" jdbcType="VARCHAR" property="tourismTavelNotesUrl"/>
        <result column="unmount_state" jdbcType="TINYINT" property="unmountState"/>
        <result column="tourism_url" jdbcType="VARCHAR" property="tourismUrl"/>
        <result column="remarks" jdbcType="VARCHAR" property="remarks"/>
        <result column="is_hotspot" jdbcType="TINYINT" property="isHotspot"/>
        <result column="note_label" jdbcType="VARCHAR" property="noteLabel"/>
        <result column="like_count" jdbcType="INTEGER" property="likeCount"/>
        <result column="page_view_count" jdbcType="INTEGER" property="pageViewCount"/>
        <result column="hot_value" jdbcType="INTEGER" property="hotValue"/>
        <result column="is_original" jdbcType="TINYINT" property="isOriginal"/>
        <result column="end_place" jdbcType="BIGINT" property="endPlace"/>
        <result column="auto_audit_status" jdbcType="TINYINT" property="autoAuditStatus"/>
        <result column="auto_reject_reason" jdbcType="VARCHAR" property="autoRejectReason"/>
        <result column="travel_mode" jdbcType="TINYINT" property="travelMode"/>
        <result column="travel_number" jdbcType="VARCHAR" property="travelNumber"/>
        <result column="tour_im_group_id" jdbcType="BIGINT" property="tourImGroupId"/>
        <result column="visible_range" jdbcType="BIGINT" property="visibleRange"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO tour_tourism_project_travel_notes_writer
            (id,
            project_title,
            departure_time,
            count_times,
            role,
            recommendation_rate,
            average_cost,
            state,
            reason,
            create_user_id,
            travel_notes_banner,
            create_time,
            update_user_id,
            update_time,
            data_state,
            tourism_tavel_notes_url,
            unmount_state,
            tourism_url,
            remarks,
            is_hotspot,
            note_label,
            like_count,
            page_view_count,
            hot_value,
            is_original,
            end_place,
            auto_audit_status,
            auto_reject_reason,
            travel_mode,
            travel_number,
            tour_im_group_id,
            visible_range)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.projectTitle},
            #{item.departureTime},
            #{item.countTimes},
            #{item.role},
            #{item.recommendationRate},
            #{item.averageCost},
            #{item.state},
            #{item.reason},
            #{item.createUserId},
            #{item.travelNotesBanner},
            #{item.createTime},
            #{item.updateUserId},
            #{item.updateTime},
            #{item.dataState},
            #{item.tourismTavelNotesUrl},
            #{item.unmountState},
            #{item.tourismUrl},
            #{item.remarks},
            #{item.isHotspot},
            #{item.noteLabel},
            #{item.likeCount},
            #{item.pageViewCount},
            #{item.hotValue},
            #{item.isOriginal},
            #{item.endPlace},
            #{item.autoAuditStatus},
            #{item.autoRejectReason},
            #{item.travelMode},
            #{item.travelNumber},
            #{item.tourImGroupId},
            #{item.visibleRange})
        </foreach>
    </insert>

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

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="tourismProjectTravelNotesWriteFilter != null">
            <if test="tourismProjectTravelNotesWriteFilter.projectTitle != null and tourismProjectTravelNotesWriteFilter.projectTitle != ''">
                <bind name = "safeTourismProjectTravelNotesWriteProjectTitle" value = "'%' + tourismProjectTravelNotesWriteFilter.projectTitle + '%'" />
                AND tour_tourism_project_travel_notes_writer.project_title LIKE #{safeTourismProjectTravelNotesWriteProjectTitle}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.departureTimeStart != null and tourismProjectTravelNotesWriteFilter.departureTimeStart != ''">
                AND tour_tourism_project_travel_notes_writer.departure_time &gt;= #{tourismProjectTravelNotesWriteFilter.departureTimeStart}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.departureTimeEnd != null and tourismProjectTravelNotesWriteFilter.departureTimeEnd != ''">
                AND tour_tourism_project_travel_notes_writer.departure_time &lt;= #{tourismProjectTravelNotesWriteFilter.departureTimeEnd}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.countTimes != null and tourismProjectTravelNotesWriteFilter.countTimes != ''">
                <bind name = "safeTourismProjectTravelNotesWriteCountTimes" value = "'%' + tourismProjectTravelNotesWriteFilter.countTimes + '%'" />
                AND tour_tourism_project_travel_notes_writer.count_times LIKE #{safeTourismProjectTravelNotesWriteCountTimes}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.role != null and tourismProjectTravelNotesWriteFilter.role != ''">
                <bind name = "safeTourismProjectTravelNotesWriteRole" value = "'%' + tourismProjectTravelNotesWriteFilter.role + '%'" />
                AND tour_tourism_project_travel_notes_writer.role LIKE #{safeTourismProjectTravelNotesWriteRole}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.recommendationRate != null">
                AND tour_tourism_project_travel_notes_writer.recommendation_rate = #{tourismProjectTravelNotesWriteFilter.recommendationRate}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.averageCost != null and tourismProjectTravelNotesWriteFilter.averageCost != ''">
                <bind name = "safeTourismProjectTravelNotesWriteAverageCost" value = "'%' + tourismProjectTravelNotesWriteFilter.averageCost + '%'" />
                AND tour_tourism_project_travel_notes_writer.average_cost LIKE #{safeTourismProjectTravelNotesWriteAverageCost}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.state != null">
                AND tour_tourism_project_travel_notes_writer.state = #{tourismProjectTravelNotesWriteFilter.state}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.createUserId != null">
                AND tour_tourism_project_travel_notes_writer.create_user_id = #{tourismProjectTravelNotesWriteFilter.createUserId}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.reason != null and tourismProjectTravelNotesWriteFilter.reason != ''">
                <bind name = "safeTourismProjectTravelNotesWriteReason" value = "'%' + tourismProjectTravelNotesWriteFilter.reason + '%'" />
                AND tour_tourism_project_travel_notes_writer.reason LIKE #{safeTourismProjectTravelNotesWriteReason}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.createTimeStart != null and tourismProjectTravelNotesWriteFilter.createTimeStart != ''">
                AND tour_tourism_project_travel_notes_writer.create_time &gt;= #{tourismProjectTravelNotesWriteFilter.createTimeStart}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.createTimeEnd != null and tourismProjectTravelNotesWriteFilter.createTimeEnd != ''">
                AND tour_tourism_project_travel_notes_writer.create_time &lt;= #{tourismProjectTravelNotesWriteFilter.createTimeEnd}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.isHotspot != null">
                AND tour_tourism_project_travel_notes_writer.is_hotspot = #{tourismProjectTravelNotesWriteFilter.isHotspot}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.isOriginal != null">
                AND tour_tourism_project_travel_notes_writer.is_original = #{tourismProjectTravelNotesWriteFilter.isOriginal}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.visibleRange != null">
                AND tour_tourism_project_travel_notes_writer.visible_range = #{tourismProjectTravelNotesWriteFilter.visibleRange}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.endPlace != null and tourismProjectTravelNotesWriteFilter.endPlace != ''">
                AND tour_tourism_project_travel_notes_writer.end_place = #{tourismProjectTravelNotesWriteFilter.endPlace}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.searchString != null and tourismProjectTravelNotesWriteFilter.searchString != ''">
                <bind name = "safeTourismProjectTravelNotesWriteSearchString" value = "'%' + tourismProjectTravelNotesWriteFilter.searchString + '%'" />
                AND CONCAT(IFNULL(tour_tourism_project_travel_notes_writer.project_title,''), IFNULL(tour_tourism_project_travel_notes_writer.count_times,''), IFNULL(tour_tourism_project_travel_notes_writer.role,''), IFNULL(tour_tourism_project_travel_notes_writer.average_cost,''), IFNULL(tour_tourism_project_travel_notes_writer.reason,'')) LIKE #{safeTourismProjectTravelNotesWriteSearchString}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.unmountState != null">
                AND tour_tourism_project_travel_notes_writer.unmount_state = #{tourismProjectTravelNotesWriteFilter.unmountState}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.tourImGroupId != null">
                AND tour_tourism_project_travel_notes_writer.tour_im_group_id = #{tourismProjectTravelNotesWriteFilter.tourImGroupId}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.directoryInfoIds != null and tourismProjectTravelNotesWriteFilter.directoryInfoIds.size() > 0">
                AND tour_tourism_project_travel_notes_writer.end_place IN
                <foreach item="id" index="index" collection="tourismProjectTravelNotesWriteFilter.directoryInfoIds" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.travelMode != null">
                AND tour_tourism_project_travel_notes_writer.travel_mode = #{tourismProjectTravelNotesWriteFilter.travelMode}
            </if>
            <if test="tourismProjectTravelNotesWriteFilter.creatUserIdFilter!= null and tourismProjectTravelNotesWriteFilter.creatUserIdFilter.size() > 0">
                AND tour_tourism_project_travel_notes_writer.create_user_id
                <foreach collection="tourismProjectTravelNotesWriteFilter.creatUserIdFilter" index="index" item="id" open="IN(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </if>
    </sql>

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

    <sql id="updateRef">
        <!-- 审核不通过，设置state为2、驳回原因 -->
        <if test="tourismProjectTravelNotesWriteFilter.state == 2">
            unmount_state = 0,
            state = 2,
            reason = #{tourismProjectTravelNotesWriteFilter.reason}
        </if>
        <!-- 审核通过，设置state为3 -->
        <if test="tourismProjectTravelNotesWriteFilter.state == 3">
            unmount_state = 1,
            state = 3,
            is_hotspot = #{tourismProjectTravelNotesWriteFilter.isHotspot},
            note_label = #{tourismProjectTravelNotesWriteFilter.noteLabel},
            like_count = #{tourismProjectTravelNotesWriteFilter.likeCount},
            page_view_count = #{tourismProjectTravelNotesWriteFilter.pageViewCount},
            hot_value = #{tourismProjectTravelNotesWriteFilter.hotValue},
            is_original = #{tourismProjectTravelNotesWriteFilter.isOriginal},
            publish_time = NOW()
        </if>
    </sql>

    <update id="auditTravelNote" parameterType="com.tourism.webadmin.back.model.TourismProjectTravelNotesWrite">
        update tour_tourism_project_travel_notes_writer
        <set>
            <include refid="updateRef"></include>
        </set>
        <where>
            id = #{tourismProjectTravelNotesWriteFilter.id}
        </where>
    </update>

    <select id="getLikeCount" resultType="java.lang.Long">
        select SUM(like_count) as likeCount
        FROM tour_tourism_project_travel_notes_writer
        WHERE id
        <foreach collection="list" item="id" open="IN(" separator="," close=")">
            #{id}
        </foreach>
    </select>
</mapper>
