<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" 
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="com.house365.beans.entity.HouseInfoEntity">
	<typeAlias alias="houseInfoEntity"
		type="com.house365.beans.entity.HouseInfoEntity" />
	<resultMap id="houseInfo"
		class="com.house365.beans.entity.HouseInfoEntity">
		<result property="id" column="id" />
		<result property="houseId" column="house_id" />
		<result property="houseName" column="house_name" />
		<result property="housePhone" column="house_phone" />
		<result property="cityId" column="city_id" />
		<result property="cityName" column="city_name" />
		<result property="isDeleted" column="is_deleted" />
		<result property="channel" column="channel" />
		<result property="cityKey" column="city_key" />
	</resultMap>

	<!-- 用于select查询公用抽取的列 -->
	<sql id="houseInfo.columns">
	    <![CDATA[
        id, house_id, house_name, house_phone, city_id, city_name, is_deleted, channel, city_key
	    ]]>
	</sql>

	<!-- 新增记录 -->
	<insert id="save" parameterClass="houseInfoEntity">
	    <![CDATA[
	        INSERT INTO house_info (
	        	id, 
	        	house_id, 
	        	house_name, 
	        	house_phone, 
	        	city_id, 
	        	city_name, 
	        	is_deleted, 
	        	channel, 
	        	city_key
	        ) VALUES (
	        	#id#, 
	        	#houseId#, 
	        	#houseName#, 
	        	#housePhone#, 
	        	#cityId#, 
	        	#cityName#, 
	        	#isDeleted#, 
	        	#channel#, 
	        	#cityKey#
	        )
	    ]]>
		<selectKey resultClass="java.lang.Integer" type="post"
			keyProperty="id">
			SELECT LAST_INSERT_ID()
		</selectKey>
	</insert>

	<!-- 通过ID修改 -->
	<update id="update" parameterClass="houseInfoEntity">
	    <![CDATA[
	        UPDATE house_info SET
		        house_id = #houseId#, 
		        house_name = #houseName#, 
		        house_phone = #housePhone#, 
		        city_id = #cityId#, 
		        city_name = #cityName#, 
		        is_deleted = #isDeleted#, 
		        channel = #channel#, 
		        city_key = #cityKey#
	        WHERE 
		        id = #id#
	    ]]>
	</update>

	<!-- 通过ID删除 -->
	<delete id="delete">
	    <![CDATA[
	        DELETE FROM house_info WHERE
	        id = #id#
	    ]]>
	</delete>

	<!-- 通过ID列表批量删除 -->
	<delete id="deleteByIds" parameterClass="java.lang.String">
	    <![CDATA[
	        DELETE FROM house_info WHERE
	        id IN ($ids$)
	    ]]>
	</delete>

	<!-- 通过ID查询 -->
	<select id="getById" resultMap="houseInfo">
		SELECT
		<include refid="houseInfo.columns" />
	    <![CDATA[
	        FROM house_info 
	        WHERE 
		        id = #id#
	    ]]>
	</select>

	<!-- 用于分页查询抽取的公共的条件 -->
	<sql id="houseInfo.queryPage">
		<dynamic prepend="WHERE">
			<!-- 默认不开启实体作为条件 -->
			<!-- 
		    <isNotNull property="entity">
				<isNotEmpty prepend="AND" property="entity.id">
		            <![CDATA[
					id = #entity.id#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.houseId">
		            <![CDATA[
					house_id = #entity.houseId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.houseName">
		            <![CDATA[
					house_name = #entity.houseName#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.housePhone">
		            <![CDATA[
					house_phone = #entity.housePhone#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.cityId">
		            <![CDATA[
					city_id = #entity.cityId#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.cityName">
		            <![CDATA[
					city_name = #entity.cityName#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.isDeleted">
		            <![CDATA[
					is_deleted = #entity.isDeleted#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.channel">
		            <![CDATA[
					channel = #entity.channel#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="entity.cityKey">
		            <![CDATA[
					city_key = #entity.cityKey#
		            ]]>
				</isNotEmpty>
			</isNotNull>
			 -->
		    <isNotNull property="searchFilters">
				<isNotEmpty prepend="AND" property="searchFilters.id">
		            <![CDATA[
					id $searchFilters.id.operation$ #searchFilters.id.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.houseId">
		            <![CDATA[
					house_id $searchFilters.houseId.operation$ #searchFilters.houseId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.houseName">
		            <![CDATA[
					house_name $searchFilters.houseName.operation$ #searchFilters.houseName.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.housePhone">
		            <![CDATA[
					house_phone $searchFilters.housePhone.operation$ #searchFilters.housePhone.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.cityId">
		            <![CDATA[
					city_id $searchFilters.cityId.operation$ #searchFilters.cityId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.cityName">
		            <![CDATA[
					city_name $searchFilters.cityName.operation$ #searchFilters.cityName.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.isDeleted">
		            <![CDATA[
					is_deleted $searchFilters.isDeleted.operation$ #searchFilters.isDeleted.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.channel">
		            <![CDATA[
					channel $searchFilters.channel.operation$ #searchFilters.channel.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.cityKey">
		            <![CDATA[
					city_key $searchFilters.cityKey.operation$ #searchFilters.cityKey.value#
		            ]]>
				</isNotEmpty>
			</isNotNull>
		</dynamic>
		<dynamic prepend="ORDER BY">
			<isNotNull property="orderColumn">
		        <![CDATA[
				$orderColumn$
				]]>
				<isNotNull property="orderMode">
				$orderMode$
				</isNotNull>
			</isNotNull>
		</dynamic>
	</sql>

	<!-- 为分页查询符合条件的记录数 -->
	<select id="queryByPage_count" resultClass="int">
		SELECT count(*) FROM house_info
		<include refid="houseInfo.queryPage" />
	</select>

	<!-- 分页查询指定页记录 -->
	<select id="queryByPage" resultMap="houseInfo">
		SELECT
		<include refid="houseInfo.columns" />
	    <![CDATA[
	        FROM house_info 
	    ]]>
		<include refid="houseInfo.queryPage" />
		<![CDATA[
		    LIMIT #paging.startRecord#, #paging.pageSize#
        ]]>
	</select>

	<!-- 查询所有记录 -->
	<select id="queryAll" resultMap="houseInfo">
		SELECT
		<include refid="houseInfo.columns" />
	    <![CDATA[
		    FROM house_info
	    ]]>
	    <include refid="houseInfo.queryPage" />
	</select>

    <select id="getHouseByHouseId" resultMap="houseInfo">
        SELECT
        <include refid="houseInfo.columns" />
        <![CDATA[
		    FROM house_info where house_id = $houseId$
	    ]]>
    </select>

    <select id="getHouseInfoListByActiveId" resultMap="houseInfo">
        SELECT
        <include refid="houseInfo.columns" />
        <![CDATA[
		    FROM house_info where house_id in (select building_id from market_building_rel where active_id = $activeId$)
	    ]]>
    </select>


</sqlMap>
