<?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.FlowBusinessKeyMapper">

    <select id="selectAllFlowTaskByParams" parameterType="com.yanzhu.system.domain.flowable.FlowTaskEntity"  resultType="com.yanzhu.system.domain.flowable.FlowTaskEntity">
        SELECT
            fa.*
        FROM
            vw_flow_all fa
        <where>
            <if test="taskName != null  and taskName != ''"> and fa.taskName like concat('%', #{taskName}, '%')</if>
            <if test="procDefName != null  and procDefName != ''"> and fa.procDefName like concat('%', #{procDefName}, '%')</if>
            <if test="businessKey != null  and businessKey != ''"> and fa.businessKey = #{businessKey}</if>
            <if test="startDeptName != null  and startDeptName != ''"> and fa.startDeptName like concat('%', #{startDeptName}, '%')</if>
            <if test="startUserName != null  and startUserName != ''"> and fa.startUserName like concat('%', #{startUserName}, '%')</if>
            <if test="businessKeyName != null  and businessKeyName != ''"> and fa.businessKeyName like concat('%', #{businessKeyName}, '%')</if>
            <if test="businessKeyParName != null  and businessKeyParName != ''"> and fa.businessKeyParName like concat('%', #{businessKeyParName}, '%')</if>
            <if test="category != null  and category != ''"> and fa.category = #{category}</if>
            <if test="params.beginDate != null and params.beginDate != '' and params.endDate != null and params.endDate != ''"> and date(fa.createTime) between #{params.beginDate} and #{params.endDate}</if>
            <!-- 查询条件-项目部门 -->
            <if test="deptAncestors != null and deptAncestors != ''"> and find_in_set(fa.businessDeptId, #{deptAncestors}) </if>
            <if test='activeName == "await"'> and fa.finishTime is null </if>
            <if test='activeName == "finished"'> and fa.finishTime is not null </if>
        </where>
        order by fa.createTime desc
    </select>

    <!--查询工作流操作日志-->
    <select id="selectCommentByProcInsId" parameterType="string"  resultType="map">
        select * from vw_flow_comment where procInstId = #{procInstId} order by startTime DESC
    </select>

    <!--查询工作流携带的参数-->
    <select id="selectFormDatasByProcInsId" parameterType="string"  resultType="map">
        select hv.NAME_ as `name`,hv.TEXT_ as text from act_hi_varinst hv where hv.PROC_INST_ID_ = #{procInstId}
    </select>

    <!--查询我的代办任务-->
    <select id="selectMyAwaitFlowTask" parameterType="com.yanzhu.system.domain.flowable.FlowTaskEntity"  resultType="map">
        select fa.* from vw_flow_await fa
        where
            1=1
            <if test="procDefName != null  and procDefName != ''"> and fa.procDefName like concat('%', #{procDefName}, '%')</if>
            <if test="businessKey != null  and businessKey != ''"> and fa.businessKey = #{businessKey}</if>
            <if test="businessKeyName != null  and businessKeyName != ''"> and fa.businessKeyName like concat('%', #{businessKeyName}, '%')</if>
            <if test="businessKeyParName != null  and businessKeyParName != ''"> and fa.businessKeyParName like concat('%', #{businessKeyParName}, '%')</if>
            <if test="category != null  and category != ''"> and fa.category = #{category}</if>
            <if test="params.beginDate != null and params.beginDate != '' and params.endDate != null and params.endDate != ''"> and date(fa.createTime) between #{params.beginDate} and #{params.endDate}</if>
            <!-- 查询条件-项目部门 -->
            <if test="deptAncestors != null and deptAncestors != ''"> and find_in_set(fa.businessDeptId, #{deptAncestors}) </if>
            <if test="assigneeId != null and assigneeId != '' and roleIds !=null and roleIds.size()>0">
                AND (fa.ASSIGNEE_ = #{assigneeId}
                    OR (
                            fa.ASSIGNEE_ IS NULL
                        AND (
                            fa.USER_ID_ = #{assigneeId}
                        OR (
                            fa.GROUP_ID_ IN
                            <foreach collection="roleIds" item="roleId" open="(" separator="," close=")">
                                #{roleId}
                            </foreach>
                            )
                        )
                    )
                )
            </if>
        order by fa.createTime desc
    </select>

    <!--查询我的已办任务-->
    <select id="selectMyFinishedFlowTask" parameterType="com.yanzhu.system.domain.flowable.FlowTaskEntity"  resultType="map">
        select fa.* from vw_flow_finished fa
        where
        fa.ASSIGNEE_=#{assigneeId}
        <if test="procDefName != null  and procDefName != ''"> and fa.procDefName like concat('%', #{procDefName}, '%')</if>
        <if test="businessKey != null  and businessKey != ''"> and fa.businessKey = #{businessKey}</if>
        <if test="businessKeyName != null  and businessKeyName != ''"> and fa.businessKeyName like concat('%', #{businessKeyName}, '%')</if>
        <if test="businessKeyParName != null  and businessKeyParName != ''"> and fa.businessKeyParName like concat('%', #{businessKeyParName}, '%')</if>
        <if test="category != null  and category != ''"> and fa.category = #{category}</if>
        <if test="params.beginDate != null and params.beginDate != '' and params.endDate != null and params.endDate != ''"> and fa.endTime between #{params.beginDate} and #{params.endDate}</if>
        order by fa.endTime desc
    </select>


</mapper>