<?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.WeChatPermissionEntity">
    <typeAlias alias="weChatPermissionEntity"
               type="com.house365.beans.entity.WeChatPermissionEntity"/>
    <resultMap id="weChatPermission"
               class="com.house365.beans.entity.WeChatPermissionEntity">
        <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"/>
    </resultMap>

    <!-- 用于select查询公用抽取的列 -->
    <sql id="weChatPermission.columns">
	    <![CDATA[



        id, code, name, parentId, orderNum, resource, type,special,hierarchy, createUserid, createUsername, createTime, createIp, updateUserid, updateUsername, updateTime, updateIp



        ]]>
	</sql>

    <!-- 新增记录 -->
    <insert id="save" parameterClass="weChatPermissionEntity">
        <![CDATA[
	        INSERT INTO wechat_permission (
	        	id, 
	        	code, 
	        	name, 
	        	parentId, 
	        	orderNum, 
	        	resource, 
	        	type, 
	        	special,
	        	hierarchy,
	        	createUserid, 
	        	createUsername, 
	        	createTime, 
	        	createIp, 
	        	updateUserid, 
	        	updateUsername, 
	        	updateTime, 
	        	updateIp
	        ) VALUES (
	        	#id#, 
	        	#code#, 
	        	#name#, 
	        	#parentId#, 
	        	#orderNum#, 
	        	#resource#, 
	        	#type#, 
	        	#special#,
	        	#hierarchy#,
	        	#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="weChatPermissionEntity">
        UPDATE wechat_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>
        </dynamic>
        WHERE
        id = #id#
    </update>

    <!-- 通过ID删除 -->
    <delete id="delete">
	    <![CDATA[



	        DELETE FROM wechat_permission WHERE
	        id = #id#



        ]]>
	</delete>

    <!-- 通过ID列表批量删除 -->
    <delete id="deleteByIds" parameterClass="java.lang.String">
	    <![CDATA[



	        DELETE FROM wechat_permission WHERE
	        id IN ($ids$)



        ]]>
	</delete>

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

    <!-- 用于分页查询抽取的公共的条件 -->
    <sql id="weChatPermission.queryPage">
        <dynamic prepend="WHERE">
            <!-- 默认不开启实体作为条件 -->
            <!--
            <isNotNull property="entity">
                <isNotEmpty prepend="AND" property="entity.id">
                    <![CDATA[
                    id = #entity.id#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.code">
                    <![CDATA[
                    code = #entity.code#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.name">
                    <![CDATA[
                    name = #entity.name#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.parentId">
                    <![CDATA[
                    parentId = #entity.parentId#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.order">
                    <![CDATA[
                    order = #entity.order#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.resource">
                    <![CDATA[
                    resource = #entity.resource#
                    ]]>
                </isNotEmpty>
                <isNotEmpty prepend="AND" property="entity.type">
                    <![CDATA[
                    type = #entity.type#
                    ]]>
                </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.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>
            </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 wechat_permission
        <include refid="weChatPermission.queryPage"/>
    </select>

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

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

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

    <select id="getPermissionsByUserId" resultClass="weChatPermissionEntity">
		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
	    <![CDATA[
	        FROM wechat_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="getPermissionsByParentId" resultMap="weChatPermission">
        SELECT
        <include refid="weChatPermission.columns"/>
        <![CDATA[
		    FROM wechat_permission  where  parentId  = #parentId# order by parentId ,orderNum asc
	    ]]>
    </select>
</sqlMap>