<?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.MarketBuildingRelEntity">
	<typeAlias alias="marketBuildingRelEntity"
		type="com.house365.beans.entity.MarketBuildingRelEntity" />
	<resultMap id="marketBuildingRel"
		class="com.house365.beans.entity.MarketBuildingRelEntity">
		<result property="id" column="id" />
		<result property="buildingId" column="building_id" />
		<result property="activeId" column="active_id" />
	</resultMap>

	<!-- 用于select查询公用抽取的列 -->
	<sql id="marketBuildingRel.columns">
	    <![CDATA[
        id, building_id, active_id
	    ]]>
	</sql>

	<!-- 新增记录 -->
	<insert id="save" parameterClass="marketBuildingRelEntity">
	    <![CDATA[
	        INSERT INTO market_building_rel (
	        	id, 
	        	building_id, 
	        	active_id
	        ) VALUES (
	        	#id#, 
	        	#buildingId#, 
	        	#activeId#
	        )
	    ]]>
		<selectKey resultClass="java.lang.Integer" type="post"
			keyProperty="id">
			SELECT LAST_INSERT_ID()
		</selectKey>
	</insert>

	<!-- 通过ID修改 -->
	<update id="update" parameterClass="marketBuildingRelEntity">
	    <![CDATA[
	        UPDATE market_building_rel SET
		        building_id = #buildingId#, 
		        active_id = #activeId#
	        WHERE 
		        id = #id#
	    ]]>
	</update>

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

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

    <delete id="deleteByActiveId" parameterClass="java.lang.Integer">
        <![CDATA[
	        DELETE FROM market_building_rel WHERE active_id = ($id$)
	    ]]>
    </delete>

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

	<!-- 用于分页查询抽取的公共的条件 -->
	<sql id="marketBuildingRel.queryPage">
		<dynamic prepend="WHERE">
			<!-- 默认不开启实体作为条件 -->
		    <isNotNull property="searchFilters">
				<isNotEmpty prepend="AND" property="searchFilters.id">
		            <![CDATA[
					id $searchFilters.id.operation$ #searchFilters.id.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.buildingId">
		            <![CDATA[
					building_id $searchFilters.buildingId.operation$ #searchFilters.buildingId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.activeId">
		            <![CDATA[
					active_id $searchFilters.activeId.operation$ #searchFilters.activeId.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 market_building_rel
		<include refid="marketBuildingRel.queryPage" />
	</select>

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

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

    <select id="getByActid" resultMap="marketBuildingRel">
        SELECT
        <include refid="marketBuildingRel.columns" />
        <![CDATA[
		    FROM market_building_rel where active_id = $activeId$
	    ]]>
    </select>


</sqlMap>
