<?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.ProjectSalerEntity">
	<typeAlias alias="projectSalerEntity"
			   type="com.house365.beans.entity.ProjectSalerEntity"/>
	<resultMap id="projectSaler"
			   class="com.house365.beans.entity.ProjectSalerEntity">
		<result property="id" column="id"/>
		<result property="projectId" column="projectId"/>
		<result property="projectCode" column="projectCode"/>
		<result property="projectName" column="projectName"/>
		<result property="userId" column="userId"/>
		<result property="userName" column="userName"/>
		<result property="createUserid" column="createUserid"/>
		<result property="createUsername" column="createUsername"/>
		<result property="createTime" column="createTime"/>
		<result property="createIp" column="createIp"/>
		<result property="updateUserid" column="updateUserid"/>
		<result property="updateUsername" column="updateUsername"/>
		<result property="updateTime" column="updateTime"/>
		<result property="updateIp" column="updateIp"/>
		<result property="channel" column="channel"/>
		<result property="city" column="city"/>
	</resultMap>

	<!-- 用于select查询公用抽取的列 -->
	<sql id="projectSaler.columns">
		<![CDATA[
        id, projectId, projectCode, projectName, userId, userName, createUserid, createUsername, createTime, createIp, updateUserid, updateUsername, updateTime, updateIp, channel, city
	    ]]>
	</sql>

	<!-- 新增记录 -->
	<insert id="save" parameterClass="projectSalerEntity">
		<![CDATA[
	        INSERT INTO project_saler (
	        	id, 
	        	projectId, 
	        	projectCode, 
	        	projectName, 
	        	userId, 
	        	userName, 
	        	createUserid, 
	        	createUsername, 
	        	createTime, 
	        	createIp, 
	        	updateUserid, 
	        	updateUsername, 
	        	updateTime, 
	        	updateIp, 
	        	channel, 
	        	city
	        ) VALUES (
	        	#id#, 
	        	#projectId#, 
	        	#projectCode#, 
	        	#projectName#, 
	        	#userId#, 
	        	#userName#, 
	        	#createUserid#, 
	        	#createUsername#, 
	        	#createTime#, 
	        	#createIp#, 
	        	#updateUserid#, 
	        	#updateUsername#, 
	        	#updateTime#, 
	        	#updateIp#, 
	        	#channel#, 
	        	#city#
	        )
	    ]]>
		<selectKey resultClass="java.lang.Integer" type="post"
				   keyProperty="id">
			SELECT LAST_INSERT_ID()
		</selectKey>
	</insert>

	<!-- 通过ID修改 -->
	<update id="update" parameterClass="projectSalerEntity">
		<![CDATA[
	        UPDATE project_saler SET
		        projectId = #projectId#, 
		        projectCode = #projectCode#, 
		        projectName = #projectName#, 
		        userId = #userId#, 
		        userName = #userName#, 
		        createUserid = #createUserid#, 
		        createUsername = #createUsername#, 
		        createTime = #createTime#, 
		        createIp = #createIp#, 
		        updateUserid = #updateUserid#, 
		        updateUsername = #updateUsername#, 
		        updateTime = #updateTime#, 
		        updateIp = #updateIp#, 
		        channel = #channel#, 
		        city = #city#
	        WHERE 
		        id = #id#
	    ]]>
	</update>

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

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

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

	<!-- 用于分页查询抽取的公共的条件 -->
	<sql id="projectSaler.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.projectId">
					<![CDATA[
					projectId $searchFilters.projectId.operation$ #searchFilters.projectId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.projectCode">
					<![CDATA[
					projectCode $searchFilters.projectCode.operation$ #searchFilters.projectCode.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.projectName">
					<![CDATA[
					projectName $searchFilters.projectName.operation$ #searchFilters.projectName.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.userId">
					<![CDATA[
					userId $searchFilters.userId.operation$ #searchFilters.userId.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.userName">
					<![CDATA[
					userName $searchFilters.userName.operation$ #searchFilters.userName.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createUserid">
					<![CDATA[
					createUserid $searchFilters.createUserid.operation$ #searchFilters.createUserid.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createUsername">
					<![CDATA[
					createUsername $searchFilters.createUsername.operation$ #searchFilters.createUsername.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createTime">
					<![CDATA[
					createTime $searchFilters.createTime.operation$ #searchFilters.createTime.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.createIp">
					<![CDATA[
					createIp $searchFilters.createIp.operation$ #searchFilters.createIp.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.updateUserid">
					<![CDATA[
					updateUserid $searchFilters.updateUserid.operation$ #searchFilters.updateUserid.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.updateUsername">
					<![CDATA[
					updateUsername $searchFilters.updateUsername.operation$ #searchFilters.updateUsername.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.updateTime">
					<![CDATA[
					updateTime $searchFilters.updateTime.operation$ #searchFilters.updateTime.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.updateIp">
					<![CDATA[
					updateIp $searchFilters.updateIp.operation$ #searchFilters.updateIp.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.channel">
					<![CDATA[
					channel $searchFilters.channel.operation$ #searchFilters.channel.value#
		            ]]>
				</isNotEmpty>
				<isNotEmpty prepend="AND" property="searchFilters.city">
					<![CDATA[
					city $searchFilters.city.operation$ #searchFilters.city.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 project_saler
		<include refid="projectSaler.queryPage"/>
	</select>

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

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

	<!-- 查询所有记录 -->
	<select id="queryUserByPrjId" resultClass="userEntity">
		<![CDATA[
		    SELECT u.id, u.name,u.realName,u.mobile,u.deptId,u.deptUrlPath,u.position
                  FROM project_saler pu , user u ,project p where pu.userId = u.id and pu.projectId = p.buildingid
                  and pu.channel=p.type and p.id = #projectId# and p.city = #city#
	    ]]>
	</select>

	<delete id="deleteByProjectId">
		<![CDATA[
	        DELETE FROM project_saler WHERE projectId = (select buildingid from project where id = #projectId#) and channel=
	        (select type from project where id = #projectId#)
	    ]]>
	</delete>

    <delete id="deleteByUserId">
	        DELETE FROM project_saler WHERE userId = #userId#
    </delete>

	<select id="queryAllList" resultMap="projectSaler">
		SELECT
		<include refid="projectSaler.columns"/>
		<![CDATA[
	        FROM project_saler
	        where  updateIp IS NULL
	    ]]>
	</select>

	<update id="updateStatus" parameterClass="java.lang.Integer">
		<![CDATA[
	        UPDATE project_saler SET
		        updateIp = '1'
	        WHERE
		        id = #id#
	    ]]>
	</update>

</sqlMap>
