<?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.TourHouseAdvisorInfoMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.TourHouseAdvisorInfo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="developers_id" jdbcType="BIGINT" property="developersId"/>
        <result column="type" jdbcType="TINYINT" property="type"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="experience_age" jdbcType="INTEGER" property="experienceAge"/>
        <result column="successful_delivery_count" jdbcType="INTEGER" property="successfulDeliveryCount"/>
        <result column="is_gold_advisor" jdbcType="TINYINT" property="isGoldAdvisor"/>
        <result column="avatar_url" jdbcType="VARCHAR" property="avatarUrl"/>
        <result column="record_number" jdbcType="VARCHAR" property="recordNumber"/>
        <result column="company_name" jdbcType="VARCHAR" property="companyName"/>
        <result column="code" jdbcType="VARCHAR" property="code"/>
        <result column="enable" jdbcType="TINYINT" property="enable"/>
        <result column="data_state" jdbcType="TINYINT" property="dataState"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <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>

    <resultMap id="BaseResultMapWithTourHouseAdvisorRelation" type="com.tourism.webadmin.back.model.TourHouseAdvisorInfo" extends="BaseResultMap">
        <association property="tourHouseAdvisorRelation" column="id" foreignColumn="advisor_id"
                     notNullColumn="id" resultMap="com.tourism.webadmin.back.dao.TourHouseAdvisorRelationMapper.BaseResultMap" />
    </resultMap>

    <insert id="insertList">
        INSERT INTO tour_house_advisor_info
        (id,
        developers_id,
        type,
        name,
        experience_age,
        successful_delivery_count,
        is_gold_advisor,
        avatar_url,
        record_number,
        company_name,
        code,
        enable,
        data_state,
        mobile,
        create_user_id,
        create_time,
        update_user_id,
        update_time)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.developersId},
            #{item.type},
            #{item.name},
            #{item.experienceAge},
            #{item.successfulDeliveryCount},
            #{item.isGoldAdvisor},
            #{item.avatarUrl},
            #{item.recordNumber},
            #{item.companyName},
            #{item.code},
            #{item.enable},
            #{item.dataState},
            #{item.mobile},
            #{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.TourHouseAdvisorInfoMapper.inputFilterRef"/>
        AND tour_house_advisor_info.data_state = ${@com.tourism.common.core.constant.GlobalDeletedFlag@NORMAL}
    </sql>

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="tourHouseAdvisorInfoFilter != null">
            <if test="tourHouseAdvisorInfoFilter.id != null">
                AND tour_house_advisor_info.id = #{tourHouseAdvisorInfoFilter.id}
            </if>
            <if test="tourHouseAdvisorInfoFilter.developersId != null">
                AND tour_house_advisor_info.developers_id = #{tourHouseAdvisorInfoFilter.developersId}
            </if>
            <if test="tourHouseAdvisorInfoFilter.name != null and tourHouseAdvisorInfoFilter.name != ''">
                <bind name = "safeTourHouseAdvisorInfoName" value = "'%' + tourHouseAdvisorInfoFilter.name + '%'" />
                AND tour_house_advisor_info.name LIKE #{safeTourHouseAdvisorInfoName}
            </if>
            <if test="tourHouseAdvisorInfoFilter.experienceAgeStart != null">
                AND tour_house_advisor_info.experience_age &gt;= #{tourHouseAdvisorInfoFilter.experienceAgeStart}
            </if>
            <if test="tourHouseAdvisorInfoFilter.experienceAgeEnd != null">
                AND tour_house_advisor_info.experience_age &lt;= #{tourHouseAdvisorInfoFilter.experienceAgeEnd}
            </if>
            <if test="tourHouseAdvisorInfoFilter.successfulDeliveryCountStart != null">
                AND tour_house_advisor_info.successful_delivery_count &gt;= #{tourHouseAdvisorInfoFilter.successfulDeliveryCountStart}
            </if>
            <if test="tourHouseAdvisorInfoFilter.successfulDeliveryCountEnd != null">
                AND tour_house_advisor_info.successful_delivery_count &lt;= #{tourHouseAdvisorInfoFilter.successfulDeliveryCountEnd}
            </if>
            <if test="tourHouseAdvisorInfoFilter.isGoldAdvisor != null">
                AND tour_house_advisor_info.is_gold_advisor = #{tourHouseAdvisorInfoFilter.isGoldAdvisor}
            </if>
            <if test="tourHouseAdvisorInfoFilter.recordNumber != null and tourHouseAdvisorInfoFilter.recordNumber != ''">
                <bind name = "safeTourHouseAdvisorInfoRecordNumber" value = "'%' + tourHouseAdvisorInfoFilter.recordNumber + '%'" />
                AND tour_house_advisor_info.record_number LIKE #{safeTourHouseAdvisorInfoRecordNumber}
            </if>
            <if test="tourHouseAdvisorInfoFilter.companyName != null and tourHouseAdvisorInfoFilter.companyName != ''">
                <bind name = "safeTourHouseAdvisorInfoCompanyName" value = "'%' + tourHouseAdvisorInfoFilter.companyName + '%'" />
                AND tour_house_advisor_info.company_name LIKE #{safeTourHouseAdvisorInfoCompanyName}
            </if>
            <if test="tourHouseAdvisorInfoFilter.enable != null">
                AND tour_house_advisor_info.enable = #{tourHouseAdvisorInfoFilter.enable}
            </if>
            <if test="tourHouseAdvisorInfoFilter.mobile != null and tourHouseAdvisorInfoFilter.mobile != ''">
                <bind name = "safeTourHouseAdvisorInfoMobile" value = "'%' + tourHouseAdvisorInfoFilter.mobile + '%'" />
                AND tour_house_advisor_info.mobile LIKE #{safeTourHouseAdvisorInfoMobile}
            </if>
            <if test="tourHouseAdvisorInfoFilter.searchString != null and tourHouseAdvisorInfoFilter.searchString != ''">
                <bind name = "safeTourHouseAdvisorInfoSearchString" value = "'%' + tourHouseAdvisorInfoFilter.searchString + '%'" />
                AND CONCAT(IFNULL(tour_house_advisor_info.name,''), IFNULL(tour_house_advisor_info.record_number,''), IFNULL(tour_house_advisor_info.company_name,'')) LIKE #{safeTourHouseAdvisorInfoSearchString}
            </if>
            <if test="tourHouseAdvisorInfoFilter.type != null">
                AND tour_house_advisor_info.type = #{tourHouseAdvisorInfoFilter.type}
            </if>
            <if test="tourHouseAdvisorInfoFilter.developersId != null">
                AND tour_house_advisor_info.developers_Id = #{tourHouseAdvisorInfoFilter.developersId}
            </if>
        </if>
    </sql>

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

    <select id="getTourHouseAdvisorInfoListByHouseId" resultMap="BaseResultMapWithTourHouseAdvisorRelation">
        SELECT
        tour_house_advisor_info.*,
        tour_house_advisor_relation.*
        FROM
        tour_house_advisor_info,
        tour_house_advisor_relation
        <where>
            AND tour_house_advisor_relation.house_id = #{houseId}
            AND tour_house_advisor_relation.advisor_id = tour_house_advisor_info.id
            <include refid="filterRef"/>
            <include refid="com.tourism.webadmin.back.dao.TourHouseAdvisorRelationMapper.filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getTourHouseAdvisorInfoListByHouseIds" resultMap="BaseResultMapWithTourHouseAdvisorRelation">
        SELECT
        tour_house_advisor_info.*,
        tour_house_advisor_relation.*
        FROM
        tour_house_advisor_info,
        tour_house_advisor_relation
        <where>
            AND tour_house_advisor_relation.house_id IN
            <foreach collection="houseIds" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
            AND tour_house_advisor_relation.advisor_id = tour_house_advisor_info.id
            <include refid="filterRef"/>
            <include refid="com.tourism.webadmin.back.dao.TourHouseAdvisorRelationMapper.filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getNotInTourHouseAdvisorInfoListByHouseId" resultMap="BaseResultMap">
        SELECT
        tour_house_advisor_info.*
        FROM
        tour_house_advisor_info
        <where>
            AND NOT EXISTS (SELECT * FROM tour_house_advisor_relation
            WHERE tour_house_advisor_relation.house_id = #{houseId} AND tour_house_advisor_relation.advisor_id = tour_house_advisor_info.id)
            <include refid="filterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <select id="getAdvisors" resultMap="BaseResultMap" parameterType="com.tourism.webadmin.back.model.TourHouseAdvisorInfo">
        SELECT * FROM tour_house_sale_info
                          INNER JOIN tour_house_advisor_relation ON tour_house_sale_info.id = tour_house_advisor_relation.house_id
                          INNER JOIN tour_house_advisor_info ON tour_house_advisor_relation.advisor_id = tour_house_advisor_info.id
        WHERE  tour_house_advisor_info.enable=1 AND tour_house_sale_info.id  = #{id}
    </select>
</mapper>
