<?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="Userrole">
	<resultMap id="RM_Userrole" type="com.blueland.bpm.identity.model.Userrole">
        <result property="roleId" column="ROLE_ID"/>
        <result property="userId" column="USER_ID"/>
        <result property="remark" column="REMARK"/>
        <result property="userCode" column="USER_CODE"/>
        <result property="userName" column="USER_NAME"/>
        <result property="orgId" column="ORG_ID"/>
        <result property="orgName" column="ORG_NAME"/>
	</resultMap>
	
	<!-- 用于select查询公用抽取的列 -->
	<sql id="Userrole.columns">
	    <![CDATA[
		ROLE_ID,USER_ID,REMARK,USER_CODE,USER_NAME,ORG_ID,ORG_NAME
	    ]]>
	</sql>
	
	<!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql -->
	<insert id="Userrole.insert" useGeneratedKeys="false" keyProperty="roleId">
    <![CDATA[
        INSERT INTO T_PUB_User2role (
		ROLE_ID,
		USER_ID,
        	REMARK ,
        	USER_CODE ,
        	USER_NAME ,
        	ORG_ID ,
        	ORG_NAME 
        ) VALUES (
					#{roleId,jdbcType=VARCHAR},
					#{userId,jdbcType=VARCHAR},
        	#{remark,jdbcType=VARCHAR} ,
        	#{userCode,jdbcType=VARCHAR} ,
        	#{userName,jdbcType=VARCHAR} ,
        	#{orgId,jdbcType=VARCHAR} ,
        	#{orgName,jdbcType=VARCHAR} 
        )
    ]]>
	<!--	
			oracle: order="BEFORE" SELECT sequenceName.nextval AS ID FROM DUAL 
			DB2: order="BEFORE"" values nextval for sequenceName
		<selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="userId">
			SELECT sequenceName.nextval AS ID FROM DUAL 
        </selectKey>
		-->
	</insert>
    
	<update id="Userrole.update" >
    <![CDATA[
        UPDATE T_PUB_User2role SET
	        REMARK = #{remark,jdbcType=VARCHAR} ,
	        USER_CODE = #{userCode,jdbcType=VARCHAR} ,
	        USER_NAME = #{userName,jdbcType=VARCHAR} ,
	        ORG_ID = #{orgId,jdbcType=VARCHAR} ,
	        ORG_NAME = #{orgName,jdbcType=VARCHAR} 
        WHERE 
	        ROLE_ID = #{roleId}  AND 
	        USER_ID = #{userId} 
    ]]>
	</update>

    <delete id="Userrole.delete">
    <![CDATA[
        DELETE FROM T_PUB_User2role WHERE
	        ROLE_ID = #{roleId}  AND 
	        USER_ID = #{userId} 
    ]]>
    </delete>
    
    <select id="Userrole.getById" resultMap="RM_Userrole">
		SELECT <include refid="Userrole.columns" />
	    <![CDATA[
		    FROM T_PUB_User2role 
	        WHERE 
	        	ROLE_ID = #{roleId}  AND 
	        	USER_ID = #{userId} 
	    ]]>
	</select>
	
	<sql id="Userrole.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(userId)">
				AND USER_ID = #{userId}
			</if>
	       
	       <if test="@Ognl@isNotEmpty(remark)">
				AND REMARK = #{remark}
			</if>
	       
	       <if test="@Ognl@isNotEmpty(userCode)">
				AND USER_CODE = #{userCode}
			</if>
	       
	       <if test="@Ognl@isNotEmpty(userName)">
				AND USER_NAME = #{userName}
			</if>
	       
	       <if test="@Ognl@isNotEmpty(orgId)">
				AND ORG_ID = #{orgId}
			</if>
	       
	       <if test="@Ognl@isNotEmpty(orgName)">
				AND ORG_NAME = #{orgName}
			</if>
		</where>
	</sql>
		
    <select id="Userrole.findPage.count" resultType="long">
        SELECT count(*) FROM T_PUB_User2role 
		<include refid="Userrole.findPage.where"/>    
    </select>
    
    <!--
    	分页查询已经使用Dialect进行分页,也可以不使用Dialect直接编写分页
    	因为分页查询将传 offset,pageSize,lastRows 三个参数,不同的数据库可以根于此三个参数属性应用不同的分页实现
    -->
    <select id="Userrole.findPage" resultMap="RM_Userrole">
    	SELECT <include refid="Userrole.columns" />
	    FROM T_PUB_User2role 
		<include refid="Userrole.findPage.where"/>
		
		<if test="@Ognl@isNotEmpty(sortColumns)">
			ORDER BY ${sortColumns}
		</if>
    </select>

	
</mapper>

