<?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.bpm.manage.countAnalysis.dao.ProcessMonitorDao">	
	<!-- 待办最多的流程 -->
	<select id="getDoProcesss" resultType="java.util.Map">
		select * from (select
              COUNT(*) COUNT,RES.TENANT_ID_ as APP_ID,RES.PROC_DEF_ID_ AS PROC_DEF_ID ,RES.PROC_DEF_NAME AS NAME
              <!-- 如果有租户就统计该应用的待办人员 -->
		<!-- <if test="@Ognl@isNotEmpty(appId)">
				,COUNT(DISTINCT RES.ASSIGNEE_USER_ID) STAFFCOUNT
		</if> -->
            FROM ACT_RU_TASK RES  WHERE 1=1
              <!--   <if test="@Ognl@isNotEmpty(appId)">
						AND RES.TENANT_ID_ = #{appId} 
				</if> -->
                GROUP BY RES.TENANT_ID_,RES.PROC_DEF_ID_,RES.PROC_DEF_NAME
                ORDER BY COUNT DESC) WHERE 1 =1
                <!-- <if test="@Ognl@isEmpty(appId)"> -->
                <![CDATA[and ROWNUM<=10]]>
                <!-- </if> -->
	</select>
	<!-- 待办最多的流程 统计总人数和总流程数 -->
	<select id="getDoTotalProcesss" resultType="java.util.Map">
	select
            COUNT(*) COUNT,COUNT(DISTINCT RES.ASSIGNEE_USER_ID) STAFFCOUNT
        FROM
            ACT_RU_TASK  RES 
    </select>
	<!-- 超时处理最多的流程 -->
	<select id="getOutTimeProcess" resultType="java.util.Map">
	SELECT * FROM (
		  SELECT 
		 <!--  <if test="@Ognl@isNotEmpty(appId)">
		 	 COUNT(*) AS TOTAL,
		  </if> -->
		  PRO.PROC_DEF_ID_ as PROC_DEF_ID,PRO.PROC_DEF_NAME,PRO.TENANT_ID_ AS APP_ID,ROUND(MAX (CASE WHEN PRO.PROC_STATE = 5 THEN (cast(PRO.END_TIME_ AS DATE)-cast(PRO.START_TIME_ AS DATE))* 24 END),2) FINISH_OVER_TIME,
ROUND(MAX (CASE WHEN PRO.PROC_STATE = 1 THEN (SYSDATE-cast(PRO.START_TIME_ AS DATE))* 24 END),2) RUN_OVER_TIME
		 FROM ACT_HI_PROCINST PRO WHERE PRO.PROC_STATE != 3
		<!--  <if test="@Ognl@isNotEmpty(appId)">
				AND PRO.TENANT_ID_ = #{appId} 
		</if> -->
		  GROUP BY PRO.PROC_DEF_ID_,PRO.PROC_DEF_NAME,PRO.TENANT_ID_ ORDER BY FINISH_OVER_TIME DESC ) TT 
		  WHERE TT.FINISH_OVER_TIME IS NOT NULL
		  <!-- <if test="@Ognl@isEmpty(appId)"> -->
                <![CDATA[and ROWNUM<=10]]>
        <!--  </if> -->
	</select>	
	<!-- 超时处理最多的流程 统计人数和超时流程总数 -->
	<select id="getOutTimeProcessTotal" resultType="java.util.Map">
		SELECT
       (COUNT(CASE WHEN PRO.PROC_STATE =5 THEN 'DEAL' END) + COUNT(CASE WHEN PRO.PROC_STATE =1 THEN 'UN_DEAL' END)) COUNT
    FROM
        ACT_HI_PROCINST  PRO WHERE PRO.PROC_STATE != 3
  UNION ALL
  SELECT COUNT(DISTINCT t.START_USER_ID_) COUNT FROM ACT_HI_PROCINST t WHERE t.PROC_STATE != 3
    </select>
	<!-- 创建最高的流程 -->
	<select id="getCreateProcess" resultType="java.util.Map">
		 SELECT * FROM (SELECT COUNT(*) CREATECOUNT,POR.TENANT_ID_ AS APP_ID,count(CASE WHEN POR.PROC_STATE =1 THEN  'un_deal' END) un_deal,
                count(CASE WHEN POR.PROC_STATE =5 THEN  'deal' END) deal,
                count(CASE WHEN POR.PROC_STATE =3 THEN  'cancle' END) cancle,
                POR.PROC_DEF_ID_ AS PROC_DEF_ID, POR.PROC_DEF_NAME
                FROM ACT_HI_PROCINST POR WHERE 1=1
               <!--  <if test="@Ognl@isNotEmpty(appId)">
					AND POR.TENANT_ID_ = #{appId} 
				</if> -->
                 GROUP BY POR.TENANT_ID_,POR.PROC_DEF_ID_, POR.PROC_DEF_NAME ORDER BY CREATECOUNT DESC
                )
                 WHERE 1 =1
                <!-- <if test="@Ognl@isEmpty(appId)"> -->
                <![CDATA[and ROWNUM<=10]]>
        <!--  </if> -->
	</select>	
	<!-- 创建最高的流程 -->
	<select id="getCreateProcessTotal" resultType="java.util.Map">
 		SELECT COUNT(*) CREATECOUNT,count(CASE WHEN POR.PROC_STATE =1 THEN  'un_deal' END) un_deal,
                count(CASE WHEN POR.PROC_STATE =5 THEN  'deal' END) deal,
                count(CASE WHEN POR.PROC_STATE =3 THEN  'cancle' END) cancle          
                FROM ACT_HI_PROCINST POR   
	</select>	
	<!-- 消耗最长的的流程 -->
	<select id="getUseTimeMaxProcess" resultType="java.util.Map">
		 select * from (SELECT 
		  	 <!-- <if test="@Ognl@isNotEmpty(appId)">
             	COUNT(*) AS TOTAL,
             </if> -->
             PRO.TENANT_ID_ AS APP_ID,PRO.PROC_DEF_NAME AS NAME,PROC_DEF_ID_ AS PROC_DEF_ID,ROUND(AVG((cast(PRO.END_TIME_ AS DATE)-cast(PRO.START_TIME_ AS DATE))* 24),2) COUNT 
            from
                ACT_HI_PROCINST PRO WHERE PRO.PROC_STATE = 5
      <!-- <if test="@Ognl@isNotEmpty(appId)">
				AND PRO.TENANT_ID_ = #{appId} 
		</if> -->
       GROUP BY PRO.TENANT_ID_,PRO.PROC_DEF_NAME,PRO.PROC_DEF_ID_
             ORDER BY COUNT DESC)  WHERE 1 =1
               <!--  <if test="@Ognl@isEmpty(appId)"> -->
                <![CDATA[and ROWNUM<=10]]>
       <!--   </if> -->
	</select>	
	<!-- 消耗最长的的流程统计 -->
	<select id="getUseTimeMaxProcessTotal" resultType="java.util.Map">
	SELECT COUNT(*) AS MODELTOTAL,SUM(TOTAL) AS TOTAL FROM (
	SELECT
            COUNT(*) AS TOTAL
        from
            ACT_HI_PROCINST PRO 
        WHERE
            PRO.PROC_STATE = 5 
        GROUP BY
            PRO.TENANT_ID_,PRO.PROC_DEF_NAME,PRO.PROC_DEF_ID_ )
     </select>
	<!-- 节点平均耗时 -->
	<select id="getActAvgUseTime" resultType="java.util.Map">
		SELECT * FROM (SELECT 
	<!-- 	<if test="@Ognl@isNotEmpty(appId)">
			COUNT(*) AS TOTAL, 
		</if> -->
		t.TASK_DEF_KEY_ as TASK_DEF_KEY,t.NAME_ AS NAME,t.TENANT_ID_ AS APP_ID, ROUND(avg((cast(t.END_TIME_ AS DATE)-cast(t.START_TIME_ AS DATE)) * 24),2) AS COUNT
  from ACT_HI_TASKINST t
		WHERE  t.NAME_ IS NOT NULL  AND t.END_TIME_ IS NOT NULL
 <!--  <if test="@Ognl@isNotEmpty(appId)">
				AND t.TENANT_ID_ = #{appId} 
		</if> -->
  GROUP BY t.TASK_DEF_KEY_,t.NAME_,t.TENANT_ID_
  ORDER BY COUNT desc) TT 
  	 WHERE 1=1
         <!-- <if test="@Ognl@isEmpty(appId)"> -->
             <![CDATA[and ROWNUM<=10]]>
         <!-- </if> -->
	</select>	
	<!-- 节点平均耗时统计 -->
	<select id="getActAvgUseTimeTotal" resultType="java.util.Map">
	  SELECT
        COUNT(*) AS TOTAL, COUNT(DISTINCT t.SEND_USER_ID) STAFFCOUNT
    from
        ACT_HI_TASKINST t 
    WHERE
        t.NAME_ IS NOT NULL 
        AND t.END_TIME_ IS  NOT NULL
    </select>
	<!-- 待办流程最多的节点 -->
	<select id="getDoTaskMaxAct" resultType="java.util.Map">
		SELECT * FROM (SELECT COUNT(*) AS COUNT, RES.TENANT_ID_ AS APP_ID,RES.NAME_ AS NAME,RES.TASK_DEF_KEY_  AS TASK_DEF_KEY  
		FROM 
		ACT_RU_TASK RES
		WHERE 1=1
		<!-- <if test="@Ognl@isNotEmpty(appId)">
				AND RES.TENANT_ID_ = #{appId} 
		</if> -->
		GROUP BY RES.TENANT_ID_,RES.NAME_,RES.TASK_DEF_KEY_ 
		ORDER BY COUNT DESC)
		 WHERE 1 =1
		<!--  <if test="@Ognl@isEmpty(appId)"> -->
                <![CDATA[and ROWNUM<=10]]>
         <!-- </if> -->
	</select>	
	<!-- 员工top50-->
	<select id="getTopUserTask" resultType="java.util.Map">
		    SELECT * FROM (SELECT RES.TENANT_ID_ AS APP_ID,RES.ASSIGNEE_USER_ID,RES.ASSIGNEE_USER_NAME AS NAME,COUNT(*) COUNT 
		    FROM ACT_RU_TASK RES
                 WHERE 1=1
               <!--   <if test="@Ognl@isNotEmpty(appId)">  
							AND RES.TENANT_ID_ = #{appId} 
					</if> -->
                 GROUP BY RES.TENANT_ID_,RES.ASSIGNEE_USER_ID,RES.ASSIGNEE_USER_NAME
                 ORDER BY COUNT DESC)
                  WHERE 1 =1
               <!--  <if test="@Ognl@isEmpty(all)"> -->
                	<![CDATA[and ROWNUM<=20]]>
         	<!-- 	</if> -->
	</select>	
</mapper>

