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

    <!--查询流程定义列表-->
    <select id="selectDeployList" parameterType="com.yanzhu.system.domain.flowable.FlowQueryVo" resultType="com.yanzhu.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,
        rpd.DEPT_ID_ as deptId,
        sd.dept_name as deptName
        FROM
        act_re_procdef rp
        LEFT JOIN act_re_deployment rd ON rp.deployment_id_ = rd.id_
        left join act_re_procdef_dept rpd on rp.id_ = rpd.PROCDEF_ID_
        left join sys_dept sd on sd.dept_id = rpd.DEPT_ID_
        <where>
            <if test="deptId != null">
                and rpd.DEPT_ID_ = #{deptId}
            </if>
            <if test="deptName != null and deptName != ''">
                and sd.dept_name like concat('%', #{deptName}, '%')
            </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>
    </select>

    <!--查询缺省流程定义列表-->
    <select id="selectDefaultDeployList" parameterType="String" resultType="com.yanzhu.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,
        null as deptId,
        null as deptName
        FROM
        act_re_procdef rp
        LEFT JOIN act_re_deployment rd ON rp.deployment_id_ = rd.id_
        left join act_re_procdef_dept rpd on rp.id_ = rpd.PROCDEF_ID_
        <where>
            and rpd.DEPT_ID_ is null
            <if test="list !=null and list.size()>0">
                and rd.category_ not in
                <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <!--新增单位流程关系-->
    <insert id="insertActReProcdefDept" parameterType="com.yanzhu.system.domain.flowable.FlowDeptVo">
        insert into act_re_procdef_dept
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="procdefId != null">PROCDEF_ID_,</if>
            <if test="deptId != null">DEPT_ID_,</if>
            <if test="sort != null">SORT_,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="procdefId != null">#{procdefId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="sort != null">#{sort},</if>
        </trim>
    </insert>

</mapper>