<?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.AnalysisDao">	
	<select id="getAppProcessRunIndex" resultType="java.util.Map">
		SELECT APP.APP_ID APPID, APP.APP_NAME APPNAME, A.RUNTASKNUM, B.PROCDEFNUM, C.PROCINSTONENUM, D.PROCINSTFIVENUM FROM T_PUB_APPLICATION APP LEFT JOIN 
		(SELECT T.TENANT_ID TENANTID, COUNT(*) RUNTASKNUM FROM T_WF_PROCESS_INFO_CONFIG T LEFT JOIN T_SYS_GL_TYPE G ON T.PROCESS_TYPE_ID = G.TYPE_ID WHERE T.PROCESS_MGR_ISSHOW = 'Y' GROUP BY T.TENANT_ID) A
		ON APP.APP_ID =A.TENANTID  LEFT JOIN 
		(SELECT TENANT_ID_ TENANTID, COUNT(*) PROCDEFNUM FROM ACT_RU_TASK 
			<where>
				<if test="@Ognl@isNotEmpty(startDate)">
					CREATE_TIME_ <![CDATA[ >= ]]> TO_DATE(#{startDate,jdbcType=TIMESTAMP} , 'yyyy-mm-dd')
				</if>
				<if test="@Ognl@isNotEmpty(endDate)">
					AND CREATE_TIME_ <![CDATA[ <= ]]> TO_DATE(#{endDate,jdbcType=TIMESTAMP} , 'yyyy-mm-dd')
				</if>
			</where>
		GROUP BY TENANT_ID_) B 
		ON APP.APP_ID =B.TENANTID  LEFT JOIN 
		(SELECT TENANT_ID_ TENANTID, COUNT(*) PROCINSTONENUM FROM ACT_HI_PROCINST 
			<where>
				PROC_STATE=1 
				<if test="@Ognl@isNotEmpty(startDate)">
					AND START_TIME_ <![CDATA[ >= ]]> TO_DATE(#{startDate,jdbcType=TIMESTAMP} , 'yyyy-mm-dd')
				</if>
				<if test="@Ognl@isNotEmpty(endDate)">
					AND START_TIME_ <![CDATA[ <= ]]> TO_DATE(#{endDate,jdbcType=TIMESTAMP} , 'yyyy-mm-dd')
				</if>
			</where>
		GROUP BY TENANT_ID_) C 
		ON APP.APP_ID =C.TENANTID  LEFT JOIN 
		(SELECT TENANT_ID_ TENANTID, COUNT(*) PROCINSTFIVENUM FROM ACT_HI_PROCINST
			<where>
			 	PROC_STATE=5  
				<if test="@Ognl@isNotEmpty(startDate)">
					AND START_TIME_ <![CDATA[ >= ]]> TO_DATE(#{startDate,jdbcType=TIMESTAMP} , 'yyyy-mm-dd')
				</if>
				<if test="@Ognl@isNotEmpty(endDate)">
					AND START_TIME_ <![CDATA[ <= ]]> TO_DATE(#{endDate,jdbcType=TIMESTAMP} , 'yyyy-mm-dd')
				</if>
			</where>
		GROUP BY TENANT_ID_) D 
		ON APP.APP_ID =D.TENANTID
		<where>
			APP.APP_TYPE = 'inner' 
			<if test="@Ognl@isNotEmpty(appId)">
				AND APP.APP_ID = #{appId} 
			</if>
		</where>
		ORDER BY APP.APP_CREATE_TIME
	</select>
	
	<select id="getAppProcessLogByYear" resultType="java.util.Map">
		SELECT APP.APP_ID AS APPID, APP.APP_NAME AS APPNAME, COUNT(1) AS RUNCOUNT, TO_CHAR(PRO.PELOG_CREATE_TIME, 'MM') AS MONTH 
		FROM T_PUB_APPLICATION APP LEFT JOIN  T_WF_PROCESS_ERROR_LOG PRO ON APP.APP_ID = PRO.APP_ID
		<where>	 
			PRO.PROCESS_LOG_LEVEL='info' 
			<if test="@Ognl@isNotEmpty(appId)">
				AND APP.APP_ID = #{appId}
			</if>
			<if test="@Ognl@isNotEmpty(searchYear)">
				AND TO_CHAR(PRO.PELOG_CREATE_TIME, 'yyyy') = #{searchYear}
			</if>
		</where>
		GROUP BY APP.APP_ID, APP.APP_NAME, TO_CHAR(PRO.PELOG_CREATE_TIME, 'MM') 
		ORDER BY RUNCOUNT DESC, MONTH 
	</select>
	
	<select id="getAppProcessLogByDay" resultType="java.util.Map">
		SELECT APP.APP_ID AS APPID, APP.APP_NAME AS APPNAME, COUNT(1) AS RUNCOUNT 
	    FROM T_PUB_APPLICATION APP LEFT JOIN  T_WF_PROCESS_ERROR_LOG PRO ON APP.APP_ID = PRO.APP_ID
	    <where>	 
	    	PRO.PROCESS_LOG_LEVEL='info' 
			<if test="@Ognl@isNotEmpty(appId)">
				AND APP.APP_ID = #{appId}
			</if>
			<if test="@Ognl@isNotEmpty(searchDay)">
	    		AND TO_CHAR(PRO.PELOG_CREATE_TIME, 'yyyy-MM-dd') = #{searchDay}
			</if>
		</where>
	    GROUP BY APP.APP_ID, APP.APP_NAME 
		ORDER BY RUNCOUNT DESC
	</select>
	
	<select id="getProEfficiencyByLine" resultType="java.util.Map">
		SELECT 
			PROC.TENANT_ID_ AS APPID, 
	        APP.APP_NAME AS APPNAME,
	        AVG(PROC.DURATION_)/86400000 AS AVGNUM,
	        MAX(PROC.DURATION_)/86400000 AS MAXNUM  
		FROM ACT_HI_PROCINST PROC LEFT JOIN T_PUB_APPLICATION APP ON PROC.TENANT_ID_ = APP.APP_ID
		<where>	 
	    	PROC.END_TIME_ IS NOT NULL
			<if test="@Ognl@isNotEmpty(appId)">
				AND PROC.TENANT_ID_ = #{appId}
			</if>
			<if test="@Ognl@isNotEmpty(searchYear)">
	    		AND TO_CHAR(PROC.START_TIME_, 'yyyy') = #{searchYear}
			</if>
		</where>
		GROUP BY PROC.TENANT_ID_, APP.APP_NAME
	</select>
	<select id="getActInstByProcInstId" resultType="java.util.Map">
		SELECT ACT.NAME_ AS ACTNAME,  
			ACT.SEND_USER_NAME AS SENDUSERNAME,
			ACT.SENDER_ORG_NAME AS SENDORGNAME,
			TO_CHAR(ACT.START_TIME_ , 'yyyy-MM-dd HH:mm:ss') AS STARTTIME, 
        	TO_CHAR(ACT.END_TIME_ , 'yyyy-MM-dd HH:mm:ss') AS ENDTIME,
			ACT.DURATION_ AS TIMENUM
		FROM ACT_HI_TASKINST ACT 
		<where>	 
	    	ACT.END_TIME_ IS NOT NULL 
			<if test="@Ognl@isNotEmpty(procInstId)">
				AND ACT.PROC_INST_ID_ = #{procInstId}
			</if>
		</where>
		ORDER BY ACT.START_TIME_ DESC
	</select>
	
	
	<select id="getProcDefEfficiency" resultType="java.util.Map">
		SELECT PRODEF.PROCESS_DEF_ID AS PROCDEFID, 
			   PRODEF.PROCESS_DEF_NAME AS PROCDEFNAME ,
			   COUNT(*) AS PROCINSTNUM ,
			   SUM(PROINST.DURATION_)/COUNT(*) AS EFFICIENCY
		FROM T_WF_PROCESS_INFO_CONFIG PRODEF LEFT JOIN ACT_HI_PROCINST PROINST ON PRODEF.PROCESS_DEF_ID = PROINST.PROC_DEF_ID_ 
		<where>	 
	    	PRODEF.PROCESS_MGR_ISSHOW = 'Y' AND PRODEF.PROCESS_MGR_STATE = 'RELEASE' AND PROINST.END_TIME_ IS NOT NULL
			<if test="@Ognl@isNotEmpty(appId)">
				AND PRODEF.TENANT_ID = #{appId}
			</if>
			<if test="@Ognl@isNotEmpty(searchYear)">
	    		AND TO_CHAR(PROINST.START_TIME_, 'yyyy') = #{searchYear}
			</if>
		</where>
		GROUP BY PRODEF.PROCESS_DEF_ID, PRODEF.PROCESS_DEF_NAME
	</select>
	<select id="getActDefEfficiency" resultType="java.util.Map">
		sELECT ACTDEF.ACTIVITY_DEF_ID AS ACTDEFID, 
			   ACTDEF.ACTIVITY_DEF_NAME AS ACTDEFNAME, 
			   SUM(ACTINST.DURATION_)/COUNT(*) AS EFFICIENCY
		FROM T_WF_ACTIVITY_INFO_CONFIG ACTDEF LEFT JOIN ACT_HI_TASKINST ACTINST ON ACTDEF.ACTIVITY_DEF_ID = ACTINST.TASK_DEF_KEY_
		<where>	 
	    	ACTINST.END_TIME_ IS NOT NULL
			<if test="@Ognl@isNotEmpty(procDefId)">
				AND ACTDEF.PROCESS_DEF_ID = #{procDefId}
			</if>
			<if test="@Ognl@isNotEmpty(searchYear)">
	    		AND TO_CHAR(ACTINST.START_TIME_, 'yyyy') = #{searchYear}
			</if>
		</where>
		GROUP BY ACTDEF.ACTIVITY_DEF_ID, ACTDEF.ACTIVITY_DEF_NAME
	</select>
	
	
	<select id="getOrgEfficiencyLineByInst" resultType="java.util.Map">
		SELECT 
			PROC.TENANT_ID_ AS APPID, 
	        APP.APP_NAME AS APPNAME,
	        AVG(PROC.DURATION_)/86400000 AS AVGNUM,
	        MAX(PROC.DURATION_)/86400000 AS MAXNUM  
		FROM ACT_HI_PROCINST PROC LEFT JOIN T_PUB_APPLICATION APP ON PROC.TENANT_ID_ = APP.APP_ID
		<where>	 
	    	PROC.END_TIME_ IS NOT NULL
	    	AND PROC.PROC_STATE = 5
			<if test="@Ognl@isNotEmpty(appId)">
				AND PROC.TENANT_ID_ = #{appId}
			</if>
			<if test="@Ognl@isNotEmpty(orgId)">
	    		AND PROC.STARTER_ORG_ID = #{orgId}
			</if>
		</where>
		GROUP BY PROC.TENANT_ID_, APP.APP_NAME
	</select>
	<select id="getOrgEfficiencyByProInst" resultType="java.util.Map">
		SELECT ORG.ORG_ID AS ORGID, 
	       ORG.ORG_NAME AS ORGNAME,
	       COUNT(*) AS PROINSTNUM,
	       SUM(ACTINST.DURATION_)/COUNT(*) AS EFFICIENCY
	    FROM T_PUB_ORG ORG LEFT JOIN ACT_HI_TASKINST ACTINST ON ORG.ORG_ID = ACTINST.ASSIGNEE_ORG_ID 
	    LEFT JOIN ACT_HI_PROCINST PROC ON PROC.PROC_INST_ID_ = ACTINST.PROC_INST_ID_ 
	    <where>	 
	    	ORG.ORG_LEVEL = '1' 
	    	AND ACTINST.END_TIME_ IS NOT NULL 
	    	AND PROC.PROC_STATE = 5
			<if test="@Ognl@isNotEmpty(orgId)">
	    		AND ORG.ORG_ID = #{orgId} 
			</if>
			<if test="@Ognl@isNotEmpty(appId)">
	    		AND ACTINST.TENANT_ID_ = #{appId}
			</if>
		</where>
	    GROUP BY ORG.ORG_ID, ORG.ORG_NAME
	</select>
	<select id="getProInstEfficiencyByOrg" resultType="java.util.Map">
		SELECT ACTINST.PROC_DEF_NAME AS PROCDEFNAME,
				ACTINST.PROC_TITLE AS PROCTITLE,
				ACTINST.NAME_ AS ACTINSTNAME,
				ACTINST.ASSIGNEE_USER_NAME AS USERNAME,
				ACTINST.START_TIME_ AS STARTTIME,
				ACTINST.END_TIME_ AS ENDTIME,
				ACTINST.DURATION_ AS EFFICIENCY
		FROM ACT_HI_TASKINST ACTINST LEFT JOIN ACT_HI_PROCINST PROC ON ACTINST.PROC_INST_ID_ = PROC.PROC_INST_ID_
	    <where>	 
	    	ACTINST.END_TIME_ IS NOT NULL
	    	AND PROC.PROC_STATE = 5
			<if test="@Ognl@isNotEmpty(orgId)">
	    		AND ACTINST.ASSIGNEE_ORG_ID = #{orgId} 
			</if>
			<if test="@Ognl@isNotEmpty(appId)">
	    		AND ACTINST.TENANT_ID_ = #{appId}
			</if>
			<if test="@Ognl@isNotEmpty(procDefName)">
	    		AND ACTINST.PROC_DEF_NAME like '%${procDefName}%'
			</if>
			<if test="@Ognl@isNotEmpty(procTitle)">
	    		AND ACTINST.PROC_TITLE like '%${procTitle}%'
			</if>
			<if test="@Ognl@isNotEmpty(actInstName)">
	    		AND ACTINST.NAME_ like '%${actInstName}%'
			</if>
			<if test="@Ognl@isNotEmpty(userName)">
	    		AND ACTINST.ASSIGNEE_USER_NAME like '%${userName}%'
			</if>
		</where>
		ORDER BY ACTINST.DURATION_ DESC
	</select>
	<select id="getProInstEfficiencyByOrgPage" resultType="java.util.Map">
		SELECT * FROM ( 
	        SELECT A.*, ROWNUM RNUM  FROM ( 
	            SELECT ACTINST.PROC_DEF_NAME AS PROCDEFNAME,
						ACTINST.PROC_TITLE AS PROCTITLE,
						ACTINST.NAME_ AS ACTINSTNAME,
						ACTINST.ASSIGNEE_USER_NAME AS USERNAME,
						ACTINST.START_TIME_ AS STARTTIME,
						ACTINST.END_TIME_ AS ENDTIME,
						ACTINST.DURATION_ AS EFFICIENCY
				FROM ACT_HI_TASKINST ACTINST LEFT JOIN ACT_HI_PROCINST PROC ON ACTINST.PROC_INST_ID_ = PROC.PROC_INST_ID_
			    <where>	 
			    	ACTINST.END_TIME_ IS NOT NULL
	    			AND PROC.PROC_STATE = 5
					<if test="@Ognl@isNotEmpty(orgId)">
			    		AND ACTINST.ASSIGNEE_ORG_ID = #{orgId} 
					</if>
					<if test="@Ognl@isNotEmpty(appId)">
			    		AND ACTINST.TENANT_ID_ = #{appId}
					</if>
					<if test="@Ognl@isNotEmpty(procDefName)">
			    		AND ACTINST.PROC_DEF_NAME like '%${procDefName}%'
					</if>
					<if test="@Ognl@isNotEmpty(procTitle)">
			    		AND ACTINST.PROC_TITLE like '%${procTitle}%'
					</if>
					<if test="@Ognl@isNotEmpty(actInstName)">
			    		AND ACTINST.NAME_ like '%${actInstName}%'
					</if>
					<if test="@Ognl@isNotEmpty(userName)">
			    		AND ACTINST.ASSIGNEE_USER_NAME like '%${userName}%'
					</if>
				</where>
				ORDER BY ACTINST.DURATION_ DESC
			) A 
			<where>	 
				<if test="@Ognl@isNotEmpty(endRow)">
		    		ROWNUM <![CDATA[ < ]]> #{endRow} 
				</if>
			</where>
	    ) 
		<where>	 
			<if test="@Ognl@isNotEmpty(startRow)">
	    		RNUM <![CDATA[ >= ]]> #{startRow} 
			</if>
		</where>
	</select>
</mapper>

