<?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"> 
<mapper namespace="com.artfess.bpm.persistence.dao.BpmDefAuthorizeDao">
	<resultMap id="BpmDefAuthorize" type="com.artfess.bpm.persistence.model.BpmDefAuthorize">
		<id property="id" column="ID_" jdbcType="VARCHAR"/>
		<result property="authorizeDesc" column="AUTHORIZE_DESC_" jdbcType="VARCHAR"/>
		<result property="createBy" column="CREATE_BY_" jdbcType="VARCHAR"/>
		<result property="creator" column="CREATOR_" jdbcType="VARCHAR"/>
		<result property="createTime" column="CREATE_TIME_" jdbcType="TIMESTAMP"/>
		<result property="multiple" column="MULTI_PLE" jdbcType="VARCHAR"/>
	</resultMap>
	
	<sql id="columns">
		ID_,AUTHORIZE_DESC_,CREATE_BY_,CREATOR_,CREATE_TIME_,MULTI_PLE
	</sql>
	
	<sql id="dynamicWhere">
		<where>
			
			<if test="id!=null"> 
				AND ID_  =#{id} 
			</if>
			
			<if test="authorizeTypes!=null"> 
				AND ID_ in (SELECT AUTHORIZE_ID_ FROM BPM_DEF_AUTH_TYPE WHERE AUTHORIZE_TYPE_ like #{authorizeTypes} )
			</if>
			
			<if test="authorizeDesc!=null"> 
			    AND AUTHORIZE_DESC_ LIKE #{authorizeDesc}  
			</if>
			<if test="ownerName!=null">
			    <if test="defName==null">
			   		AND ID_ in (SELECT AUTHORIZE_ID_ FROM BPM_DEF_USER WHERE OWNER_NAME_ LIKE #{ownerName} )
				</if>
			</if>
			<if test="defName!=null">
			    <if test="ownerName==null">
				    AND ID_ in (SELECT AUTHORIZE_ID_ FROM BPM_DEF_ACT WHERE DEF_NAME_ LIKE #{defName} )
				</if>
			</if>
			<if test="ownerName!=null">
			    <if test="defName!=null">
				    AND ID_ in (
				                  SELECT U.AUTHORIZE_ID_ FROM BPM_DEF_USER U,BPM_DEF_ACT A 
				                  		WHERE U.AUTHORIZE_ID_=A.AUTHORIZE_ID_ AND U.OWNER_NAME_ LIKE #{ownerName} AND A.DEF_NAME_ LIKE #{defName}
									         )
				</if>
			</if>
			<if test="multiple!=null"> 
				AND MULTI_PLE  =#{multiple} 
			</if>
		</where>
	</sql>
	
	<select id="query" parameterType="java.util.Map" resultMap="BpmDefAuthorize">
		SELECT <include refid="columns"/> FROM BPM_DEF_AUTHORIZE
		<!-- <where>
			<if test="whereSql!=null">
				${whereSql}
			</if>
		</where> -->
		<include refid="dynamicWhere" />
		
		<if test="orderBySql!=null">
			ORDER BY ${orderBySql}
		</if>
		<if test="orderBySql==null">
			ORDER BY ID_ DESC
		</if>
	</select>
	<!--判断用户是否有某个流程的启动权限-->
	<select id="getStartRightByUserIdAndDefId" resultType="java.lang.Integer">
		select count(1) from bpm_def_authorize bda
    join bpm_def_user bdu
        on bdu.AUTHORIZE_ID_ = bda.ID_
        and OWNER_ID_ = #{userId}
    join bpm_def_act bdact
        on bdact.AUTHORIZE_ID_ = bda.ID_
    join bpm_definition bd
        on bd.DEF_KEY_ = bdact.DEF_KEY_
        and bd.DEF_ID_ = #{defId}
    join bpm_def_auth_type bdat
        on bdat.AUTHORIZE_ID_ = bda.ID_
        and bdat.AUTHORIZE_TYPE_ = 'start'
	</select>

	<select id="getOwnerIdAndRightTypeByDefId" resultType="java.util.HashMap" parameterType="java.lang.String">
		select bdu.OWNER_ID_ as "ownerId", RIGHT_TYPE_ as "rightType" from bpm_def_user bdu join bpm_def_act bda on bda.AUTHORIZE_ID_ = bdu.AUTHORIZE_ID_
		join bpm_definition bd on bd.DEF_KEY_ = bda.DEF_KEY_ and bd.DEF_ID_ = #{defId}
		join bpm_def_auth_type bdat on bdat.AUTHORIZE_ID_ = bdu.AUTHORIZE_ID_ and bdat.AUTHORIZE_TYPE_ = 'start'
	</select>
	
	
	<select id="getAuthDetailPage" parameterType="java.util.Map" resultType="java.util.HashMap">
		select a.defName as "defName",a.defKey as "defKey",a.id as "id",a.authDesc as "authDesc",a.multiPle as "multiPle",a.ownerId as "ownerId",a.ownerName as "ownerName",a.rightType as "rightType",
		SUM(CASE a.atype WHEN 'start' THEN 1 ELSE 0 END) AS "start", 
		SUM(CASE a.atype WHEN 'management' THEN 1 ELSE 0 END) AS "management", 
		SUM(CASE a.atype WHEN 'instance' THEN 1 ELSE 0 END) AS "instance", 
		SUM(CASE a.atype WHEN 'task' THEN 1 ELSE 0 END) AS "task",a.rcontent as "content"
		from
		(SELECT def.DEF_NAME_ as defName,def.DEF_KEY_ as defKey,auth.id_ id,auth.AUTHORIZE_DESC_ as authDesc,auth.MULTI_PLE multiPle,authtype.AUTHORIZE_TYPE_ atype,defuser.OWNER_ID_ ownerId,defuser.OWNER_NAME_ ownerName,defuser.RIGHT_TYPE_ rightType,def.RIGHT_CONTENT_ rcontent FROM bpm_def_act def 
		LEFT JOIN bpm_def_authorize auth on def.AUTHORIZE_ID_ = auth.ID_
		LEFT JOIN bpm_def_user defuser on def.AUTHORIZE_ID_=defuser.AUTHORIZE_ID_
		LEFT JOIN bpm_def_auth_type authtype on def.AUTHORIZE_ID_=authtype.AUTHORIZE_ID_) a
		${ew.customSqlSegment}
		GROUP BY a.id,a.defName,a.defKey,a.authDesc,a.multiPle,a.ownerId,a.ownerName,a.rightType,a.rcontent  ORDER BY a.defName,a.authDesc
	</select>

</mapper>
