<?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.bonus.material.work.mapper.SysWorkflowRecordHistoryMapper">


    <insert id="addSysWorkflowRecordHistory" useGeneratedKeys="true" keyProperty="id">
        insert into sys_workflow_record_history
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="recordId != null">record_id,</if>
            <if test="nodeId != null">node_id,</if>
            <if test="nextNodeId != null">next_node_id,</if>
            <if test="isAccept != null">is_accept,</if>
            <if test="createBy != null">create_by,</if>
            create_time,
            <if test="remark != null">remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="recordId != null">#{recordId},</if>
            <if test="nodeId != null">#{nodeId},</if>
            <if test="nextNodeId != null">#{nextNodeId},</if>
            <if test="isAccept != null">#{isAccept},</if>
            <if test="createBy != null">#{createBy},</if>
            now(),
            <if test="remark != null">#{remark},</if>
        </trim>
    </insert>
    <select id="seleteSysWorkflowRecordHistory" resultType="com.bonus.material.work.domain.SysWorkflowRecordHistory">
            select swrh.is_accept as isAccept,swrh.create_time as createTime,swrh.remark as remark,swrh.next_node_id as nextNodeId,
                swrh.create_by as createBy,swrh.node_id as nodeId, su.nick_name as auditBy
            from sys_workflow_record_history swrh
            left join sys_user su on su.user_id = swrh.create_by
            where swrh.record_id=#{recordId} and swrh.node_id= #{nodeId}
    </select>
    <select id="getWorkflowRecordHistoryByRecordId"
            resultType="com.bonus.material.work.domain.SysWorkflowRecordHistory">
        SELECT  swrh.is_accept as isAccept,swrh.create_time as createTime,swrh.remark as remark,swrh.next_node_id as nextNodeId,
                swrh.create_by as createBy,swrh.node_id as nodeId,swn.node_sort as nodeSort
        FROM sys_workflow_record_history swrh
        left join sys_workflow_node swn  on swn.id = swrh.node_id
        WHERE swrh.record_id =#{recordId}  ORDER BY swrh.create_time DESC limit 1
    </select>

    <select id="getWorkFlowRecordNodeInfoByTaskId" resultType="com.bonus.material.work.domain.SysWorkflowRecordHistory">
        SELECT r.id as recordId, h.node_id as nodeId, h.next_node_id as nextNodeId
        FROM sys_workflow_record r
                 JOIN (
            SELECT *
            FROM sys_workflow_record_history
            WHERE record_id = (
                SELECT id
                FROM sys_workflow_record
                WHERE task_id = #{taskId}
                LIMIT 1
            )
            ORDER BY create_time DESC
            LIMIT 1
        ) h ON r.id = h.record_id
        WHERE r.task_id = #{taskId}
    </select>
</mapper>
