<?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.PermissionEntity">
    <typeAlias alias="permissionEntity" type="com.house365.beans.entity.PermissionEntity" />
    <resultMap id="permission" class="com.house365.beans.entity.PermissionEntity">
        <result property="id" column="id" />
        <result property="code" column="code" />
        <result property="name" column="name" />
        <result property="parentId" column="parentId" />
        <result property="orderNum" column="orderNum" />
        <result property="resource" column="resource" />
        <result property="type" column="type" />
        <result property="special" column="special" />
        <result property="hierarchy" column="hierarchy" />
        <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="isShow" column="is_show" />
        <result property="permission" column="permission" />
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="permission.columns">
        <![CDATA[
        id, code, name, parentId, orderNum, resource, type,special,hierarchy, createUserid,
        createUsername, createTime, createIp, updateUserid, updateUsername, updateTime, updateIp,
        is_show, permission
	    ]]>
    </sql>

    <!-- 新增记录 -->
    <insert id="save" parameterClass="permissionEntity">
        <![CDATA[
	        INSERT INTO permission (
	        	id, 
	        	code, 
	        	name, 
	        	parentId, 
	        	orderNum, 
	        	resource, 
	        	type, 
	        	special,
	        	hierarchy,
	        	createUserid, 
	        	createUsername, 
	        	createTime, 
	        	createIp, 
	        	updateUserid, 
	        	updateUsername, 
	        	updateTime, 
	        	updateIp,
	        	is_show, permission
	        ) VALUES (
	        	#id#, 
	        	#code#, 
	        	#name#, 
	        	#parentId#, 
	        	#orderNum#, 
	        	#resource#, 
	        	#type#, 
	        	#special#,
	        	#hierarchy#,
	        	#createUserid#, 
	        	#createUsername#, 
	        	#createTime#, 
	        	#createIp#, 
	        	#updateUserid#, 
	        	#updateUsername#, 
	        	#updateTime#, 
	        	#updateIp#,
	        	#isShow#, #permission#
	        )
	    ]]>
        <selectKey resultClass="java.lang.Integer" type="post" keyProperty="id">
            SELECT LAST_INSERT_ID()
        </selectKey>
    </insert>

    <!-- 通过ID修改 -->
    <update id="update" parameterClass="permissionEntity">
        UPDATE permission
        <dynamic prepend="SET">
            <isNotEmpty prepend="," property="code">
                <![CDATA[
					code = #code#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="name">
                <![CDATA[
					name = #name#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="parentId">
                <![CDATA[
					parentId = #parentId#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="orderNum">
                <![CDATA[
					orderNum = #orderNum#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="resource">
                <![CDATA[
					resource = #resource#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="type">
                <![CDATA[
					type = #type#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="special">
                <![CDATA[
					special = #special#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="hierarchy">
                <![CDATA[
					hierarchy = #hierarchy#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="createUserid">
                <![CDATA[
					createUserid = #createUserid#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="createUsername">
                <![CDATA[
					createUsername = #createUsername#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="createTime">
                <![CDATA[
					createTime = #createTime#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="createIp">
                <![CDATA[
					createIp = #createIp#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="updateUserid">
                <![CDATA[
					updateUserid = #updateUserid#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="updateUsername">
                <![CDATA[
					updateUsername = #updateUsername#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="updateTime">
                <![CDATA[
					updateTime = #updateTime#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="updateIp">
                <![CDATA[
					updateIp = #updateIp#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="isShow">
                <![CDATA[
					is_show = #isShow#
		            ]]>
            </isNotEmpty>
            <isNotEmpty prepend="," property="permission">
                <![CDATA[
					permission = #permission#
		            ]]>
            </isNotEmpty>
        </dynamic>
        WHERE
        id = #id#
    </update>

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

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

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

    <!-- 用于分页查询抽取的公共的条件 -->
    <sql id="permission.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.code">
                    <![CDATA[
					code $searchFilters.code.operation$ #searchFilters.code.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.name">
                    <![CDATA[
					name $searchFilters.name.operation$ #searchFilters.name.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.parentId">
                    <![CDATA[
					parentId $searchFilters.parentId.operation$ #searchFilters.parentId.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.orderNum">
                    <![CDATA[
					orderNum $searchFilters.orderNum.operation$ #searchFilters.orderNum.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.resource">
                    <![CDATA[
					resource $searchFilters.resource.operation$ #searchFilters.resource.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.type">
                    <![CDATA[
					type $searchFilters.type.operation$ #searchFilters.type.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.special">
                    <![CDATA[
					special $searchFilters.special.operation$ #searchFilters.special.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.hierarchy">
                    <![CDATA[
					hierarchy $searchFilters.hierarchy.operation$ #searchFilters.hierarchy.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.isShow">
                    <![CDATA[
					is_show $searchFilters.isShow.operation$ #searchFilters.isShow.value#
		            ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="searchFilters.permission">
                    <![CDATA[
					permission $searchFilters.permission.operation$ #searchFilters.permission.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 permission
        <include refid="permission.queryPage" />
    </select>

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

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

    <!-- 查询所有记录 -->
    <select id="queryAllByOrder" resultMap="permission">
        SELECT
        <include refid="permission.columns" />
        <![CDATA[
		    FROM permission  group by parentId  order by orderNum asc
	    ]]>
    </select>

    <select id="getPermissionsByUserId" resultClass="permissionEntity">
        SELECT
        p.id, p.code, p.name, p.parentId, p.orderNum, p.resource, p.type,p.special,p.hierarchy, p.createUserid,
        p.createUsername, p.createTime,
        p.is_show, p.permission
        <![CDATA[
	        FROM permission  p left join role_permission rp on p.id = rp.permissionId 
	        left join user_role ur on  ur.roleId = rp.roleId
	        where ur.userId = #userId#
	    ]]>
    </select>

    <select id="getFunctionPermissionsByUserId" resultClass="permissionEntity">
        SELECT
        p.id, p.code, p.name, p.parentId, p.orderNum, p.resource, p.type,p.special,p.hierarchy, p.createUserid,
        p.createUsername, p.createTime,
        p.is_show, p.permission
        <![CDATA[
	        FROM permission  p left join role_permission rp on p.id = rp.permissionId
	        left join user_role ur on  ur.roleId = rp.roleId
	        where ur.userId = #userId#
	        and p.type = '2'

        ]]>
    </select>
</sqlMap>
