<?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.TourHouseRentInfoMapper">
    <resultMap id="BaseResultMap" type="com.tourism.webadmin.back.model.TourHouseRentInfo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="area_id" jdbcType="BIGINT" property="areaId"/>
        <result column="advisor_id" jdbcType="BIGINT" property="advisorId"/>
        <result column="rent_type" jdbcType="TINYINT" property="rentType"/>
        <result column="office_type" jdbcType="INTEGER" property="officeType"/>
        <result column="rent_price" jdbcType="DECIMAL" property="rentPrice"/>
        <result column="area" jdbcType="INTEGER" property="area"/>
        <result column="house_type" jdbcType="INTEGER" property="houseType"/>
        <result column="rent_kind" jdbcType="INTEGER" property="rentKind"/>
        <result column="orientation" jdbcType="INTEGER" property="orientation"/>
        <result column="current_floor" jdbcType="INTEGER" property="currentFloor"/>
        <result column="decoration" jdbcType="INTEGER" property="decoration"/>
        <result column="community_name" jdbcType="VARCHAR" property="communityName"/>
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <result column="currency" jdbcType="VARCHAR" property="currency"/>
        <result column="region" jdbcType="VARCHAR" property="region"/>
        <result column="cover_url" jdbcType="VARCHAR" property="coverUrl"/>
        <result column="house_title" jdbcType="VARCHAR" property="houseTitle"/>
        <result column="total_floor" jdbcType="INTEGER" property="totalFloor"/>
        <result column="code" jdbcType="VARCHAR" property="code"/>
        <result column="mobile" jdbcType="VARCHAR" property="mobile"/>
        <result column="data_state" jdbcType="TINYINT" property="dataState"/>
        <result column="create_user_id" jdbcType="BIGINT" property="createUserId"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="update_user_id" jdbcType="BIGINT" property="updateUserId"/>
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="show_order" jdbcType="INTEGER" property="showOrder"/>
        <result column="rent_state" jdbcType="INTEGER" property="rentState"/>
        <result column="floor_type" jdbcType="TINYINT" property="floorType"/>
        <result column="is_hot_push" jdbcType="TINYINT" property="isHotPush"/>
    </resultMap>

    <insert id="insertList">
        INSERT INTO tour_house_rent_info
            (id,
            area_id,
            advisor_id,
            rent_type,
            office_type,
            rent_price,
            area,
            house_type,
            rent_kind,
            orientation,
            current_floor,
            decoration,
            community_name,
            address,
            currency,
            region,
            cover_url,
            house_title,
            total_floor,
            code,
            mobile,
            data_state,
            create_user_id,
            create_time,
            remark,
            update_user_id,
            update_time,
            show_order,
            rent_state,
            floor_type,
            is_hot_push)
        VALUES
        <foreach collection="list" index="index" item="item" separator="," >
            (#{item.id},
            #{item.areaId},
            #{item.advisorId},
            #{item.rentType},
            #{item.officeType},
            #{item.rentPrice},
            #{item.area},
            #{item.houseType},
            #{item.rentKind},
            #{item.orientation},
            #{item.currentFloor},
            #{item.decoration},
            #{item.communityName},
            #{item.address},
            #{item.currency},
            #{item.region},
            #{item.coverUrl},
            #{item.houseTitle},
            #{item.totalFloor},
            #{item.code},
            #{item.mobile},
            #{item.dataState},
            #{item.createUserId},
            #{item.createTime},
            #{item.remark},
            #{item.updateUserId},
            #{item.updateTime},
            #{item.showOrder},
            #{item.rentState},
            #{item.floorType},
            #{item.isHotPush})
        </foreach>
    </insert>

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

    <!-- 这里仅包含调用接口输入的主表过滤条件 -->
    <sql id="inputFilterRef">
        <if test="tourHouseRentInfoFilter != null">
            <if test="tourHouseRentInfoFilter.areaId != null">
                AND tour_house_rent_info.area_id = #{tourHouseRentInfoFilter.areaId}
            </if>
            <if test="tourHouseRentInfoFilter.rentType != null">
                AND tour_house_rent_info.rent_type = #{tourHouseRentInfoFilter.rentType}
            </if>
            <if test="tourHouseRentInfoFilter.officeType != null">
                AND tour_house_rent_info.office_type = #{tourHouseRentInfoFilter.officeType}
            </if>
            <if test="tourHouseRentInfoFilter.rentPriceStart != null">
                AND tour_house_rent_info.rent_price &gt;= #{tourHouseRentInfoFilter.rentPriceStart}
            </if>
            <if test="tourHouseRentInfoFilter.rentPriceEnd != null">
                AND tour_house_rent_info.rent_price &lt;= #{tourHouseRentInfoFilter.rentPriceEnd}
            </if>
            <if test="tourHouseRentInfoFilter.areaStart != null">
                AND tour_house_rent_info.area &gt;= #{tourHouseRentInfoFilter.areaStart}
            </if>
            <if test="tourHouseRentInfoFilter.areaEnd != null">
                AND tour_house_rent_info.area &lt;= #{tourHouseRentInfoFilter.areaEnd}
            </if>
            <if test="tourHouseRentInfoFilter.houseType != null">
                AND tour_house_rent_info.house_type = #{tourHouseRentInfoFilter.houseType}
            </if>
            <if test="tourHouseRentInfoFilter.orientation != null">
                AND tour_house_rent_info.orientation = #{tourHouseRentInfoFilter.orientation}
            </if>
            <if test="tourHouseRentInfoFilter.decoration != null">
                AND tour_house_rent_info.decoration = #{tourHouseRentInfoFilter.decoration}
            </if>
            <if test="tourHouseRentInfoFilter.communityName != null and tourHouseRentInfoFilter.communityName != ''">
                <bind name = "safeTourHouseRentInfoCommunityName" value = "'%' + tourHouseRentInfoFilter.communityName + '%'" />
                AND tour_house_rent_info.community_name LIKE #{safeTourHouseRentInfoCommunityName}
            </if>
            <if test="tourHouseRentInfoFilter.address != null and tourHouseRentInfoFilter.address != ''">
                <bind name = "safeTourHouseRentInfoAddress" value = "'%' + tourHouseRentInfoFilter.address + '%'" />
                AND tour_house_rent_info.address LIKE #{safeTourHouseRentInfoAddress}
            </if>
            <if test="tourHouseRentInfoFilter.region != null and tourHouseRentInfoFilter.region != ''">
                AND tour_house_rent_info.region = #{tourHouseRentInfoFilter.region}
            </if>
            <if test="tourHouseRentInfoFilter.houseTitle != null and tourHouseRentInfoFilter.houseTitle != ''">
                <bind name = "safeTourHouseRentInfoHouseTitle" value = "'%' + tourHouseRentInfoFilter.houseTitle + '%'" />
                AND tour_house_rent_info.house_title LIKE #{safeTourHouseRentInfoHouseTitle}
            </if>
            <if test="tourHouseRentInfoFilter.rentState != null">
                AND tour_house_rent_info.rent_state = #{tourHouseRentInfoFilter.rentState}
            </if>
            <if test="tourHouseRentInfoFilter.floorType != null">
                AND tour_house_rent_info.floor_type = #{tourHouseRentInfoFilter.floorType}
            </if>
            <if test="tourHouseRentInfoFilter.searchString != null and tourHouseRentInfoFilter.searchString != ''">
                <bind name = "safeTourHouseRentInfoSearchString" value = "'%' + tourHouseRentInfoFilter.searchString + '%'" />
                AND CONCAT(IFNULL(tour_house_rent_info.community_name,''), IFNULL(tour_house_rent_info.address,''), IFNULL(tour_house_rent_info.house_title,'')) LIKE #{safeTourHouseRentInfoSearchString}
            </if>
            <if test="tourHouseRentInfoFilter.isHotPush != null">
                AND tour_house_rent_info.is_hot_push = #{tourHouseRentInfoFilter.isHotPush}
            </if>
        </if>
    </sql>

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

    <!-- 支持基于一对一或者一对多从表字段过滤的SQL_ID -->
    <select id="getTourHouseRentInfoListEx" resultMap="BaseResultMap" >
        SELECT
            tour_house_rent_info.*
        FROM
            tour_house_rent_info
        LEFT JOIN
            tour_house_equipment ON tour_house_rent_info.id = tour_house_equipment.house_id AND tour_house_equipment.data_state = ${@com.tourism.common.core.constant.GlobalDeletedFlag@NORMAL}
        <where>
            <include refid="filterRef"/>
            <include refid="com.tourism.webadmin.back.dao.TourHouseEquipmentMapper.inputFilterRef"/>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>

    <!-- 区域条件&房屋条件混合查询 -->
    <select id="getTourHouseRentInfoListWithArea" resultMap="BaseResultMap" parameterType="com.tourism.webadmin.back.model.TourHouseRentInfo">
        SELECT *
        FROM tour_house_rent_info
        JOIN tour_house_area_menu AS tham
        ON tour_house_rent_info.area_id = tham.id
        <where>
            <if test="tourHouseRentInfoFilter != null">
                <if test="tourHouseRentInfoFilter.areaIds != null and tourHouseRentInfoFilter.areaIds.size > 0">
                    AND tham.id IN
                    <foreach collection="tourHouseRentInfoFilter.areaIds" item="areaId" open="(" close=")" separator=",">
                        #{areaId}
                    </foreach>
                </if>
            </if>
            <include refid="com.tourism.webadmin.back.dao.TourHouseRentInfoMapper.inputFilterRef"/>
            AND tour_house_rent_info.data_state = ${@com.tourism.common.core.constant.GlobalDeletedFlag@NORMAL}
            <if test="tourHouseRentInfoFilter.searchString != null and tourHouseRentInfoFilter.searchString != ''">
                and
                (
                tour_house_rent_info.community_name like concat('%',#{tourHouseRentInfoFilter.searchString},'%') or
                tour_house_rent_info.address like concat('%',#{tourHouseRentInfoFilter.searchString},'%') or
                tour_house_rent_info.house_title like concat('%',#{tourHouseRentInfoFilter.searchString},'%')
                )
            </if>
        </where>
        <if test="orderBy != null and orderBy != ''">
            ORDER BY ${orderBy}
        </if>
    </select>
</mapper>
