<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">


<!-- 不使用namespace的话sql搜索定位会比较方便 -->
<mapper namespace="WFRole">

	<resultMap id="RM_WFRole" type="com.blueland.bpm.identity.model.Role">
        <result property="roleId" column="ROLE_ID"/>
        <result property="roleName" column="ROLE_NAME"/>
        <result property="roleType" column="ROLE_TYPE"/>
        <result property="roleSort" column="ROLE_SORT"/>
        <result property="roleAppId" column="ROLE_APP_ID"/>
        <result property="roleStatus" column="ROLE_STATUS"/>
        <result property="roleCreateTime" column="ROLE_CREATE_TIME"/>
        <result property="roleCreator" column="ROLE_CREATOR"/>
        <result property="remark" column="REMARK"/>
	</resultMap>
	
	<!-- 用于select查询公用抽取的列 -->
	<sql id="WFRole.columns">
	    <![CDATA[
		r.ROLE_ID,r.ROLE_NAME,r.ROLE_TYPE,r.ROLE_SORT,r.ROLE_APP_ID,r.ROLE_STATUS,r.ROLE_CREATE_TIME,r.ROLE_CREATOR,r.REMARK
	    ]]>
	</sql>

    
    <select id="WFRole.getById" resultMap="RM_WFRole">
		SELECT <include refid="WFRole.columns" />
	    <![CDATA[
		    FROM T_PUB_ROLE r 
	        WHERE 
		        ROLE_ID = #{id} 
	    ]]>
	</select>
	
	<sql id="WFRole.findPage.where">
		<!-- ognl访问静态方法的表达式 为@class@method(args),以下为调用rapid中的Ognl.isNotEmpty()方法,还有其它方法如isNotBlank()可以使用，具体请查看Ognl类 -->
		<where>	      				
	       <if test="@Ognl@isNotEmpty(roleId)">
				AND ROLE_ID = #{roleId}
			</if>
	       <if test="@Ognl@isNotEmpty(roleName)">
				AND ROLE_NAME = #{roleName}
			</if>
	       <if test="@Ognl@isNotEmpty(roleType)">
				AND ROLE_TYPE = #{roleType}
			</if>
	       <if test="@Ognl@isNotEmpty(roleSort)">
				AND ROLE_SORT = #{roleSort}
			</if>
	       <if test="@Ognl@isNotEmpty(roleAppId)">
				AND ROLE_APP_ID = #{roleAppId}
			</if>
	       <if test="@Ognl@isNotEmpty(roleStatus)">
				AND ROLE_STATUS = #{roleStatus}
			</if>
	       
	       <if test="@Ognl@isNotEmpty(roleCreator)">
				AND ROLE_CREATOR = #{roleCreator}
			</if>
	       <if test="@Ognl@isNotEmpty(remark)">
				AND REMARK = #{remark}
			</if>
		</where>
	</sql>
		
    <select id="WFRole.findPage.count" resultType="long">
        SELECT count(*) FROM T_PUB_ROLE r  
		<include refid="WFRole.findPage.where"/>    
    </select>
    
    <!--
    	分页查询已经使用Dialect进行分页,也可以不使用Dialect直接编写分页
    	因为分页查询将传 offset,pageSize,lastRows 三个参数,不同的数据库可以根于此三个参数属性应用不同的分页实现
    -->
    <select id="WFRole.findPage" resultMap="RM_WFRole">
    	SELECT <include refid="WFRole.columns" />
	    FROM T_PUB_ROLE r 
		<include refid="WFRole.findPage.where"/>
		
		<if test="@Ognl@isNotEmpty(sortColumns)">
			ORDER BY ${sortColumns}
		</if>
    </select>
    
     
      <select id="WFRole.findRoleByUser" resultMap="RM_WFRole">
    	SELECT <include refid="WFRole.columns" />
	   	 FROM T_PUB_ROLE r,T_PUB_USER2ROLE ur
	    where r.role_id=ur.role_id
	    and ur.user_id=#{userId}
      </select>
	<select id="WFRole.findRoleByRoleAppIdAndRoleId" resultMap="RM_WFRole">
    	SELECT <include refid="WFRole.columns" />
	   	 FROM T_PUB_ROLE r 
	    where r.role_id=#{roleId} and r.ROLE_APP_ID =#{roleAppId}
      </select>
	 <select id="WFRole.isUserInRole" resultMap="RM_WFRole">
    	SELECT <include refid="WFRole.columns" />
	     FROM T_PUB_ROLE r,T_PUB_USER2ROLE ur
	    where r.role_id=ur.role_id 
	    and r.role_id=#{roleId} and ur.user_id=#{userId}
      </select>
      
      <select id="WFRole.findRoleByUserCode" resultMap="RM_WFRole">
    	SELECT <include refid="WFRole.columns" />
	   	 FROM T_PUB_ROLE r,T_PUB_USER2ROLE ur 
	    where r.role_id=ur.role_id 
	    and ur.user_code=#{userCode}
      </select>
</mapper>

