<?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.AdminUserDao">
	<resultMap id="RM_PfAdminUserEntity" type="com.blueland.bo.AdminUserEntity">
        <result property="id" column="id"/>
        <result property="appId" column="app_id"/>
        <result property="adminType" column="admin_type"/>
        <result property="userCode" column="user_code"/>
        <result property="userName" column="user_name"/>
        <result property="userOrgId" column="user_org_id"/>
        <result property="userOrgName" column="user_org_name"/>
        <result property="userDeptId" column="user_dept_id"/>
        <result property="userDeptName" column="user_dept_name"/>
	</resultMap>

	<sql id="PfAdminUserEntityColumns">
        	id,
        	app_id,
        	admin_type,
        	user_code,
        	user_name,
        	user_org_id,
        	user_org_name,
        	user_dept_id,
        	user_dept_name
	</sql>
	
	<sql id="PfAdminUserEntityQueryColumns">
        	id,
        	app_id,
        	admin_type,
        	user_code,
        	user_name,
        	user_org_id,
        	user_org_name,
        	user_dept_id,
        	user_dept_name
	</sql>
	<!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql -->
	<insert id="insert" useGeneratedKeys="false" keyProperty="id">
        INSERT INTO t_pf_admin_user (<include refid="PfAdminUserEntityColumns"/>) VALUES (
        	#{id,jdbcType=VARCHAR},
        	#{appId,jdbcType=VARCHAR},
        	#{adminType,jdbcType=VARCHAR},
        	#{userCode,jdbcType=VARCHAR},
        	#{userName,jdbcType=VARCHAR},
        	#{userOrgId,jdbcType=VARCHAR},
        	#{userOrgName,jdbcType=VARCHAR},
        	#{userDeptId,jdbcType=VARCHAR},
        	#{userDeptName,jdbcType=VARCHAR}
        )
	</insert>


    <select id="get" resultMap="RM_PfAdminUserEntity">
		SELECT <include refid="PfAdminUserEntityQueryColumns"/>
	    FROM t_pf_admin_user 
        WHERE 
	        id = #{id,jdbcType=VARCHAR} 
	</select>
	
    <!--
    	分页查询已经使用Dialect进行分页,也可以不使用Dialect直接编写分页
    	因为分页查询将传 offset,pageSize,lastRows 三个参数,不同的数据库可以根于此三个参数属性应用不同的分页实现
    -->
    <select id="findList" resultMap="RM_PfAdminUserEntity">
    	SELECT <include refid="PfAdminUserEntityQueryColumns"/>
	    FROM t_pf_admin_user 
		<include refid="findList_where"/>
		
		<if test="@Ognl@isNotEmpty(bo.sortColumns)">
			ORDER BY ${bo.sortColumns}
		</if>
    </select>

	<select id="findPage" resultMap="RM_PfAdminUserEntity">
		SELECT <include refid="PfAdminUserEntityQueryColumns"/>
		FROM t_pf_admin_user
		<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.id)">
				AND id = #{bo.id,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.appId)">
				AND app_id = #{bo.appId,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.adminType)">
				AND admin_type = #{bo.adminType,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.userCode)">
				AND user_code = #{bo.userCode,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.userName)">
				AND user_name = #{bo.userName,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.userOrgId)">
				AND user_org_id = #{bo.userOrgId,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.userOrgName)">
				AND user_org_name = #{bo.userOrgName,jdbcType=VARCHAR}
			</if>
			<if test="@Ognl@isNotEmpty(bo.userDeptId)">
				AND user_dept_id = #{bo.userDeptId,jdbcType=VARCHAR}
			</if>
	       <if test="@Ognl@isNotEmpty(bo.userDeptName)">
				AND user_dept_name = #{bo.userDeptName,jdbcType=VARCHAR}
			</if>
		</where>
	</sql>
	
	<sql id="where">
		<where>
	 			<if test="@Ognl@isNotBlank(bo.id)">T1.id =  #{bo.id} </if>
	 			<if test="@Ognl@isNotBlank(bo.appId)">and T1.app_id = #{bo.appId}  </if>
	 			<if test="@Ognl@isNotBlank(bo.adminType)">and T1.admin_type = #{bo.adminType}  </if>
	 			<if test="@Ognl@isNotBlank(bo.userCode)">and T1.user_code = #{bo.userCode}  </if>
	 			<if test="@Ognl@isNotBlank(bo.userName)">and T1.user_name = #{bo.userName}  </if>
	 			<if test="@Ognl@isNotBlank(bo.userOrgId)">and T1.user_org_id = #{bo.userOrgId}  </if>
	 			<if test="@Ognl@isNotBlank(bo.userOrgName)">and T1.user_org_name = #{bo.userOrgName}  </if>
	 			<if test="@Ognl@isNotBlank(bo.userDeptId)">and T1.user_dept_id = #{bo.userDeptId}</if>
	       		<if test="@Ognl@isNotBlank(bo.userDeptName)">and T1.user_dept_name = #{bo.userDeptName}</if>
		</where>
	</sql>
    
    <!-- find pfAdminUser list -->
	<select id="findAdminUserListByParams" resultMap="RM_PfAdminUserEntity">
		select <include refid="PfAdminUserEntityQueryColumns" /> from t_pf_admin_user T1
		<include refid="where"/>
        <if test="@Ognl@isNotBlank(bo.orderByColumns)">order by ${bo.orderByColumns}</if>
	</select>
	
 	<select id="findAdminOrgListByParams" resultMap="RM_PfAdminUserEntity">
		select <include refid="PfAdminUserEntityQueryColumns" /> from t_pf_admin_user T1
		<include refid="where"/>
        <if test="@Ognl@isNotBlank(bo.orderByColumns)">order by ${bo.orderByColumns}</if>
	</select>
	
	<!-- delete one tagAdminUser by appId -->
	 <delete id="deleteAdminUserByBizId"  parameterType="java.lang.String">
		DELETE FROM t_pf_admin_user WHERE app_id=#{appId,jdbcType=VARCHAR}
	</delete>
	
	<!-- 批量插入 -->
     <insert id="insertBatchAdminUser" parameterType="java.util.List">
         insert into t_pf_admin_user(<include refid="PfAdminUserEntityQueryColumns"/>) values
         <foreach collection="list" item="item" index="index" separator=",">
             (#{item.id,jdbcType=VARCHAR},
	 			#{item.appId,jdbcType=VARCHAR},
	 			#{item.adminType,jdbcType=VARCHAR},
	 			#{item.userCode,jdbcType=VARCHAR},
	 			#{item.userName,jdbcType=VARCHAR},
	 			#{item.userOrgId,jdbcType=VARCHAR},
	 			#{item.userOrgName,jdbcType=VARCHAR},
	 			#{item.userDeptId,jdbcType=VARCHAR},
	 			#{item.userDeptName,jdbcType=VARCHAR})
         </foreach>
     </insert>
     
     <!-- 批量删除 -->
    <delete id="batchDeleteByAppId" parameterType="java.lang.String">
        delete from t_pf_admin_user where app_id in
        <foreach item="appId" collection="array" open="(" separator="," close=")">
            #{appId}
        </foreach>
    </delete>
    
    <select id="getByAppidAndUsercode" resultMap="RM_PfAdminUserEntity">
    	select * from t_pf_admin_user where app_id=#{appId} and user_code =#{userCode}
    </select>
	
</mapper>

