<?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.SysWorkflowNodeMapper">
    <resultMap type="com.bonus.material.work.domain.SysWorkflowNode" id="SysWorkflowNodeResult">
        <result property="id"    column="id"    />
        <result property="typeId"    column="type_id"    />
        <result property="nodeName"    column="node_name"    />
        <result property="nodeSort"    column="node_sort"    />
        <result property="nodeSignType"    column="node_sign_type"    />
        <result property="nodeSignConfig"    column="node_sign_config"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="isEnable"    column="is_enable"    />
    </resultMap>

    <sql id="selectSysWorkflowNodeVo">
        select id, type_id, node_name, node_sort, node_sign_type, node_sign_config,
               create_by,create_time,is_enable from sys_workflow_node
    </sql>
    <insert id="addSysWorkflowNode" useGeneratedKeys="true" keyProperty="id">
        insert into sys_workflow_node
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="typeId != null">type_id,</if>
            <if test="nodeName != null">node_name,</if>
            <if test="nodeSort != null">node_sort,</if>
            <if test="nodeSignType != null">node_sign_type,</if>
            <if test="nodeSignConfig != null">node_sign_config,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isEnable != null">is_enable,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="typeId != null">#{typeId},</if>
            <if test="nodeName != null">#{nodeName},</if>
            <if test="nodeSort != null">#{nodeSort},</if>
            <if test="nodeSignType != null">#{nodeSignType},</if>
            <if test="nodeSignConfig != null">#{nodeSignConfig},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isEnable != null">#{isEnable},</if>
        </trim>
    </insert>

    <update id="updateSysWorkflowNode">
        update sys_workflow_node
        <trim prefix="SET" suffixOverrides=",">
            <if test="nodeName != null">node_name = #{nodeName},</if>
            <if test="nodeSort != null">node_sort = #{nodeSort},</if>
            <if test="nodeSignType != null">node_sign_type = #{nodeSignType},</if>
            <if test="nodeSignConfig != null">node_sign_config = #{nodeSignConfig},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysWorkflowNode">
        delete from sys_workflow_node where id =#{id}
    </delete>

    <select id="selectSysWorkflowNodeList" resultType="com.bonus.material.work.domain.SysWorkflowNode">
        select swn.id as id, swn.type_id typeId, swn.node_name as nodeName, swn.node_sort as nodeSort,
        swn.node_sign_type as nodeSignType, swn.node_sign_config as nodeSignConfig,swn.node_label as nodeLabel,
        swn.create_by as createBy,swn.create_time as createTime,swn.is_enable as isEnable,sec.roleIds as roleIds,
        GROUP_CONCAT(sec.config_value SEPARATOR ',') AS configValues
        from sys_workflow_node swn
        left join sys_workflow_config  sec on swn.id= sec.node_id
        where swn.type_id=#{typeId}
        <if test="nodeName != null "> and swn.node_name like concat('%', #{nodeName}, '%')</if>
        <if test="nodeSignType != null "> and swn.node_sign_type = #{nodeSignType}</if>
        GROUP BY swn.id ORDER BY swn.node_sort
    </select>

    <select id="listByTaskId" resultType="com.bonus.material.work.domain.SysWorkflowNode">
        select swn.id as id, swn.type_id typeId, swn.node_name as nodeName, swn.node_sort as nodeSort,
        swn.node_sign_type as nodeSignType, swn.node_sign_config as nodeSignConfig,swn.id as nodeId,
        swrh.create_by as createBy,swn.is_enable as isEnable,
        GROUP_CONCAT(DISTINCT sec.config_value SEPARATOR ',') AS configValues,sec.config_type as configType,sec.roleIds,
        swr.id as recordId
        from sys_workflow_node swn
        left join sys_workflow_config  sec on swn.id= sec.node_id
        left join sys_workflow_record swr on swr.workflow_id = swn.type_id
        left join sys_workflow_record_history swrh on swr.id = swrh.record_id
        where swr.task_id=#{taskId}
        <if test="nodeName != null and nodeName != '' "> and type_name like concat('%', #{typeName}, '%')</if>
        <if test="nodeSignType != null and nodeSignType != ''"> and node_sign_type = #{nodeSignType}</if>
        GROUP BY swn.id

    </select>
    <select id="selectSysWorkflowNodeById" resultType="com.bonus.material.work.domain.SysWorkflowNode">
        select  node_name as nodeName, node_sign_type as nodeSignType
        from sys_workflow_node where  id = #{nodeId}
    </select>


</mapper>
