<?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.ruoyi.system.mapper.FlowDeployMapper">

    <select id="selectDeployList" resultType="com.ruoyi.system.domain.FlowProcDefDto">
        SELECT
            rp.id_ as id,
            rp.deployment_id_ as deploymentId,
            rd.name_ as name,
            rd.category_ as category,
            rp.key_ as flowKey,
            rp.version_ as version,
            rp.suspension_state_ as suspensionState,
            rd.deploy_time_  as deploymentTime,
            group_concat(sr.role_name) as roleNames,
            group_concat(rpr.SORT_) as sorts
        FROM
            act_re_procdef rp
            LEFT JOIN act_re_deployment rd ON rp.deployment_id_ = rd.id_
            left join act_re_procdef_role rpr on rp.key_ = rpr.PROCDEF_KEY_
            left join sys_role sr on sr.role_id = rpr.ROLE_ID_
        <where>
            <if test="roleId != null and roleId != ''">
                and rpr.ROLE_ID_ = #{roleId}
            </if>
            <if test="name != null and name != ''">
               and rd.name_ like concat('%', #{name}, '%')
            </if>
            <if test="category != null and category != ''">
                and rd.category_ = #{category}
            </if>
        </where>
<!--        GROUP BY rp.key_-->
        group by rp.deployment_id_
        order by rpr.SORT_
    </select>

    <select id="selectMyDeployList" resultType="com.ruoyi.system.domain.FlowProcDefDto">
        SELECT
        DISTINCT rp.id_ as id,
        rp.deployment_id_ as deploymentId,
        rd.name_ as name,
        rd.category_ as category,
        sdd.dict_label as categoryName,
        rp.key_ as flowKey,
        rp.version_ as version,
        rp.suspension_state_ as suspensionState,
        rd.deploy_time_  as deploymentTime
        FROM
        act_re_procdef rp
        LEFT JOIN act_re_deployment rd ON rp.deployment_id_ = rd.id_
        left join sys_dict_data sdd on sdd.dict_type='sys_process_category' and sdd.dict_value = rd.category_
        left join act_re_procdef_role rpr on rp.key_ = rpr.PROCDEF_KEY_
        left join sys_user_role sur on sur.role_id = rpr.ROLE_ID_
        left join sys_user su on su.user_id = sur.user_id
        <where>
            su.user_name = #{username}
            <if test="name != null and name != ''">
                and rd.name_ like concat('%', #{name}, '%')
            </if>
            <if test="category != null and category != ''">
                and rd.category_ = #{category}
            </if>
        </where>
        order by rd.deploy_time_ desc
    </select>

    <delete id="deleteDeployByRoleId" parameterType="string">
        delete from act_re_procdef_role where ROLE_ID_ = #{roleId}
    </delete>

    <insert id="batchDeployRoleRole">
        insert into act_re_procdef_role( PROCDEF_KEY_, ROLE_ID_) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.key}, #{item.roleId})
        </foreach>
    </insert>

    <update id="updateProcKeyRoleSort">
        update act_re_procdef_role set SORT_=#{sort} where PROCDEF_KEY_=#{key} and ROLE_ID_=#{roleId}
    </update>
</mapper>