<?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_restrict_code as nodeRestrictCode,
            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>
        </where>
        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_restrict_code as nodeRestrictCode,
            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="taskType != null">and swr.task_type = #{taskType}</if>
            <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
            id as nodeId, node_name as nodeName, node_sign_type as nodeSignType, node_sign_config as nodeSignConfig,
            node_restrict_code as nodeRestrictCode, type_id as typeId
        from
            sys_workflow_node
        where
            id = #{nodeId}
    </select>
    <select id="getTaskIdByProId" resultType="com.bonus.material.work.domain.SysWorkflowNode">
        select
            bp.pro_id as proId,
            lai.task_id as taskId
        from
            lease_apply_info lai
                left join tm_task tt on lai.task_id = tt.task_id
                left join lease_apply_details lad on lai.id = lad.parent_id
                left join tm_task_agreement tta on lai.task_id = tta.task_id
                left join bm_agreement_info bai on tta.agreement_id = bai.agreement_id
                left join bm_unit bu on bu.unit_id = lai.unit_id
                left join bm_project bp on bp.pro_id = lai.project_id
                left join sys_dept sd on sd.dept_id = bp.imp_unit
                left join sys_dict_data sda on tt.task_status = sda.dict_value
                and sda.dict_type = 'lease_task_status'
                left join ma_type mt on lad.type_id = mt.type_id and mt.del_flag = '0'
                left join ma_type mt1 on mt.parent_id = mt1.type_id and mt1.del_flag = '0'
                left join sys_workflow_record swr on tt.task_id = swr.task_id
                left join sys_workflow_type swt on swr.workflow_id = swt.id
                left join sys_workflow_node swn on swt.id = swn.type_id
                left join sys_workflow_config swc on swn.id = swc.node_id
                left join sys_workflow_record_history  swrs on swr.id = swrs.record_id
        where 1=1
          and bp.pro_id = #{proId}
        GROUP BY lai.id
        ORDER BY tt.create_time desc
    </select>


</mapper>
