<?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.TourVisaMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.TourVisa">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="img" jdbcType="VARCHAR" property="img"/>
        <result column="visa_title" jdbcType="VARCHAR" property="visaTitle"/>
        <result column="visa_type_id" jdbcType="BIGINT" property="visaTypeId"/>
        <result column="country_code_id" jdbcType="VARCHAR" property="countryCodeId"/>
        <result column="embassy_name" jdbcType="VARCHAR" property="embassyName"/>
        <result column="embassy_address" jdbcType="VARCHAR" property="embassyAddress"/>
        <result column="interview_flag" jdbcType="INTEGER" property="interviewFlag"/>
        <result column="validity" jdbcType="INTEGER" property="validity"/>
        <result column="idle_days" jdbcType="INTEGER" property="idleDays"/>
        <result column="processing_days" jdbcType="INTEGER" property="processingDays"/>
        <result column="floor_price" jdbcType="INTEGER" property="floorPrice"/>
        <result column="acceptance_range" jdbcType="VARCHAR" property="acceptanceRange"/>
        <result column="entries_num" jdbcType="INTEGER" property="entriesNum"/>
        <result column="data_text" jdbcType="LONGVARCHAR" property="dataText"/>
        <result column="issue_visa_code" jdbcType="BIGINT" property="issueVisaCode"/>
        <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"/>
        <result column="counselor_id" jdbcType="BIGINT" property="counselorId"/>
        <result column="hot_value" jdbcType="INTEGER" property="hotValue"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO tour_visa
            (id,
            img,
            visa_title,
            visa_type_id,
            country_code_id,
            embassy_name,
            embassy_address,
            interview_flag,
            validity,
            idle_days,
            processing_days,
            floor_price,
            acceptance_range,
            entries_num,
            data_text,
            issue_visa_code,
            delete_flag,
            create_user_id,
            create_time,
            update_user_id,
            update_time,
            counselor_id,
            hot_value)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.img},
            #{item.visaTitle},
            #{item.visaTypeId},
            #{item.countryCodeId},
            #{item.embassyName},
            #{item.embassyAddress},
            #{item.interviewFlag},
            #{item.validity},
            #{item.idleDays},
            #{item.processingDays},
            #{item.floorPrice},
            #{item.acceptanceRange},
            #{item.entriesNum},
            #{item.dataText},
            #{item.issueVisaCode},
            #{item.deleteFlag},
            #{item.createUserId},
            #{item.createTime},
            #{item.updateUserId},
            #{item.updateTime},
            #{item.counselorId},
            #{item.hotValue})
        </foreach>
    </insert>

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

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="tourVisaFilter != null">
            <if test="tourVisaFilter.visaTitle != null and tourVisaFilter.visaTitle != ''">
                <bind name = "safeTourVisaVisaTitle" value = "'%' + tourVisaFilter.visaTitle + '%'" />
                AND tour_visa.visa_title LIKE #{safeTourVisaVisaTitle}
            </if>
            <if test="tourVisaFilter.visaTypeId != null">
                AND tour_visa.visa_type_id = #{tourVisaFilter.visaTypeId}
            </if>
            <if test="tourVisaFilter.countryCodeId != null and tourVisaFilter.countryCodeId != ''">
                AND tour_visa.country_code_id = #{tourVisaFilter.countryCodeId}
            </if>
            <if test="tourVisaFilter.embassyName != null and tourVisaFilter.embassyName != ''">
                <bind name = "safeTourVisaEmbassyName" value = "'%' + tourVisaFilter.embassyName + '%'" />
                AND tour_visa.embassy_name LIKE #{safeTourVisaEmbassyName}
            </if>
            <if test="tourVisaFilter.interviewFlag != null">
                AND tour_visa.interview_flag = #{tourVisaFilter.interviewFlag}
            </if>
            <if test="tourVisaFilter.issueVisaCode != null">
                and tour_visa.issue_visa_code = #{tourVisaFilter.issueVisaCode}
            </if>
            <if test="tourVisaFilter.searchString != null and tourVisaFilter.searchString != ''">
                <bind name = "safeTourVisaSearchString" value = "'%' + tourVisaFilter.searchString + '%'" />
                AND CONCAT(IFNULL(tour_visa.visa_title,''), IFNULL(tour_visa.embassy_name,'')) LIKE #{safeTourVisaSearchString}
            </if>
        </if>
    </sql>

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

    <!-- 支持基于一对一或者一对多从表字段过滤的SQL_ID -->
    <select id="getTourVisaListEx" resultMap="BaseResultMap" >
        SELECT
            tour_visa.*
        FROM
            tour_visa
        <where>
            <include refid="filterRef"/>
            <if test="visaMetarialsListFilter != null">
                AND EXISTS (SELECT * FROM tour_visa_metarials_list
                <where>
                    tour_visa.id = tour_visa_metarials_list.visa_id
                    <include refid="com.tourism.webadmin.back.dao.VisaMetarialsListMapper.filterRef"/>
                </where>)
            </if>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>
</mapper>
