<?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="WFOrg">
	<resultMap id="RM_WFOrg" type="com.blueland.bpm.identity.model.Org">
        <result property="orgId" column="ORG_ID"/>
        <result property="orgName" column="ORG_NAME"/>
        <result property="orgFullName" column="ORG_FULL_NAME"/>
        <result property="orgFullPathName" column="ORG_FULL_PATH_NAME"/>
        <result property="orgFullPathId" column="ORG_FULL_PATH_ID"/>
        <result property="orgParentId" column="ORG_PARENT_ID"/>
        <result property="orgType" column="ORG_TYPE"/>
        <result property="orgLevel" column="ORG_LEVEL"/>
        <result property="orgAreaType" column="ORG_AREA_TYPE"/>
        <result property="orgSort" column="ORG_SORT"/>
        <result property="orgWorkPhone" column="ORG_WORK_PHONE"/>
        <result property="orgWorkAddress" column="ORG_WORK_ADDRESS"/>
        <result property="orgPrincipal" column="ORG_PRINCIPAL"/>
        <result property="orgStatus" column="ORG_STATUS"/>
        <result property="orgCreateTime" column="ORG_CREATE_TIME"/>
        <result property="orgBranchLeader" column="ORG_BRANCH_LEADER"/>
        <result property="companyId" column="COMPANY_ID"/>
        <result property="companyName" column="COMPANY_NAME"/>
        <result property="deptName" column="DEPT_NAME"/>
        <result property="deptId" column="DEPT_ID"/>
        <result property="remark" column="REMARK"/>
	</resultMap>
	
	<!-- 用于select查询公用抽取的列 -->
	<sql id="WFOrg.columns">
	    <![CDATA[
		o.COMPANY_ID,o.COMPANY_NAME,o.DEPT_NAME,o.DEPT_ID,o.ORG_ID,o.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,o.REMARK
	    ]]>
	</sql>

    <select id="WFOrg.getById" resultMap="RM_WFOrg">
		SELECT <include refid="WFOrg.columns" />,'' as orgBranchLeaderName
	    <![CDATA[
		    FROM T_PUB_ORG o 
	        WHERE 
		        o.ORG_ID = #{id} 
	    ]]>
	</select>
	
	<sql id="WFOrg.findPage.where">
		<!-- ognl访问静态方法的表达式 为@class@method(args),以下为调用rapid中的Ognl.isNotEmpty()方法,还有其它方法如isNotBlank()可以使用，具体请查看Ognl类 -->
		<where>	      
		    org_status='QY'				
	       <if test="@Ognl@isNotEmpty(orgId)">
				AND ORG_ID = #{orgId}
			</if>
	       <if test="@Ognl@isNotEmpty(orgName)">
				AND ORG_NAME = #{orgName}
			</if>
	       <if test="@Ognl@isNotEmpty(orgFullName)">
				AND ORG_FULL_NAME = #{orgFullName}
			</if>
	       <if test="@Ognl@isNotEmpty(orgFullPathName)">
				AND ORG_FULL_PATH_NAME = #{orgFullPathName}
			</if>
	       <if test="@Ognl@isNotEmpty(orgFullPathId)">
				AND ORG_FULL_PATH_ID = #{orgFullPathId}
			</if>
	       <if test="@Ognl@isNotEmpty(orgParentId)">
				AND ORG_PARENT_ID = #{orgParentId}
			</if>
	       <if test="@Ognl@isNotEmpty(orgType)">
				AND ORG_TYPE = #{orgType}
			</if>
	       <if test="@Ognl@isNotEmpty(orgLevel)">
				AND ORG_LEVEL = #{orgLevel}
			</if>
	       <if test="@Ognl@isNotEmpty(orgAreaType)">
				AND ORG_AREA_TYPE = #{orgAreaType}
			</if>
	       <if test="@Ognl@isNotEmpty(orgSort)">
				AND ORG_SORT = #{orgSort}
			</if>
	       <if test="@Ognl@isNotEmpty(orgWorkPhone)">
				AND ORG_WORK_PHONE = #{orgWorkPhone}
			</if>
			<if test="@Ognl@isNotEmpty(orgBranchLeader)">
				AND ORG_BRANCH_LEADER = #{orgBranchLeader}
			</if>
	       <if test="@Ognl@isNotEmpty(orgWorkAddress)">
				AND ORG_WORK_ADDRESS = #{orgWorkAddress}
			</if>
	       <if test="@Ognl@isNotEmpty(orgPrincipal)">
				AND ORG_PRINCIPAL = #{orgPrincipal}
			</if>
	       <if test="@Ognl@isNotEmpty(orgStatus)">
				AND ORG_STATUS = #{orgStatus}
			</if>
	     
	       <if test="@Ognl@isNotEmpty(remark)">
				AND REMARK = #{remark}
			</if>
		</where>
	</sql>
		
    <select id="WFOrg.findPage.count" resultType="long">
        SELECT count(*) FROM T_PUB_ORG o
		<include refid="WFOrg.findPage.where"/>    
    </select>
    
    <!--
    	分页查询已经使用Dialect进行分页,也可以不使用Dialect直接编写分页
    	因为分页查询将传 offset,pageSize,lastRows 三个参数,不同的数据库可以根于此三个参数属性应用不同的分页实现
    -->
    <select id="WFOrg.findPage" resultMap="RM_WFOrg">
    	SELECT <include refid="WFOrg.columns" />
	    FROM T_PUB_ORG o
		<include refid="WFOrg.findPage.where"/>
		ORDER BY ORG_SORT
    </select>

	<select id="WFOrg.findSubOrgTree" resultMap="RM_WFOrg">
    	select <include refid="WFOrg.columns" />
    	 from T_PUB_ORG o where FIND_IN_SET(org_id, getOrgChildLst(#{orgId}))
    	<!-- 
    	SELECT <include refid="WFOrg.columns" />
    	FROM T_PUB_ORG o  WHERE  o.org_status='QY' START WITH o.ORG_ID=#{orgId}
    	CONNECT BY nocycle o.org_parent_id = PRIOR o.org_id
		-->
		<!-- 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 * from p WHERE p.org_status='QY'  -->
		ORDER BY ORG_SORT
    </select>
    
	<select id="WFOrg.findParentOrgTreeIncludeSelf" resultMap="RM_WFOrg">
		select <include refid="WFOrg.columns" />
    	 from T_PUB_ORG o
		<where>	  
    	 org_status='QY' and FIND_IN_SET(org_id, getOrgParentLst(#{orgId})) 
		 <if test="@Ognl@isNotEmpty(orgLevel)">
				AND ORG_LEVEL = #{orgLevel}
			</if>
	       <if test="@Ognl@isNotEmpty(orgAreaType)">
				AND ORG_AREA_TYPE = #{orgAreaType}
			</if>
			<if test="@Ognl@isNotEmpty(orgType)">
				AND ORG_TYPE = #{orgType}
			</if>
		</where>   
		
    	<!-- 
    	SELECT <include refid="WFOrg.columns" />
    	FROM T_PUB_ORG o 
    	<where>	  
    	 o.org_status='QY' 
		 <if test="@Ognl@isNotEmpty(orgLevel)">
				AND ORG_LEVEL = #{orgLevel}
			</if>
	       <if test="@Ognl@isNotEmpty(orgAreaType)">
				AND ORG_AREA_TYPE = #{orgAreaType}
			</if>
			<if test="@Ognl@isNotEmpty(orgType)">
				AND ORG_TYPE = #{orgType}
			</if>
		</where>   
		START WITH o.ORG_ID=#{orgId,jdbcType=VARCHAR}
    	CONNECT BY nocycle PRIOR o.org_id = o.org_parent_id
    	select <include refid="WFOrg.columns" />
    	 from T_PUB_ORG where FIND_IN_SET(org_id, getOrgParentLst(#{orgId}));
		-->
		
		<!-- 
    	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,jdbcType=VARCHAR}
		 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_parent_id = o.org_id )
		select * from p   -->
    </select>
    
 	<select id="WFOrg.findParentOrgTree" resultMap="RM_WFOrg">
    	select <include refid="WFOrg.columns" />
    	 from T_PUB_ORG o
		<where>	 
    	 org_status='QY' and   FIND_IN_SET(org_id, getOrgParentLst(#{orgId}))   
		 <if test="@Ognl@isNotEmpty(orgLevel)">
				AND ORG_LEVEL = #{orgLevel}
			</if>
	       <if test="@Ognl@isNotEmpty(orgAreaType)">
				AND ORG_AREA_TYPE = #{orgAreaType}
			</if>
			<if test="@Ognl@isNotEmpty(orgType)">
				AND ORG_TYPE = #{orgType}
			</if>
		</where>
    	<!-- 
    	oracle 
    	SELECT <include refid="WFOrg.columns" />
    	FROM T_PUB_ORG o 
    	<where>	 
    	 o.org_status='QY'  
		 <if test="@Ognl@isNotEmpty(orgLevel)">
				AND ORG_LEVEL = #{orgLevel}
			</if>
	       <if test="@Ognl@isNotEmpty(orgAreaType)">
				AND ORG_AREA_TYPE = #{orgAreaType}
			</if>
			<if test="@Ognl@isNotEmpty(orgType)">
				AND ORG_TYPE = #{orgType}
			</if>
		</where>   
		START WITH o.ORG_ID=#{orgId,jdbcType=VARCHAR}
    	CONNECT BY nocycle o.org_id = PRIOR o.org_parent_id
    	
    	mysql
    	select <include refid="WFOrg.columns" />
    	 from T_PUB_ORG where FIND_IN_SET(org_id, getOrgParentLst(#{orgId}));
		-->
		<!--
		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,jdbcType=VARCHAR}
		 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_parent_id = o.org_id )
		select * from p   
		 -->
    </select>
    
    
     
 	<select id="WFOrg.findFullPath" resultMap="RM_WFOrg">
    	select <include refid="WFOrg.columns" />
    	 from T_PUB_ORG o where FIND_IN_SET(org_id, getOrgParentLst(#{orgId}));
		<!-- SELECT <include refid="WFOrg.columns" />
    	FROM T_PUB_ORG o where  o.org_status='QY'  
		START WITH o.ORG_ID=#{orgId,jdbcType=VARCHAR}
    	CONNECT BY nocycle o.org_id = PRIOR o.org_parent_id -->

    	<!-- 
    	select <include refid="WFOrg.columns" />
    	 from T_PUB_ORG where FIND_IN_SET(org_id, getOrgParentLst(#{orgId}));
		-->
		
		<!-- 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,jdbcType=VARCHAR}
		 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_parent_id = o.org_id )
		select * from p where  p.org_status='QY'   -->
    </select>
	
	
	<select id="WFOrg.findOrgByIds.count" resultType="Long">
    	select count(1) 
    	 from T_PUB_ORG o where  org_status='QY'  and org_id in
		<foreach item="orgId" collection="orgIds" open="(" separator="," close=")">
         #{orgId,jdbcType=VARCHAR}
        </foreach>
    </select>
    
    
	<select id="WFOrg.findOrgByIds" resultMap="RM_WFOrg">
    	select <include refid="WFOrg.columns" />
    	 from T_PUB_ORG o where  org_status='QY'  and org_id in
		<foreach item="orgId" collection="orgIds" open="(" separator="," close=")">
         #{orgId,jdbcType=VARCHAR}
        </foreach> order by org_sort
    </select>
    
    
    <select id="WFOrg.isInOrg" resultMap="RM_WFOrg">
    	select <include refid="WFOrg.columns" />
    	 from T_PUB_ORG o where FIND_IN_SET(org_id, getOrgChildLst(#{orgParentId})) and org_id=#{orgId}

		<!-- SELECT <include refid="WFOrg.columns" />
    	FROM T_PUB_ORG o   where o.org_id=#{orgId} and o.org_status='QY' START WITH o.ORG_ID=#{orgParentId}
    	 CONNECT BY nocycle o.org_parent_id = PRIOR o.org_id
    	  -->
    	  
    	  <!-- 	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 = #{orgParentId}
		 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 * from p WHERE ORG_ID=#{orgId} and org_status='QY' -->
    </select>
	
</mapper>

