<?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.project.business.mapper.flow.FlowTaskMapper">

    <sql id="FlowTaskColumn">
        ft
        .
        id
        , ft.flow_id, ft.node_id, ft.prev_tasks, ft.next_tasks, ft.node_name ,ft.status, ft.status_code,
        ft.auditor_type, ft.sign_type
    </sql>

    <resultMap type="FlowTask" id="FlowTaskResult">
        <result property="id" column="id"/>
        <result property="flowId" column="flow_id"/>
        <result property="nodeId" column="node_id"/>
        <result property="nodeName" column="node_name"/>
        <result property="prevTasks" column="prev_tasks"/>
        <result property="nextTasks" column="next_tasks"/>
        <result property="status" column="status"/>
        <result property="statusCode" column="status_code"/>
        <result property="auditorType" column="auditor_type"/>
        <result property="signType" column="sign_type"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>

        <result property="auditTime" column="audit_time"/>
    </resultMap>

    <insert id="saveBatch" useGeneratedKeys="true" keyProperty="id">
        insert into flow_task
        (flow_id, node_id,node_name, prev_tasks, next_tasks, status, status_code, auditor_type, sign_type, del_flag,
        create_by, create_time, update_by, update_time) values
        <foreach collection="list" item="item" separator=",">
            (#{item.flowId}, #{item.nodeId},#{item.nodeName}, #{item.prevTasks}, #{item.nextTasks}, #{item.status},
            #{item.statusCode}, #{item.auditorType}, #{item.signType},
            '0', #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>;
    </insert>

    <select id="getFlowTasks" resultMap="FlowTaskResult">
        select
        <include refid="FlowTaskColumn"/>
        from flow_task ft where ft.flow_id = #{flowId} and ft.del_flag = 0
    </select>

    <select id="getFlowTask" resultMap="FlowTaskResult">
        select
        <include refid="FlowTaskColumn"/>
        from flow_task ft where ft.flow_id = #{flowId} and ft.node_id=#{nodeId} and ft.del_flag = 0
    </select>

    <select id="getNext" resultMap="FlowTaskResult">
        select
        <include refid="FlowTaskColumn"/>
        from flow_task ft where ft.flow_id = #{flowId} and ft.node_id = #{nodeId} and ft.del_flag = 0 limit 1
    </select>

    <update id="updateStatus">
        update flow_task
        set status      = #{param.status},
            update_by   = #{param.updateBy},
            update_time = #{param.updateTime}
        where id = #{param.id}
    </update>
    <update id="updateNextTasks">
        update flow_task
        set next_tasks  = #{param.nextTasks},
            update_by   = #{param.updateBy},
            update_time = #{param.updateTime}
        where id = #{param.id}
    </update>

    <select id="getFLowTasksByFlowId" resultMap="FlowTaskResult">
        SELECT ft.id,
               ft.flow_id,
               ft.node_id,
               ft.prev_tasks,
               ft.next_tasks,
               ft.status,
               ft.status_code,
               ft.auditor_type,
               ft.sign_type user.audit_time
        FROM
            flow_task ft
            LEFT JOIN flow_task_user user
        on user.task_id = ft.id
        WHERE
            ft.flow_id = #{flowId}
          and ft.del_flag = 0
    </select>
    <select id="getHistoryFlowTasks" resultMap="FlowTaskResult">
        select
        <include refid="FlowTaskColumn"/>
        from flow_task ft where ft.flow_id in
        <foreach collection="flowIds" item="flowId" open="(" separator="," close=")">
            #{flowId}
        </foreach>
    </select>
    <select id="getById" resultType="com.ruoyi.project.business.domain.flow.FlowTask">
        select
        <include refid="FlowTaskColumn"/>
        from flow_task ft where ft.id = #{id}
    </select>
</mapper>
