<?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="WFUser">

	<resultMap id="RM_WFUser" type="com.blueland.bpm.identity.model.User">
        <result property="userId" column="USER_ID"/>
        <result property="userCode" column="USER_CODE"/>
        <result property="userName" column="USER_NAME"/>
        <result property="userSex" column="USER_SEX"/>
        <result property="userAge" column="USER_AGE"/>
        <result property="companyId" column="COMPANY_ID"/>
        <result property="companyName" column="COMPANY_NAME"/>
        <result property="orgId" column="ORG_ID"/>
        <result property="orgName" column="ORG_NAME"/>
        <result property="deptId" column="DEPT_ID"/>
        <result property="deptName" column="DEPT_NAME"/>
        <result property="companyName" column="company_name"/>
        <result property="userMobile" column="USER_MOBILE"/>
        <result property="userMail" column="USER_MAIL"/>
        <result property="userWorkAddress" column="USER_WORK_ADDRESS"/>
        <result property="userWorkPhone" column="USER_WORK_PHONE"/>
        <result property="userHomeAddree" column="USER_HOME_ADDREE"/>
        <result property="userHomePhone" column="USER_HOME_PHONE"/>
        <result property="positionId" column="POSITION_ID"/>
        <result property="pluralityPositionId" column="PLURALITY_POSITION_ID"/>
        <result property="titleId" column="TITLE_ID"/>
        <result property="pluralityTitleId" column="PLURALITY_TITLE_ID"/>
        <result property="userSort" column="USER_SORT"/>
        <result property="userType" column="USER_TYPE"/>
        <result property="userStatus" column="USER_STATUS"/>
        <result property="userPwd" column="USER_PWD"/>
        <result property="userCreateTime" column="USER_CREATE_TIME"/>
        <result property="userUpdateTime" column="USER_UPDATE_TIME"/>
        <result property="userCreator" column="USER_CREATOR"/>
        <result property="remark" column="REMARK"/>
        <result property="deptId" column="DEPT_ID"/>
        <result property="deptName" column="DEPT_NAME"/>
	</resultMap>
	
	<!-- 用于select查询公用抽取的列 -->
	<sql id="WFUser.columns">
	    <![CDATA[
		u.Company_Name,u.USER_ID,u.USER_CODE,u.USER_NAME,u.USER_SEX,u.USER_AGE,u.COMPANY_ID,u.ORG_ID,u.ORG_NAME,u.USER_MOBILE,u.USER_MAIL,u.USER_WORK_ADDRESS,u.USER_WORK_PHONE,u.USER_HOME_ADDREE,u.USER_HOME_PHONE,u.POSITION_ID,u.PLURALITY_POSITION_ID,u.TITLE_ID,u.PLURALITY_TITLE_ID,u.USER_SORT,USER_TYPE,u.USER_STATUS,USER_PWD,u.USER_CREATE_TIME,u.USER_UPDATE_TIME,u.USER_CREATOR,u.REMARK,
		u.DEPT_ID,u.DEPT_NAME
	    ]]>
	</sql>

	<select id="WFUser.findUserMobiles" resultType="string">
		SELECT user_mobile  FROM T_PUB_USER u  WHERE USER_ID in
		<foreach item="userId" collection="userIds" open="(" separator="," close=")">
         #{userId,jdbcType=VARCHAR}
        </foreach>
	</select>
	
    <select id="WFUser.getByIdCascode" resultMap="RM_WFUser">
		SELECT <include refid="WFUser.columns" />,o.ORG_FULL_PATH_ID as orgFullPath
	    <![CDATA[
		    FROM T_PUB_USER u left join T_PUB_ORG o on u.org_id=o.org_id 
	        WHERE 
		        USER_ID = #{id}
	    ]]>
	</select>
	<!-- 	SELECT <include refid="WFUser.columns" />,(SELECT max(substr(sys_connect_by_path(org_id,','),2)) FROM T_PUB_ORG o where o.ORG_ID='43' START WITH o.ORG_ID=u.org_id CONNECT BY  nocycle o.org_id = PRIOR o.org_parent_id ) as orgFullPath -->
	
	<select id="WFUser.getById" resultMap="RM_WFUser">
		SELECT <include refid="WFUser.columns" />
	    <![CDATA[
		    FROM T_PUB_USER u left join T_PUB_ORG o on u.org_id=o.org_id 
	        WHERE 
		        USER_ID = #{id}
	    ]]>
	</select>

	
	<sql id="WFUser.findPage.where">
		<!-- ognl访问静态方法的表达式 为@class@method(args),以下为调用rapid中的Ognl.isNotEmpty()方法,还有其它方法如isNotBlank()可以使用，具体请查看Ognl类 -->
		<where>	      			
			 USER_STATUS='QY'	 
	       <if test="@Ognl@isNotEmpty(userId)">
				AND USER_ID = #{userId}
			</if>
	       <if test="@Ognl@isNotEmpty(userCode)">
				AND USER_CODE = #{userCode}
			</if>
	       <if test="@Ognl@isNotEmpty(userName)">
				AND USER_NAME like CONCAT('%',#{userName},'%')
			</if>
	       <if test="@Ognl@isNotEmpty(userSex)">
				AND USER_SEX = #{userSex}
			</if>
	       <if test="@Ognl@isNotEmpty(userAge)">
				AND USER_AGE = #{userAge}
			</if>
	       <if test="@Ognl@isNotEmpty(companyId)">
				AND COMPANY_ID = #{companyId}
			</if>
	       <if test="@Ognl@isNotEmpty(orgId)">
				AND ORG_ID = #{orgId}
			</if>
	       <if test="@Ognl@isNotEmpty(userMobile)">
				AND USER_MOBILE = #{userMobile}
			</if>
	       <if test="@Ognl@isNotEmpty(userMail)">
				AND USER_MAIL = #{userMail}
			</if>
	       <if test="@Ognl@isNotEmpty(userWorkAddress)">
				AND USER_WORK_ADDRESS = #{userWorkAddress}
			</if>
	       <if test="@Ognl@isNotEmpty(userWorkPhone)">
				AND USER_WORK_PHONE = #{userWorkPhone}
			</if>
	       <if test="@Ognl@isNotEmpty(userHomeAddree)">
				AND USER_HOME_ADDREE = #{userHomeAddree}
			</if>
	       <if test="@Ognl@isNotEmpty(userHomePhone)">
				AND USER_HOME_PHONE = #{userHomePhone}
			</if>
	       <if test="@Ognl@isNotEmpty(positionId)">
				AND POSITION_ID = #{positionId}
			</if>
	       <if test="@Ognl@isNotEmpty(pluralityPositionId)">
				AND PLURALITY_POSITION_ID = #{pluralityPositionId}
			</if>
	       <if test="@Ognl@isNotEmpty(titleId)">
				AND TITLE_ID = #{titleId}
			</if>
	       <if test="@Ognl@isNotEmpty(pluralityTitleId)">
				AND PLURALITY_TITLE_ID = #{pluralityTitleId}
			</if>
	       <if test="@Ognl@isNotEmpty(userType)">
				AND USER_TYPE = #{userType}
			</if>
	       
	       <if test="@Ognl@isNotEmpty(userPwd)">
				AND USER_PWD = #{userPwd}
			</if>
	      
	       <if test="@Ognl@isNotEmpty(userCreator)">
				AND USER_CREATOR = #{userCreator}
			</if>
	       <if test="@Ognl@isNotEmpty(remark)">
				AND REMARK = #{remark}
			</if>
			<if test="@Ognl@isNotEmpty(deptId)">
				AND DEPT_ID = #{deptId}
			</if>
	       <if test="@Ognl@isNotEmpty(deptName)">
				AND DEPT_NAME = #{deptName}
			</if>
		</where>
	</sql>
	
	
	<sql id="WFUser.findUserByOrg.where">
		<!-- ognl访问静态方法的表达式 为@class@method(args),以下为调用rapid中的Ognl.isNotEmpty()方法,还有其它方法如isNotBlank()可以使用，具体请查看Ognl类 -->
		<where>	      				
			u.org_id=o.org_id and u.USER_STATUS='QY'
	       <if test="@Ognl@isNotEmpty(orgId)">
				AND o.ORG_ID = #{orgId}
			</if>
	       <if test="@Ognl@isNotEmpty(orgName)">
				AND o.ORG_NAME = #{orgName}
			</if>
	       <if test="@Ognl@isNotEmpty(orgFullName)">
				AND o.ORG_FULL_NAME = #{orgFullName}
			</if>
	       <if test="@Ognl@isNotEmpty(orgFullPathName)">
				AND o.ORG_FULL_PATH_NAME = #{orgFullPathName}
			</if>
	       <if test="@Ognl@isNotEmpty(orgFullPathId)">
				AND o.ORG_FULL_PATH_ID = #{orgFullPathId}
			</if>
	       <if test="@Ognl@isNotEmpty(orgParentId)">
				AND o.ORG_PARENT_ID = #{orgParentId}
			</if>
	       <if test="@Ognl@isNotEmpty(orgType)">
				AND o.ORG_TYPE = #{orgType}
			</if>
	       <if test="@Ognl@isNotEmpty(orgLevel)">
				AND o.ORG_LEVEL = #{orgLevel}
			</if>
	       <if test="@Ognl@isNotEmpty(orgAreaType)">
				AND o.ORG_AREA_TYPE = #{orgAreaType}
			</if>
		</where>
	</sql>
		
    <select id="WFUser.findUserCount" resultType="long">
        SELECT count(*) FROM T_PUB_USER u,T_PUB_ORG o where u.org_id=o.org_id  and u.USER_STATUS='QY'
        and u.org_id=#{orgId} 
    </select>
    
    <!--
    	分页查询已经使用Dialect进行分页,也可以不使用Dialect直接编写分页
    	因为分页查询将传 offset,pageSize,lastRows 三个参数,不同的数据库可以根于此三个参数属性应用不同的分页实现
    -->
    <select id="WFUser.findPage" resultMap="RM_WFUser">
    	SELECT <include refid="WFUser.columns" />
	    FROM T_PUB_USER u
		<include refid="WFUser.findPage.where"/> 
		ORDER BY USER_SORT
    </select>

	<select id="WFUser.findUserTreeByOrgId" resultMap="RM_WFUser">
 		SELECT <include refid="WFUser.columns" /> FROM T_PUB_USER U,
	(select *from T_PUB_ORG where FIND_IN_SET(org_id, getOrgChildLst(#{orgId}))) O
 		WHERE U.ORG_ID=O.ORG_ID  and u.USER_STATUS='QY' ORDER BY U.USER_SORT
 		
 		
	<!-- SELECT <include refid="WFUser.columns" /> FROM T_PUB_USER U,
	(SELECT * FROM T_PUB_ORG o where o.ORG_ID='43'   START WITH o.ORG_ID=#{orgId} CONNECT BY nocycle o.org_parent_id = PRIOR o.org_id ORDER BY ORG_SORT) O
 		WHERE U.ORG_ID=O.ORG_ID  and u.USER_STATUS='QY' ORDER BY U.USER_SORT -->
    <!--  with p(COMPANY_ID,COMPANY_NAME,DEPT_NAME,DEPT_ID,ORG_ID,ORG_NAME,ORG_FULL_NAME,ORG_FULL_PATH_NAME,ORG_FULL_PATH_ID,ORG_PARENT_ID,ORG_TYPE,ORG_LEVEL,ORG_AREA_TYPE,ORG_SORT,ORG_WORK_PHONE,ORG_WORK_ADDRESS,ORG_PRINCIPAL,ORG_STATUS,ORG_CREATE_TIME,ORG_BRANCH_LEADER,REMARK) as (
		 SELECT o.COMPANY_ID,o.COMPANY_NAME,o.DEPT_NAME,o.DEPT_ID,o.ORG_ID,o.ORG_NAME,o.ORG_FULL_NAME,o.ORG_FULL_PATH_NAME,o.ORG_FULL_PATH_ID,o.ORG_PARENT_ID,o.ORG_TYPE,o.ORG_LEVEL,o.ORG_AREA_TYPE,o.ORG_SORT,o.ORG_WORK_PHONE,o.ORG_WORK_ADDRESS,o.ORG_PRINCIPAL,o.ORG_STATUS,o.ORG_CREATE_TIME,o.ORG_BRANCH_LEADER,o.REMARK 
		 FROM T_PUB_ORG o WHERE o.ORG_ID = #{orgId}
		 union all
		 select o.COMPANY_ID,o.COMPANY_NAME,o.DEPT_NAME,o.DEPT_ID,o.ORG_ID,o.ORG_NAME,o.ORG_FULL_NAME,o.ORG_FULL_PATH_NAME,o.ORG_FULL_PATH_ID,o.ORG_PARENT_ID,o.ORG_TYPE,o.ORG_LEVEL,o.ORG_AREA_TYPE,o.ORG_SORT,o.ORG_WORK_PHONE,o.ORG_WORK_ADDRESS,o.ORG_PRINCIPAL,o.ORG_STATUS,o.ORG_CREATE_TIME,o.ORG_BRANCH_LEADER,o.REMARK
		  from T_PUB_ORG as o,p
		  where p.org_id = o.org_parent_id )
		select u.* from p,T_PUB_USER U WHERE U.ORG_ID=p.ORG_ID  and u.USER_STATUS='QY'  ORDER BY u.USER_SORT -->
    </select>
    
 	<select id="WFUser.findUserByRole" resultMap="RM_WFUser">
    	SELECT <include refid="WFUser.columns" />
	    FROM T_PUB_USER u,T_PUB_USER2ROLE ur,T_PUB_ROLE r,T_PUB_ORG c,T_PUB_ORG o 
	     where u.user_id=ur.user_id and ur.role_id=r.role_id   and u.USER_STATUS='QY' 
	     and ur.role_id=#{roleId} and u.company_id=c.org_id and u.org_id=o.org_id  ORDER BY c.org_sort,o.org_sort,u.user_sort
    </select>
    
    <select id="WFUser.findUserIdByRoleCascade" resultMap="RM_WFUser">
    	SELECT <include refid="WFUser.columns" />,o.ORG_FULL_PATH_ID as orgFullPath
	    FROM T_PUB_USER u,T_PUB_USER2ROLE ur,T_PUB_ROLE r,T_PUB_ORG c,T_PUB_ORG o 
	     where u.user_id=ur.user_id and ur.role_id=r.role_id and u.USER_STATUS='QY' 
	     and ur.role_id=#{roleId} and u.company_id=c.org_id and u.org_id=o.org_id ORDER BY c.org_sort,o.org_sort,u.user_sort
    </select>
    
    <select id="WFUser.findUserByRoleCascade" resultMap="RM_WFUser">
    	SELECT <include refid="WFUser.columns" />
	    FROM T_PUB_USER2ROLE ur,T_PUB_ROLE r
	     where  ur.role_id=r.role_id and ur.role_id=#{roleId} 
	     <if test="@Ognl@isNotEmpty(rowNumCount)">
	    	 where rownum &lt;= #{rowNumCount}
	     </if>
    </select>
    
    <select id="WFUser.findUserIdByRole" resultType="String">
    	SELECT user_id FROM T_PUB_USER2ROLE ur,T_PUB_ROLE r  where  ur.role_id=r.role_id  and ur.role_id=#{roleId}
    </select>
    
    <select id="WFUser.findAllUserByRole" resultMap="RM_WFUser">
    	SELECT ur.user_id  as userId,ur.user_code as userCode,ur.user_name as userName,ur.org_id as orgId,ur.org_name as orgName 
	    FROM T_PUB_USER2ROLE ur,T_PUB_ROLE r
	     where ur.role_id=r.role_id 
	     and ur.role_id=#{roleId}
	 </select>
	
	<select id="WFUser.findUserByOrg" resultMap="RM_WFUser">
    	SELECT <include refid="WFUser.columns" /> 
	    FROM T_PUB_USER u,T_PUB_ORG o 
	    <include refid="WFUser.findUserByOrg.where"/>
	    ORDER BY USER_SORT
    </select>
    
    
    <select id="WFUser.findUserByOrgs" resultMap="RM_WFUser">
    	SELECT <include refid="WFUser.columns" /> 
	    FROM T_PUB_USER u,T_PUB_ORG o where u.org_id=o.org_id  and u.USER_STATUS='QY'  and u.org_id in  
		 <foreach item="item" index="index" collection="orgs" open="(" separator="," close=")">  
		  #{item.orgId}  
		 </foreach>
		  ORDER BY USER_SORT
    </select>
    
    <select id="WFUser.getUserByUserNameOrUserCode" resultMap="RM_WFUser">
    	SELECT <include refid="WFUser.columns" /> 
	    FROM T_PUB_USER u
	    left join T_PUB_ORG o
		on u.org_id=o.org_id
		where u.USER_NAME CONCAT('%',#{userName},'%')
	    or u.USER_CODE like  CONCAT('%',#{userCode},'%')
    </select>
</mapper>

