<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
		PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
		"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- 不使用namespace的话sql搜索定位会比较方便 -->
<mapper namespace="com.blueland.dao.AuthDao">
	<resultMap id="RM_AuthEntity" type="com.blueland.bo.AuthEntity">
        <result property="authId" column="AUTH_ID"/>
        <result property="authType" column="AUTH_TYPE"/>
        <result property="menuId" column="MENU_ID"/>
        <result property="appId" column="APP_ID"/>
        <result property="status" column="STATUS"/>
        <result property="creatorId" column="CREATOR_ID"/>
        <result property="createDate" column="CREATE_DATE"/>
        <result property="updatorId" column="UPDATOR_ID"/>
        <result property="updateDate" column="UPDATE_DATE"/>
        <result property="roleId" column="ROLE_ID"/>
	</resultMap>

	<sql id="AuthEntityColumns">
        	AUTH_ID,
        	AUTH_TYPE,
        	MENU_ID,
        	APP_ID,
        	STATUS,
        	CREATOR_ID,
        	CREATE_DATE,
        	UPDATOR_ID,
        	UPDATE_DATE,
        	ROLE_ID
	</sql>
	
	<sql id="AuthEntityQueryColumns">
        	AUTH_ID,
        	AUTH_TYPE,
        	MENU_ID,
        	APP_ID,
        	STATUS,
        	CREATOR_ID,
        	CREATE_DATE,
        	UPDATOR_ID,
        	UPDATE_DATE,
        	ROLE_ID
	</sql>
	<!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql -->
	<insert id="insert" useGeneratedKeys="false" keyProperty="authId">
        INSERT INTO t_pf_auth (<include refid="AuthEntityColumns"/>) VALUES (
        	#{authId,jdbcType=VARCHAR},
        	#{authType,jdbcType=VARCHAR},
        	#{menuId,jdbcType=VARCHAR},
        	#{appId,jdbcType=VARCHAR},
        	#{status,jdbcType=VARCHAR},
        	#{creatorId,jdbcType=VARCHAR},
        	#{createDate,jdbcType=TIMESTAMP},
        	#{updatorId,jdbcType=VARCHAR},
        	#{updateDate,jdbcType=TIMESTAMP},
        	#{roleId,jdbcType=VARCHAR}
        )
	</insert>

    <delete id="deleteRole">
        DELETE FROM t_pf_auth WHERE
        role_id = #{roleId,jdbcType=VARCHAR} 
        AND app_id = #{appId,jdbcType=VARCHAR} 
    </delete>
    
    <select id="findAuthByMenuIdRoleId" resultType="java.lang.Integer">
		select count(*) from t_pf_auth t1
		where t1.status = 'y'
		<if test="@Ognl@isNotBlank(menuId)">and t1.menu_id = #{menuId}</if>
		<if test="@Ognl@isNotBlank(roleId)">and t1.role_id = #{roleId}</if>
	</select>
    
    <select id="get" resultMap="RM_AuthEntity">
		SELECT <include refid="AuthEntityQueryColumns"/>
	    FROM t_pf_auth 
        WHERE 
	        AUTH_ID = #{authId,jdbcType=VARCHAR} 
	</select>
	
    <!--
    	分页查询已经使用Dialect进行分页,也可以不使用Dialect直接编写分页
    	因为分页查询将传 offset,pageSize,lastRows 三个参数,不同的数据库可以根于此三个参数属性应用不同的分页实现
    -->
    <select id="findList" resultMap="RM_AuthEntity">
    	SELECT <include refid="AuthEntityQueryColumns"/>
	    FROM t_pf_auth 
		<include refid="findList_where"/>
		
		<if test="@Ognl@isNotEmpty(bo.sortColumns)">
			ORDER BY ${bo.sortColumns}
		</if>
    </select>

	<select id="findPage" resultMap="RM_AuthEntity">
		SELECT <include refid="AuthEntityQueryColumns"/>
		FROM t_pf_auth
		<include refid="findList_where"/>

		<if test="@Ognl@isNotEmpty(bo.sortColumns)">
			ORDER BY ${bo.sortColumns}
		</if>
	</select>
	
	<sql id="findList_where">
		<!-- ognl访问静态方法的表达式 为@class@method(args),以下为调用rapid中的Ognl.isNotEmpty()方法,还有其它方法如isNotBlank()可以使用，具体请查看Ognl类 -->
		<where>	      				
	       <if test="@Ognl@isNotEmpty(bo.authId)">
				AND AUTH_ID = #{bo.authId,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.authType)">
				AND AUTH_TYPE = #{bo.authType,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.menuId)">
				AND MENU_ID = #{bo.menuId,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.appId)">
				AND APP_ID = #{bo.appId,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.status)">
				AND STATUS = #{bo.status,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.creatorId)">
				AND CREATOR_ID = #{bo.creatorId,jdbcType=VARCHAR}
			</if>
	       
	       <if test="@Ognl@isNotEmpty(bo.updatorId)">
				AND UPDATOR_ID = #{bo.updatorId,jdbcType=VARCHAR}
			</if>
	       
	       <if test="@Ognl@isNotEmpty(bo.roleId)">
				AND ROLE_ID = #{bo.roleId,jdbcType=VARCHAR}
			</if>
		</where>
	</sql>
    
    <!-- 根据角色ID获取 -->
    <select id="getListByRoleId" resultMap="RM_AuthEntity">
    	SELECT 
    		<include refid="AuthEntityQueryColumns"/>
	    FROM t_pf_auth 
		where status = 'y'
		<if test="@Ognl@isNotEmpty(roleId)">
			AND ROLE_ID = #{roleId,jdbcType=VARCHAR}
		</if>
    </select>

	<delete id="deleteAuthByAppId">
		delete from t_pf_auth where app_id = #{appId}
	</delete>

	<select id="findListByAppId" resultMap="RM_AuthEntity">
		SELECT <include refid="AuthEntityQueryColumns"/>
		FROM t_pf_auth
		where STATUS = "Y" and APP_ID = #{appId}
	</select>
	
</mapper>

