<?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.dayou.mapper.WorkNodeMapper">

    <resultMap id="flowNodeMap" type="com.dayou.dto.WorkNodeDTO">
        <result column="id" property="id" />
        <result column="created" property="created" />
        <result column="deleted" property="deleted" />
        <result column="modified" property="modified" />
        <result column="flow_id" property="flowId" />
        <result column="privious_node_id" property="priviousNodeId" />
        <result column="name" property="name" />
        <result column="code" property="code" />
        <result column="reversible" property="reversible" />
        <result column="restartable" property="restartable" />
        <result column="terminable" property="terminable" />
        <result column="skippable" property="skippable" />
        <result column="type" property="type" />
        <result column="remark" property="remark" />
        <result column="deadline_type" property="deadlineType" />
        <result column="deadline_number" property="deadlineNumber" />
        <result column="verify" property="verify" />
        <result column="businessType" property="businessType" />
        <collection property="tasks"  ofType="com.dayou.entity.WorkNodeTask" select="selectNodeTasks" column="{id=id,businessType=businessType}"/>
    </resultMap>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id,
        created,
        deleted,
        modified,
        flow_id, privious_node_id, name, code, reversible, restartable, terminable, skippable, type, remark,deadline_type,deadline_number,verify
    </sql>

    <select id="getFlowNodes"  resultMap="flowNodeMap">
        select <include refid="Base_Column_List" />,#{businessType} as businessType from work_node where flow_id = #{flowId} and deleted = 0
    </select>

    <select id="selectNodeTasks"  resultType="com.dayou.entity.WorkNodeTask">
        SELECT
            id,
            node_id,
            description,
            task_type,
            JSON_UNQUOTE( `handler_permission` -> '$.name' ) AS name,
            JSON_UNQUOTE( `handler_permission` -> '$.powerId' ) AS powerId,
            handler_permission,
            business_handle
        from
            work_node_task
        WHERE
            node_id = #{id} and deleted = 0 and version = "LATEST" and business_type = #{businessType}
    </select>

    <select id="getNodesByMainBusiness" parameterType="java.lang.String" resultType="com.dayou.entity.WorkNode">
        select id,name ,code  from work_node where flow_id = (select id from work_flow where code=#{businessType} AND deleted = 0 and state = 0) and deleted = 0
    </select>

    <select id="getUniqueNodeByFlowNameAndNodeName" resultMap="flowNodeMap">
        SELECT
        <include refid="Base_Column_List" />,#{businessType} as businessType
        FROM
            work_node
        WHERE
                flow_id = ( SELECT id FROM `work_flow` WHERE CODE = #{businessType} AND deleted = 0 AND state = 0 )
          AND CODE = #{nodeEnum}
          AND deleted = 0
    </select>
</mapper>
