<?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.flowable.mapper.WfFormMapper">

    <resultMap type="WfForm" id="WfFormResult">
        <result property="formId" column="form_id"/>
        <result property="deptId" column="dept_id"/>
        <result property="formName" column="form_name"/>
        <result property="content" column="content"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectWfFormVo">
        form_id,
        dept_id,
        user_name,
        dept_name,
        form_name,
        type,
        content,
        create_time,
        remark
    </sql>

    <select id="selectFormVoList" resultType="WfFormVo">
        SELECT t1.form_id   AS formId,
               t1.form_name AS formName,
               t1.content   AS content
        FROM wf_form t1
                 LEFT JOIN wf_deploy_form t2 ON t1.form_id = t2.form_id
            ${ew.getCustomSqlSegment}
    </select>

    <select id="selectWfFormList" resultMap="WfFormResult">
        select DISTINCT
        <include refid="selectWfFormVo"/>
        FROM (
        select
        <include refid="selectWfFormVo"/>
        FROM wf_form where dept_id in
        <foreach item="deptId" collection="deptIdList" open="(" separator="," close=")">
            #{deptId}
        </foreach>
        UNION ALL
        select
        <include refid="selectWfFormVo"/>
        FROM wf_form where dept_id = #{wfForm.deptId} and type = '1'
        UNION ALL
        select
        <include refid="selectWfFormVo"/>
        FROM wf_form where dept_id in
        <foreach item="deptId" collection="ancestorsList" open="(" separator="," close=")">
            #{deptId}
        </foreach>
        and type = '2'
        ) f
        <where>
            <if test="wfForm.formName != null and wfForm.formName != ''">and form_name = #{wfForm.formName}</if>
        </where>
    </select>
</mapper>
