<?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.ProjectUserEntity">
    <typeAlias alias="userEntity" type="com.house365.beans.entity.UserEntity" />
    <typeAlias alias="projectUserEntity" type="com.house365.beans.entity.ProjectUserEntity" />
    <typeAlias alias="projectEntity" type="com.house365.beans.entity.ProjectEntity" />
    <resultMap id="projectUser" class="com.house365.beans.entity.ProjectUserEntity">
        <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" />
    </resultMap>

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

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

    <!-- 通过ID修改 -->
    <update id="update" parameterClass="projectUserEntity">
        <![CDATA[
	        UPDATE project_user 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#
	        WHERE 
		        id = #id#
	    ]]>
    </update>

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

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

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

    <!-- 用于分页查询抽取的公共的条件 -->
    <sql id="projectUser.queryPage">
        <dynamic prepend="WHERE">
            <!-- 默认不开启实体作为条件 -->
            <!--
            <isNotNull property="entity">
                <isNotEmpty prepend="AND" property="entity.id">
                    <![CDATA[
                    id = #entity.id#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.projectId">
                    <![CDATA[
                    projectId = #entity.projectId#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.projectCode">
                    <![CDATA[
                    projectCode = #entity.projectCode#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.projectName">
                    <![CDATA[
                    projectName = #entity.projectName#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.userId">
                    <![CDATA[
                    userId = #entity.userId#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.userName">
                    <![CDATA[
                    userName = #entity.userName#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.createUserid">
                    <![CDATA[
                    createUserid = #entity.createUserid#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.createUsername">
                    <![CDATA[
                    createUsername = #entity.createUsername#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.createTime">
                    <![CDATA[
                    createTime = #entity.createTime#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.createIp">
                    <![CDATA[
                    createIp = #entity.createIp#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.updateUserid">
                    <![CDATA[
                    updateUserid = #entity.updateUserid#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.updateUsername">
                    <![CDATA[
                    updateUsername = #entity.updateUsername#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.updateTime">
                    <![CDATA[
                    updateTime = #entity.updateTime#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.updateIp">
                    <![CDATA[
                    updateIp = #entity.updateIp#
                    ]]>
                </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.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>
            </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_user
        <include refid="projectUser.queryPage" />
    </select>

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

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

    <!-- 查询所有记录 -->
    <select id="queryUserByPrjId" resultClass="userEntity">
        SELECT u.id, u.name,u.realName,u.mobile,u.deptId,u.deptUrlPath,u.position
        <![CDATA[
		    FROM project_user  pu join user u on pu.userId = u.id where pu.projectId = #projectId# 
	    ]]>
    </select>

    <!--根据楼盘ID获取用户信息-->
    <select id="getProjectUser" resultClass="userEntity" parameterClass="java.util.LinkedList">
        SELECT u.id, u.name,u.realName,u.mobile,u.deptId,u.deptUrlPath,u.position,pu.projectId
        <![CDATA[
		     FROM project_user  pu join user u on pu.userId = u.id 
	    ]]>
        <dynamic prepend="where">
            pu.projectId in
            <iterate open="(" close=")" conjunction=",">
                <![CDATA[  #paths[]# ]]>
            </iterate>
        </dynamic>
    </select>
    <!--根据用户ID获取楼盘信息-->
    <select id="getProjectByUserId" parameterClass="java.util.HashMap" resultClass="projectEntity">
        SELECT p.id, p.contractCode, p.projectCode, p.projectName, p.buildingId, p.buildingName, p.status,
        p.manager, p.type,p.business, p.kfs,p.city,p.cityName,p.district, p.address, p.startTime,p.createTime
        <![CDATA[
		     FROM project_user  pu join project p on pu.projectId = p.id where pu.userId = #userId#
	    ]]>
        <isNotNull property="cityCode">
            and p.city = #cityCode#
        </isNotNull>
        order by p.createTime desc
    </select>
    <select id="getProjectByUserIdAndStatus" parameterClass="java.util.HashMap" resultClass="projectEntity">
        SELECT p.id, p.contractCode, p.projectCode, p.projectName, p.buildingId, p.buildingName, p.status,
        p.manager, p.type,p.business, p.kfs,p.city,p.cityName,p.district, p.address, p.startTime,p.createTime
        <![CDATA[
		     FROM project_user  pu join project p on pu.projectId = p.id where pu.userId = #userId#
	    ]]>
        <isNotNull property="cityCode">
            and p.city = #cityCode#
        </isNotNull>
        <isNotNull property="showStatus">
            and p.showStatus = #showStatus#
        </isNotNull>
        order by p.createTime desc
    </select>

</sqlMap>
