<?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.CustomerUserEntity">
	<typeAlias alias="customerUserEntity"
			   type="com.house365.beans.entity.CustomerUserEntity" />
	<resultMap id="customerUser"
			   class="com.house365.beans.entity.CustomerUserEntity">
		<result property="id" column="id" />
		<result property="customerId" column="customer_id" />
		<result property="userId" column="user_id" />
		<result property="star" column="star" />
		<result property="createTime" column="create_time" />
	</resultMap>

	<!-- 用于select查询公用抽取的列 -->
	<sql id="customerUser.columns">
	    <![CDATA[
        id, customer_id, user_id, star, create_time
	    ]]>
	</sql>

	<!-- 新增记录 -->
	<insert id="save" parameterClass="customerUserEntity">
		<![CDATA[
	        INSERT INTO customer_user (
	        	id, 
	        	customer_id, 
	        	user_id, 
	        	star, 
	        	create_time
	        ) VALUES (
	        	#id#, 
	        	#customerId#, 
	        	#userId#, 
	        	#star#, 
	        	#createTime#
	        )
	    ]]>
		<selectKey resultClass="java.lang.Integer" type="post"
				   keyProperty="id">
			SELECT LAST_INSERT_ID()
		</selectKey>
	</insert>

	<!-- 通过ID修改 -->
	<update id="update" parameterClass="customerUserEntity">
	    <![CDATA[
	        UPDATE customer_user SET
		        customer_id = #customerId#, 
		        user_id = #userId#, 
		        star = #star#, 
		        create_time = #createTime#
	        WHERE 
		        id = #id#
	    ]]>
	</update>

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

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

	<delete id="removeStar">
        <![CDATA[
	        DELETE FROM customer_user WHERE customer_id = #customerId# and user_id = #userId#
	    ]]>
    </delete>

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

	<!-- 用于分页查询抽取的公共的条件 -->
	<sql id="customerUser.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.customerId">
					<![CDATA[
					customer_id $searchFilters.customerId.operation$ #searchFilters.customerId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.userId">
					<![CDATA[
					(user_id = $searchFilters.userId.value$ or user_id in (select id from user where depturlpath like (select concat(depturlpath,'/%') from user where id = $searchFilters.userId.value$)))
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.star">
					<![CDATA[
					star $searchFilters.star.operation$ #searchFilters.star.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createTime">
					<![CDATA[
					create_time $searchFilters.createTime.operation$ #searchFilters.createTime.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.customerIds">
					<![CDATA[ customer_id in ]]>
					<iterate prepend="" property="searchFilters.customerIds.value" open="(" close=")" conjunction=",">
						<![CDATA[ #searchFilters.customerIds.value[]# ]]>
					</iterate>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.userIds">
					<![CDATA[ user_id in ]]>
					<iterate prepend="" property="searchFilters.userIds.value" open="(" close=")" conjunction=",">
						<![CDATA[ #searchFilters.userIds.value[]# ]]>
					</iterate>
				</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 customer_user
		<include refid="customerUser.queryPage" />
	</select>

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

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


</sqlMap>
