<?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.zkqy.execution.produce.dispersed.mapper.BpmExecuteProcessMapper">

    <resultMap type="com.zkqy.execution.produce.dispersed.entity.BpmExecuteProcess" id="BpmExecuteProcessResult">
        <result property="id" column="id"/>
        <result property="taskKey" column="task_key"/>
        <result property="taskName" column="task_name"/>
        <result property="taskNodeKey" column="task_node_key"/>
        <result property="taskNodeNextKey" column="task_node_next_key"/>
        <result property="taskProcessKey" column="task_process_key"/>
        <result property="taskProcessXmlContent" column="task_process_xml_content"/>
        <result property="taskProcessState" column="task_process_state"/>
        <result property="taskProcessNote" column="task_process_note"/>
        <result property="delFlag" column="del_flag"/>
        <result property="remark" column="remark"/>
        <result property="taskProcessType" column="task_process_type"/>
    </resultMap>

    <resultMap id="retMap" type="com.zkqy.system.entity.CommonEntity" autoMapping="true">
        <collection property="resultMap" ofType="map" autoMapping="true"
                    javaType="map"></collection>
    </resultMap>

    <sql id="selectBpmExecuteProcessVo">
        select id,
               task_key,
               task_name,
               task_node_key,
               task_node_next_key,
               task_process_key,
               task_process_xml_content,
               task_process_state,
               task_process_note,
               del_flag,
               remark,
               task_process_type
        from bpm_execute_process
    </sql>

    <select id="selectBpmExecuteProcessList"
            parameterType="com.zkqy.execution.produce.dispersed.entity.BpmExecuteProcess"
            resultMap="BpmExecuteProcessResult">
        <include refid="selectBpmExecuteProcessVo"/>
        <where>
            <if test="taskKey != null  and taskKey != ''">and task_key = #{taskKey}</if>
            <if test="taskName != null  and taskName != ''">and task_name like concat('%', #{taskName}, '%')</if>
            <if test="taskNodeKey != null  and taskNodeKey != ''">and task_node_key = #{taskNodeKey}</if>
            <if test="taskNodeNextKey != null  and taskNodeNextKey != ''">and task_node_next_key = #{taskNodeNextKey}
            </if>
            <if test="taskProcessKey != null  and taskProcessKey != ''">and task_process_key = #{taskProcessKey}</if>
            <if test="taskProcessXmlContent != null  and taskProcessXmlContent != ''">and task_process_xml_content =
                #{taskProcessXmlContent}
            </if>
            <if test="taskProcessState != null ">and task_process_state = #{taskProcessState}</if>
            <if test="taskProcessNote != null  and taskProcessNote != ''">and task_process_note = #{taskProcessNote}
            </if>
            <if test="taskProcessType != null  and taskProcessType != ''">and task_process_type = #{taskProcessType}
            </if>
        </where>
    </select>

    <select id="selectBpmExecuteProcessById" parameterType="Long" resultMap="BpmExecuteProcessResult">
        <include refid="selectBpmExecuteProcessVo"/>
        where id = #{id}
    </select>

    <insert id="insertBpmExecuteProcess" parameterType="com.zkqy.execution.produce.dispersed.entity.BpmExecuteProcess"
            useGeneratedKeys="true" keyProperty="id">
        insert into bpm_execute_process
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="taskKey != null">task_key,</if>
            <if test="taskPlanKey != null">task_plan_key,</if>
            <if test="taskName != null">task_name,</if>
            <if test="taskNodeKey != null">task_node_key,</if>
            <if test="taskNodeNextKey != null">task_node_next_key,</if>
            <if test="taskProcessKey != null">task_process_key,</if>
            <if test="taskProcessXmlContent != null">task_process_xml_content,</if>
            <if test="taskProcessState != null">task_process_state,</if>
            <if test="taskProcessNote != null">task_process_note,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="remark != null">remark,</if>
            <if test="taskProcessType != null">task_process_type,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="taskKey != null">#{taskKey},</if>
            <if test="taskPlanKey != null">#{taskPlanKey},</if>
            <if test="taskName != null">#{taskName},</if>
            <if test="taskNodeKey != null">#{taskNodeKey},</if>
            <if test="taskNodeNextKey != null">#{taskNodeNextKey},</if>
            <if test="taskProcessKey != null">#{taskProcessKey},</if>
            <if test="taskProcessXmlContent != null">#{taskProcessXmlContent},</if>
            <if test="taskProcessState != null">#{taskProcessState},</if>
            <if test="taskProcessNote != null">#{taskProcessNote},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="remark != null">#{remark},</if>
            <if test="taskProcessType != null">#{taskProcessType},</if>
        </trim>
    </insert>

    <update id="updateBpmExecuteProcess"
            parameterType="com.zkqy.execution.produce.dispersed.entity.BpmExecuteProcess">
        update bpm_execute_process
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskKey != null">task_key = #{taskKey},</if>
            <if test="taskName != null">task_name = #{taskName},</if>
            <if test="taskNodeKey != null">task_node_key = #{taskNodeKey},</if>
            <if test="taskNodeNextKey != null">task_node_next_key = #{taskNodeNextKey},</if>
            <if test="taskProcessKey != null">task_process_key = #{taskProcessKey},</if>
            <if test="taskProcessXmlContent != null">task_process_xml_content = #{taskProcessXmlContent},</if>
            <if test="taskProcessState != null">task_process_state = #{taskProcessState},</if>
            <if test="taskProcessNote != null">task_process_note = #{taskProcessNote},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="taskProcessType != null">task_process_type = #{taskProcessType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBpmExecuteProcessById" parameterType="Long">
        delete
        from bpm_execute_process
        where id = #{id}
    </delete>

    <delete id="deleteBpmExecuteProcessByIds" parameterType="String">
        delete from bpm_execute_process where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="queryExecuteProcess" resultMap="retMap">
        select
        bep.id as bep_id,
        bep.task_key as bep_task_key,
        bep.task_key as bpm_execute_process_task_key,
        plan.${planName} as bep_task_name,
        bep.task_plan_key as bep_task_plan_key,
        bep.task_plan_key as bpm_execute_process_task_plan_key,
        bep.task_node_key as bep_task_node_key,
        bep.task_node_next_key as bep_task_node_next_key,
        bep.task_node_next_key as bpm_execute_process_task_node_next_key,
        bep.task_process_type as bep_task_process_type,
        bep.task_process_key as bep_task_process_key,
        bep.task_process_xml_content as bep_task_process_xml_content,
        bep.task_process_state as bep_task_process_state,
        bep.task_process_note as bep_task_process_note,
        bep.del_flag as bep_del_flag,
        bep.remark as bep_remark,
        ben.id as ben_id,
        ben.task_process_key as ben_task_process_key,
        ben.task_node_key as ben_task_node_key,
        ben.task_node_name as ben_task_node_name,
        ben.task_node_form_key as ben_task_node_form_key,
        ben.task_node_form_type as ben_task_node_form_type,
        ben.task_node_type as ben_task_node_type,
        ben.task_node_before as ben_task_node_before,
        ben.task_node_after as ben_task_node_after,
        ben.task_node_role_permission as ben_task_node_role_permission,
        ben.task_remark as ben_task_remark,
        ben.task_node_number as ben_task_node_number,
        ben.task_node_weight as ben_task_node_weight,
        ben.task_node_state as ben_task_node_state,
        ben.task_priority as ben_task_priority,
        ben.task1 as ben_task1,
        ben.task2 as ben_task2,
        ben.task3 as ben_task3,
        ben.task4 as ben_task4,
        ben.task5 as ben_task5,
        ben.del_flag as ben_del_flag,
        ben.create_by_id as ben_create_by_id,
        ben.create_by as ben_create_by,
        ben.create_time as ben_create_time,
        benm.id as benm_id,
        benm.task_node_key as benm_task_node_key,
        benm.task_virtually_role as benm_task_virtually_role,
        benm.task_real_role as benm_task_real_role,
        benm.task_execute_user_no as benm_task_execute_user_no,
        benm.task_artificial_script_key as benm_task_artificial_script_key,
        benm.task_automatic_script_trigger_type as benm_task_automatic_script_trigger_type
        from
        {DBNAME}.bpm_execute_process as bep
        inner join {DBNAME}.bpm_execute_node as ben on
        ben.task_process_key = bep.task_key
        and ben.task_node_key = bep.task_node_next_key
        join {DBNAME}.bpm_execute_node_middle as benm on
        benm.task_node_key = ben.task_node_key
        and benm.task_process_key = bep.task_key
        join {DBNAME}.${planTableName} as plan on
        plan.${planID} = bep.task_plan_key
        where
        bep.task_node_next_key in (select task_node_key
        from {DBNAME}.bpm_execute_node_middle
        where
        FIND_IN_SET(#{userId}, task_execute_user_no) > 0
        <if test="taskRealRoleList != null and taskRealRoleList.contains('specificValue')">
            OR
            <foreach collection="taskRealRoleList" item="role" separator=" OR ">
                FIND_IN_SET(#{role}, task_real_role) > 0
            </foreach>
        </if>
        )
        <if test="taskKey != null  and taskKey != ''">and bep.task_key = #{taskKey}</if>
        <if test="taskName != null  and taskName != ''">and bep.task_name like concat('%', #{taskName}, '%')</if>
        <if test="taskNodeKey != null  and taskNodeKey != ''">and bep.task_node_key = #{taskNodeKey}</if>
        <if test="taskNodeNextKey != null  and taskNodeNextKey != ''">and bep.task_node_next_key = #{executeProcessTaskNodeNextKey}
        </if>
        <if test="taskProcessKey != null  and taskProcessKey != ''">and bep.task_process_key = #{taskProcessKey}</if>
        <if test="taskProcessState != null ">and bep.task_process_state = #{taskProcessState}</if>
        <if test="taskProcessNote != null  and taskProcessNote != ''">and bep.task_process_note = #{taskProcessNote}
        </if>
        <if test="taskProcessType != null  and taskProcessType != ''">and bep.task_process_type = #{taskProcessType}
        </if>
        order by ben.create_time desc
    </select>

    <select id="selectBpmExecuteProcessByTaskKey" parameterType="string" resultMap="BpmExecuteProcessResult">
        <include refid="selectBpmExecuteProcessVo"/>
        where task_key = #{taskKey} and del_flag = '0'
    </select>

    <update id="endProcess" parameterType="String">
        update bpm_execute_process
        SET task_process_state = 3
        where task_key = #{taskKey}
    </update>

    <update id="updateProcessNode" parameterType="com.zkqy.execution.produce.dispersed.entity.BpmExecuteProcess">
        update bpm_execute_process
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskKey != null">task_key = #{taskKey},</if>
            <if test="taskName != null">task_name = #{taskName},</if>
            <if test="taskNodeKey != null">task_node_key = #{taskNodeKey},</if>
            <if test="taskNodeNextKey != null">task_node_next_key = #{taskNodeNextKey},</if>
            <if test="taskProcessKey != null">task_process_key = #{taskProcessKey},</if>
            <if test="taskProcessXmlContent != null">task_process_xml_content = #{taskProcessXmlContent},</if>
            <if test="taskProcessState != null">task_process_state = #{taskProcessState},</if>
            <if test="taskProcessNote != null">task_process_note = #{taskProcessNote},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="taskProcessType != null">task_process_type = #{taskProcessType},</if>
        </trim>
        where task_key = #{taskKey}
    </update>

    <select id="getFromInfo" resultType="java.util.Map" parameterType="string">
        select * from {DBNAME}.drag_form where f_id=#{fid}
    </select>

    <select id="getGroupInfo" resultType="java.util.Map" parameterType="string">
        select * from {DBNAME}.drag_table_group where group_key=#{groupKey}
    </select>

    <select id="getTableInfo" resultType="java.util.Map" parameterType="string">
        select * from {DBNAME}.drag_table where table_key=#{tableKey}
    </select>

    <select id="getTableSqlInfo" resultType="java.util.Map">
        select table_sql,table_condition from {DBNAME}.table_sql where sql_key=#{sqlKey}
    </select>

    <select id="getTableCondition" resultType="java.util.Map">
        select * from  {DBNAME}.drag_table_condition where t_id=#{tId}
    </select>

</mapper>

